#include "util.h" #include #include #include /* Copyright (c) 2013-2021 Devine Lu Linvega 2018 web port by rezmason 2021 C89 port by rezmason Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE. */ FuncWrapper * wrapFunction(void (*f)(void)) { FuncWrapper *func = malloc(sizeof(FuncWrapper)); func->f = f; return func; } char * concat(const char *s1, const char *s2) { char *result = malloc(strlen(s1) + strlen(s2) + 1); strcpy(result, s1); strcat(result, s2); return result; } char * duplicate(const char *s) { char *result = malloc(strlen(s) + 1); strcpy(result, s); return result; } int isMatch(char *c, const char *candidates) { int i; for(i = 0; candidates[i] != 0; i++) if(*c == candidates[i]) return 1; return 0; } /* a homemade strtok_r, just like mom used to make */ char * getNextToken(char *s, const char *delim, char **save) { char *start, *end; if(s == NULL) { s = *save; } if(s == NULL) { return NULL; } start = s; while(*start != 0 && isMatch(start, delim)) { start++; /* skip past the first delimiters */ } if(*start == 0) { /* there is no token in the buffer */ *save = NULL; return NULL; } end = start; while(*end != 0 && !isMatch(end, delim)) { end++; /* skip past the non-delimiters */ } s = (*end != 0) ? end + 1 : NULL; /* save what comes after the end */ *end = 0; *save = s; return start; }