Back to index

avfs  1.0.1
Defines | Typedefs | Functions
string_utils.h File Reference
#include "neon_defs.h"
#include <stdarg.h>
#include <ctype.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Defines

#define ASC2HEX(x)   (((x) <= '9') ? ((x) - '0') : (tolower((x)) + 10 - 'a'))
#define HEX2ASC(x)   ((x) > 9 ? ((x) - 10 + 'a') : ((x) + '0'))
#define EOL   "\r\n"
#define STRIP_EOL(str)
#define SBUFFER_CAST(buf)   (*(char **)buf)
#define SAFE_FREE(x)   do { if ((x)!=NULL) free((x)); (x) = NULL; } while (0)
#define CONCAT2(out, str1, str2)
#define CONCAT3(out, str1, str2, str3)
#define CONCAT4(out, str1, str2, str3, str4)

Typedefs

typedef struct sbuffer_ssbuffer

Functions

char * ne_utf8_encode (const char *str)
char ** split_string (const char *str, const char seperator, const char *quotes, const char *whitespace)
char ** split_string_c (const char *str, const char seperator, const char *quotes, const char *whitespace, int *count)
char ** pair_string (const char *str, const char compsep, const char kvsep, const char *quotes, const char *whitespace)
void split_string_free (char **components)
void pair_string_free (char **pairs)
char * shave_string (const char *str, const char ch)
char * ne_concat (const char *str,...)
char * sbuffer_data (sbuffer buf)
int sbuffer_size (sbuffer buf)
int sbuffer_concat (sbuffer buf,...)
sbuffer sbuffer_create (void)
sbuffer sbuffer_create_sized (size_t size)
void sbuffer_destroy (sbuffer buf)
int sbuffer_zappend (sbuffer buf, const char *str)
int sbuffer_append (sbuffer buf, const char *data, size_t len)
void sbuffer_clear (sbuffer buf)
int sbuffer_grow (sbuffer buf, size_t size)
void sbuffer_altered (sbuffer buf)
void md5_to_ascii (const unsigned char md5_buf[16], char *buffer)
void ascii_to_md5 (const char *buffer, unsigned char md5_buf[16])
char * sbuffer_finish (sbuffer buf)

Define Documentation

#define ASC2HEX (   x)    (((x) <= '9') ? ((x) - '0') : (tolower((x)) + 10 - 'a'))

Definition at line 33 of file string_utils.h.

#define CONCAT2 (   out,
  str1,
  str2 
)
Value:
do {                                             \
    out = malloc(strlen(str1) + strlen(str2) + 1);      \
    if (out != NULL) {                                  \
       strcpy(out, str1);                        \
       strcat(out, str2);                        \
    } else {                                     \
       abort();                                  \
    }                                            \
} while (0)

Definition at line 177 of file string_utils.h.

#define CONCAT3 (   out,
  str1,
  str2,
  str3 
)
Value:
do {                                                           \
    out = malloc(strlen(str1) + strlen(str2) + strlen(str3) + 1);     \
    if (out != NULL) {                                                \
       strcpy(out, str1);                                      \
       strcat(out, str2);                                      \
       strcat(out, str3);                                      \
    } else {                                                   \
       abort();                                                \
    }                                                          \
} while (0)

Definition at line 188 of file string_utils.h.

#define CONCAT4 (   out,
  str1,
  str2,
  str3,
  str4 
)
Value:
do {                                             \
    out = malloc(strlen(str1) + strlen(str2)            \
                + strlen(str3) + strlen(str4) + 1);     \
    if (out != NULL) {                                  \
       strcpy(out, str1);                        \
       strcat(out, str2);                        \
       strcat(out, str3);                        \
       strcat(out, str4);                        \
    } else {                                     \
       abort();                                  \
    }                                            \
} while (0)

Definition at line 200 of file string_utils.h.

#define EOL   "\r\n"

Definition at line 88 of file string_utils.h.

