Raised This Month: $ Target: $400
 0% 

How do I parse a text file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Fire-Wired
Member
Join Date: May 2005
Location: Maryland
Old 06-25-2005 , 23:33   How do I parse a text file
Reply With Quote #1

I need some help in creating a plugin that has dynamic commands. I'm writting a plugin that pulls its data from a text file, it goes in the format !command <name from txt file>.

What i need help doing is ignoring comments, and parsing the multiple tabed data into a 2D array. How should i go about tokenizing a line of data from a text file? I have a text file of about 20 lines with 9 multitabed data parts.

In the text file it looks like this, but with out the <>:
<name> <setting 1> <setting 2> <setting 3> ... <setting 9>
Fire-Wired is offline
Send a message via AIM to Fire-Wired Send a message via MSN to Fire-Wired Send a message via Yahoo to Fire-Wired
jtp10181
Veteran Member
Join Date: May 2004
Location: Madison, WI
Old 06-26-2005 , 10:02  
Reply With Quote #2

one of these should do the trick

Code:
/* Breaks a string into two halves, by token.
   See strbreak() for doing this with parameters.
   Example:
   str1[] = This *is*some text
   strtok(str1, left, 24, right, 24, '*')
   left will be "This "
   Right will be "is*some text"
   If you use trimSpaces, all spaces are trimmed from Left.
*/
native strtok(const text[], Left[], leftLen, Right[], rightLen, token=' ', trimSpaces=0);
   

/* Gets parameters from text one at a time
   It breaks a string into the first parameter and the rest of the parameters
   (A left side and right side of the string)
   Example: to split text: "^"This is^" the best year",
   strbreak(text, arg1, len1, arg2, len2)
   arg1="This is", arg2=the best year
   This is more useful than parse() because you can keep breaking
   any number of arguments */
native strbreak(const text[], Left[], leftLen, Right[], rightLen);
you can use a while loop and when the strlen of "Right" is 0 then its gone through everything.

Here is an example using my own stock that I made cause I didn't like the two in amxx

Code:
	//Get the data tag out of the way
	strbrkqt(XP, LeftXP, 31, XP, 1023)
	strbrkqt(XPG, LeftXPG,31, XPG, 1023)

	while ( strlen(XP) > 0 && strlen(XPG) > 0 && loadCount < gNumLevels ) {
		loadCount++

		strbrkqt(XP, LeftXP, 31, XP, 1023)
		strbrkqt(XPG, LeftXPG, 31, XPG, 1023)

		gXPLevel[loadCount] = str_to_num(LeftXP)
		gXPGiven[loadCount] = str_to_num(LeftXPG)

		if (loadCount == 0 && gXPLevel[loadCount] != 0) {
			debugMessage("Level 0 must have an XP setting of 0, adjusting automatically",0,0)
			gXPLevel[loadCount] = 0
		}
		if (loadCount > 0 && gXPLevel[loadCount] < gXPLevel[loadCount - 1]) {
			format(debugt,255,"Level %d is less XP than the level before it (%d < %d), adjusting NUMLEVELS to %d", loadCount, gXPLevel[loadCount], gXPLevel[loadCount - 1], loadCount - 1)
			debugMessage(debugt,0,0)
			gNumLevels = loadCount - 1
			break
		}

		format(debugt,255,"XP Loaded - Level: %d  -  XP Required: %d  -  XP Given: %d",loadCount,gXPLevel[loadCount],gXPGiven[loadCount])
		debugMessage(debugt,0,3)
	}
and my stock....

