[plug] Reading a horrible C file in PHP.

Mark Haselden levsky at staff.iinet.net.au
Mon Aug 7 11:25:06 WST 2006


Couldn't you read in the whole record and use unpack?

-----Original Message-----
From: plug-bounces at plug.org.au [mailto:plug-bounces at plug.org.au] On
Behalf Of Shayne O'Neill
Sent: Monday, 7 August 2006 11:17 AM
To: plug at plug.org.au
Subject: [plug] Reading a horrible C file in PHP.


Ok. I've been tasked to convert this horrible C cgi to php.

The stupid thing reads in a data file like this;-

struct departure {
   int         hidden;
   char        period[32];
   char        price1[32];
   char        price2[32];
   char        price3[32];
   char        price4[32];
   char        price5[32];
   char        price6[32];
   char        price7[32];
   char        price8[32];
   char        price9[32];
};

struct flight {
   int               flightid;
   char              name[256];
   char              price[128];
   char              description[65535];
   struct departure  departures[50];
   int               departurescount;
   char              conditions[16384];
   char              changed[512];
   int               hidden;
};

(...cruft...)

unsigned int get_flights() {
        FILE *flightsfile;
   int bread;

        flightsfile = fopen("flights.dat","r");
   if (flightsfile==NULL) {
      printf("ERROR! get_flights could not open flights.dat\n");
      return 1;
   };

   flightscount=0;

        while (!feof(flightsfile)) {
      bread = fread((char
*)&flights[flightscount].flightid,sizeof(flights[flightscount]),1,flight
sfile);
      if (bread!=1) break;
      flightscount++;
        };

   fclose(flightsfile);
   return 0;
};

Nasty huh?

Anyway, does anyone have any idea how I'm supposed to read that damn
thing into PHP? Im kind of miffed on multiple levels on how to do this.

I sort of wrote some code that does this;-

function get_flights ($section) {
		global $base_cgi_path;
		print "Waiting...";
		flush();
		$file =
fopen($base_cgi_path.$section."/flights.dat",'r');
		$data = array();
		$pointer = 0;
		while (!feof($file)) {
				print "Iiterate";
				flush();
				$record = array();
				$record['int'] = fread($file,2); //Maybe
this is 4?
				$record['name'] = fread($file,256);
				$record['price'] = fread($file,65535);
//wow
				$innerpointer = 0;
				$record['departures'] = array();
				while ($innerpointer < 50) {
	
$record['departures'][$innerpointer] = array();
	
$record['departures'][$innerpointer]['hidden']= fread($file,2);
	
$record['departures'][$innerpointer]['1']= fread($file,32);
	
$record['departures'][$innerpointer]['2']= fread($file,32);
	
$record['departures'][$innerpointer]['3']= fread($file,32);
	
$record['departures'][$innerpointer]['4']= fread($file,32);
	
$record['departures'][$innerpointer]['5']= fread($file,32);
	
$record['departures'][$innerpointer]['6']= fread($file,32);
	
$record['departures'][$innerpointer]['7']= fread($file,32);
	
$record['departures'][$innerpointer]['8']= fread($file,32);
					$innerpointer++;
					}
				$record['departurescount'] =
fread($file,2);
				$record['conditions'] =
fread($file,16384);
				$record['changed'] = fread($file,512);
				$record['hidden'] = fread($file,2);
				$data[$pointer] = $record;
				$pointer++;
				//print_r ($record);
		}

}

But yeah. No real joy. Any suggestions where I might be going wrong
here? I'm no proficient C coder
_______________________________________________
PLUG discussion list: plug at plug.org.au
http://www.plug.org.au/mailman/listinfo/plug
Committee e-mail: committee at plug.linux.org.au




More information about the plug mailing list