commit 13b3ba4f4f50de70733373ea1be9e49fd268674e
parent 8bf5bfcd8caa1dd8a88341c7a3795882e8486a27
Author: Wilson Gheen <wilson@wilsonrgheen.com>
Date: Tue, 17 Jan 2023 16:51:10 -0600
Delete old functions and break cache_init into separate function
Diffstat:
M | photomosaics.c | | | 104 | ++++++++++++++++++++++++++++++++++++++----------------------------------------- |
1 file changed, 50 insertions(+), 54 deletions(-)
diff --git a/photomosaics.c b/photomosaics.c
@@ -87,12 +87,6 @@ static bool parse_num(const char *str, NUM_TYPES type, void *out) {
/*N.B. fails on "partial" conversions or if str is empty*/
return *str != '\0' && *endptr == '\0';
}
-/*static bool parse_float(char *str, float *out) {*/
-/* return parse_num(str, F, out);*/
-/*}*/
-/*static bool parse_long(char *str, long *out) {*/
-/* return parse_num(str, L, out);*/
-/*}*/
static bool parse_hex_tou(char *str, unsigned int *out) {
return parse_num(str, XU, out);
}
@@ -111,62 +105,64 @@ static Pixel hexstr_top(const char *hs) {
return p;
}
-static ssize_t cache_grep(char *key) {
- if(cache_size == -1)
- return -1;
- if(!cache_buf) {
- /* init cache */
- errno = 0;
- FILE *cache_file = fopen(cache_filename, "r");
- struct stat cache_st;
- if(!cache_file) {
- WARN("Couldn't open cache file '%s'. "
- "Please ensure the directory exists.", cache_filename);
- perror("fopen");
- }
- else {
- if(stat(cache_filename, &cache_st) == 0)
- errno = 0;
- else if(errno == ENOENT) {
- /* create the file and try again just in case that's the only error */
- errno = 0;
- FILE *tmp_cache_file = fopen(cache_filename, "a");
- if(!tmp_cache_file) {
- WARN("Couldn't open cache file '%s'. "
- "Please ensure the directory exists.", cache_filename);
- perror("fopen");
- }
- else {
- assert_call_succeeds(fclose(tmp_cache_file), "fclose");
- if(stat(cache_filename, &cache_st) != 0) {
- WARN("Could not stat cache file '%s'", cache_filename);
- perror("stat");
- }
+static bool cache_init() {
+ errno = 0;
+ FILE *cache_file = fopen(cache_filename, "r");
+ struct stat cache_st;
+ if(!cache_file) {
+ WARN("Couldn't open cache file '%s'. "
+ "Please ensure the directory exists.", cache_filename);
+ perror("fopen");
+ }
+ else {
+ if(stat(cache_filename, &cache_st) == 0)
+ errno = 0;
+ else if(errno == ENOENT) {
+ /* create the file and try again just in case that's the only error */
+ errno = 0;
+ FILE *tmp_cache_file = fopen(cache_filename, "a");
+ if(!tmp_cache_file) {
+ WARN("Couldn't open cache file '%s'. "
+ "Please ensure the directory exists.", cache_filename);
+ perror("fopen");
+ }
+ else {
+ assert_call_succeeds(fclose(tmp_cache_file), "fclose");
+ if(stat(cache_filename, &cache_st) != 0) {
+ WARN("Could not stat cache file '%s'", cache_filename);
+ perror("stat");
}
}
}
+ }
- if(errno) {
- WARN("Will stop attempting to cache to '%s' for the remainder of execution.", cache_filename);
- cache_size = -1;
- return -1;
- }
+ if(errno) {
+ WARN("Will stop attempting to cache to '%s' for the remainder of execution.", cache_filename);
+ cache_size = -1;
+ return false;
+ }
- /* No errors, proceed to populate cache_buf */
+ /* No errors, proceed to populate cache_buf */
- cache_mtime = cache_st.st_mtime;
- long cache_file_size = cache_st.st_size;
- /* The following 2 mallocs are guesses; will realloc later if needed */
- cache_max_size = (cache_file_size < 5822 ? 5822 : cache_file_size) + 5 * MAX_FN_LEN;
- cache_buf = malloc(cache_max_size);
- deletables = malloc(DELETABLES_INITIAL_SIZE * sizeof(long));
- initial_cache_size = cache_size = fread(cache_buf, 1, cache_file_size, cache_file);
- cache_buf[cache_size] = 0; /* For the initial strncat later */
+ cache_mtime = cache_st.st_mtime;
+ long cache_file_size = cache_st.st_size;
+ /* The following 2 mallocs are guesses; will realloc later if needed */
+ cache_max_size = (cache_file_size < 5822 ? 5822 : cache_file_size) + 5 * MAX_FN_LEN;
+ cache_buf = malloc(cache_max_size);
+ deletables = malloc(DELETABLES_INITIAL_SIZE * sizeof(long));
+ initial_cache_size = cache_size = fread(cache_buf, 1, cache_file_size, cache_file);
+ cache_buf[cache_size] = 0; /* For the initial strncat later */
- assert(cache_size == cache_file_size);
- assert_call_succeeds(fclose(cache_file), "fclose");
+ assert(cache_size == cache_file_size);
+ assert_call_succeeds(fclose(cache_file), "fclose");
+ return true;
+}
+static ssize_t cache_grep(char *key) {
+ if(cache_size == -1)
+ return -1;
+ if(!cache_buf) {
+ if(!cache_init()) return -1;
}
-
if(cache_size == 0) return -1;
char filename[MAX_FN_LEN];