Fix issue for get_word:
the separators need to be skipped first.
This commit is contained in:
1 parent
0fda117ffe
commit
817724ce39
2 files changed
+10
-1
No files matched your search
@@ -657,6 +657,7 @@ string_fget_word (string_t v, const char separator[], FILE *f)
|
|||||||
{
|
{
|
||||||
char buffer[128];
|
char buffer[128];
|
||||||
char c = 0;
|
char c = 0;
|
||||||
|
int d;
|
||||||
STRINGI_CONTRACT(v);
|
STRINGI_CONTRACT(v);
|
||||||
assert (f != NULL);
|
assert (f != NULL);
|
||||||
assert (1+20+2+strlen(separator)+3 < sizeof buffer);
|
assert (1+20+2+strlen(separator)+3 < sizeof buffer);
|
||||||
@@ -664,6 +665,14 @@ string_fget_word (string_t v, const char separator[], FILE *f)
|
|||||||
v->size = 0;
|
v->size = 0;
|
||||||
v->ptr[0] = 0;
|
v->ptr[0] = 0;
|
||||||
bool retcode = false;
|
bool retcode = false;
|
||||||
|
/* Skip separator first */
|
||||||
|
do {
|
||||||
|
d = fgetc(f);
|
||||||
|
if (d == EOF) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} while (strchr(separator, d) != NULL);
|
||||||
|
ungetc(d, f);
|
||||||
/* NOTE: We generate a buffer which we give to scanf to parse the string,
|
/* NOTE: We generate a buffer which we give to scanf to parse the string,
|
||||||
that it is to say, we generate the format dynamically!
|
that it is to say, we generate the format dynamically!
|
||||||
The format is like " %49[^ \t.\n]%c"
|
The format is like " %49[^ \t.\n]%c"
|
||||||
|
|||||||
@@ -409,7 +409,7 @@ static void test0(void)
|
|||||||
string_init(s1);
|
string_init(s1);
|
||||||
f = fopen("a-mstring.dat", "wt");
|
f = fopen("a-mstring.dat", "wt");
|
||||||
assert(f != NULL);
|
assert(f != NULL);
|
||||||
fprintf(f, "hello world.\n\tHowwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww do you do?");
|
fprintf(f, "hello world...\n\tHowwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww do you do?");
|
||||||
fclose(f);
|
fclose(f);
|
||||||
f = fopen("a-mstring.dat", "rt");
|
f = fopen("a-mstring.dat", "rt");
|
||||||
b = string_fget_word(s1, " \t.\n?", f);
|
b = string_fget_word(s1, " \t.\n?", f);
|
||||||
|
|||||||
Reference in new issue
Block a user