#define HEX2ASC (   x)    ((x) > 9 ? ((x) - 10 + 'a') : ((x) + '0'))

Definition at line 34 of file string_utils.h.

#define SAFE_FREE (   x)    do { if ((x)!=NULL) free((x)); (x) = NULL; } while (0)

Definition at line 163 of file string_utils.h.

#define SBUFFER_CAST (   buf)    (*(char **)buf)

Definition at line 113 of file string_utils.h.

#define STRIP_EOL (   str)
Value:
do {                                             \
   char *p;                                      \
   if ((p = strrchr(str, '\r')) != NULL) *p = '\0';     \
   if ((p = strrchr(str, '\n')) != NULL) *p = '\0';     \
} while (0)

Definition at line 90 of file string_utils.h.


Typedef Documentation

typedef struct sbuffer_s* sbuffer

Definition at line 104 of file string_utils.h.


Function Documentation

void ascii_to_md5 ( const char *  buffer,
unsigned char  md5_buf[16] 
)

Definition at line 389 of file string_utils.c.

{
    int count;
    for (count = 0; count<16; count++) {
       md5_buf[count] = ((ASC2HEX(buffer[count*2])) << 4) |
           ASC2HEX(buffer[count*2+1]);
    }
}
void md5_to_ascii ( const unsigned char  md5_buf[16],
char *  buffer 
)

Definition at line 377 of file string_utils.c.

{
    int count;
    for (count = 0; count<16; count++) {
       buffer[count*2] = HEX2ASC(md5_buf[count] >> 4);
       buffer[count*2+1] = HEX2ASC(md5_buf[count] & 0x0f);
    }
    buffer[32] = '\0';
}
char* ne_concat ( const char *  str,
  ... 
)

Definition at line 229 of file string_utils.c.

{
    va_list ap;
    sbuffer tmp = sbuffer_create();

    sbuffer_zappend(tmp, str);

    va_start(ap, str);
    sbuffer_concat(tmp, ap);
    va_end(ap);
    
    return sbuffer_finish(tmp);
}
char* ne_utf8_encode ( const char *  str)

Definition at line 398 of file string_utils.c.

{
    char *buffer = ne_malloc(strlen(str) * 2 + 1);
    int in, len = strlen(str);
    char *out;

    for (in = 0, out = buffer; in < len; in++, out++) {
       if ((unsigned char)str[in] <= 0x7D) {
           *out = str[in];
       } else {
           *out++ = 0xC0 | ((str[in] & 0xFC) >> 6);
           *out = str[in] & 0xBF;
       }
    }

    /* nul-terminate */
    *out = '\0';
    return buffer;
}
char** pair_string ( const char *  str,
const char  compsep,
const char  kvsep,
const char *  quotes,
const char *  whitespace 
)

Definition at line 161 of file string_utils.c.

{
    char **comps, **pairs, *split;
    int count = 0, n, length;
    comps = split_string_c(str, compsep, quotes, whitespace, &count);
    /* Allocate space for 2* as many components as split_string returned,
     * +2 for the NULLS. */
    pairs = ne_malloc((2*count+2) * sizeof(char *));
    if (pairs == NULL) {
       return NULL;
    }
    for (n = 0; n < count; n++) {
       /* Find the split */
       split = strchr(comps[n], kvsep);
       if (split == NULL) {
           /* No seperator found */
           length = strlen(comps[n]);
       } else {
           length = split-comps[n];
       }
       /* Enter the key into the array */
       pairs[2*n] = comps[n];
       /* Null-terminate the key */
       pairs[2*n][length] = '\0';
       pairs[2*n+1] = split?(split + 1):NULL;
    }
    free(comps);
    pairs[2*count] = pairs[2*count+1] = NULL;    
    return pairs;
}
void pair_string_free ( char **  pairs)

Definition at line 203 of file string_utils.c.

