Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Text Parser (https://forums.graalonline.com/forums/showthread.php?t=73857)

cbk1994 05-06-2007 11:26 PM

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 messageI used Parser.parseFile"data/servermessage.txt" ); This will return an array of the filesimply parsed.

parseTextstring/array )
You can give a string, or an array as a paramaterWill only parse the first param thoughso put things in an array if you want. If you give it an array, it will return an array. If you give it a stringit 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 
parseFilefilepath )
{
  if ( 
temp.fileContents != NULL )
  {
    
temp.fileContents NULL;
  }
  
  if ( 
temp.!= NULL )
  {
    
temp.a.clear();
  }
  
temp.fileContents.loadLinesfilepath );
  
  for ( 
temp.ntemp.fileContents )
  {
    
temp.a.addtemp.);
  }
  
  
temp.fileText parseTexttemp.);
  
  return 
temp.fileText;
}
// "Smart Parser" returns the same type (array or string)
public function parseTexttext )
{
  if ( 
temp.returnText != NULL )
  {
    if ( 
temp.returnText.size() > )
    {
      
temp.returnText.clear();
    }
    
    
temp.returnText NULL;
  }
  if ( 
text.size() > )
  {
    for ( 
temp.atext )
    {
      
temp.returnText.addparseLinetemp.) );
    }
  }
  else
  {
    
temp.returnText parseLinetext );
  }
  
  return 
temp.returnText;
}

// Actually parse the text.
function parseLinerealText )
{
    if ( 
temp.chars != NULL )
  {
    
temp.chars.clear();
  }
  
  for ( 
temp.0temp.realText.length(); temp.++ )
  {
    
temp.chars.addrealText.substringtemp.i) );
  }
  
  if ( 
temp.isVar != false )
  {
    
temp.isVar false;
  }
  for ( 
temp.ntemp.chars )
  {
    if ( 
temp.isVar == false // Variable has not been detected.
    
{
      if ( 
temp.== this.sepText // Start of a variable.
      
{
        
temp.isVar true;
      }
      else
      {
        
temp.parsedText temp.parsedText temp.n;
      }
    }
    else
    {
      if ( 
temp.!= this.sepText )
      {
        
temp.varAdd temp.varAdd temp.n;
      }
      else
      {
        
temp.isVar false;
        
temp.finalVar makevartemp.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.nickof @player.guild@! You currently have @clientr.moneymoney, and you have been online for @player.onlinetimeseconds! If we had a player.ip variableyours would be @player.ip@. 

Have fun, please leave comments.

killerogue 05-07-2007 12:19 AM

I've always wondered what the hell parsing even is.

Sum41Freeeeek 05-07-2007 12:55 AM

this is pretty neat, good job :)

cbk1994 05-07-2007 01:35 AM

Quote:

Originally Posted by Sum41Freeeeek (Post 1305971)
this is pretty neat, good job :)

ego ++; thanks.

Rapidwolve 05-07-2007 02:35 AM

An alternative is that you can use a substring to find the length of 'sepText' meaning that it can be more than one character, this is important because if there is are @'s in the file then they can easily be screwed up by this parser. I would make the 'sepText' something like '###obj.var###'

cbk1994 05-07-2007 03:08 AM

Quote:

Originally Posted by Rapidwolve (Post 1306011)
An alternative is that you can use a substring to find the length of 'sepText' meaning that it can be more than one character, this is important because if there is are @'s in the file then they can easily be screwed up by this parser. I would make the 'sepText' something like '###obj.var###'

Well, you can always find something fancy. ` is hardly used, for example.

` (squiggly key)

Rapidwolve 05-07-2007 03:09 AM

Or you can script it so its compatible with tags.
HTML Code:

<variable>player.nick</variable>

cbk1994 05-07-2007 02:17 PM

True, maybe I'll make an update for that.

Twinny 05-07-2007 03:04 PM

Wouldn't it be better to use format(); ?

Rapidwolve 05-07-2007 08:39 PM

Quote:

Originally Posted by Twinny (Post 1306095)
Wouldn't it be better to use format(); ?

What does format() have to do with this? We're talking about concatination in text files.

Deadly_Killer 05-07-2007 08:57 PM

Hmm. It may be better to do something like:

PHP Code:

while (temp.something.pos("@") > 0)
{
  
// stuff here.


Either way, cool :)

Rapidwolve 05-07-2007 08:59 PM

Quote:

Originally Posted by Deadly_Killer (Post 1306122)
Hmm. It may be better to do something like:

PHP Code:

while (temp.something.pos("@") > 0)
{
  
// stuff here.


Either way, cool :)

Didn't think of it like that thats a good idea. Could save alot of time too

cbk1994 05-08-2007 01:26 AM

Quote:

Originally Posted by Rapidwolve (Post 1306125)
Didn't think of it like that thats a good idea. Could save alot of time too

I don't tend to trust while() in GS2. Never has worked right for me.

Rapidwolve 05-08-2007 02:39 AM

Quote:

Originally Posted by cbkbud (Post 1306181)
I don't tend to trust while() in GS2. Never has worked right for me.

Most likely because you forgot to use the sleep function so you dont crash yourself. I've never liked using while() either

zokemon 05-08-2007 03:11 AM

You guys must not realize that this:
for (i = 0; i < j; i++) {
func();
}
is simply:
i = 0;
while (i < j) {
func();
i++;
}

Also, checking every single letters seems to be A LOT of work. Much more effcient if you just found the first position of "@" and just moved like such. Hell, you could even tokenize("@") the whole thing and just do a for loop through the tokens (changing the mode of "variable" or "text" on every other token).


All times are GMT +2. The time now is 08:27 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.