Complete.Org: Mailing Lists: Archives: freeciv-dev: October 2001:
[Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs)
Home

[Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs)

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: Freeciv Developers <freeciv-dev@xxxxxxxxxxx>
Subject: [Freeciv-Dev] Re: Split patch (was Re: [RFC PATCH] init_techs)
From: Daniel L Speyer <dspeyer@xxxxxxxxxxx>
Date: Fri, 5 Oct 2001 16:55:18 -0400 (EDT)

I suppose I'm duplicating effort a little, but what do people think of
this split function?

char ** splitprotect(char* string,char* on){
  int i,j,n=0,l=strlen(string),q=0,b=0;
  char** out;
  for(i=0;i<l;i++){
    if (string[i]=='"')
      q=!q;
    if (q)
      continue;
    if (string[i]=='{'){
      b++;
      continue;
    }
    if (string[i]=='}'){
      b--;
      continue;
    }
    if (!b){
      for(j=0;j<strlen(on);j++){
        if (string[i]==on[j]){
          string[i]=0;
          n++;
        }
      }
    }
  }
  out=(char**)malloc((n+2)*sizeof(char*));
  *out=string;
  j=1;
  for(i=0;i<l;i++){
    if (!string[i])
      out[j++]=string+i+1;
  }
  out[n+1]=0;
  return(out);
}

It doesn't do regexps, but I don't think we'll need them.  It does handle
multiple split characters.  It only allocates memory for the specific
array it returns, and it does mangle the input string.  I think this is
the function I would want if I were writing the entire parser.  If not,
well, I didn't put all that much work into it anayway :)

--Daniel Speyer
"May the /src be with you, always"





[Prev in Thread] Current Thread [Next in Thread]