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