-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
fileio.c
231 lines (207 loc) · 5.92 KB
/
fileio.c
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// fileio.c
// C-ray
//
// Created by Valtteri Koskivuori on 28/02/2015.
// Copyright © 2015-2020 Valtteri Koskivuori. All rights reserved.
//
#include "../includes.h"
#include "fileio.h"
#include "../utils/logging.h"
#include "assert.h"
#include <limits.h> //For SSIZE_MAX
#ifndef WINDOWS
#include <libgen.h>
#endif
#include "string.h"
#include <errno.h>
#include <string.h>
#include <stdio.h>
char *loadFile(const char *fileName, size_t *bytes) {
FILE *file = fopen(fileName, "rb");
if (!file) {
logr(warning, "Can't access '%.*s': %s\n", (int)strlen(fileName), fileName, strerror(errno));
return NULL;
}
size_t fileBytes = getFileSize(fileName);
if (!fileBytes) {
fclose(file);
return NULL;
}
char *buf = malloc(fileBytes + 1 * sizeof(char));
size_t readBytes = fread(buf, sizeof(char), fileBytes, file);
ASSERT(readBytes == fileBytes);
if (ferror(file) != 0) {
logr(warning, "Error reading file\n");
} else {
buf[fileBytes] = '\0';
}
fclose(file);
if (bytes) *bytes = readBytes;
return buf;
}
void writeFile(const unsigned char *buf, size_t bufsize, const char *filePath) {
FILE *file = fopen(filePath, "wb" );
char *backupPath = NULL;
if(!file) {
char *name = getFileName(filePath);
backupPath = stringConcat("./", name);
free(name);
file = fopen(backupPath, "wb");
if (file) {
char *path = getFilePath(filePath);
logr(warning, "The specified output directory \"%s\" was not writeable, dumping the file in CWD instead.\n", path);
free(path);
} else {
logr(warning, "Neither the specified output directory nor the current working directory were writeable. Image can't be saved. Fix your permissions!");
return;
}
}
logr(info, "Saving result in \"%s\"\n", backupPath ? backupPath : filePath);
fwrite(buf, 1, bufsize, file);
fclose(file);
//We determine the file size after saving, because the lodePNG library doesn't have a way to tell the compressed file size
//This will work for all image formats
unsigned long bytes = getFileSize(backupPath ? backupPath : filePath);
char *sizeString = humanFileSize(bytes);
logr(info, "Wrote %s to file.\n", sizeString);
free(sizeString);
}
bool isValidFile(char *path) {
FILE *f = fopen(path, "r");
if (f) {
fclose(f);
return true;
}
return false;
}
//Wait for 2 secs and abort if nothing is coming in from stdin
void checkBuf() {
#ifndef WINDOWS
fd_set set;
struct timeval timeout;
int rv;
FD_ZERO(&set);
FD_SET(0, &set);
timeout.tv_sec = 2;
timeout.tv_usec = 1000;
rv = select(1, &set, NULL, NULL, &timeout);
if (rv == -1) {
logr(error, "Error on stdin timeout\n");
} else if (rv == 0) {
logr(error, "No input found after %li seconds. Hint: Try `./bin/c-ray input/scene.json`.\n", timeout.tv_sec);
} else {
return;
}
#endif
}
/**
Extract the filename from a given file path
@param input File path to be processed
@return Filename string, including file type extension
*/
//FIXME: Just return a pointer to the first byte of the filename? Why do we do all this
char *getFileName(const char *input) {
//FIXME: We're doing two copies here, maybe just rework the algorithm instead.
char *copy = stringCopy(input);
char *fn;
/* handle trailing '/' e.g.
input == "/home/me/myprogram/" */
if (copy[(strlen(copy) - 1)] == '/')
copy[(strlen(copy) - 1)] = '\0';
(fn = strrchr(copy, '/')) ? ++fn : (fn = copy);
char *ret = stringCopy(fn);
free(copy);
return ret;
}
//For Windows
#define CRAY_PATH_MAX 4096
char *getFilePath(const char *input) {
char *dir = NULL;
#ifdef WINDOWS
dir = calloc(256, sizeof(*dir));
_splitpath_s(input, NULL, 0, dir, sizeof(dir), NULL, 0, NULL, 0);
#else
char *inputCopy = stringCopy(input);
dir = stringCopy(dirname(inputCopy));
free(inputCopy);
#endif
char *final = stringConcat(dir, "/");
free(dir);
return final;
}
#define chunksize 1024
//Get scene data from stdin and return a pointer to it
char *readStdin(size_t *bytes) {
checkBuf();
char chunk[chunksize];
size_t bufSize = 1;
char *buf = malloc(chunksize * sizeof(*buf));
if (!buf) {
logr(error, "Failed to malloc stdin buffer\n");
return NULL;
}
buf[0] = '\0';
while (fgets(chunk, chunksize, stdin)) {
char *old = buf;
bufSize += strlen(chunk);
buf = realloc(buf, bufSize);
if (!buf) {
logr(error, "Failed to realloc stdin buffer\n");
free(old);
return NULL;
}
strcat(buf, chunk);
}
if (ferror(stdin)) {
logr(error, "Failed to read from stdin\n");
free(buf);
return NULL;
}
if (bytes) *bytes = bufSize - 1;
return buf;
}
char *humanFileSize(unsigned long bytes) {
float kilobytes, megabytes, gigabytes, terabytes, petabytes, exabytes, zettabytes, yottabytes; // <- Futureproofing?!
kilobytes = bytes / 1000.0f;
megabytes = kilobytes / 1000.0f;
gigabytes = megabytes / 1000.0f;
terabytes = gigabytes / 1000.0f;
petabytes = terabytes / 1000.0f;
exabytes = petabytes / 1000.0f;
zettabytes = exabytes / 1000.0f;
yottabytes = zettabytes / 1000.0f;
// Okay, okay. In reality, this never gets even close to a zettabyte,
// it'll overflow at around 18 exabytes.
// I *did* get it to go to yottabytes using __uint128_t, but that's
// not in C99. Maybe in the future.
char *buf = calloc(64, sizeof(*buf));
if (zettabytes >= 1000) {
sprintf(buf, "%.02fYB", yottabytes);
} else if (exabytes >= 1000) {
sprintf(buf, "%.02fZB", zettabytes);
} else if (petabytes >= 1000) {
sprintf(buf, "%.02fEB", exabytes);
} else if (terabytes >= 1000) {
sprintf(buf, "%.02fPB", petabytes);
} else if (gigabytes >= 1000) {
sprintf(buf, "%.02fTB", terabytes);
} else if (megabytes >= 1000) {
sprintf(buf, "%.02fGB", gigabytes);
} else if (kilobytes >= 1000) {
sprintf(buf, "%.02fMB", megabytes);
} else if (bytes >= 1000) {
sprintf(buf, "%.02fkB", kilobytes);
} else {
sprintf(buf, "%ldB", bytes);
}
return buf;
}
size_t getFileSize(const char *fileName) {
FILE *file = fopen(fileName, "r");
if (!file) return 0;
fseek(file, 0L, SEEK_END);
size_t size = ftell(file);
fclose(file);
return size;
}