0 && strlen($_SERVER[argv][2])>0 && strlen($_SERVER[argv][3])>0 && ctype_digit($_SERVER[argv][3])) { $source_file = $_SERVER[argv][1]; $destination_dir = $_SERVER[argv][2]; $patch_position = $_SERVER[argv][3]; echo "info: source file is ".$source_file."\n"; echo "info: destination directory is ".$destination_dir."\n"; echo "info: patch position is ".$patch_position."\n"; } // command line is wrong, die else { // help $usage_help ='Usage:'."\n"; $usage_help.='php '.$_SERVER[argv][0].' '."\n"; die($usage_help."\n".'error: source file, destination directory or patch position not present'."\n"); } // check if file and directory exist if ( (!is_file($source_file)) || (!is_dir($destination_dir)) ) { die('error: source file or destination directory does not exist'."\n"); } // time statistics $process_begin=microtime(); // try to open the source file if ( $handle=fopen($source_file, 'r') ) { echo "info: source file opened, split started\n"; // var init $patch_file = ''; $patch_content = ''; $patch_count = 0; $total_size = 0; // read the file while ( ! feof($handle) ) { // read a line $line=fgets($handle); // if current line begins with diff, then it's a new patch if ( substr($line, 0, 4) == 'diff' ) { // explode line to get file path $foo=explode(' ', $line); // explode file path $foo=explode('/', $foo[2]); // write the patch if (strlen($patch_content)>0) { if ($handle2=fopen($patch_file, 'w')) { echo "writing ".$patch_file.", length: ".strlen($patch_content)." bytes\n"; fputs($handle2, $patch_content); fclose($handle2); // statistics $patch_count++; $total_size+=strlen($patch_content); } else { die("error: unable to write file ".$patch_file."\n"); } } // reinit patch filename $patch_file = ''; // create patch file, strip some directories if asked for ($i=$patch_position; $i<=(count($foo)-1); $i++) { $patch_file .= $foo[$i].'-'; } // strip the last dash $patch_file = $destination_dir.'/'.substr($patch_file, 0, strlen($patch_file)-1); // begin a new patch $patch_content = $line; } // current line does not begin with diff, so it's the content else { // append current line to patch $patch_content .= $line; } } // we've hit eof, so we need to write the last patch if ($handle2=fopen($patch_file, 'w')) { echo "writing ".$patch_file.", length: ".strlen($patch_content)." bytes\n"; fputs($handle2, $patch_content); fclose($handle2); // statistics $patch_count++; $total_size+=strlen($patch_content); } else { die("error: unable to write file ".$patch_file."\n"); } // close source file fclose($handle); // time statistics $process_duration=abs($process_begin-microtime()); // display infos echo "info: source file size is ".filesize($source_file)." bytes\n"; echo "info: patch count is ".$patch_count." for a total of ".$total_size." bytes\n"; echo "info: process lasted ".$process_duration." microseconds\n"; } // unable to open sourcefile for reading else { die('error: unable to open source file'."\n"); } ?>