{
    int n;
    for (n = 0; pairs[n] != NULL; n+=2) {
       free(pairs[n]);
    }
    free(pairs);
}
void sbuffer_altered ( sbuffer  buf)

Definition at line 370 of file string_utils.c.

{
    buf->used = strlen(buf->data) + 1;
}
int sbuffer_append ( sbuffer  buf,
const char *  data,
size_t  len 
)

Definition at line 330 of file string_utils.c.

{
    if (sbuffer_grow(buf, buf->used + len)) {
       return -1;
    }
    memcpy(buf->data + buf->used - 1, data, len);
    buf->used += len;
    buf->data[buf->used - 1] = '\0';
    return 0;
}
void sbuffer_clear ( sbuffer  buf)

Definition at line 253 of file string_utils.c.

{
    memset(buf->data, 0, buf->length);
    buf->used = 1;
}  
int sbuffer_concat ( sbuffer  buf,
  ... 
)

Definition at line 281 of file string_utils.c.

{
    va_list ap;
    char *next;
    size_t totallen = buf->used; 

    /* Find out how much space we need for all the args */
    va_start(ap, buf);
    do {
       next = va_arg(ap, char *);
       if (next != NULL) {
           totallen += strlen(next);
       }
    } while (next != NULL);
    va_end(ap);
    
    /* Grow the buffer */
    if (sbuffer_grow(buf, totallen))
       return -1;
    
    /* Now append the arguments to the buffer */
    va_start(ap, buf);
    do {
       next = va_arg(ap, char *);
       if (next != NULL) {
           /* TODO: use stpcpy */
           strcat(buf->data, next);
       }
    } while (next != NULL);
    va_end(ap);
    
    buf->used = totallen;
    return 0;
}
sbuffer sbuffer_create ( void  )

Definition at line 341 of file string_utils.c.

{
    return sbuffer_create_sized(512);
}
sbuffer sbuffer_create_sized ( size_t  size)

Definition at line 346 of file string_utils.c.

{
    sbuffer buf = ne_malloc(sizeof(struct sbuffer_s));
    buf->data = ne_calloc(s);
    buf->length = s;
    buf->used = 1;
    return buf;
}
char* sbuffer_data ( sbuffer  buf)

Definition at line 243 of file string_utils.c.

{
    return buf->data;
}
void sbuffer_destroy ( sbuffer  buf)

Definition at line 355 of file string_utils.c.

{
    if (buf->data) {
       free(buf->data);
    }
    free(buf);
}
char* sbuffer_finish ( sbuffer  buf)

Definition at line 363 of file string_utils.c.

{
    char *ret = buf->data;
    free(buf);
    return ret;
}
int sbuffer_grow ( sbuffer  buf,
size_t  size 
)

Definition at line 260 of file string_utils.c.

{
    size_t newlen, oldbuflen;

#define SBUFFER_GROWTH 512

    if (newsize <= buf->length) return 0; /* big enough already */
    /* FIXME: ah, can't remember my maths... better way to do this? */
    newlen = ((newsize / SBUFFER_GROWTH) + 1) * SBUFFER_GROWTH;
    
    oldbuflen = buf->length;
    /* Reallocate bigger buffer */
    buf->data = realloc(buf->data, newlen);
    if (buf->data == NULL) return -1;
    buf->length = newlen;
    /* Zero-out the new bit of buffer */
    memset(buf->data+oldbuflen, 0, newlen-oldbuflen);

    return 0;
}
int sbuffer_size ( sbuffer  buf)

Definition at line 248 of file string_utils.c.

{
    return buf->used - 1;
}
int sbuffer_zappend ( sbuffer  buf,
const char *  str 
)

Definition at line 318 of file string_utils.c.

{
    size_t len = strlen(str);

    if (sbuffer_grow(buf, buf->used + len)) {
       return -1;
    }
    strcat(buf->data, str);
    buf->used += len;
    return 0;
}
char* shave_string ( const char *  str,
const char  ch 
)

Definition at line 212 of file string_utils.c.

