00001 <?php
00014 class LFSStringProc
00015 {
00020 var $m_sColourPattern = '/(?:\^)([0-8])/';
00021
00026 var $m_sColourProc = '__sProcColourCSSStyle';
00027
00032 var $m_aColourMap = null;
00033
00042 function __construct($singletonCreator = false)
00043 {
00044 if(!$singletonCreator)
00045 trigger_error('Do not use \'new\' to instantiate LFSStringProc. Use LFSStringProc::singleton instead)', E_USER_ERROR);
00046 }
00047
00054 function &singleton()
00055 {
00056 static $singleton;
00057 if($singleton == null)
00058 $singleton =& new LFSStringProc(true);
00059
00060 return $singleton;
00061 }
00062
00072 function _chunkString($string, $pattern, $current)
00073 {
00074 $output = array(array('',''));
00075 $last = $current;
00076
00077
00078 $splitString = preg_split($pattern, $string, -1, PREG_SPLIT_DELIM_CAPTURE);
00079
00080
00081 for($i=0, $j=0; $i < count($splitString); $i+=2)
00082 {
00083 if($splitString[$i] !== '')
00084 {
00085
00086 $output[$j][0] = $current;
00087 $output[$j][1] .= $splitString[$i];
00088
00089 if(isset($splitString[$i+1]) && isset($splitString[$i+2]))
00090 {
00091 $last = $current;
00092 $current = $splitString[$i+1];
00093
00094
00095 if($last !== $current)
00096 {
00097 $j++;
00098 $output[$j] = array('', '');
00099 }
00100 }
00101 }
00102 }
00103 return $output;
00104 }
00105
00115 function process($string, $cProc=null, $cMap=null)
00116 {
00117 $string = $this->processCodePages($string);
00118 return $this->processColours($string, $cProc, $cMap);
00119 }
00120
00129 function processCodePages($string, $cpFile='xhtml_table.php')
00130 {
00131 static $cp;
00132 if($cp[$cpFile] == null)
00133 {
00134 $cp[$cpFile] = include(dirname(__FILE__).'/codepage-tables/'.$cpFile);
00135 }
00136
00137 $codePage =& $cp[$cpFile]['L'];
00138 $output = '';
00139 $len = strlen($string);
00140
00141 for($i=0; $i < $len; $i++)
00142 {
00143 $cChar = $string[$i];
00144 if($cChar === '^' && isset($cp[$cpFile][$string[$i+1]]))
00145 {
00146 $i++;
00147 $codePage =& $cp[$cpFile][$string[$i]];
00148 continue;
00149 }
00150
00151 $output .= (isset($codePage[$cChar])) ? ($codePage[$cChar]) : ($cChar);
00152 }
00153
00154 return $output;
00155 }
00156
00166 function processColours($string, $proc=null, $map=null)
00167 {
00168 if($proc == null)
00169 $proc = $this->m_sColourProc;
00170 if($map == null)
00171 $map = $this->m_aColourMap;
00172
00173 $string = $this->_chunkString($string, $this->m_sColourPattern, '8');
00174 return call_user_func($proc, $string, $map);
00175 }
00176
00185 function stripCodePages($string)
00186 {
00187 return str_replace(array('^E', '^C', '^L', '^:', '^G', '^T', '^B', '^J'), '', $string);
00188 }
00189
00198 function stripColours($string)
00199 {
00200 return str_replace(array('^0', '^1', '^2', '^3', '^4', '^5', '^6', '^7', '^8'), '', $string);
00201 }
00202
00210 function setColourProcessor($proc)
00211 {
00212 if(function_exists($proc))
00213 $this->m_sColourProc = $proc;
00214 }
00215
00224 function setColourMap($map)
00225 {
00226 $this->m_aColourMap = $map;
00227 }
00228 }
00229
00237 function __sProcColourCSSClass($input, $inMap=null)
00238 {
00239 static $defMap;
00240 if($inMap == null)
00241 {
00242 if($defMap == null)
00243 $inMap = $defMap = array('lfsColZero', 'lfsColOne', 'lfsColTwo', 'lfsColThree', 'lfsColFour', 'lfsColFive', 'lfsColSix', 'lfsColSeven', 'lfsColEight', 'lfsColEight');
00244 else
00245 $inMap = $defMap;
00246 }
00247 $output = '';
00248 for($i=0; $i < count($input); $i++)
00249 $output .= "<span class=\"{$inMap[$input[$i][0]]}\">{$input[$i][1]}</span>";
00250
00251 return $output;
00252 }
00253
00261 function __sProcColourCSSStyle($input, $inMap=null)
00262 {
00263 static $defMap;
00264 if($inMap == null)
00265 {
00266 if($defMap == null)
00267 $inMap = $defMap = array('000', 'F00', '3F0', 'FF0', '30F', 'F0F', '0FF', 'FFF', '999', '999');
00268 else
00269 $inMap = $defMap;
00270 }
00271 $output = '';
00272 for($i=0; $i < count($input); $i++)
00273 if($input[$i][0] != '')
00274 $output .= "<span style=\"color: #{$inMap[$input[$i][0]]}\">{$input[$i][1]}</span>";
00275
00276 return $output;
00277 }
00278 ?>