1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

New utility function to read a whole disk file.

I'm going to want this in a moment for Uppity, and it seems like the
sort of thing I should put straight into utils.c now, rather than
having to move it over later when I inevitably find another use for
it.

Rather than insisting on allocating a string buffer the way fgetline
does, it reads a whole file and transfers the result into an arbitrary
BinarySink, which works out the same if you use a strbuf at the call
site, but can do other things too if that turns out useful.
This commit is contained in:
Simon Tatham 2019-03-28 18:12:48 +00:00
parent fda1e6b71f
commit 4d69032d2c
2 changed files with 15 additions and 0 deletions

1
misc.h
View File

@ -97,6 +97,7 @@ static inline int toint(unsigned u)
}
char *fgetline(FILE *fp);
bool read_file_into(BinarySink *bs, FILE *fp);
char *chomp(char *str);
bool strstartswith(const char *s, const char *t);
bool strendswith(const char *s, const char *t);

14
utils.c
View File

@ -501,6 +501,20 @@ char *fgetline(FILE *fp)
return ret;
}
/*
* Read an entire file into a BinarySink.
*/
bool read_file_into(BinarySink *bs, FILE *fp)
{
char buf[4096];
while (1) {
size_t retd = fread(buf, 1, sizeof(buf), fp);
if (retd == 0)
return !ferror(fp);
put_data(bs, buf, retd);
}
}
/*
* Perl-style 'chomp', for a line we just read with fgetline. Unlike
* Perl chomp, however, we're deliberately forgiving of strange