2022-12-27 23:45:57 -05:00
|
|
|
#define OMIT_DAY
|
|
|
|
#define OMIT_PROTO
|
|
|
|
#include "../src/journal.h" /* base */
|
2022-12-27 02:31:08 -05:00
|
|
|
#include <inttypes.h> /* C99 */
|
|
|
|
#include <limits.h>
|
2022-12-27 23:45:57 -05:00
|
|
|
#include <stdio.h>
|
2022-12-27 02:31:08 -05:00
|
|
|
#include <assert.h>
|
2022-12-27 23:45:57 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h> /* chdir (POSIX) */
|
|
|
|
#include <sys/types.h> /* mode_t (POSIX) */
|
|
|
|
#include <sys/stat.h> /* umask (POSIX) */
|
|
|
|
#include <dirent.h> /* opendir readdir closedir */
|
|
|
|
|
2022-12-27 02:31:08 -05:00
|
|
|
|
2022-12-28 03:03:55 -05:00
|
|
|
union load { const char *text; size_t offset; };
|
2022-12-27 23:45:57 -05:00
|
|
|
static void date32_to_string(const union date32 d, char (*const a)[12]) {
|
|
|
|
sprintf(*a, "%" PRIu32 "-%2.2" PRIu32 "-%2.2" PRIu32,
|
2022-12-28 12:34:19 -05:00
|
|
|
d.year % 10000, d.month % 100, d.day % 100);
|
2022-12-27 02:31:08 -05:00
|
|
|
}
|
2022-12-27 23:45:57 -05:00
|
|
|
static int day_compare(const union date32 a, const union date32 b)
|
2022-12-27 02:31:08 -05:00
|
|
|
{ return a.u32 > b.u32; }
|
2022-12-28 03:03:55 -05:00
|
|
|
static void day_to_string(const union date32 d, const union load *const entry,
|
2022-12-27 23:45:57 -05:00
|
|
|
char (*const a)[12]) { (void)entry; date32_to_string(d, a); }
|
|
|
|
#define TREE_NAME day
|
2022-12-27 02:31:08 -05:00
|
|
|
#define TREE_KEY union date32
|
2022-12-28 03:03:55 -05:00
|
|
|
#define TREE_VALUE union load
|
2022-12-27 02:31:08 -05:00
|
|
|
#define TREE_COMPARE
|
|
|
|
#define TREE_TO_STRING
|
|
|
|
#include "../src/tree.h"
|
|
|
|
|
|
|
|
|
2022-12-28 03:03:55 -05:00
|
|
|
/* Temporary filename arrangement. */
|
|
|
|
#if INT_MAX >= 100000000000
|
|
|
|
#error int_to_string requires truncation on this compiler.
|
|
|
|
#endif
|
|
|
|
static void int_to_string(const int *const n, char (*const a)[12])
|
|
|
|
{ sprintf(*a, "%d", *n); }
|
|
|
|
#define ARRAY_NAME int
|
|
|
|
#define ARRAY_TYPE int
|
|
|
|
#define ARRAY_TO_STRING
|
|
|
|
#include "../src/array.h"
|
|
|
|
static int int_cmp(const int *const a, const int *const b)
|
|
|
|
{ return (*b < *a) - (*a < *b); }
|
|
|
|
static int void_int_cmp(const void *const a, const void *const b)
|
|
|
|
{ return int_cmp(a, b); }
|
|
|
|
|
|
|
|
|
2022-12-27 02:31:08 -05:00
|
|
|
/*!re2c
|
|
|
|
re2c:yyfill:enable = 0;
|
|
|
|
re2c:define:YYCTYPE = char;
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** "-"? [1-9][0-9]*$, within the range of `INT_MAX`. */
|
|
|
|
static int looks_like_year(const char *const a, int *const year) {
|
|
|
|
const char *YYCURSOR = a, *YYMARKER = a, *s0;
|
|
|
|
/*!stags:re2c format = 'const char *@@;\n'; */
|
|
|
|
assert(a && year);
|
|
|
|
/*!re2c /**/
|
|
|
|
@s0 ("-"? [1-9][0-9]* | "0") "\x00" {
|
|
|
|
int sign = 1, mag;
|
|
|
|
if(*s0 == '-') { sign = -1; s0++; }
|
|
|
|
for(mag = 0; *s0 != '\0'; s0++) {
|
|
|
|
int d = *s0 - '0';
|
|
|
|
if((INT_MAX - d) / 10 < mag) return 0;
|
|
|
|
mag = mag * 10 + d;
|
|
|
|
}
|
|
|
|
*year = sign * mag;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
* { return 0; }
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 1 <= [0-1][0-9]$ <= 12 */
|
|
|
|
static unsigned looks_like_month(const char *const a) {
|
|
|
|
const char *YYCURSOR = a, *YYMARKER = a, *s0;
|
|
|
|
/*!stags:re2c format = 'const char *@@;\n'; */
|
|
|
|
(void)yyt1;
|
|
|
|
assert(a);
|
|
|
|
/*!re2c /**/
|
|
|
|
@s0 [0-1][0-9] "\x00" {
|
|
|
|
unsigned val = 10u * (unsigned)(s0[0] - '0') + (unsigned)(s0[1] - '0');
|
|
|
|
return val < 1 || val > 12 ? 0 : val;
|
|
|
|
}
|
|
|
|
* { return 0; }
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/** 1 <= [0-3][0-9].txt$ <= 31 */
|
|
|
|
static unsigned looks_like_day(const char *const a) {
|
|
|
|
const char *YYCURSOR = a, *YYMARKER = a, *s0;
|
|
|
|
/*!stags:re2c format = 'const char *@@;\n'; */
|
|
|
|
(void)yyt1;
|
|
|
|
assert(a);
|
|
|
|
/*!re2c /**/
|
|
|
|
@s0 [0-3][0-9] ".txt\x00" {
|
|
|
|
unsigned val = 10u * (unsigned)(s0[0] - '0') + (unsigned)(s0[1] - '0');
|
|
|
|
return val < 1 || val > 31 ? 0 : val;
|
|
|
|
}
|
|
|
|
* { return 0; }
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2022-12-27 23:45:57 -05:00
|
|
|
#if 0
|
|
|
|
/** Is `y` a leap-year? */
|
|
|
|
static int leap(int y) {
|
|
|
|
assert(y >= 1582);
|
|
|
|
if(!(y % 400)) return 1;
|
|
|
|
if(!(y % 100)) return 0;
|
|
|
|
if(!(y % 4)) return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/** @return Pack into `date32` or return zero. */
|
|
|
|
static union date32 date_to_32(const int y, const int m, const int d) {
|
|
|
|
union date32 d32 = { 0 };
|
|
|
|
/* Leap year calculations only work at y>=1 and Gregorian Calendar and max
|
|
|
|
23 bits. */
|
|
|
|
if(y < 1582 || y > 8388607 || m < 1 || m > 12 || d < 1 || d > 31) goto no;
|
|
|
|
switch(m) {
|
|
|
|
case 1: case 3: case 5: case 7: case 8: case 10: case 12: break;
|
|
|
|
case 4: case 6: case 9: case 11: if(d > 30) goto no; break;
|
|
|
|
case 2: if(d > 28 + leap(y)) goto no; break;
|
|
|
|
default: assert(0); break;
|
|
|
|
}
|
|
|
|
d32.year = (unsigned)y, d32.month = (unsigned)m, d32.day = (unsigned)d;
|
|
|
|
no:
|
|
|
|
return d32;
|
|
|
|
}
|
|
|
|
/** Tomohiko Sakamoto comp.lang.c 1993-04-10. */
|
|
|
|
static unsigned weekday(union date32 d) {
|
|
|
|
d.year -= d.month < 3;
|
|
|
|
return (d.year + d.year / 4 - d.year / 100 + d.year / 400
|
|
|
|
+ "-bed=pen+mad."[d.month] + d.day) % 7;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define OMIT_BASE
|
|
|
|
#define OMIT_DAY
|
|
|
|
#include "../src/journal.h" /* Just prototypes. */
|
|
|
|
|
2022-12-28 12:34:19 -05:00
|
|
|
/** Dynamic memory allocation for `j` will be zero, <fn:journal_is_valid> will
|
|
|
|
be false. */
|
2022-12-27 23:45:57 -05:00
|
|
|
void journal_(struct journal *const j) {
|
2022-12-28 03:03:55 -05:00
|
|
|
if(!j) return;
|
|
|
|
day_tree_(&j->days);
|
|
|
|
text_(&j->backing);
|
2022-12-27 23:45:57 -05:00
|
|
|
}
|
|
|
|
|
2022-12-28 12:34:19 -05:00
|
|
|
/** @return A completed journal out of "journal". */
|
2022-12-27 23:45:57 -05:00
|
|
|
struct journal journal(void) {
|
2022-12-28 03:03:55 -05:00
|
|
|
const char *const dir_journal = "journal";
|
2022-12-27 23:45:57 -05:00
|
|
|
struct journal j = {0};
|
|
|
|
char *intent = 0;
|
|
|
|
DIR *dir = 0;
|
2022-12-28 03:03:55 -05:00
|
|
|
struct dirent *de = 0;
|
2022-12-27 23:45:57 -05:00
|
|
|
struct int_array years = int_array(), months = int_array(),
|
|
|
|
days = int_array();
|
2022-12-28 03:03:55 -05:00
|
|
|
int *y = 0, *y_end, *m = 0, *m_end, *d = 0, *d_end;
|
2022-12-27 23:45:57 -05:00
|
|
|
|
|
|
|
/* Get the years list as directories matching a year. */
|
2022-12-28 03:03:55 -05:00
|
|
|
if(chdir(dir_journal) == -1 || !(dir = opendir("."))) goto catch;
|
2022-12-27 23:45:57 -05:00
|
|
|
while((de = readdir(dir))) {
|
|
|
|
struct stat st;
|
|
|
|
int year, *p;
|
2022-12-28 03:03:55 -05:00
|
|
|
if(!looks_like_year(de->d_name, &year)) continue;
|
2022-12-27 23:45:57 -05:00
|
|
|
if(stat(de->d_name, &st)) goto catch;
|
|
|
|
if(!S_ISDIR(st.st_mode)) continue;
|
|
|
|
if(!(p = int_array_new(&years))) goto catch;
|
|
|
|
*p = year;
|
|
|
|
}
|
2022-12-28 03:03:55 -05:00
|
|
|
if(closedir(dir)) { dir = 0; goto catch; } dir = 0;
|
2022-12-27 23:45:57 -05:00
|
|
|
/* Sort the years for sensible ordering of parsing. */
|
|
|
|
qsort(years.data, years.size, sizeof *years.data, &void_int_cmp);
|
|
|
|
fprintf(stderr, "Years in <<%s>>: %s.\n",
|
2022-12-28 03:03:55 -05:00
|
|
|
dir_journal, int_array_to_string(&years));
|
2022-12-27 23:45:57 -05:00
|
|
|
|
|
|
|
/* Go though each year. */
|
|
|
|
for(y = years.data, y_end = y + years.size; y < y_end; y++) {
|
|
|
|
char fn[64];
|
|
|
|
sprintf(fn, "%d", *y);
|
|
|
|
/* Get the months as directories. */
|
|
|
|
if(chdir(fn) == -1 || !(dir = opendir("."))) goto catch;
|
|
|
|
while((de = readdir(dir))) {
|
|
|
|
struct stat st;
|
|
|
|
int month, *p;
|
2022-12-28 03:03:55 -05:00
|
|
|
if(!(month = (int)looks_like_month(de->d_name))) continue;
|
2022-12-27 23:45:57 -05:00
|
|
|
if(stat(de->d_name, &st)) goto catch;
|
|
|
|
if(!S_ISDIR(st.st_mode)) continue;
|
|
|
|
if(!(p = int_array_new(&months))) goto catch;
|
|
|
|
*p = month;
|
|
|
|
}
|
2022-12-28 03:03:55 -05:00
|
|
|
if(closedir(dir)) { dir = 0; goto catch; } dir = 0;
|
2022-12-27 23:45:57 -05:00
|
|
|
qsort(months.data, months.size, sizeof *months.data, &void_int_cmp);
|
2022-12-28 12:34:19 -05:00
|
|
|
fprintf(stderr, "Months in <<%s>>: %s.\n",
|
2022-12-27 23:45:57 -05:00
|
|
|
fn, int_array_to_string(&months));
|
|
|
|
|
|
|
|
/* Go though each month. */
|
|
|
|
for(m = months.data, m_end = m + months.size; m < m_end; m++) {
|
|
|
|
sprintf(fn, "%.2d", *m);
|
|
|
|
/* Get the days as files. */
|
|
|
|
if(chdir(fn) == -1 || !(dir = opendir("."))) goto catch;
|
|
|
|
while((de = readdir(dir))) {
|
|
|
|
struct stat st;
|
|
|
|
int day, *p;
|
2022-12-28 03:03:55 -05:00
|
|
|
if(!(day = (int)looks_like_day(de->d_name))) continue;
|
2022-12-27 23:45:57 -05:00
|
|
|
if(stat(de->d_name, &st)) goto catch;
|
|
|
|
if(S_ISDIR(st.st_mode)) continue;
|
|
|
|
if(!(p = int_array_new(&days))) goto catch;
|
|
|
|
*p = day;
|
|
|
|
}
|
2022-12-28 03:03:55 -05:00
|
|
|
if(closedir(dir)) { dir = 0; goto catch; } dir = 0;
|
2022-12-27 23:45:57 -05:00
|
|
|
qsort(days.data, days.size, sizeof *days.data, &void_int_cmp);
|
|
|
|
fprintf(stderr, "Days in <<%s>>: %s.\n",
|
|
|
|
fn, int_array_to_string(&days));
|
|
|
|
|
|
|
|
for(d = days.data, d_end = d + days.size; d < d_end; d++) {
|
2022-12-28 03:03:55 -05:00
|
|
|
union load *load;
|
|
|
|
const union date32 d32 = { .year = (uint32_t)*y,
|
|
|
|
.month = (uint32_t)*m, .day = (uint32_t)*d };
|
|
|
|
char *contents = 0;
|
|
|
|
sprintf(fn, "%.2d.txt", *d); /* Reconstruct. */
|
2022-12-28 12:34:19 -05:00
|
|
|
if(!(contents = text_append_file(&j.backing, fn))) goto catch;
|
2022-12-28 03:03:55 -05:00
|
|
|
switch(day_tree_bulk_add(&j.days, d32, &load)) {
|
2022-12-28 12:34:19 -05:00
|
|
|
case TREE_PRESENT: intent = "not unique", errno = EDOM; /*sic*/
|
|
|
|
case TREE_ERROR: goto catch;
|
2022-12-28 03:03:55 -05:00
|
|
|
case TREE_ABSENT: break; /* Expected. */
|
2022-12-27 23:45:57 -05:00
|
|
|
}
|
2022-12-28 03:03:55 -05:00
|
|
|
/* The pointers are not stable while we are loading it. */
|
|
|
|
load->offset = (size_t)(contents - j.backing.a.data);
|
2022-12-27 23:45:57 -05:00
|
|
|
}
|
2022-12-28 03:03:55 -05:00
|
|
|
d = 0, int_array_clear(&days);
|
2022-12-27 23:45:57 -05:00
|
|
|
if(chdir("..") == -1) goto catch;
|
|
|
|
}
|
2022-12-28 03:03:55 -05:00
|
|
|
m = 0, int_array_clear(&months);
|
2022-12-27 23:45:57 -05:00
|
|
|
if(chdir("..") == -1) goto catch;
|
2022-12-28 12:34:19 -05:00
|
|
|
/*if(*y == 1993) break;*/
|
2022-12-27 23:45:57 -05:00
|
|
|
}
|
2022-12-28 03:03:55 -05:00
|
|
|
day_tree_bulk_finish(&j.days);
|
2022-12-28 12:34:19 -05:00
|
|
|
if(chdir("..") == -1) goto catch;
|
|
|
|
/*fprintf(stderr, "Journal has entries: %s\n",
|
|
|
|
day_tree_to_string(&j.days));*/
|
2022-12-27 23:45:57 -05:00
|
|
|
goto finally;
|
|
|
|
catch:
|
2022-12-28 12:34:19 -05:00
|
|
|
fprintf(stderr, "On date: %s/%d-%.2d-%.2d.\n",
|
|
|
|
dir_journal, y ? *y : 0, m ? *m : 0, d ? *d : 0 );
|
2022-12-28 03:03:55 -05:00
|
|
|
if(intent) fprintf(stderr, "Explanation: %s.\n", intent);
|
2022-12-28 12:34:19 -05:00
|
|
|
recatch:
|
2022-12-28 03:03:55 -05:00
|
|
|
day_tree_(&j.days);
|
2022-12-28 12:34:19 -05:00
|
|
|
text_(&j.backing);
|
2022-12-27 23:45:57 -05:00
|
|
|
finally:
|
2022-12-28 12:34:19 -05:00
|
|
|
if(dir) if(closedir(dir)) { dir = 0; goto recatch; } dir = 0;
|
2022-12-27 23:45:57 -05:00
|
|
|
int_array_(&years), int_array_(&months), int_array_(&days);
|
2022-12-28 03:03:55 -05:00
|
|
|
return j;
|
2022-12-27 23:45:57 -05:00
|
|
|
}
|
2022-12-27 02:31:08 -05:00
|
|
|
|
2022-12-28 12:34:19 -05:00
|
|
|
/** @return `j` read in some data? */
|
2022-12-28 03:03:55 -05:00
|
|
|
int journal_is_valid(const struct journal *const j) {
|
2022-12-28 12:34:19 -05:00
|
|
|
return j && j->days.root.node && j->backing.a.data;
|
2022-12-28 03:03:55 -05:00
|
|
|
}
|
|
|
|
|
2022-12-28 12:34:19 -05:00
|
|
|
/** @return `j` as a string. */
|
2022-12-28 03:03:55 -05:00
|
|
|
const char *journal_to_string(const struct journal *const j)
|
|
|
|
{ return day_tree_to_string(&j->days); }
|