<!DOCTYPE html>         
<html>
<head>                    
<meta charset="utf-8">
</head>
<body>
<?php
include('../lib/generic.inc.php');

$fx = (int)@$_REQUEST['FX'];

switch ($fx)
{
case 0:
   hello_menu();
   break;
case 1:
   ask_std_params();
   break;
case 2:
   process_std_params();
   echo "<hr>";
   ask_std_params();
   break;
case 3:
   ask_for_eveywhere();
   break;      
case 4:
   process_for_everywhere();
   echo "<hr>";
   ask_for_eveywhere();
   break;   
}
//------------------------------------------------
function hello_menu()
{
   echo '<ol>
      <li><a href="'.$_SERVER['SCRIPT_NAME'].'?FX=1">Add std::</a></li>
      <li><a href="'.$_SERVER['SCRIPT_NAME'].'?FX=3">Find everywhere</a></li>
      </ol>';
}
//------------------------------------------------
function ask_for_eveywhere()
{
?>
<form action="<?php echo myhtml($_SERVER['SCRIPT_NAME']); ?>">
<input type=hidden name="FX" value="4">
<table>
<tr>
   <td>String (i.e. function name) to find</td>
   <td><input type=text name="WORD" value="<?php echo myhtml(@$_REQUEST['WORD']); ?>"></td>
</tr>
<tr>
   <td></td>
   <td>
      <input type=submit value="Process">
      <input type=button value="Cancel" onclick="location.href='<?php echo $_SERVER['SCRIPT_NAME']; ?>';">
   </td>
</tr>
</table>
</form>
<?php
}
//------------------------------------------------
function process_for_everywhere()
{
   $word = $_REQUEST['WORD'];
   
   $ar1 = array( 'C:\\inca\\prog\\taxspooler', 'C:\\inca\\prog\\xlead', 'C:\\inca\\prog\\iform');
   foreach ($ar1 as $folder)
   {
      $ar1 = glob("$folder\\*.*");     
      if ($ar1===false) {
         echo "Nothing found!";
         return;
      }
      foreach ($ar1 as $f1) {
         $ext = pathinfo($f1, PATHINFO_EXTENSION);
         if (strcasecmp($ext, "cpp")==0) {
            if (($text = file_get_contents($f1))===false)
               continue;
               
            if (stripos($text, $word) !== false) {
               echo "FOUND in $folder<br>";
               break;
            }
         }
      }
   }
}
//------------------------------------------------
function ask_std_params()
{
?>
<form action="<?php echo myhtml($_SERVER['SCRIPT_NAME']); ?>">
<input type=hidden name="FX" value="2">
<table>
<tr>
   <td>File or Folder</td>
   <td><input type=text name="FIFOLDER" value="<?php echo myhtml(@$_REQUEST['FIFOLDER']); ?>"></td>
</tr>
<tr>
   <td>Type</td>
   <td>
      <label><input type=radio name="ISFILE" value="1" <?php if (!isset($_REQUEST['ISFILE']) || $_REQUEST['ISFILE']==1) echo 'checked'; ?> > File</label><br>
      <label><input type=radio name="ISFILE" value="0" <?php if (isset($_REQUEST['ISFILE']) && $_REQUEST['ISFILE']==0) echo 'checked'; ?> > Folder</label>
   </td>
</tr>
<tr>
   <td>Word</td>
   <td><input type=text name="WORD" value="<?php echo myhtml(@$_REQUEST['WORD']); ?>" placeholder="i.e. vector"></td>
</tr>
<tr>
   <td></td>
   <td>
      <input type=submit value="Process">
      <input type=button value="Cancel" onclick="location.href='<?php echo $_SERVER['SCRIPT_NAME']; ?>';">
   </td>
</tr>
</table>
</form>
<?php
}
//------------------------------------------------
function process_std_params()
{
   if (@$_REQUEST['ISFILE'])
      process_std_as_file($_REQUEST['FIFOLDER']);
   else
      process_std_as_folder();
}
//------------------------------------------------
function process_std_as_file($f1)
{
   if (empty($f1)) // precaution
      return;
      
   $word = $_REQUEST['WORD'];
   $needsave = false;
   
   $lines = file($f1, FILE_IGNORE_NEW_LINES);
   $n = count($lines);
   for ($i=0;$i<$n;$i++) {
      $line1 = $lines[$i];
      $matches = array();
      if (preg_match_all("/\b$word\b/", $line1, $matches, PREG_PATTERN_ORDER|PREG_OFFSET_CAPTURE)) {
         if (!empty($matches)) {
            foreach ($matches[0] as $m1) 
            {
               $j = $m1[1];
               if ($j>=1 && ($line1{$j-1} === '<' || $line1{$j-1} === '"'))
                  continue;
                  
               if ($j>=5 && substr($line1, $j-5, 5) === 'std::' )
                  continue;
                  
               $newline = substr($line1, 0, $j).'std::'.substr($line1, $j); //reset
               $lines[$i] = $newline;
               $needsave = true;
               break;
            }
         } 
      }
   }
   
   if ($needsave) {
      echo "--------------------------------------------------<br>$f1<br>";
      /*
      $lines8 = mb_convert_encoding ($lines, 'UTF-8', 'ISO-8859-7');            
      echo '<pre>'.implode("\r\n", $lines8).'</pre>';
      */
      file_put_contents($f1, implode("\r\n", $lines) );
   }
}      
//------------------------------------------------
function process_std_as_folder()
{      
   $folder = $_REQUEST['FIFOLDER'];
   
   $ar1 = glob("$folder/*.*"); 
   if ($ar1===false) {
      echo "Nothing found!";
      return;
   }
   foreach ($ar1 as $f1) {
      $ext = pathinfo($f1, PATHINFO_EXTENSION);
      if (strcasecmp($ext, "cpp")==0) {
         process_std_as_file($f1);
      }
   }
}
//------------------------------------------------
?>
</body>
</html>