note 27995 added to function.fgets

From: Date: Sat, 28 Dec 2002 23:30:38 +0000
Subject: note 27995 added to function.fgets
Groups: php.notes 
Request: Send a blank email to php-notes+get-41401@lists.php.net to get a copy of this message
While working with some binary files for the eMule/edonkey2000 (ed2k) linux clients I ran across some c-code for reading in the server.met files. I ported the code to PHP which uses fgets to pull in binary data from a server.met file piped into the php script. For those of you trying to port code to PHP this might come in handy for how to do it using fgets(). <? # # call like so "php thisfile.php < server.met" # function ireadch ($fp, $number) { do $c.= fgetc($fp); while (--$number); return (int)$c; } function lreadch ($fp, $number) { do $c.= ord(fgetc($fp)); while (--$number); return (int)$c; } function readlong ($fp) { $c = ord(fgetc($fp)); $c |= (ord(fgetc($fp)) << 8); $c |= (ord(fgetc($fp)) << 16); $c |= (ord(fgetc($fp)) << 24); return (int)$c; } #$fp = fopen("/var/www/donk/server.met.1","r"); $fp = fopen('php://stdin', 'r'); fgetc($fp); $serversf = readlong($fp); do { for ($icount=0;$icount<4;$icount++){ $ip[$icount]=ord(fgetc($fp)); } $port = ord(fgetc($fp)); $port |= (ord(fgetc($fp)) << 8); $meta = readlong($fp); for ($lcount=0;$lcount<$meta;$lcount++) { $t_type = intval(ord(fgetc($fp))); $t_length = ord(fgetc($fp)); // Name Length $t_length |= (ord(fgetc($fp)) << 8); if ($t_length===EOF) exit(0); if ($t_length) lreadch($fp,$t_length); if ($t_type==2) { // String Tag $t_length = ord(fgetc($fp)); // Value Length $t_length |= (ord(fgetc($fp)) << 8); if ($t_length===EOF) exit(0); if ($t_length){ lreadch($fp,$t_length); } // else jump over } else if ($t_type==3) { // Number Tag $o = ireadch($fp,4); } }//rof printf ("%u.%u.%u.%u %ld\n",$ip[0],$ip[1],$ip[2],$ip[3],$port); $serversc++; } while($serversc <= $serversf); ?> -- http://www.php.net/manual/en/function.fgets.php http://master.php.net/manage/user-notes.php?action=edit+27995 http://master.php.net/manage/user-notes.php?action=delete+27995 http://master.php.net/manage/user-notes.php?action=reject+27995

« previous php.notes (#41401) next »