Less usage of C stdio

This commit is contained in:
topjohnwu 2024-02-28 15:52:03 -08:00
parent 9ae328fd84
commit b1297c4192
3 changed files with 21 additions and 37 deletions

View File

@ -60,9 +60,7 @@ struct in_stream {
};
// A stream is something that is writable and readable
struct stream : public out_stream, public in_stream {
virtual ~stream() = default;
};
struct stream : public out_stream, public in_stream {};
using stream_ptr = std::unique_ptr<stream>;
@ -122,18 +120,6 @@ private:
* Bridge between stream class and C stdio
* ****************************************/
// sFILE -> stream_ptr
class fp_stream final : public file_stream {
public:
fp_stream(FILE *fp = nullptr) : fp(fp, fclose) {}
fp_stream(sFILE &&fp) : fp(std::move(fp)) {}
ssize_t read(void *buf, size_t len) override;
protected:
ssize_t do_write(const void *buf, size_t len) override;
private:
sFILE fp;
};
// stream_ptr -> sFILE
sFILE make_stream_fp(stream_ptr &&strm);

View File

@ -45,15 +45,6 @@ ssize_t in_stream::readFully(void *buf, size_t len) {
return read_sz;
}
ssize_t fp_stream::read(void *buf, size_t len) {
auto ret = fread(buf, 1, len, fp.get());
return ret ? ret : (ferror(fp.get()) ? -1 : 0);
}
ssize_t fp_stream::do_write(const void *buf, size_t len) {
return fwrite(buf, 1, len, fp.get());
}
bool filter_out_stream::write(const void *buf, size_t len) {
return base->write(buf, len);
}

View File

@ -627,12 +627,13 @@ void decompress(char *infile, const char *outfile) {
bool in_std = infile == "-"sv;
bool rm_in = false;
FILE *in_fp = in_std ? stdin : xfopen(infile, "re");
int in_fd = in_std ? STDIN_FILENO : xopen(infile, O_RDONLY);
int out_fd = -1;
out_strm_ptr strm;
char buf[4096];
size_t len;
while ((len = fread(buf, 1, sizeof(buf), in_fp))) {
while ((len = read(in_fd, buf, sizeof(buf)))) {
if (!strm) {
format_t type = check_fmt(buf, len);
@ -660,8 +661,10 @@ void decompress(char *infile, const char *outfile) {
}
}
FILE *out_fp = outfile == "-"sv ? stdout : xfopen(outfile, "we");
strm = get_decoder(type, make_unique<fp_stream>(out_fp));
out_fd = outfile == "-"sv ?
STDOUT_FILENO :
xopen(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
strm = get_decoder(type, make_unique<fd_stream>(out_fd));
if (ext) *ext = '.';
}
if (!strm->write(buf, len))
@ -669,7 +672,8 @@ void decompress(char *infile, const char *outfile) {
}
strm.reset(nullptr);
fclose(in_fp);
if (in_fd != STDIN_FILENO) close(in_fd);
if (out_fd != STDOUT_FILENO) close(out_fd);
if (rm_in)
unlink(infile);
@ -683,36 +687,39 @@ void compress(const char *method, const char *infile, const char *outfile) {
bool in_std = infile == "-"sv;
bool rm_in = false;
FILE *in_fp = in_std ? stdin : xfopen(infile, "re");
FILE *out_fp;
int in_fd = in_std ? STDIN_FILENO : xopen(infile, O_RDONLY);
int out_fd = -1;
if (outfile == nullptr) {
if (in_std) {
out_fp = stdout;
out_fd = STDOUT_FILENO;
} else {
/* If user does not provide outfile and infile is not
* STDIN, output to <infile>.[ext] */
string tmp(infile);
tmp += fmt2ext[fmt];
out_fp = xfopen(tmp.data(), "we");
out_fd = xopen(tmp.data(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
fprintf(stderr, "Compressing to [%s]\n", tmp.data());
rm_in = true;
}
} else {
out_fp = outfile == "-"sv ? stdout : xfopen(outfile, "we");
out_fd = outfile == "-"sv ?
STDOUT_FILENO :
xopen(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
}
auto strm = get_encoder(fmt, make_unique<fp_stream>(out_fp));
auto strm = get_encoder(fmt, make_unique<fd_stream>(out_fd));
char buf[4096];
size_t len;
while ((len = fread(buf, 1, sizeof(buf), in_fp))) {
while ((len = read(in_fd, buf, sizeof(buf)))) {
if (!strm->write(buf, len))
LOGE("Compression error!\n");
}
strm.reset(nullptr);
fclose(in_fp);
if (in_fd != STDIN_FILENO) close(in_fd);
if (out_fd != STDOUT_FILENO) close(out_fd);
if (rm_in)
unlink(infile);