Split String Library Function

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
chrisa128

Split String Library Function

Post by chrisa128 »

Hi all,

I have just come to creating my "cd" command in my console and have relised I have no way to pass params.

Can anyone give me a pointer on how I should do this.

1 String in = Multiple Strings out

How would I define the Multiple strings out?
Therx

Re:Split String Library Function

Post by Therx »

Don't know off hand but if you take a look at the source to some perl interpreter it may help. Perl has a split function which takes the form of something like:-

split(char div, char *buffer);

the div specifies what character seperates the arguments. I'm not sure how you'd return a 3D array in C but you may be able to return it as a standard char* but using the va_start and va_arg macros which are defined as:-

Code: Select all

typedef unsigned char *va_list;

#define???STACKITEM???int

#define???VA_SIZE(TYPE)???????????????\
???((sizeof(TYPE) + sizeof(STACKITEM) - 1)???\
??????& ~(sizeof(STACKITEM) - 1))

#define???va_start(AP, LASTARG)???\
???(AP=((va_list)&(LASTARG) + VA_SIZE(LASTARG)))

#define va_end(AP)???/* nothing */

#define va_arg(AP, TYPE)???\
???(AP += VA_SIZE(TYPE), *((TYPE *)(AP - VA_SIZE(TYPE))))
But maybe you'd have the problem that the code to handle the returned value being longer than the parsing code

EDIT : Can you have an array of pointers? As I remember char *pointers[10] won't compile. By the way my method of getting an argument(singular) is to just do a memcpy(arg, buffer + length_of_command, strlen(buffer) - length_of_command);
Tim

Re:Split String Library Function

Post by Tim »

You probably want to implement your own strtok function. If you do that, you won't need to mess with variable argument lists or arrays of pointers.
Post Reply