-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileWriter.cpp
More file actions
45 lines (34 loc) · 932 Bytes
/
Copy pathFileWriter.cpp
File metadata and controls
45 lines (34 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "FileWriter.hpp"
#include <stdlib.h>
#include <string.h>
FileWriter::FileWriter(char *filepath, Interface *i) {
in = i;
if (strcmp(filepath,"-") == 0) {
fp = stdout;
std = false;
} else {
fp = fopen(filepath, "w");
if (fp == NULL) {
fprintf(stderr, "Unable to open file!\n");
exit(EXIT_FAILURE);
}
std = true;
}
}
void FileWriter::run() {
for (;;) {
const void *buf = in->getResource();
const struct resource *r = (const struct resource *)buf;
// We've reached the end of the file
if (buf == NULL) return;
fprintf(stderr, "Writing to file %zu\n", r->size);
fwrite(((char *)buf)+sizeof(struct resource), r->size, 1, fp);
fprintf(stderr, "Wrote to file\n");
free((void *)buf);
};
}
FileWriter::~FileWriter() {
if (!std) {
fclose(fp);
}
}