Changeset 65c753e for util.c


Ignore:
Timestamp:
Feb 4, 2011, 3:49:44 PM (10 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.8, release-1.9
Children:
fc7481a
Parents:
d3941a0
git-author:
David Benjamin <davidben@mit.edu> (01/24/11 19:38:53)
git-committer:
David Benjamin <davidben@mit.edu> (02/04/11 15:49:44)
Message:
Use a GPtrArray to manage the growing list in owl_parseline

It's much nicer than dealing with piles of owl_realloc. Also ensure that
our argvs are NULL-terminated so we may used the various *v glib
functions.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • util.c

    rf47696f r65c753e  
    100100void owl_parse_delete(char **argv, int argc)
    101101{
    102   int i;
    103 
    104   if (!argv) return;
    105  
    106   for (i=0; i<argc; i++) {
    107     if (argv[i]) owl_free(argv[i]);
    108   }
    109   owl_free(argv);
     102  g_strfreev(argv);
    110103}
    111104
     
    115108     the returned values.  If there is an error argc will be set to
    116109     -1, argv will be NULL and the caller does not need to free
    117      anything */
    118 
    119   char **argv;
     110     anything. The returned vector is NULL-terminated. */
     111
     112  GPtrArray *argv;
    120113  int i, len, between=1;
    121114  char *curarg;
    122115  char quote;
    123116
    124   argv=owl_malloc(sizeof(char *));
     117  argv = g_ptr_array_new_with_free_func(owl_free);
    125118  len=strlen(line);
    126119  curarg=owl_malloc(len+10);
     
    170163    if (quote=='\0') {
    171164      /* add the argument */
    172       argv=owl_realloc(argv, sizeof(char *)*((*argc)+1));
    173       argv[*argc] = owl_strdup(curarg);
    174       *argc=*argc+1;
     165      g_ptr_array_add(argv, owl_strdup(curarg));
    175166      strcpy(curarg, "");
    176167      between=1;
     
    183174  }
    184175
     176  *argc = argv->len;
     177  g_ptr_array_add(argv, NULL);
    185178  owl_free(curarg);
    186179
    187180  /* check for unbalanced quotes */
    188181  if (quote!='\0') {
    189     owl_parse_delete(argv, *argc);
    190     *argc=-1;
     182    g_ptr_array_free(argv, true);
     183    *argc = -1;
    191184    return(NULL);
    192185  }
    193186
    194   return(argv);
     187  return (char**)g_ptr_array_free(argv, false);
    195188}
    196189
Note: See TracChangeset for help on using the changeset viewer.