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