Jump to content

Converting PHP to C

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Nile

Nile

    Newbie

  • Members
  • Pip
  • 2 posts
Hey, I'm new here.

I just made this php script to move a webcam using it's built in features to exactly where I want it, but I kinda want it in C (which I'm not experienced in) and not PHP. Can somebody please help me or lead me in the right direction to start converting it?

<?php

$options = array(

	"home" => "http://example.com/webcam/ptz.cgi?camera=1&move=home",

	"up" => "http://example.com/webcam/ptz.cgi?camera=1&move=up",

	"down" => "http://example.com/webcam/ptz.cgi?camera=1&move=down",

	"left" => "http://example.com/webcam/ptz.cgi?camera=1&move=left",

	"right" => "http://example.com/webcam/ptz.cgi?camera=1&move=right",

	"in" => "http://example.com/webcam/ptz.cgi?camera=1&rzoom=500",

	"out" => "http://example.com/webcam/ptz.cgi?camera=1&rzoom=500",

	"near" => "http://example.com/webcam/ptz.cgi?camera=1&rfocus=500",

	"far" => "http://example.com/webcam/ptz.cgi?camera=1&rfocus=500"

);


$directions = array("home", "left", "left", "in", "in", "in", "left", "left");


$curl = curl_init();


foreach($directions as $direction){

	curl_setopt($curl, CURLOPT_URL, $options[$direction]);

	curl_setopt($curl, CURLOPT_HEADER, 0);


	curl_exec($curl);

	sleep(1);

}


curl_close($curl);


sleep(5);

$new = fopen("snaps/".time().".jpg", 'w');

fwrite($new, file_get_contents("http://example.com/webcam/jpg//image.jpg?timestamp=".time()));

fclose($new);

I'd really appreciate it.

#2
Revolt

Revolt

    Programmer

  • Members
  • PipPipPip
  • 99 posts
Well, this particular code shouldn't be too difficult to port to C.

First of all, you'll probably need libcurl - API.

Then all the code you have there could go into the main C function changing things where appropriate. Things to keep in mind:
- C doesn't have foreach but the for (init; cond; increment) instruction.
- Variables in C are typed
- AFAIK there is no standard equivalent to that PHP associative array you have there (C++ would have std::map). This problem is easily fixed by declaring an enum or defining each of the DOWN, LEFT, NEAR, ... as constants with incremental values from 0 to N. This way you can easily index a normal C array and get its value.
- For file management check C File I/O Tutorial - Cprogramming.com

Try it out and get back to us if you face any problem ;)

#3
Nile

Nile

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks! I got help at programmingforums.org though, but I do have problems:

#include <stdio.h>

#include <curl/curl.h>

#include <string.h>


struct CommandUrls

{

	char *name;

	char *url;

};

static struct CommandUrls commandUrls [] =

{

	{"home", "http://camera.com/axis-cgi/com/ptz.cgi?camera=1&move=home"},

	{"up", "http://camera.com/axis-cgi/com/ptz.cgi?camera=1&move=up"},

	{"down", "http://camera.com/axis-cgi/com/ptz.cgi?camera=1&move=down"},

	{"left", "http://camera.com/axis-cgi/com/ptz.cgi?camera=1&move=left"},

	{"right", "http://camera.com/axis-cgi/com/ptz.cgi?camera=1&move=right"},

	{"in", "http://camera.com/axis-cgi/com/ptz.cgi?camera=1&rzoom=500"},

	{"out", "http://camera.com/axis-cgi/com/ptz.cgi?camera=1&rzoom=500"},

	{"near", "http://camera.com/axis-cgi/com/ptz.cgi?camera=1&rfocus=500"},

	{"far", "http://camera.com/axis-cgi/com/ptz.cgi?camera=1&rfocus=500"}

};

char *findUrl(char *command)

{

	int i;

	for (i = 0; i < (sizeof commandUrls / sizeof commandUrls[0]); i++)

	{

		if (strcmp (commandUrls[i].name, command) == 0)

			return commandUrls[i].url;

	}

	return NULL;

};

int *execUrl(char *url)

{

	CURL *curl;

	CURLcode res;

	curl = curl_easy_init();

	if(curl)

	{

		curl_easy_setopt(curl, CURLOPT_URL, url);

		curl_easy_setopt(curl, CURLOPT_HEADER, 0);

		res = curl_easy_perform(curl);

		curl_easy_cleanup(curl);

	}

	return 0;


};

int main(void){

	char *directions[] = {"home", "left", "left", "in", "in", "in", "left", "left"};

	int i;

	for(i = 0; i < (sizeof directions / sizeof directions[0]); i++){

		execUrl(findUrl(directions[i]));

		sleep(1);

	}


	FILE *new;

	CURL *url;

	CURLcode res;

	new = fopen(time(NULL) + ".jpg", "w");

	url = curl_easy_init();

	if(url){

		curl_easy_setopt(url, CURLOPT_URL, strcat("http://camera.com/jpg//image.jpg?timestamp=", time(NULL)));

		curl_easy_setopt(url, CURLOPT_HEADER, 0);

		res = curl_easy_perform(url);

		curl_easy_cleanup(url);

	}

	fwrite(res, 1, sizeof res, new);

	fclose(new);

	return 0;

};
I know there must be something wrong with the fopens and curls at the bottom.

garden.c: In function ‘main’:

garden.c:52: warning: implicit declaration of function ‘sleep’

garden.c:61: warning: passing argument 2 of ‘__builtin___strcat_chk’ makes pointer from integer without a cast

garden.c:61: warning: passing argument 2 of ‘__inline_strcat_chk’ makes pointer from integer without a cast

garden.c:66: error: incompatible type for argument 1 of ‘fwrite’
Thanks

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
C does not perform automatic conversion from numbers to ASCII digit strings for you.

time() should return an integral type of time_t type and will not be suitable for adding to a string.

You may wish to look in to using a sprintf() related function to do this conversion and concatenation for you. As well you should convert your struct in to using limited string arrays, i.e. char string[255] instead of char* string to prevent any of these functions from overwriting portions of memory, you will need to ensure each sprintf call and string will be under that 255 character limit.

The sleep() warning is due to the fact you are not supplying the appropriate header for the function, from your usage I would assume you are using the POSIX variant and you would need to include the header unistd.h to remove this warning.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users