Home
 
 
Search:  
C C++ Perl PHP Python HTML ShellScripts
 
 
  Coding Books
  Tutorials
  Search Code
  Browse Code
  Link to Us
  Site News
  Contact Metalshell
 
 
 
  Submit Code   Statistics
 



Standard C Library, The    (ISBN: 0131315099)


 

 List Price: $53.00
 Our Price: $53.00
 Used Price: $26.70

 Release Date: 01 July, 1991
 Manufacturer: Prentice Hall PTR (Paperback)
 Sales Rank: 60,769

 Author: P. J. Plauger









More Info

 Custom String Tokenizer 2004-06-18 22:03:18
 
Category: source:c:strings
Description: Example on creating your own string tokenizer without using strtok()
Platform: all
Author: mind
Viewed: 7941
Rating: 3.1/5 (26 votes)
If you have any questions about this piece of code or still need help, try posting your question on the forum.

 

Printable Version
custom_string_tok.c
/* Custom String Tokenizer - by mind@metalshell.com
 *
 * This example is more of an example on predetermined strings such as
 * reading in a configuration file and splitting the variable from the value
 * in or around quotation marks or equal signs.
 *
 * See below for porting this to read a configuration file or similar
 *
 * http://www.metalshell.com
 */
#include <stdio.h>
#include <string.h>

int RunExample();
int main()
{
	int	x, i;
	char	tmp[]="writing:your:own:string!tokenizer:yay";
	char	tnp[strlen(tmp)];

	/* See below
	 *
	 * int	z;
	 * char	exempt[]="!@:$.,";
	 * char	tmp[]="writing,your!own@str.ingto$kenize:ryay";
	 *
	 */

	/* Uncomment the next two lines to run the second example */

	// RunExample();

	// return 1;


	/* zero out our temp string */
	bzero(tnp, sizeof(tnp));

	for (x = 0, i = 0; x < strlen(tmp); x++)
	{
		/* Use this if you want to parse segments from
		 * numerous splitters.
		 *
		 * for (z = 0; z < strlen(exempt); z++)
		 * {
		 *		if (tmp[x] == exempt[z])
		 *		{
		 *			.....
		 *		}
		 * }
		 *
		 */

		/* check for end of segment */
		if (tmp[x] == ':' || tmp[x] == '!')
		{
			printf("Extracted segment: %s\n", tnp);

			/* zero out our temp string */
			bzero(tnp, sizeof(tnp));

			/* reset our writing point back to the beginning */
			i = 0;

			/* start from the beginning of our loop
			 * this is needed so the loop wont finish
			 * and write : or ! to tnp[0] */
			continue;
		}

		/* write our character at position tnp[i] from tmp[x] */
		tnp[i] = tmp[x];

		/* increase our temp string's position */
		i++;
	}
	return 1;
}

/* Extraction method; */
void Extract(char *str)
{
	int		x, i, y;
	char	tmp[strlen(str)];

	/* zero out our temp string */
	bzero(tmp, sizeof(tmp));

	for (x = 0, i = 0, y = 0; x < strlen(str); x++)
	{
		/* check to see if the last loop reached the end of
		 * our first segment. */
		if (!y)
		{
			/* nope.. check to see if this loop does */
			if (str[x] == '=')
			{
				y = 1;

				/* First segment passed; continue back to a new loop
				 * and search for the next segment */
				continue;
			}

			/* keep looping back until we've gone through the
			 * first segment (skipping) */
			continue;
		}

		/* check to see if we have passed the first segment */
		if (y == 1)
		{
			/* check if we are at the start of our valued segment */
			if (str[x] == 34)
			{
				/* we are.. setup y so we can start writing our value */
				y = 2;

				/* continue back to the beginning so we dont write '"' */
				continue;
			}

			/* '"' hasn't been reached yet continue looping.. */
			continue;
		}

		/* check to see if our value is done with an ending '"' */
		if (y == 2 && str[x] == 34)
		{
			/* Everything looks good; zero out our string and
			 * start writing our temp string to our real string */
			bzero(str, strlen(tmp) + 1);
			for (x = 0; x < strlen(tmp); x++)
				str[x] = tmp[x];

			/* end loop */
			break;
		}

		/* write to our temp string */
		tmp[i] = str[x];
		i++;
	}
}

/*
 * This section will show the ways you can use this example to
 * read in a confiruation file and extract what you want.
 *
 * user="mind"
 * pass="metalshell"
 *
 */
int RunExample()
{
	char	str1[]="user=\"mind\"";
	char	str2[]="pass=\"metalshell\"";

	Extract(str1);
	Extract(str2);

	printf("User: %s\n", str1);
	printf("Pass: %s\n", str2);

	return 1;
}

/*
 * Since this example is using predetermined strings, I strongly suggest
 * if you use this code to read in a configuration file; Please add checks
 * on the string your reading in before using Extract(). This example is
 * safe to use and has been tested for stability but could still cause issues.
 *
 * If you have any question's or want help adding checks to this please visit:
 * http://www.metalshell.com/forum
 *
 */
Rate this code:
(Not Helpful)  (Very Helpful) 

 
 
   Developer.*  
   Blue Parrots  
   Technipal  
   Defy Magazine  
   Code Project  
   Prog. Heaven  


Got Money?