simplifying php code

This commit is contained in:
myvesta 2023-04-16 00:10:54 +02:00 committed by GitHub
parent 502acb44c0
commit ae45e4a571
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 17 deletions

View file

@ -41,13 +41,15 @@ if ($myvesta_stdin!='' && $insert_stdin_at_position===false) {$params[]=$myvesta
for ($i=2; $i<$counter; $i++) { for ($i=2; $i<$counter; $i++) {
$argv[$i]=myvesta_fix_backslashes($argv[$i]); $argv[$i]=myvesta_fix_backslashes($argv[$i]);
if ($insert_stdin_at_position!==false && $myvesta_stdin=='') if ($insert_stdin_at_position==$added) {$stdin_content=$argv[$i]; $added++; continue;} //if ($insert_stdin_at_position!==false && $myvesta_stdin=='') if ($insert_stdin_at_position==$added) {$stdin_content=$argv[$i]; $added++; continue;}
$params[]=$argv[$i]; $params[]=$argv[$i];
$added++; $added++;
} }
//print_r($params); exit;
if ($insert_stdin_at_position!=false) { if ($insert_stdin_at_position!=false) {
if ($myvesta_stdin=='') { if ($myvesta_stdin=='') {
$file_or_stdin=$stdin_content; $file_or_stdin=$params[$insert_stdin_at_position];
if (!file_exists($file_or_stdin)) { if (!file_exists($file_or_stdin)) {
$myvesta_stdin_return_not_found=true; $myvesta_stdin_return_not_found=true;
$myvesta_stdin=''; $myvesta_stdin='';
@ -55,9 +57,10 @@ if ($insert_stdin_at_position!=false) {
$myvesta_stdin=file_get_contents($file_or_stdin); $myvesta_stdin=file_get_contents($file_or_stdin);
$myvesta_stdin_from_file=$file_or_stdin; $myvesta_stdin_from_file=$file_or_stdin;
} }
$params[$insert_stdin_at_position]=$myvesta_stdin;
} else {
array_splice($params, $insert_stdin_at_position, 0, array($myvesta_stdin));
} }
if (isset($params[$insert_stdin_at_position])) array_splice($params, $insert_stdin_at_position, 0, array($myvesta_stdin));
else $params[$insert_stdin_at_position]=$myvesta_stdin;
} }
//print_r($params); exit; //print_r($params); exit;

View file

@ -74,25 +74,25 @@ function myvesta_strip_in_file_between_including_borders($file, $left, $right) {
// --- mixed functions --- // --- mixed functions ---
function myvesta_grep($find, $file_or_stdin, $count=0, $quiet=0) { function myvesta_grep($find, $content, $count=0, $quiet=0) {
global $myvesta_stdin, $myvesta_stdin_return_not_found, $myvesta_quiet_mode; global $myvesta_stdin, $myvesta_stdin_return_not_found, $myvesta_quiet_mode;
if ($count==='-c') {$count=1; $quiet=0;} if ($count==='-c') {$count=1; $quiet=0;}
if ($count==='-q') {$count=0; $quiet=1;} if ($count==='-q') {$count=0; $quiet=1;}
$myvesta_quiet_mode=$quiet; if ($myvesta_quiet_mode==0) $myvesta_quiet_mode=$quiet;
//echo "find = " . $find."\n"; echo "file_or_stdin = " . $file_or_stdin."\n"; echo "count = " . $count."\n"; echo "quiet = " . $quiet."\n"; exit; //echo "find = " . $find."\n"; echo "file_or_stdin = " . $content."\n"; echo "count = " . $count."\n"; echo "quiet = " . $quiet."\n"; exit;
if ($myvesta_stdin_return_not_found==true) { if ($myvesta_stdin_return_not_found==true) {
if ($count==1) return myvesta_throw_error (MYVESTA_ERROR_FILE_DOES_NOT_EXISTS, "0"); if ($count==1) return myvesta_throw_error (MYVESTA_ERROR_FILE_DOES_NOT_EXISTS, "0");
return myvesta_throw_error (MYVESTA_ERROR_FILE_DOES_NOT_EXISTS, ""); return myvesta_throw_error (MYVESTA_ERROR_FILE_DOES_NOT_EXISTS, "");
} }
$arr=explode("\n", $file_or_stdin); $arr=explode("\n", $content);
$buf=''; $buffer='';
$hits=0; $hits=0;
foreach ($arr as $line) { foreach ($arr as $line) {
if (strpos($line, $find)!==false) { if (strpos($line, $find)!==false) {
$hits++; $hits++;
if ($quiet==false && $count==false) $buf.=$line."\n"; if ($quiet==false && $count==false) $buffer.=$line."\n";
} }
} }
if ($count==1) { if ($count==1) {
@ -104,24 +104,24 @@ function myvesta_grep($find, $file_or_stdin, $count=0, $quiet=0) {
return true; return true;
} }
if ($hits==0) return myvesta_exit (MYVESTA_ERROR_STRING_NOT_FOUND, ""); if ($hits==0) return myvesta_exit (MYVESTA_ERROR_STRING_NOT_FOUND, "");
return $buf; return $buffer;
} }
function myvesta_sed($find, $replace, $file_or_stdin) { function myvesta_sed($find, $replace, $content) {
global $myvesta_stdin, $myvesta_stdin_return_not_found, $myvesta_stdin_from_file; global $myvesta_stdin, $myvesta_stdin_return_not_found, $myvesta_stdin_from_file;
//echo "find = " . $find."\n"; echo "replace = " . $replace."\n"; echo "file_or_stdin = " . $file_or_stdin."\n"; echo "stdin_from_file = " . $myvesta_stdin_from_file."\n"; exit; //echo "find = " . $find."\n"; echo "replace = " . $replace."\n"; echo "file_or_stdin = " . $content."\n"; echo "stdin_from_file = " . $myvesta_stdin_from_file."\n"; exit;
if ($myvesta_stdin_return_not_found==true) { if ($myvesta_stdin_return_not_found==true) {
return myvesta_throw_error (MYVESTA_ERROR_FILE_DOES_NOT_EXISTS, "File not found"); return myvesta_throw_error (MYVESTA_ERROR_FILE_DOES_NOT_EXISTS, "File not found");
} }
if (strpos($file_or_stdin, $find)===false) return myvesta_throw_error (MYVESTA_ERROR_STRING_NOT_FOUND, "String '$find' not found"); if (strpos($content, $find)===false) return myvesta_throw_error (MYVESTA_ERROR_STRING_NOT_FOUND, "String '$find' not found");
$file_or_stdin=str_replace($find, $replace, $file_or_stdin); $content=str_replace($find, $replace, $content);
if ($myvesta_stdin_from_file!='') { if ($myvesta_stdin_from_file!='') {
$r=file_put_contents($myvesta_stdin_from_file, $file_or_stdin); $r=file_put_contents($myvesta_stdin_from_file, $content);
if ($r===false) return false; if ($r===false) return false;
} else { } else {
myvesta_echo ($file_or_stdin); myvesta_echo ($content);
} }
return true; return true;
} }