Code:
stock strbrkqt(const text[], Left[], leftLen, Right[], rightLen)
{
	//Breaks text[] into two parts, Left[], and Right[]
	// Left[] will contain the first parameter (either quoted or non-quoted)
	// Right[] contain the rest of the string after Left[], not including the space
	new bool:in_quotes = false
	new bool:done_flag = false
	new i, left_pos = 0

	for ( i = 0; i < strlen(text); i++) {

		if (equali(text[i], "^"", 1) && !done_flag) {
			if (in_quotes) {
				done_flag = true
				in_quotes = false
			}
			else in_quotes = true
		}
		else if ( isspace(text[i]) && !in_quotes ) {
			if (left_pos > 0) {
				done_flag = true
			}
		}
		else if (!done_flag && left_pos <= leftLen) {
			setc(Left[left_pos], 1, text[i])
			left_pos++
		}
		else if (done_flag) break
	}

	Left[left_pos] = 0
	copy(Right,rightLen,text[i])

	return true
}
__________________
jtp10181 is offline
Send a message via ICQ to jtp10181 Send a message via AIM to jtp10181 Send a message via MSN to jtp10181 Send a message via Yahoo to jtp10181
Fire-Wired
Member
Join Date: May 2005
Location: Maryland
Old 06-26-2005 , 18:02  
Reply With Quote #3

To be honest this is my first time writting a plugin in the small language. I'm not stranger to programming i know c and basic c++. I have just finished skimming the small language intro from the compu phase site.

With that said, did you add the native to the core.inc ? Then where did you add your stock function you defined in the native?

This is a really good function, but i'm looking for a function as flexable as the php strtok().
Fire-Wired is offline
Send a message via AIM to Fire-Wired Send a message via MSN to Fire-Wired Send a message via Yahoo to Fire-Wired
jtp10181
Veteran Member
Join Date: May 2004
Location: Madison, WI
Old 06-26-2005 , 21:11  
Reply With Quote #4

you can add a stock right into the sma

those natives are IN the string.inc, all you have to do is use them.
__________________
jtp10181 is offline
Send a message via ICQ to jtp10181 Send a message via AIM to jtp10181 Send a message via MSN to jtp10181 Send a message via Yahoo to jtp10181
Fire-Wired
Member
Join Date: May 2005
Location: Maryland
Old 06-28-2005 , 01:43  
Reply With Quote #5

I wrote out the base functions to grab and parse the text file. And I'm getting compile errors:
Code:
//// glow_list.sma
// D:\HLServer\SvenCoop\addons\amxmodx\scripting\glow_list.sma(53) : error 035:
argument type mismatch (argument 2)
// D:\HLServer\SvenCoop\addons\amxmodx\scripting\glow_list.sma(60) : warning 204
: symbol is assigned a value that is never used: "in_comment"
// D:\HLServer\SvenCoop\addons\amxmodx\scripting\glow_list.sma(65) : error 035:
argument type mismatch (argument 1)
// D:\HLServer\SvenCoop\addons\amxmodx\scripting\glow_list.sma(74) : error 035:
argument type mismatch (argument 1)
// D:\HLServer\SvenCoop\addons\amxmodx\scripting\glow_list.sma(78) : error 029:
invalid expression, assumed zero
// D:\HLServer\SvenCoop\addons\amxmodx\scripting\glow_list.sma(88) : error 035:
argument type mismatch (argument 1)
// D:\HLServer\SvenCoop\addons\amxmodx\scripting\glow_list.sma(91) : warning 203
: symbol is never used: "delim"
// D:\HLServer\SvenCoop\addons\amxmodx\scripting\glow_list.sma(91) : warning 203
: symbol is never used: "rawline"
//
// 5 Errors.
// Could not locate output file compiled\glow_list.amx (compile failed).
//
// Compilation Time: 0.2 sec
// ----------------------------------------
I'm not sure how to fix the "argument type mismatch" error, and why line 78 has an error.

Here is the plugin source:
Code:
/* Test strbreak to work on tabs */ #include <amxmodx> #include <amxmisc> new glows[50][9]        // max glows 50, parameters 9 public plugin_init() {   register_plugin("Glow Function Parse Test", "0.1", "Steven W")   register_clcmd("say /glowlist", "glowlist") } public glowlist(id) {     fetch_glows(id)   // list_glows()   return PLUGIN_CONTINUE } fetch_glows(id) {     new rawline[256],filename[128],linenum = 0,textlen = 0     new totalglows = 0,in_comment = 0     new delim[] = "\t"         get_configsdir(filename,127)     format(filename,127,"%s/glows.cfg",filename)        // Grab the glows file in amx config folder     if( ! file_exists(filename) )     {         log_amx("%s file not found, custom glows could not be loaded.",filename)         return PLUGIN_CONTINUE     }     while (read_file(filename,linenum,rawline,256,textlen))     // returns false when EOF     {         if (contain(rawline, "*/"))         {             in_comment = 0         }         else if (equal(rawline, "/*",2))         {             in_comment = 1         }         else if (equal(rawline, "//",2) && in_comment == 0)         {             //found comment line ignore this line         }else{             line_parser(id,rawline,delim[],totalglows)  // if works correctly line_parser will add into glows automaticly             totalglows++         }         linenum++     }     client_print(id, print_chat, "Complete, Total Glows: %d \n", totalglows)     return linenum } line_parser(id,rawline,delim,totalglows) {     if (strlen(rawline) == 0 || strlen(delim[]) == 0)     {         return 0     }         new token[256]     new remaining[256]     new param = 0         strbreak(rawline, token[], 256, remaining[], 256)         while( strlen(remaining) > 0 || param < 10 )     {         if (isspace(token[]) || equal(token,delim))         {             // ignore token because its a tab         }else{                         copy(glows[totalglows][param],128,token)             client_print(id, print_chat, "Loading... %s. At line: %d \n", token, totalglows)             token = ""             param++         }         strbreak(rawline, token[], 256, remaining, 256)     }     return PLUGIN_HANDLED }
Fire-Wired is offline
Send a message via AIM to Fire-Wired Send a message via MSN to Fire-Wired Send a message via Yahoo to Fire-Wired
v3x
Veteran Member
Join Date: Oct 2004
Location: US
Old 06-28-2005 , 03:37  
Reply With Quote #6

Code:
        if (isspace(token[]) || equal(token,delim)
Needs another ) at the end.
__________________
What am I doing these days? Well, I run my own Rust server. It's heavily modded. If you'd like to join, the ip is 167.114.101.67:28116

I also created a website called Rust Tools. It will calculate and tell you the raw amounts of resources needed to craft items.
v3x is offline
Fire-Wired
Member
Join Date: May 2005
Location: Maryland
Old 06-29-2005 , 03:20  
Reply With Quote #7

Thanks. I still get the same compile errors, I don't understand what it means by argument type mismatch. All the errors point to the rawline variable. But i just don't see how its a problem.
Code:
//// glow_list.sma
// D:\HLServer\SvenCoop\addons\amxmodx\scripting\glow_list.sma(53) : error 035:
argument type mismatch (argument 2)
// D:\HLServer\SvenCoop\addons\amxmodx\scripting\glow_list.sma(65) : error 035:
argument type mismatch (argument 1)
// D:\HLServer\SvenCoop\addons\amxmodx\scripting\glow_list.sma(74) : error 035:
argument type mismatch (argument 1)
// D:\HLServer\SvenCoop\addons\amxmodx\scripting\glow_list.sma(78) : error 029:
invalid expression, assumed zero
// D:\HLServer\SvenCoop\addons\amxmodx\scripting\glow_list.sma(88) : error 035:
argument type mismatch (argument 1)
//
// 5 Errors.
// Could not locate output file compiled\glow_list.amx (compile failed).
//
// Compilation Time: 0.2 sec
// ----------------------------------------
Fire-Wired is offline
Send a message via AIM to Fire-Wired Send a message via MSN to Fire-Wired Send a message via Yahoo to Fire-Wired
mobytoss
Senior Member
Join Date: Jun 2004
Location: On my TS server
Old 06-29-2005 , 03:48  
Reply With Quote #8

Can you tell us which lines are the lines with the errors are on?
__________________
"As we know, There are known knowns. There are things we know we know. We also know There are known unknowns. That is to say We know there are some things We do not know. But there are also unknown unknowns, The ones we don't know We don't know."
mobytoss is offline
DahVid
Senior Member
Join Date: Jun 2005
Old 06-29-2005 , 04:01  
Reply With Quote #9

All the lines containing rawline.
I'm a newb but can you use an array for that?
DahVid is offline
mobytoss
Senior Member
Join Date: Jun 2004
Location: On my TS server
Old 06-29-2005 , 04:05  
Reply With Quote #10

Aha, I think it's cause he's defining the stuff as 0 at the beginning, then trying to use the variable with strings, after it's become an int array, I think. Define the = 0 variables on a new line
__________________
"As we know, There are known knowns. There are things we know we know. We also know There are known unknowns. That is to say We know there are some things We do not know. But there are also unknown unknowns, The ones we don't know We don't know."
mobytoss is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 15:56.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode