Changeset d275eb2


Ignore:
Timestamp:
Jan 2, 2011, 3:31:58 PM (11 years ago)
Author:
David Benjamin <davidben@mit.edu>
Branches:
master, release-1.8, release-1.9
Children:
2bc6ad35
Parents:
f640876
git-author:
David Benjamin <davidben@mit.edu> (12/15/10 13:18:34)
git-committer:
David Benjamin <davidben@mit.edu> (01/02/11 15:31:58)
Message:
Replace atokenize with glib's g_strsplit_set

Glib's had g_strsplit_set since forever, and it does exactly the same
thing as atokenize, modulo needing a pesky NULL check. We may as well
use it.
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • commands.c

    rb31f1c9 rd275eb2  
    16701670  char *lastrv = NULL, *newbuff;
    16711671  char **commands;
    1672   int  ncommands, i;
     1672  int  i;
    16731673  if (argc < 2) {
    16741674    owl_function_makemsg("Invalid arguments to 'multi' command.");   
     
    16881688    }
    16891689  }
    1690   commands = atokenize(newbuff, ";", &ncommands);
    1691   for (i=0; i<ncommands; i++) {
     1690  commands = g_strsplit_set(newbuff, ";", 0);
     1691  for (i = 0; commands[i] != NULL; i++) {
    16921692    if (lastrv) {
    16931693      owl_free(lastrv);
     
    16961696  }
    16971697  owl_free(newbuff);
    1698   atokenize_delete(commands, ncommands);
     1698  g_strfreev(commands);
    16991699  return lastrv;
    17001700}
  • keybinding.c

    r8a921b5 rd275eb2  
    3939  int    nktokens, i;
    4040
    41   ktokens = atokenize(keyseq, " ", &nktokens);
    42   if (!ktokens) return(-1);
    43   if (nktokens > OWL_KEYMAP_MAXSTACK) {
    44     atokenize_delete(ktokens, nktokens);
     41  ktokens = g_strsplit_set(keyseq, " ", 0);
     42  nktokens = g_strv_length(ktokens);
     43  if (nktokens < 1 || nktokens > OWL_KEYMAP_MAXSTACK) {
     44    g_strfreev(ktokens);
    4545    return(-1);
    4646  }
     
    4848  for (i=0; i<nktokens; i++) {
    4949    kb->keys[i] = owl_keypress_fromstring(ktokens[i]);
    50     if (kb->keys[i] == ERR) { 
    51       atokenize_delete(ktokens, nktokens);
     50    if (kb->keys[i] == ERR) {
     51      g_strfreev(ktokens);
    5252      owl_free(kb->keys);
    5353      return(-1);
     
    5555  }
    5656  kb->len = nktokens;
    57   atokenize_delete(ktokens, nktokens);
     57  g_strfreev(ktokens);
    5858  return(0);
    5959}
  • util.c

    rfa90c34 rd275eb2  
    1111#include <glib/gstdio.h>
    1212#include <glib-object.h>
    13 
    14 char **atokenize(const char *buffer, const char *sep, int *i)
    15 {
    16   /* each element of return must be freed by user */
    17   char **args;
    18   char *workbuff, *foo;
    19   int done=0, first=1, count=0;
    20 
    21   workbuff = owl_strdup(buffer);
    22 
    23   args=NULL;
    24   while (!done) {
    25     if (first) {
    26       first=0;
    27       foo=strtok(workbuff, sep);
    28     } else {
    29       foo=strtok(NULL, sep);
    30     }
    31     if (foo==NULL) {
    32       done=1;
    33     } else {
    34       args=owl_realloc(args, sizeof(char *) * (count+1));
    35       args[count] = owl_strdup(foo);
    36       count++;
    37     }
    38   }
    39   *i=count;
    40   owl_free(workbuff);
    41   return(args);
    42 }
    4313
    4414const char *skiptokens(const char *buff, int n) {
     
    12797}
    12898
    129 void atokenize_delete(char **tok, int nels)
    130 {
    131   int i;
    132   for (i=0; i<nels; i++) {
    133     owl_free(tok[i]);
    134   }
    135   owl_free(tok);
    136 }
    137 
    138 
    13999void owl_parse_delete(char **argv, int argc)
    140100{
  • variable.c

    rbc14adc rd275eb2  
    952952  int nenums, val;
    953953  if (newval == NULL) return(0);
    954   enums = atokenize(v->validsettings, ",", &nenums);
    955   if (enums == NULL) return(0);
    956   atokenize_delete(enums, nenums);
     954  enums = g_strsplit_set(v->validsettings, ",", 0);
     955  nenums = g_strv_length(enums);
     956  g_strfreev(enums);
    957957  val = *(const int*)newval;
    958958  if (val < 0 || val >= nenums) {
     
    964964int owl_variable_enum_set_fromstring(owl_variable *v, const char *newval) {
    965965  char **enums;
    966   int nenums, i, val=-1;
     966  int i, val=-1;
    967967  if (newval == NULL) return(-1);
    968   enums = atokenize(v->validsettings, ",", &nenums);
    969   if (enums == NULL) return(-1);
    970   for (i=0; i<nenums; i++) {
     968  enums = g_strsplit_set(v->validsettings, ",", 0);
     969  for (i = 0; enums[i] != NULL; i++) {
    971970    if (0==strcmp(newval, enums[i])) {
    972971      val = i;
    973972    }
    974973  }
    975   atokenize_delete(enums, nenums);
     974  g_strfreev(enums);
    976975  if (val == -1) return(-1);
    977976  return (v->set_fn(v, &val));
     
    986985    return -1;
    987986  }
    988   enums = atokenize(v->validsettings, ",", &nenums);
     987  enums = g_strsplit_set(v->validsettings, ",", 0);
     988  nenums = g_strv_length(enums);
    989989  i = *(const int*)val;
    990990  if (i<0 || i>=nenums) {
    991991    snprintf(buf, bufsize, "<invalid:%d>",i);
    992     atokenize_delete(enums, nenums);
     992    g_strfreev(enums);
    993993    return(-1);
    994994  }
    995995  snprintf(buf, bufsize, "%s", enums[i]);
    996   atokenize_delete(enums, nenums);
     996  g_strfreev(enums);
    997997  return 0;
    998998}
Note: See TracChangeset for help on using the changeset viewer.