]> icculus.org git repositories - btb/d2x.git/blob - misc/strio.c
fix bugfix for 1067
[btb/d2x.git] / misc / strio.c
1 /* $Id: strio.c,v 1.4 2003-06-16 06:57:34 btb Exp $ */
2 /*
3  * strio.c: string/file manipulation functions by Victor Rachels
4  */
5
6 #ifdef HAVE_CONFIG_H
7 #include <conf.h>
8 #endif
9
10 #include <stdlib.h>
11
12 #include "cfile.h"
13 #include "strio.h"
14 //added on 9/16/98 by adb to add memory tracking for this module
15 #include "u_mem.h"
16 //end additions - adb
17
18 char* fsplitword(CFILE *f, char splitchar)
19 {
20  int x,y,mem,memx;
21  char *word,*buf;
22   memx=1;
23   mem=memx*256;
24   word=(char *) d_malloc(sizeof(char) * mem);
25   x=0;
26   word[x] = cfgetc(f);
27   while(word[x] != splitchar && !cfeof(f))
28   {
29      x++;
30       if(x==mem)
31        {
32         buf=word;
33         memx*=2;
34         mem=memx*256;
35         word=(char *) d_malloc(sizeof(char) * mem);
36          for(y=0;y<x;y++)
37           word[y]=buf[y];
38         d_free(buf);
39        }
40      word[x] = cfgetc(f);
41   }
42   word[x]=0;
43   return word;
44 }
45
46 char* splitword(char *s, char splitchar)
47 {
48  int x,l,l2;
49  char *word;
50
51    for(l=0;s[l]!=0;l++);
52    for(x=0;s[x]!=splitchar&&x<l;x++);
53   l2=x;
54   s[x]=0;
55   word = (char *) d_malloc(sizeof(char) * (l2+1));
56    for(x=0;x<=l2;x++)
57     word[x]=s[x];
58
59    if(l==l2)
60     s[0]=0;
61    else
62     {
63      while(x<=l)
64       {
65        s[x-l2-1]=s[x];
66        x++;
67       }
68     }
69   return word;
70 }