Thread: Text Parser
View Single Post
  #1  
Old 05-06-2007, 11:26 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Text Parser

Someone needed this, so I made it.

This reads text, and uses a "Smart Parser" to go through it and change variables to the value. To signal a variable, put @VARNAME@ in the code. GS1 will not work, and things such as servername will also not. I have tested this with clientr, client, player, server, and serverr. They all work. Here are the commands:

PHP Code:
parseFile( "path/to/file.txt" );
The way I used this was in a server message, I used Parser.parseFile( "data/servermessage.txt" ); This will return an array of the file, simply parsed.

parseText( string/array )
You can give a string, or an array as a paramater. Will only parse the first param though, so put things in an array if you want. If you give it an array, it will return an array. If you give it a string, it will return a string. 
Code for wNPC Parser:
PHP Code:
// Made by Chris Zakuto
function doInit()
{
  
// Must be a single character, sorry!
  
this.sepText = "@"; // The symbol that seperates variables from text.
}
function 
onCreated()
{
  
doInit();
}

public function 
parseFile( filepath )
{
  if ( 
temp.fileContents != NULL )
  {
    
temp.fileContents = NULL;
  }
  
  if ( 
temp.a != NULL )
  {
    
temp.a.clear();
  }
  
temp.fileContents.loadLines( filepath );
  
  for ( 
temp.n: temp.fileContents )
  {
    
temp.a.add( temp.n );
  }
  
  
temp.fileText = parseText( temp.a );
  
  return 
temp.fileText;
}
// "Smart Parser" returns the same type (array or string)
public function parseText( text )
{
  if ( 
temp.returnText != NULL )
  {
    if ( 
temp.returnText.size() > 0 )
    {
      
temp.returnText.clear();
    }
    
    
temp.returnText = NULL;
  }
  if ( 
text.size() > 0 )
  {
    for ( 
temp.a: text )
    {
      
temp.returnText.add( parseLine( temp.a ) );
    }
  }
  else
  {
    
temp.returnText = parseLine( text );
  }
  
  return 
temp.returnText;
}

// Actually parse the text.
function parseLine( realText )
{
    if ( 
temp.chars != NULL )
  {
    
temp.chars.clear();
  }
  
  for ( 
temp.i = 0; temp.i < realText.length(); temp.i ++ )
  {
    
temp.chars.add( realText.substring( temp.i, 1 ) );
  }
  
  if ( 
temp.isVar != false )
  {
    
temp.isVar = false;
  }
  for ( 
temp.n: temp.chars )
  {
    if ( 
temp.isVar == false ) // Variable has not been detected.
    
{
      if ( 
temp.n == this.sepText ) // Start of a variable.
      
{
        
temp.isVar = true;
      }
      else
      {
        
temp.parsedText = temp.parsedText @ temp.n;
      }
    }
    else
    {
      if ( 
temp.n != this.sepText )
      {
        
temp.varAdd = temp.varAdd @ temp.n;
      }
      else
      {
        
temp.isVar = false;
        
temp.finalVar = makevar( temp.varAdd );
        
temp.parsedText = temp.parsedText @ temp.finalVar;
        
temp.varAdd = "";
      }
    }
  }
  
  
// temp.parsedText is final output.
  
return temp.parsedText;
} 
To use this, input text like this:
PHP Code:
Welcome to @serverr.servername@, @player.account@:@player.nick@ of @player.guild@! You currently have @clientr.money@ money, and you have been online for @player.onlinetime@ seconds! If we had a player.ip variable, yours would be @player.ip@. 
Have fun, please leave comments.
__________________
Reply With Quote