00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00026 #include <errno.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029
00030 #include "treeconf_int.h"
00031
00032 RCSTAG("@(#)$Id: tc_break.c,v 1.3 2005/06/07 04:53:50 klmitch Exp $");
00033
00040 struct stringbuf {
00041 treeconf_str_t *sb_comps;
00042 int sb_count;
00043 int sb_size;
00044 };
00045
00051 #define STRINGBUF_INIT { 0, 0, 0 }
00052
00059 #define STRINGBUF_CHUNK 10
00060
00067 static unsigned int
00068 _add_string(struct stringbuf *buf, const char *str, int len)
00069 {
00070 treeconf_str_t *tmp = 0;
00071
00072 if (buf->sb_count + 1 >= buf->sb_size) {
00073 if (!(tmp = (treeconf_str_t *)realloc(buf->sb_comps,
00074 sizeof(treeconf_str_t) *
00075 (buf->sb_size + STRINGBUF_CHUNK))))
00076 return ENOMEM;
00077 else {
00078 buf->sb_comps = tmp;
00079 buf->sb_size += STRINGBUF_CHUNK;
00080 }
00081 }
00082
00083 buf->sb_comps[buf->sb_count].ts_string = str;
00084 buf->sb_comps[buf->sb_count].ts_length = len;
00085
00086 buf->sb_count++;
00087
00088 return 0;
00089 }
00090
00091 unsigned int
00092 tc_break(treeconf_str_t **str_p, int *cnt_p, const char *name,
00093 const char *delims)
00094 {
00095 struct stringbuf buf = STRINGBUF_INIT;
00096 char *tmp;
00097 unsigned int err = 0;
00098
00099 initialize_trcf_error_table();
00100
00101 if (!str_p || !cnt_p || !name || !*name)
00102 return EINVAL;
00103
00104 while ((tmp = strpbrk(name, delims))) {
00105 if ((err = _add_string(&buf, name, tmp - name))) {
00106 free(buf.sb_comps);
00107 return err;
00108 }
00109 name = tmp + 1;
00110 }
00111
00112
00113 if ((err = _add_string(&buf, name, strlen(name)))) {
00114 free(buf.sb_comps);
00115 return err;
00116 }
00117
00118
00119 *str_p = buf.sb_comps;
00120 *cnt_p = buf.sb_count;
00121
00122 return 0;
00123 }