Barely working!
This commit is contained in:
parent
b6d4d5dd95
commit
bed73757ed
@ -21,7 +21,8 @@ struct day_tree { struct tree_day_tree root; };
|
||||
|
||||
|
||||
#ifndef OMIT_PROTO /* <!-- proto */
|
||||
struct journal { struct day_tree days; };
|
||||
#include "text.h"
|
||||
struct journal { struct day_tree days; struct text backing; };
|
||||
struct journal journal(void);
|
||||
void journal_(struct journal *);
|
||||
int journal_is_valid(const struct journal *);
|
||||
|
316
src/journal.re.c
316
src/journal.re.c
@ -1,4 +1,3 @@
|
||||
#include "../src/text.h"
|
||||
#define OMIT_DAY
|
||||
#define OMIT_PROTO
|
||||
#include "../src/journal.h" /* base */
|
||||
@ -13,6 +12,7 @@
|
||||
#include <dirent.h> /* opendir readdir closedir */
|
||||
|
||||
|
||||
union load { const char *text; size_t offset; };
|
||||
static void date32_to_string(const union date32 d, char (*const a)[12]) {
|
||||
assert(a
|
||||
&& d.year < 10000 && d.month && d.month <= 31 && d.day && d.day <= 31);
|
||||
@ -21,16 +21,32 @@ static void date32_to_string(const union date32 d, char (*const a)[12]) {
|
||||
}
|
||||
static int day_compare(const union date32 a, const union date32 b)
|
||||
{ return a.u32 > b.u32; }
|
||||
static void day_to_string(const union date32 d, char *const*const entry,
|
||||
static void day_to_string(const union date32 d, const union load *const entry,
|
||||
char (*const a)[12]) { (void)entry; date32_to_string(d, a); }
|
||||
#define TREE_NAME day
|
||||
#define TREE_KEY union date32
|
||||
#define TREE_VALUE char *
|
||||
#define TREE_VALUE union load
|
||||
#define TREE_COMPARE
|
||||
#define TREE_TO_STRING
|
||||
#include "../src/tree.h"
|
||||
|
||||
|
||||
/* 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); }
|
||||
|
||||
|
||||
/*!re2c
|
||||
re2c:yyfill:enable = 0;
|
||||
re2c:define:YYCTYPE = char;
|
||||
@ -126,10 +142,114 @@ static unsigned weekday(union date32 d) {
|
||||
#include "../src/journal.h" /* Just prototypes. */
|
||||
|
||||
void journal_(struct journal *const j) {
|
||||
if(!j) return;
|
||||
day_tree_(&j->days);
|
||||
text_(&j->backing);
|
||||
}
|
||||
|
||||
struct journal journal(void) {
|
||||
const char *const dir_journal = "journal";
|
||||
struct journal j = {0};
|
||||
char *intent = 0;
|
||||
DIR *dir = 0;
|
||||
struct dirent *de = 0;
|
||||
struct int_array years = int_array(), months = int_array(),
|
||||
days = int_array();
|
||||
int *y = 0, *y_end, *m = 0, *m_end, *d = 0, *d_end;
|
||||
|
||||
/* Get the years list as directories matching a year. */
|
||||
if(chdir(dir_journal) == -1 || !(dir = opendir("."))) goto catch;
|
||||
while((de = readdir(dir))) {
|
||||
struct stat st;
|
||||
int year, *p;
|
||||
if(!looks_like_year(de->d_name, &year)) continue;
|
||||
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;
|
||||
}
|
||||
if(closedir(dir)) { dir = 0; goto catch; } dir = 0;
|
||||
/* 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",
|
||||
dir_journal, int_array_to_string(&years));
|
||||
|
||||
/* 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;
|
||||
if(!(month = (int)looks_like_month(de->d_name))) continue;
|
||||
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;
|
||||
}
|
||||
if(closedir(dir)) { dir = 0; goto catch; } dir = 0;
|
||||
qsort(months.data, months.size, sizeof *months.data, &void_int_cmp);
|
||||
fprintf(stderr, "Months in <<%s>>: %s.)\n",
|
||||
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;
|
||||
if(!(day = (int)looks_like_day(de->d_name))) continue;
|
||||
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;
|
||||
}
|
||||
if(closedir(dir)) { dir = 0; goto catch; } dir = 0;
|
||||
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++) {
|
||||
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. */
|
||||
if(!(contents = text_append_file(&j.backing, fn))) goto syntax;
|
||||
switch(day_tree_bulk_add(&j.days, d32, &load)) {
|
||||
case TREE_PRESENT: intent = "not unique", errno = EDOM; /*Sic*/
|
||||
case TREE_ERROR: goto syntax;
|
||||
case TREE_ABSENT: break; /* Expected. */
|
||||
}
|
||||
/* The pointers are not stable while we are loading it. */
|
||||
load->offset = (size_t)(contents - j.backing.a.data);
|
||||
continue;
|
||||
syntax:
|
||||
goto catch;
|
||||
}
|
||||
d = 0, int_array_clear(&days);
|
||||
if(chdir("..") == -1) goto catch;
|
||||
}
|
||||
m = 0, int_array_clear(&months);
|
||||
if(chdir("..") == -1) goto catch;
|
||||
/* fixme: Expand, contact is the next thing that it doesn't get. */
|
||||
if(*y == 1993/*1996*/) break;
|
||||
}
|
||||
day_tree_bulk_finish(&j.days);
|
||||
fprintf(stderr, "Journal has entries: %s\n", day_tree_to_string(&j.days));
|
||||
goto finally;
|
||||
catch:
|
||||
fprintf(stderr, "On date: %d-%.2d-%.2d.\n",
|
||||
y ? *y : -1, m ? *m : -1, d ? *d : -1);
|
||||
if(intent) fprintf(stderr, "Explanation: %s.\n", intent);
|
||||
day_tree_(&j.days);
|
||||
finally:
|
||||
if(dir) closedir(dir); dir = 0;
|
||||
int_array_(&years), int_array_(&months), int_array_(&days);
|
||||
return j;
|
||||
}
|
||||
|
||||
@ -139,193 +259,3 @@ int journal_is_valid(const struct journal *const j) {
|
||||
|
||||
const char *journal_to_string(const struct journal *const j)
|
||||
{ return day_tree_to_string(&j->days); }
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int success = EXIT_SUCCESS;
|
||||
char *intent = 0;
|
||||
|
||||
/* For reading in files, overwritten. */
|
||||
DIR *dir = 0;
|
||||
struct dirent *de;
|
||||
struct int_array years = int_array(), months = int_array(),
|
||||
days = int_array();
|
||||
int *y, *y_end;
|
||||
|
||||
struct page_tree journal = page_tree();
|
||||
|
||||
errno = 0;
|
||||
if(argc != 2) { intent = "needs journal location, which should"
|
||||
" contain <year>/<month>/<day>.txt"; goto catch; }
|
||||
|
||||
/* Get the years list as directories matching a year. */
|
||||
if(chdir(argv[1]) == -1 || !(dir = opendir("."))) goto catch;
|
||||
while((de = readdir(dir))) {
|
||||
struct stat st;
|
||||
int year, *p;
|
||||
if(!lex_looks_like_year(de->d_name, &year)) continue;
|
||||
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;
|
||||
}
|
||||
closedir(dir), dir = 0;
|
||||
/* 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",
|
||||
argv[1], int_array_to_string(&years));
|
||||
|
||||
/* Go though each year. */
|
||||
for(y = years.data, y_end = y + years.size; y < y_end; y++) {
|
||||
char fn[64];
|
||||
int *m, *m_end;
|
||||
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;
|
||||
if(!(month = lex_looks_like_month(de->d_name))) continue;
|
||||
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;
|
||||
}
|
||||
closedir(dir), dir = 0;
|
||||
qsort(months.data, months.size, sizeof *months.data, &void_int_cmp);
|
||||
fprintf(stderr, "Months in <<%s>>: %s.)\n",
|
||||
fn, int_array_to_string(&months));
|
||||
|
||||
/* Go though each month. */
|
||||
for(m = months.data, m_end = m + months.size; m < m_end; m++) {
|
||||
int *d, *d_end;
|
||||
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;
|
||||
/* fixme: Have yyyy-mm-dd to figure out how many days. */
|
||||
if(!(day = lex_looks_like_day(de->d_name))) continue;
|
||||
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;
|
||||
}
|
||||
closedir(dir), dir = 0;
|
||||
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++) {
|
||||
struct lex *lex = 0;
|
||||
struct page *page = 0;
|
||||
union date32 d32;
|
||||
if(!(d32 = date_to_32(*y, *m, *d)).year) { errno = EILSEQ;
|
||||
intent = "date parse error"; goto syntax; }
|
||||
sprintf(fn, "%.2d.txt", *d);
|
||||
if(page_tree_bulk_add(&journal, d32, &page) != TREE_UNIQUE) {
|
||||
if(!errno) intent = "not unique", errno = EDOM;
|
||||
goto syntax;
|
||||
}
|
||||
page->entry = char_array();
|
||||
page->meaning = lex_array();
|
||||
if(!append_file(&page->entry, fn)) goto syntax;
|
||||
int first = 1;
|
||||
for(lex_reset(page->entry.data); ; ) {
|
||||
if(!(lex = lex_array_new(&page->meaning))) goto syntax;
|
||||
if(!lex_next(lex)) {
|
||||
if(lex->symbol != END) { errno = EILSEQ; goto syntax; }
|
||||
break; /* Terminated successfully. */
|
||||
}
|
||||
switch(lex->symbol) {
|
||||
case TEXT: printf("%s%.*s",
|
||||
first ? "" : " ", (int)(lex->s1 - lex->s0), lex->s0);
|
||||
first = 0; break;
|
||||
case PARAGRAPH: printf("\n" C_RESET); break;
|
||||
case KJV_BOOK: printf(C_YELLOW "%.*s",
|
||||
(int)(lex->s1 - lex->s0), lex->s0); break;
|
||||
case KJV_CHAPTER_VERSE: printf(" ch. %.*s",
|
||||
(int)(lex->s1 - lex->s0), lex->s0); break;
|
||||
case KJV_TEXT: printf("%.*s",
|
||||
(int)(lex->s1 - lex->s0), lex->s0); break;
|
||||
case KJV_NEXT: printf("(next)\n"); break;
|
||||
default:
|
||||
fprintf(stderr, "%lu: %s",
|
||||
(unsigned long)lex->line, lex_symbols[lex->symbol]);
|
||||
if(lex->s0 && lex->s1) {
|
||||
if(lex->s0 + INT_MAX < lex->s1)
|
||||
intent = "line too long", errno = EILSEQ;
|
||||
else
|
||||
fprintf(stderr, " <<%.*s>>",
|
||||
(int)(lex->s1 - lex->s0), lex->s0);
|
||||
}
|
||||
fprintf(stderr, ".\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
syntax:
|
||||
fprintf(stderr, "On date: %d-%.2d-%.2d.\n", *y, *m, *d);
|
||||
if(!page) goto catch;
|
||||
if(!lex) { fprintf(stderr, "While parsing <<<\n%s>>>.\n",
|
||||
page->entry.data); goto catch; }
|
||||
for(struct lex_array_iterator it
|
||||
= lex_array_iterator(&page->meaning);
|
||||
lex = lex_array_next(&it); ) {
|
||||
fprintf(stderr, "%lu: %s",
|
||||
(unsigned long)lex->line, lex_symbols[lex->symbol]);
|
||||
if(lex->s0 && lex->s1) {
|
||||
if(lex->s0 + INT_MAX < lex->s1)
|
||||
intent = "line too long", errno = EILSEQ;
|
||||
else
|
||||
fprintf(stderr, " <<%.*s>>",
|
||||
(int)(lex->s1 - lex->s0), lex->s0);
|
||||
}
|
||||
fprintf(stderr, ".\n");
|
||||
}
|
||||
goto catch;
|
||||
}
|
||||
|
||||
int_array_clear(&days);
|
||||
if(chdir("..") == -1) goto catch;
|
||||
}
|
||||
|
||||
int_array_clear(&months);
|
||||
if(chdir("..") == -1) goto catch;
|
||||
/* fixme: Expand, contact is the next thing that it doesn't get. */
|
||||
if(*y == 1993/*1996*/) break;
|
||||
}
|
||||
page_tree_bulk_finish(&journal);
|
||||
int_array_(&years), int_array_(&months), int_array_(&days);
|
||||
fprintf(stderr, "Journal has entries: %s\n", page_tree_to_string(&journal));
|
||||
|
||||
/* Do something interesting? */
|
||||
if(!bible_graph(&journal)) goto catch;
|
||||
|
||||
goto finally;
|
||||
catch:
|
||||
success = EXIT_FAILURE;
|
||||
perror("interpret");
|
||||
if(intent) fprintf(stderr, "Further explanation: %s.\n", intent);
|
||||
finally:
|
||||
if(dir && closedir(dir)) success = EXIT_FAILURE, perror("dir");
|
||||
int_array_(&years), int_array_(&months), int_array_(&days);
|
||||
struct page_tree_entry entry;
|
||||
for(struct page_tree_iterator it = page_tree_begin(&journal);
|
||||
(entry = page_tree_next(&it)).key; ) {
|
||||
struct page *const page = entry.value;
|
||||
char z[12];
|
||||
date32_to_string(*entry.key, &z);
|
||||
lex_array_(&page->meaning);
|
||||
char_array_(&page->entry);
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,11 +1,12 @@
|
||||
#include "../src/kjv.h"
|
||||
#include "../src/journal.h"
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h> /* C99 */
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
|
||||
int main(void) {
|
||||
printf("Hello, World!\n");
|
||||
struct journal j = journal();
|
||||
//verify...
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user