Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $esc=char(27);
- function unesc($str)
- {
- global $esc;
- $arr=explode($esc,$str);
- foreach($arr as $i=>$s)
- $arr[$i][0]^='@';
- return(implode($arr));
- }
- //$cescarr=array($esc=>$esc.($esc^'@'),'{'=>$esc.('{'^'@'),'}'=>$esc.('}'^'@'),'|'=>$esc.('|'^'@'));
- $cescarr=array($esc=>$esc,'{'=>$esc,'}'=>$esc,'|'=>$esc);
- foreach($cescarr as $i=>$s)
- $cescarr[$i].=$i^'@';
- function esccells($str)
- {
- global $cescarr;
- return(strtr($str,$cescarr));
- }
- function splitlineone($str)
- {
- $fields=explode("}",$str);
- if(strlen($fields[count($fields)-1]))
- $fields[count($fields)-1].="{";
- else
- unset($fields[count($fields)-1]);
- foreach($fields as $f)
- {
- if(count($arr=explode("{",$f))<>2)
- echo "error"; // pattern: { { } etc
- else
- {
- $cell=unesc($arr[0]);
- $subcells=array_map("unesc",split("|";$arr[0]));
- // now do something with the cell data
- }
- }
- }
- $cells=array('v'=>"null",'s'=>array());
- $index=array(0,0);
- $depth=1;
- function setdepth($dep)
- {
- global $index,$depth;
- $ndep=$dep+1;
- if($ndep<$depth) // new line at shallower depth
- {
- $depth=$ndep;
- $index[$depth]++;
- }
- else if($ndep>$depth) // descend to new depth
- {
- $index[0]=0;
- $index[$depth]--; // subcells of previous line
- while($depth<$ndep)
- {
- $depth++;
- $index[$depth]=0;
- }
- }
- }
- function advanceline()
- {
- global $index,$depth;
- $index[0]=0; //return to first colummn
- $index[$depth]++; //and advance the line
- }
- function putcell($val)
- {
- global $cells,$index,$depth;
- $cell=&$cells;
- for($d=0;$d=$depth;$d++)
- {
- $i=$index[$d];
- if(!isset($cell['s'][$i]))
- $cell['s'][$i]=array('v'=>"null",'s'=>array());
- $cell=&$cell['s'][$i];
- }
- $cell['v']=$val;
- $index[0]++; //advance the column
- }
- function splitline($str)
- {
- if($str[0]=='>')
- {
- $i=strpos($str,":");
- setdepth(intval(substr($str,1,$i-1)));
- $str=substr($str,$i+1);
- }
- $fields=explode("|",$str);
- foreach($fields as $f)
- {
- $cell=unesc($f);
- putcell($cell);
- }
- }
- $filename="test.core";
- $file=fopen($filename,"r");
- while(!feof($file))
- {
- splitline(fgets($file));
- advanceline();
- }
- function output(&$cell)
- {
- echo "new Cell(".$cell['v'].",[";
- if(count($cell['s'])>0)
- {
- echo "\n";
- $first=true;
- foreach($cell['s'] as &$subcell)
- {
- if($first)
- $first=false;
- else
- echo ",\n";
- output($subcell);
- }
- echo "\n";
- }
- echo "])";
- }
- echo "var data=";
- output($cells);
- echo ";\n\n";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement