strops.c 2.3 KB
Newer Older
Y
yangwenjun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#include <ctype.h>
#include "strops.h"

/* string to lower */
void strlwc(char *str)
{
    if (str == NULL) return;
    while (*str != '\0') {
        *str = (char)tolower(*str);
        str++ ;
    }
    return;
}
/* trim head and tail spaces of string */
size_t strtrim(char *str)
{
    char *last = NULL;
    char *dest = str;

    if (str == NULL) return 0;

    last = str + strlen(str);
    while (isspace((int)*str) && *str) str++;
    while (last > str) {
        if (!isspace((int)*(last-1))) break;
        last--;
    }
    *last = (char)0;

    memmove(dest, str, last-str+1);
    return last-str;
}
/* split string to list by given string */
strlist *strsplit(const char *str, const char *split_s)
{
    char *cur, *next;
    if(!str) return NULL;

    strlist *sl = strlist_alloc(0);
    char *ss = strdup(str);
    if (!sl || !ss) {
        strlist_free(sl);
        free(ss);
        return NULL;
    }

    cur = ss;
    while (next = strstr(cur, split_s)) {
        *next = 0;
        strtrim(cur);
        strlist_set(sl, cur);
        cur = next + strlen(split_s);
    }
    strtrim(cur);
    strlist_set(sl, cur);
    free(ss);
    return sl;
}

#define STR_DEFAULT_SIZE   16

strlist *strlist_alloc(size_t size)
{
    strlist *strs;
    if (size < STR_DEFAULT_SIZE) size = STR_DEFAULT_SIZE ;

    strs = (strlist *)calloc(1, sizeof *strs) ;

    if (strs) {
        strs->strs  = (char **)calloc(size, sizeof *strs->strs);
        if (strs->strs) {
            strs->size = size;
        } else {
            free(strs);
            strs = NULL;
        }
    }
    return strs ;
}

static void strlist_realloc(strlist *strs)
{
    if(!strs) return;
    size_t size = 2*strs->size;
    if (size) {
        char **ss = (char **)realloc(strs->strs, size * (sizeof *strs->strs));
        if (ss) {
            strs->size = size;
            strs->strs = ss;
        }
    }
    return;
}

void strlist_free(strlist *strs)
{
    if (!strs) return;
    for (size_t i=0; i < strs->num; i++) {
        free(strs->strs[i]);
    }
    free(strs->strs);
    free(strs);
}

void strlist_set(strlist *strs,const char *str)
{
    if (!strs || !str) return;
    if (strs->num == strs->size) {
       strlist_realloc(strs);
    }
    if (strs->num < strs->size) {
        strs->strs[strs->num] = strdup(str);
        if (strs->strs[strs->num]) strs->num++;
    }
}