User Tools

Site Tools


c:key-value_pairs
// takes a string and a delimiter.  turns the first instance of the delimiter into a null and returns a pointer to the next character
// if no delimiter found, returns null
// basically, a cheap one-token tokenizer (without the mess of strtok)
char* split2(char* str,char c) {
	char* p;
	for (p=str; *p; p++) {
		if ((*p)==c) {
			(*p)='\0';
			return p+1;
		}
	}
	return NULL;
}
 
int main(int argc, char** argv) {
	if (argc==1) usage();
	int i;
	for (i=1; i<argc; i++) {
		char* key;
		char* value;
		if (value=split2(argv[i],'=')) key = argv[i];
		else die("Parameter '%s' is not a key-value pair.\n",argv[i]);
		printf("'%s'='%s'\n",key,value);
	}
}
c/key-value_pairs.txt · Last modified: 2009/08/13 09:47 by tkbletsc