Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

tc_break.c

Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 2004, 2005 by Kevin L. Mitchell <klmitch@mit.edu>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 **
00018 ** @(#)$Id: tc_break.c,v 1.3 2005/06/07 04:53:50 klmitch Exp $
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) { /* need more space? */
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; /* OK, remember the new space */
00079       buf->sb_size += STRINGBUF_CHUNK;
00080     }
00081   }
00082 
00083   buf->sb_comps[buf->sb_count].ts_string = str; /* store the string */
00084   buf->sb_comps[buf->sb_count].ts_length = len;
00085 
00086   buf->sb_count++; /* increment the count... */
00087 
00088   return 0; /* And return success! */
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) /* um, is it a good name? */
00102     return EINVAL;
00103 
00104   while ((tmp = strpbrk(name, delims))) { /* go through string components */
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   /* Add the last component */
00113   if ((err = _add_string(&buf, name, strlen(name)))) {
00114     free(buf.sb_comps);
00115     return err;
00116   }
00117 
00118   /* Return the results */
00119   *str_p = buf.sb_comps;
00120   *cnt_p = buf.sb_count;
00121 
00122   return 0; /* we succeeded! */
00123 }

Generated on Wed Jun 8 09:18:27 2005 for treeconf by  doxygen 1.3.9.1