mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-01-25 01:02:24 +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:
parent
fda1e6b71f
commit
4d69032d2c
1
misc.h
1
misc.h
@ -97,6 +97,7 @@ static inline int toint(unsigned u)
|
|||||||
}
|
}
|
||||||
|
|
||||||
char *fgetline(FILE *fp);
|
char *fgetline(FILE *fp);
|
||||||
|
bool read_file_into(BinarySink *bs, FILE *fp);
|
||||||
char *chomp(char *str);
|
char *chomp(char *str);
|
||||||
bool strstartswith(const char *s, const char *t);
|
bool strstartswith(const char *s, const char *t);
|
||||||
bool strendswith(const char *s, const char *t);
|
bool strendswith(const char *s, const char *t);
|
||||||
|
14
utils.c
14
utils.c
@ -501,6 +501,20 @@ char *fgetline(FILE *fp)
|
|||||||
return ret;
|
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-style 'chomp', for a line we just read with fgetline. Unlike
|
||||||
* Perl chomp, however, we're deliberately forgiving of strange
|
* Perl chomp, however, we're deliberately forgiving of strange
|
||||||
|
Loading…
Reference in New Issue
Block a user