]> icculus.org git repositories - btb/d2x.git/blob - misc/strio.c
d2x can now handle any line ending when reading d2x.ini
[btb/d2x.git] / misc / strio.c
1 /* $Id: strio.c,v 1.5 2004-08-06 20:28:57 schaffner 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 #include <string.h>
12
13 #include "cfile.h"
14 #include "strio.h"
15 //added on 9/16/98 by adb to add memory tracking for this module
16 #include "u_mem.h"
17 //end additions - adb
18
19 char *fgets_unlimited(CFILE *f)
20 {
21     int         mem = 256;
22     char        *word, *buf, *p;
23
24     MALLOC(word, char, mem);
25     p = word;
26
27     while (word && cfgets(p, mem, f) == word + mem) {
28         int i;
29         
30         // Make a bigger buffer, because it read to the end of the buffer.
31         buf = word;
32         mem *= 2;
33         MALLOC(word, char, mem);
34         for (i = 0; i < mem/2; i++)
35             word[i] = buf[i];
36         d_free(buf);
37         p = word + mem/2;
38     }
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 }