{
    size_t len = strlen(str);
    char *ret;
    if (str[len-1] == ch) {
       len--;
    }
    if (str[0] == ch) {
       len--;
       str++;
    }
    ret = ne_malloc(len + 1);
    memcpy(ret, str, len);
    ret[len] = '\0';
    return ret;
}
char** split_string ( const char *  str,
const char  seperator,
const char *  quotes,
const char *  whitespace 
)

Definition at line 48 of file string_utils.c.

{
    return split_string_c(str, separator, quotes, whitespace, NULL);
}
char** split_string_c ( const char *  str,
const char  seperator,
const char *  quotes,
const char *  whitespace,
int *  count 
)

Definition at line 54 of file string_utils.c.

{
    char **comps;
    const char *pnt, *quot = NULL,
       *start, *end; /* The start of the current component */
    int count, /* The number of components */
       iswhite, /* is it whitespace */
       issep, /* is it the separator */
       curr, /* current component index */
       length, /* length of component */
       leading_wspace; /* in leading whitespace still? */

    /* Inefficient, but easier - first off, count the number of 
     * components we have. */
    count = 1;
    for (pnt = str; *pnt!='\0'; pnt++) {
       if (quotes != NULL) {
           quot = strchr(quotes, *pnt);
       }
       if (quot != NULL) {
           /* We found a quote, so skip till the next quote */
           for (pnt++; (*pnt!=*quot) && (*pnt!='\0'); pnt++)
              /* nullop */;
       } else if (*pnt == separator) {
           count++;
       }
    }

    if (give_count) {
       /* Write the count */
       *give_count = count;
    }

    /* Now, have got the number of components.
     * Allocate the comps array. +1 for the NULL */
    comps = ne_malloc(sizeof(char *) * (count + 1));

    comps[count] = NULL;
    
    quot = end = start = NULL;
    curr = 0;
    leading_wspace = 1;

    /* Now fill in the array */
    for (pnt = str; *pnt != '\0'; pnt++) {
       /* What is the current character - quote, whitespace, separator? */
       if (quotes != NULL) {
           quot = strchr(quotes, *pnt);
       }
       iswhite = (whitespace!=NULL) && 
           (strchr(whitespace, *pnt) != NULL);
       issep = (*pnt == separator);
       /* What to do? */
       if (leading_wspace) {
           if (quot!=NULL) {
              /* Quoted bit */
              start = pnt;
              length = 1;
              leading_wspace = 0;
           } else if (issep) {
              /* Zero-length component */
              comps[curr++] = ne_strdup("");
           } else if (!iswhite) {
              start = end = pnt;
              length = 1;
              leading_wspace = 0;
           }
       } else {
           if (quot!=NULL) {
              /* Quoted bit */
              length++;
           } else if (issep) {
              /* End of component - enter it into the array */
              length = (end - start) + 1;
              comps[curr] = ne_malloc(length+1);
              memcpy(comps[curr], start, length);
              comps[curr][length] = '\0';
              curr++;
              leading_wspace = 1;
           } else if (!iswhite) {
              /* Not whitespace - update end marker */
              end = pnt;
           }
       }
       if (quot != NULL) {
           /* Skip to closing quote */
           for (pnt++; *pnt!=*quot && *pnt != '\0'; ++pnt)
              /* nullop */;
           /* Last non-wspace char is closing quote */
           end = pnt;
       }
    }
    /* Handle final component */
    if (leading_wspace) {
       comps[curr] = ne_strdup("");
    } else {
       /* End of component - enter it into the array */
       length = (end - start) + 1;
       comps[curr] = ne_malloc(length+1);
       memcpy(comps[curr], start, length);
       comps[curr][length] = '\0';
    }
    return comps;
}
void split_string_free ( char **  components)

Definition at line 193 of file string_utils.c.

{
    char **pnt = components;
    while (*pnt != NULL) {
       free(*pnt);
       pnt++;
    }
    free(components);
}