#include #include #include /** * Malloc, fatal on error. */ void *abac_xmalloc(size_t size) { void *ret; ret = malloc(size); if (ret == NULL) err(1, "malloc"); return ret; } /** * strdup fatal on error */ char *abac_xstrdup(char *source) { char *ret; if (source == NULL) return NULL; ret = strdup(source); if (ret == NULL) err(1, "strdup"); return ret; } /* make a brand new string with or without being trimmed */ char *abac_trim_quotes(char *string) { char *tmp=abac_xstrdup(string); int len=strlen(tmp); if( (tmp[len-1] == '\'' || tmp[len-1] == '"') && (tmp[0] == '\'' || tmp[0] == '"')) { tmp[len-1]='\0'; char *ntmp=abac_xstrdup(&tmp[1]); free(tmp); return ntmp; } return tmp; } /** * Split a string based on the given delimiter. */ void abac_split(char *string, char *delim, char **ret, int *num) { int len = strlen(delim); char *start = string; int count = 0; // split the string by the delim while ((start = strstr(string, delim)) != NULL) { *start = 0; ret[count++] = string; string = start + len; } ret[count++] = string; *num = count; }