tokenizing arguments

From: Neal Lahndorff (stulahnd@ACS.EKU.EDU)
Date: 09/04/98


A week or so ago, someone posted a suggestion about making args look
something like the argc argv arrangement.  These are two functions, one
that will break the argument into individual words, and one to clean up
the malloc'ed space when you're done with it.  Generally, any function
that you want to use this in needs two new variables, a char *arg_list[]
and an int, the syntax looks like

count = liquify(arg, arg_list, max_count);

where the max_count is the max number of words you want chopped from the
argument, the function will take any words that are left from the original
string and leave them in the last spot in arg_list, for example,

rename obj prototype 3001 A big bazooka

called with a max_count of 4 from inside the rename function would leave

arg[0] -> obj
arg[1] -> prototype
arg[2] -> 3001
arg[3] -> A big bazooka

but anyways, enough about the syntax, here's the functions..



int liquify(char *arg, char *arg_list[], int max_count)
{
  int count = 0;
  char *tmp = NULL;
  char *tmp2[1024];

  /* we bump tmp2 up one because the first char in arg is always a space */
  *tmp2 = arg + 1;

  while (*tmp2 && count < max_count - 1) {
    tmp = strsep(tmp2, " ");
    arg_list[count] = str_dup(tmp);
    count++;
  }

  /* now, for the last arg, we'll just copy everything else over.. if there's
   * anything left.
   */
  if (*tmp2 != NULL) {
    arg_list[count] = str_dup(*tmp2);
    count++;
  }
  return count;
}

/* free the memory that liquify malloced for storage */
void deliquify(char *arg_list[], int count)
{
  int i;

  for (i = 0; i < count; i++) {
    free(arg_list[i]);
    arg_list[i] = NULL;
  }
}

Any bug fixes, improvements etc appreciated.. and if you use this, please
just drop me a line and tell me you're using it, i'm interested to know
just how useful the idea is.

Neal


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST