All pastes #435026 Raw Edit

IRC-format-parser

public php v1 · immutable
·published 2007-04-11 18:44 UTC
rendered paste body
<?php// Formatparse, as of April 11th 2007// Designed for my chatbot.// It's pretty cool stuff.// --Mathewdefine('DEBUG_NONE', 0);define('DEBUG_FATAL', 1);define('DEBUG_ERROR', 2);define('DEBUG_WARNING', 3);define('DEBUG_LOG', 4);define('DEBUG_VERBOSE', 5);define('DEBUG_DEBUG', 6);define('endl', "\n");define('winendl', "\r\n");define('windows', PHP_OS === 'WINNT');define('dirsep', DIRECTORY_SEPARATOR);define('bold',      "\x02");define('color',     "\x03");//define('bell',      "\x07");define('noformat',  "\x0F");//define('invert',    "\x16"); shh, it's not supporteddefine('underline', "\x1F");function logmsg($msg, $lvl, $force = 0){  if ($lvl <= DEBUG_WARNING or ($force & 1)) {    echo $msg."<br />\n";  }}function formatparse($s) {  // function formatparse($s);  $returnval = array();  $bold = false;  $underline = false;  $fg = -1; // INVISIBLE SANDWICH  $bg = -1;  $curpos = 0;  unset($arrid);  if (is_array($s)) {    $dbt = debug_backtrace();    $aaah = '';    foreach ($dbt as $dbt_) {      $aaah .= '< '.($dbt_['filename']).'\'s '.$dbt_['function'].':'.$dbt_['line'];    }    logmsg('Array to string conversion. '.$aaah, DEBUG_DEBUG, 1);  }  for ($i = 0; $i < strlen($s); $i++) {    $curchar = $s{$i};    switch ($curchar) {      case bold:        if ($bold = !$bold) {          if (!isset($curpos)) $curpos = $i;        } else {          unset($curpos);        }        unset($arrid);      break;      case underline:        if ($underline = !$underline) {          if (!isset($curpos)) $curpos = $i;        } else {          unset($curpos);        }        unset($arrid);      break;      case color:        if (preg_match('/(\d\d?)?(?:,(\d\d?)?)?/', $s, $colormatch, 0, $i+1) and strlen($colormatch[0]) and $colormatch[0] !== ',') {          if (!isset($curpos)) $curpos = $i;          if (isset($colormatch[1]) and !empty($colormatch[1])) {            $fg = intval($colormatch[1]);          }          if (isset($colormatch[2]) and !empty($colormatch[2])) {            $bg = intval($colormatch[2]);          }          $i += strlen($colormatch[0]);        } else {          unset($curpos);          $fg = -1;          $bg = -1;        }        unset($arrid);      break;      case noformat:        $bold = false;        $underline = false;        $fg = -1; // INVISIBLE SANDWICH        $bg = -1;        unset($arrid);        unset($curchar);      break;      default:        if (!isset($arrid)) {$returnval[$arrid = count($returnval)] = array('', 0, $bold, $underline, $fg, $bg);}        $returnval[$arrid][0] .= $curchar;      break;    }  }  //$returnval = array($s, $returnval);  return $returnval;}function formatparse_cat() {  //function formatparse_cat(..);  $returnval = array();  foreach (func_get_args() as $s) {    if (is_formatparse($s)) foreach ($s as $t) $returnval[] = $t;    elseif (is_string($s)) $returnval[] = formatparse($s);    else {      logmsg('argument not a formatparse', DEBUG_WARNING);      return false;    }  }  return $returnval;}function formatparse_substr($s, $start) {  // function formatparse_substr($s, $start, $len);  $argc = count($argv = func_get_args());  if ($argc >= 3) $len = $argv[1];  if ($start < 0 or !isset($len) or !is_int($len) or $len < 0) {    $slen = strlen(formatparse_toplain($s));    if ($start < 0) if (($start += $slen) < 0) {logmsg('$start underflow!', DEBUG_ERROR); return false;}    if (!isset($len) or !is_int($len)) $len = $slen-$start;    elseif ($len < 0) if (($len += $slen) < 0) {logmsg('$len underflow!', DEBUG_ERROR); return false;}  }  $modfp_substr_debug = 0;  if (!is_formatparse($s)) {logmsg('argument not a formatparse', DEBUG_WARNING); return false;}  $a = $s;//[1];  $a_ = array();  $curpos = 0;  unset($nextpos);  $end = $start+$len;  $bold = $underline = false;  $fg = $bg = -1;  logmsg($start.','.$len.','.$end, DEBUG_DEBUG, $modfp_substr_debug);  foreach ($a as $c) {    if (isset($nextpos)) $curpos = $nextpos;    $nextpos = $curpos+strlen($c[0]);    logmsg('-- '.$curpos.','.$nextpos.' '.$c[0], DEBUG_DEBUG, $modfp_substr_debug);    if ($start >= $nextpos) continue;    $a_[$id = count($a_)] = $c;    if ($start > $curpos) $a_[$id][0] = substr($a_[$id][0], $start-$curpos);    if ($end < $nextpos) $a_[$id][0] = substr($a_[$id][0], 0, $end-$nextpos);  }  //$returnval = array($s_, $a_);  logmsg('- '.formatparse_toirc($a_), DEBUG_DEBUG, $modfp_substr_debug);  return $a_;}function formatparse_toplain($s) {  // function formatparse_toplain($s);  if (!is_formatparse($s)) {logmsg('argument not a formatparse', DEBUG_WARNING); return false;}  $a = $s;//[1];  $returnval = '';  foreach ($a as $s) $returnval .= $s[0];  return $returnval;}function formatparse_toirc($s) {  // function formatparse_toirc($s);  if (!is_formatparse($s)) {logmsg('argument not a formatparse', DEBUG_WARNING); return false;}  $a = $s;//[1];  $returnval = '';  $bold = $underline = false;  $fg = $bg = -1;  foreach ($a as $s) {    if ($bold != $s[2]) {      $bold = $s[2];      $returnval .= bold;    }    if ($underline != $s[3]) {      $underline = $s[3];      $returnval .= underline;    }    if ($fg != $s[4]) {      $fg = $s[4];      if ($fg == -1) {        if ($bg > -1) {          $returnval .= color.','.str_pad($bg, 2, '0', STR_PAD_LEFT);        } else $returnval .= color;      } else {        $returnval .= color.str_pad($fg, 2, '0', STR_PAD_LEFT);      }    }    if ($bg != $s[5]) {      $bg = $s[5];      if ($bg == -1) {        $returnval .= color;        if ($fg > -1) {          $returnval .= color.str_pad($fg, 2, '0', STR_PAD_LEFT);        }      } else {        $returnval .= color;        if ($fg > -1) {          $returnval .= color.str_pad($fg, 2, '0', STR_PAD_LEFT);        }        $returnval .= ','.str_pad($bg, 2, '0', STR_PAD_LEFT);      }    }    $returnval .= $s[0];  }  return $returnval;}function formatparse_export($s) {  // function formatparse_export($s);  // This is rather ded, needs to be updated to conform with current formatparse structure.  if (!is_formatparse($s)) {logmsg('argument not a formatparse', DEBUG_WARNING); return false;}  $a = $s;  $output = '<'.'?xml version="1.0"?'.'>  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  <html xmlns="http://www.w3.org/1999/xhtml">  <head>  <meta http-equiv="Content-Type"  content="text/html; charset=iso-8859-1" />  <title>Eggtimer variable export</title>  <link rel="stylesheet" type="text/css" href="exportstyle.css" />  </head>  <body>  <h1>Eggtimer line export</h1>  <table class="line">  <tr>  ';  for ($i = 0; $i < count($a); $i++) {    $output .= '<td class="'.($a[$i][2] ? 'bold ': '').($a[$i][3] ? 'underline ':'').($a[$i][4]>=0 ? 'fg'.$a[$i][4].' ':'').($a[$i][5]>=0 ? 'bg'.$a[$i][5].' ':'').'">'.$a[$i][0].'</td>';  }  $output .= '</tr>  </table>  ';  $output .= '<h2>Source array</h2>'.endl.  highlight_string('<'.'?php'.endl.var_export($a, true).endl.'?'.'>', true).endl.  '</body>  </html>  ';  file_put_contents($filename = (dirname(__FILE__).dirsep.'exports'.dirsep.time().'.html'), $output);  return $filename;}function is_formatparse($o) {  // function is_formatparse($o);  $a = func_get_arg(0);  if (!is_array($a)) return false;  foreach ($a as $b) {    if (!is_array($b) or !(count($b) == 6) or !is_string($b[0]) or !is_int($b[1]) or (!is_bool($b[2]) and !($b[2] === ($b[2] ? 1 : 0))) or (!is_bool($b[3]) and !($b[3] === ($b[3] ? 1 : 0))) or !is_int($b[4]) or !is_int($b[5])) return false;  }  return true;}$s = formatparse(bold.'Hello world!');if (!is_formatparse($s)) logmsg('is_formatparse failed!', DEBUG_WARNING);if (strlen(formatparse_toplain($s)) !== 12) logmsg('Strlen of toplain doesn\'t match!', DEBUG_WARNING);if (strlen(formatparse_toirc($s)) !== 13) logmsg('Strlen of toirc doesn\'t match!', DEBUG_WARNING);echo 'Hello world!';?>