2006-03-20 16:43:15 +00:00
|
|
|
/**strio.c: string/file manipulation functions by Victor Rachels **/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2008-01-23 17:25:09 +00:00
|
|
|
|
2011-06-01 07:59:51 +00:00
|
|
|
#include "physfsx.h"
|
2006-03-20 16:43:15 +00:00
|
|
|
#include "strio.h"
|
|
|
|
#include "u_mem.h"
|
|
|
|
|
2008-01-23 17:25:09 +00:00
|
|
|
char *fgets_unlimited(PHYSFS_file *f)
|
2006-03-20 16:43:15 +00:00
|
|
|
{
|
2008-01-23 17:25:09 +00:00
|
|
|
int mem = 256;
|
|
|
|
char *word, *buf, *p;
|
|
|
|
|
|
|
|
MALLOC(word, char, mem);
|
|
|
|
p = word;
|
|
|
|
|
2011-06-01 07:59:51 +00:00
|
|
|
while (word && PHYSFSX_fgets(p, mem, f) == word + mem) {
|
2008-01-23 17:25:09 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
// Make a bigger buffer, because it read to the end of the buffer.
|
|
|
|
buf = word;
|
|
|
|
mem *= 2;
|
|
|
|
MALLOC(word, char, mem);
|
|
|
|
for (i = 0; i < mem/2; i++)
|
|
|
|
word[i] = buf[i];
|
|
|
|
d_free(buf);
|
|
|
|
p = word + mem/2;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
2008-01-23 17:25:09 +00:00
|
|
|
return word;
|
2006-03-20 16:43:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char* splitword(char *s, char splitchar)
|
|
|
|
{
|
|
|
|
int x,l,l2;
|
|
|
|
char *word;
|
|
|
|
|
|
|
|
for(l=0;s[l]!=0;l++);
|
|
|
|
for(x=0;s[x]!=splitchar&&x<l;x++);
|
|
|
|
l2=x;
|
|
|
|
s[x]=0;
|
2008-01-23 17:25:09 +00:00
|
|
|
word = (char *) d_malloc(sizeof(char) * (l2+1));
|
2006-03-20 16:43:15 +00:00
|
|
|
for(x=0;x<=l2;x++)
|
|
|
|
word[x]=s[x];
|
|
|
|
|
|
|
|
if(l==l2)
|
|
|
|
s[0]=0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while(x<=l)
|
|
|
|
{
|
|
|
|
s[x-l2-1]=s[x];
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return word;
|
|
|
|
}
|