In need of a string splitting function.


(jaybird) #1

Need to see if anyone knows of a good way to split a string into multiple strings delimited by spaces. I need this because it seems that when someone uses a global chat (or just chat, period) through one of the messagemode functions (press t, y, or u while playing), it puts the entire message as one single arg, so you can’t trap the individual words. Example:

When typing from console, args get split up like this:
“say” “!setlevel” “1” “1”

When typing in a message box, args get split up like this:
“say” “!setlevel 1 1”

I need a way to get those values out of that string. Anyone have a function to do so, or a better way to do this?

Thanks for the help!

BTW, I’m sure I could edit the cgame source to change this, but I need this to be server-side only.


(carnage) #2

you need a to get the hole string and a vairaibale

then using two more vairibales have one start at 0 and the other at 0
have the second do increaments of one and as it does so look for spaces in the character poistion in the string when it finds a spance you have the starting character and ending character you need to take a work out of string out

then chanage the vairiable that is 0 to the value of the other and continuse the proces

sorry ime not good at explaining this but i only do lame VB.net programing


(jaybird) #3

I actually tried making a function like that, but my pointer arithmetic skills are a bit sub-par. I’d think there has to be an easier way to do this…


(SCDS_reyalP) #4

This is a good reason to implement your mod commands as proper commands, rather than parsing them out of chat. E.g. /setlevel in the console instead of !setlevel in chat.That said, you could use something like strtok (from the C runtime libary) or roll your own. There are also some functions in the game that might be helpful, either as examples, or to use directly. Q_str* and COM_Parse* come to mind. Failing that, search google :moo:


(jaybird) #5

It would be much better to do them as console commands, however, not all of our admins are too familiar with doing it that way, and have been using chat commands for while. That’s the only drawback to doing it that way.

I did, however, figure out the solution to my own problem. It’s amazing how things just come to you after you stop digging through code and take a break. sscanf() is my friend in this case ;]