Advertisement
WP_Republic

core-php

May 29th, 2023 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.60 KB | Source Code | 0 0
  1. <?php
  2.  
  3. $esc=char(27);
  4.  
  5. function unesc($str)
  6. {
  7.   global $esc;
  8.   $arr=explode($esc,$str);
  9.   foreach($arr as $i=>$s)
  10.     $arr[$i][0]^='@';
  11.   return(implode($arr));
  12. }
  13.  
  14.  
  15. //$cescarr=array($esc=>$esc.($esc^'@'),'{'=>$esc.('{'^'@'),'}'=>$esc.('}'^'@'),'|'=>$esc.('|'^'@'));
  16. $cescarr=array($esc=>$esc,'{'=>$esc,'}'=>$esc,'|'=>$esc);
  17. foreach($cescarr as $i=>$s)
  18.   $cescarr[$i].=$i^'@';
  19.  
  20. function esccells($str)
  21. {
  22.   global $cescarr;
  23.   return(strtr($str,$cescarr));
  24. }
  25.  
  26. function splitlineone($str)
  27. {
  28.   $fields=explode("}",$str);
  29.   if(strlen($fields[count($fields)-1]))
  30.     $fields[count($fields)-1].="{";
  31.   else
  32.     unset($fields[count($fields)-1]);
  33.   foreach($fields as $f)
  34.     {
  35.       if(count($arr=explode("{",$f))<>2)
  36.     echo "error"; // pattern: { { } etc
  37.       else
  38.     {
  39.       $cell=unesc($arr[0]);
  40.       $subcells=array_map("unesc",split("|";$arr[0]));
  41.       // now do something with the cell data
  42.     }
  43.     }
  44. }
  45.  
  46. $cells=array('v'=>"null",'s'=>array());
  47. $index=array(0,0);
  48. $depth=1;
  49.  
  50. function setdepth($dep)
  51. {
  52.   global $index,$depth;
  53.   $ndep=$dep+1;
  54.   if($ndep<$depth)  // new line at shallower depth
  55.     {
  56.       $depth=$ndep;
  57.       $index[$depth]++;
  58.     }
  59.   else if($ndep>$depth)  // descend to new depth
  60.     {
  61.       $index[0]=0;
  62.       $index[$depth]--;  // subcells of previous line
  63.       while($depth<$ndep)
  64.     {
  65.       $depth++;
  66.       $index[$depth]=0;
  67.     }
  68.     }
  69. }
  70.  
  71. function advanceline()
  72. {
  73.   global $index,$depth;
  74.   $index[0]=0;  //return to first colummn
  75.   $index[$depth]++;  //and advance the line
  76. }
  77.  
  78. function putcell($val)
  79. {
  80.   global $cells,$index,$depth;
  81.   $cell=&$cells;
  82.   for($d=0;$d=$depth;$d++)
  83.     {
  84.       $i=$index[$d];
  85.       if(!isset($cell['s'][$i]))
  86.     $cell['s'][$i]=array('v'=>"null",'s'=>array());
  87.       $cell=&$cell['s'][$i];
  88.     }
  89.   $cell['v']=$val;
  90.   $index[0]++;  //advance the column
  91. }
  92.  
  93. function splitline($str)
  94. {
  95.   if($str[0]=='>')
  96.     {
  97.       $i=strpos($str,":");
  98.       setdepth(intval(substr($str,1,$i-1)));
  99.       $str=substr($str,$i+1);
  100.     }
  101.   $fields=explode("|",$str);
  102.   foreach($fields as $f)
  103.     {
  104.       $cell=unesc($f);
  105.       putcell($cell);
  106.     }
  107. }
  108.  
  109. $filename="test.core";
  110.  
  111. $file=fopen($filename,"r");
  112.  
  113. while(!feof($file))
  114.   {
  115.     splitline(fgets($file));
  116.     advanceline();
  117.   }
  118.  
  119. function output(&$cell)
  120. {
  121.   echo "new Cell(".$cell['v'].",[";
  122.   if(count($cell['s'])>0)
  123.     {
  124.       echo "\n";
  125.       $first=true;
  126.       foreach($cell['s'] as &$subcell)
  127.     {
  128.       if($first)
  129.         $first=false;
  130.       else
  131.         echo ",\n";
  132.       output($subcell);
  133.     }
  134.       echo "\n";
  135.     }
  136.   echo "])";
  137. }
  138.  
  139. echo "var data=";
  140. output($cells);
  141. echo ";\n\n";
  142. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement