Created
April 30, 2018 21:14
-
-
Save miquels/73157dd90b7902c1a52d072712996b9d to your computer and use it in GitHub Desktop.
Advanced demo voor mies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <malloc.h> | |
struct key_val { | |
char *key; | |
char *value; | |
}; | |
struct key_val *key_val_split(char *input) | |
{ | |
char key[32]; | |
char val[128]; | |
if (sscanf(input, "%31[^=]=%127s", key, val) != 2) { | |
return NULL; | |
} | |
struct key_val *kv = calloc(1, sizeof(struct key_val)); | |
kv->key = strdup(key); | |
kv->value = strdup(val); | |
return kv; | |
} | |
void key_val_free(struct key_val *kv) { | |
if (kv != NULL) { | |
free(kv->key); | |
free(kv->value); | |
free(kv); | |
} | |
} | |
int main() | |
{ | |
char *teststr = strdup("hello=world"); | |
struct key_val *kv = key_val_split(teststr); | |
if (kv != NULL) { | |
printf("succes! [%s] [%s]\n", kv->key, kv->value); | |
} else { | |
printf("fail\n"); | |
} | |
key_val_free(kv); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment