Thread: Parsing a string

  1. #1
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60

    Parsing a string

    Hi! I wrote a function which parses a string like this: "str0\\0str1\\0.....strN\\0"
    into an array of string like this:

    arg[0]="str0"
    arg[1]="str1"
    .
    .
    arg[N]="strN"

    it works fine,... but when the last string (strN) is not followed by "\\0" it doesn't work!

    Code:
    char** get_args(char *args, int lim) {
    	int read=0, i, k=0;
    	char **tmp;printf("Get args riceve: %s\n", args);
    	ec_null( (tmp=(char**)malloc(sizeof(char*)*lim)), 0 )
    	for (i=0; i<=strlen(args); i++) {
    		if (read>lim) return NULL;
    		if ((args[i]=='\\' && (!(args+i+1) ||
    				args[i+1]=='0')) || i==strlen(args)) {
    			ec_null( (tmp[read]=(char*)malloc(sizeof(char)*(i-k+1))), 0);
    			snprintf(tmp[read++], i-k+1, "%s", args+k);
    			k=(++i)+1;
    		}
    	}
    	if (read==lim)
    		return tmp;
    	return NULL;
    }
    The parameter "lim" is an integer that indicates the number of arguments the function has to find, if more or less than lim arguments will be found the function will return NULL to indicate there's an error!
    Thanks!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    First off, I presume by
    a string like this: "str0\\0str1\\0.....strN\\0"
    You actually meant a string like this str0\0str1\0strN\0, and that you are trying to match a \ with'\\'.

    There are number of problems in this line:
    Code:
    if ((args[i]=='\\' && (!(args+i+1) ||
    				args[i+1]=='0')) || i==strlen(args)) {
    This part is wrong. Advancing a pointer to point to something undefined does not make it NULL.
    So this part would mean "(if) the last character in args is \", except that it will never be true because of that.
    This part needs brackets to be functional, and it would mean "(if) this is the last character in args".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Yes i meant a string in that way, exactly!
    Thanks for the useful information, i'll change the code tomorrow and i will let you know!

  4. #4
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    I changed something in the code, what you said about the undefined value of the pointers is true! But the function assumes that the string args (parameter), is null terminated. Now it's working as i wanted, here there's the new version.

    Code:
    char** get_args(char *args, int lim) {
    	int read=0, i, k=0;
    	char **tmp;
    	ec_null( (tmp=(char**)malloc(sizeof(char*)*lim)), 0 )
    	for (i=0; i<=strlen(args); i++) {
    		if (read>lim) return NULL;
    		if (i<strlen(args)) {
    			if ((args[i]=='\\' && (!(args+i+1) ||
    					args[i+1]=='0'))) {
    				ec_null( (tmp[read]=(char*)malloc(sizeof(char)*(i-k+1))), 0);
    				snprintf(tmp[read++], i-k+1, "%s", args+k);
    				k=(++i)+1;
    			}
    		}
    	}
    	if (read==lim)
    		return tmp;
    	return NULL;
    }
    Your suggestions have been appreciated!
    Cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String parsing
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 07-03-2008, 05:06 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM