Skip to content

Instantly share code, notes, and snippets.

@ACEfanatic02
Created September 2, 2014 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ACEfanatic02/dd2ac5fc6a04aa310065 to your computer and use it in GitHub Desktop.
Save ACEfanatic02/dd2ac5fc6a04aa310065 to your computer and use it in GitHub Desktop.
A different take on strtok_r.
#include <stdlib.h>
#include <stdio.h>
typedef struct tokenize_state_t {
const char * position;
} tokenize_state_t;
int check_delim(char c, const char * delim) {
for (const char * cur = delim; *cur != '\0'; ++cur) {
if (c == *cur) {
return 1;
}
}
return 0;
}
int tokenize(const char * str, const char * delim, char ** token, tokenize_state_t ** state) {
if (delim == NULL || token == NULL || state == NULL) {
goto error;
}
const char * start = str;
if (*state == NULL) {
*state = calloc(1, sizeof(tokenize_state_t));
if (*state == NULL) {
goto error;
}
}
else {
start = (*state)->position;
}
if (start == NULL) {
// No string given, and no state struct.
goto error;
}
// Skip leading delim characters.
while (*start && check_delim(*start, delim)) start++;
const char * cur = start;
*token = (char *)start;
// Find end of token.
while (*cur && !check_delim(*cur, delim)) cur++;
(*state)->position = cur;
return cur - start;
error:
return 0;
}
int main() {
const unsigned int BUFFER_SIZE = 1024;
char buffer[BUFFER_SIZE];
if (fgets(buffer, BUFFER_SIZE, stdin)) {
tokenize_state_t * state = NULL;
char * tok_start = buffer;
int tok_len = 0;
do {
tok_len = tokenize(tok_start, " ", &tok_start, &state);
fwrite(tok_start, sizeof(char), tok_len, stdout);
} while(tok_len > 0);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment