Tried to simplify reading by making it the surrent directory.

This commit is contained in:
Neil 2023-07-27 02:10:54 -07:00
parent 8b4e83e2ea
commit ea4cf06785
4 changed files with 39 additions and 38 deletions

View File

@ -17,8 +17,8 @@ int main(void) {
struct scan scn = {0}; struct scan scn = {0};
errno = 0; errno = 0;
intent = "../journal"; intent = "journal (cwd)";
jrnl = journal(intent); jrnl = journal();
fprintf(stderr, "Journal: %s.\n", journal_to_string(&jrnl)); fprintf(stderr, "Journal: %s.\n", journal_to_string(&jrnl));
if(errno) goto catch; if(errno) goto catch;

View File

@ -25,7 +25,7 @@ union date32 date32_next_month(union date32 d);
#include "text.h" #include "text.h"
struct journal { struct day_tree days; struct char_array backing; }; struct journal { struct day_tree days; struct char_array backing; };
struct journal_iterator { struct day_tree_iterator _; }; struct journal_iterator { struct day_tree_iterator _; };
struct journal journal(const char *); struct journal journal(void);
void journal_(struct journal *); void journal_(struct journal *);
int journal_is_empty(const struct journal *); int journal_is_empty(const struct journal *);
const char *journal_to_string(const struct journal *); const char *journal_to_string(const struct journal *);

View File

@ -124,9 +124,9 @@ void journal_(struct journal *const j) {
text_(&j->backing); text_(&j->backing);
} }
/** @return A completed journal out of `dir_journal`. Any reading errors and /** @return A completed journal. Any reading errors and `errno` will be set,
`errno` will be set, it will be idle. */ and will return will be idle. */
struct journal journal(const char *const dir_journal) { struct journal journal(void/*const char *const dir_journal*/) {
struct journal j = {0}; struct journal j = {0};
char *intent = 0; char *intent = 0;
DIR *dir = 0; DIR *dir = 0;
@ -136,12 +136,10 @@ struct journal journal(const char *const dir_journal) {
int *y = 0, *y_end, *m = 0, *m_end, *d = 0, *d_end; int *y = 0, *y_end, *m = 0, *m_end, *d = 0, *d_end;
struct day_tree_iterator it; struct day_tree_iterator it;
union { const char **text; uintptr_t *offset; } v; union { const char **text; uintptr_t *offset; } v;
char cwd[256];
int is_dir_journal = 0;
/* Get the years list as directories matching a year. */ /* Get the years list as directories matching a year. */
if(!getcwd(cwd, sizeof cwd) || chdir(dir_journal) == -1) goto catch; /*if(!getcwd(cwd, sizeof cwd) || chdir(dir_journal) == -1) goto catch;
is_dir_journal = 1; is_dir_journal = 1;*/
if(!(dir = opendir("."))) goto catch; if(!(dir = opendir("."))) goto catch;
while((de = readdir(dir))) { while((de = readdir(dir))) {
struct stat st; struct stat st;
@ -155,8 +153,7 @@ struct journal journal(const char *const dir_journal) {
if(closedir(dir)) { dir = 0; goto catch; } dir = 0; if(closedir(dir)) { dir = 0; goto catch; } dir = 0;
/* Sort the years. */ /* Sort the years. */
qsort(years.data, years.size, sizeof *years.data, &void_int_cmp); qsort(years.data, years.size, sizeof *years.data, &void_int_cmp);
fprintf(stderr, "Loading years in <<%s>>: %s.\n", fprintf(stderr, "Loading years: %s.\n", int_array_to_string(&years));
dir_journal, int_array_to_string(&years));
/* Go though each year. */ /* Go though each year. */
for(y = years.data, y_end = y + years.size; y < y_end; y++) { for(y = years.data, y_end = y + years.size; y < y_end; y++) {
@ -218,7 +215,7 @@ struct journal journal(const char *const dir_journal) {
m = 0, int_array_clear(&months); m = 0, int_array_clear(&months);
if(chdir("..") == -1) goto catch; if(chdir("..") == -1) goto catch;
} }
if(chdir("../..") == -1 || !day_tree_bulk_finish(&j.days)) goto catch; if(/*chdir("../..") == -1 ||*/ !day_tree_bulk_finish(&j.days)) goto catch;
/* Structure is now stable because we aren't going to move it; /* Structure is now stable because we aren't going to move it;
convert all of offsets back to pointers. */ convert all of offsets back to pointers. */
@ -228,17 +225,17 @@ struct journal journal(const char *const dir_journal) {
goto finally; goto finally;
catch: catch:
fprintf(stderr, "On date: %s/%d-%.2d-%.2d.\n", fprintf(stderr, "On date: %d-%.2d-%.2d.\n",
dir_journal, y ? *y : 0, m ? *m : 0, d ? *d : 0); y ? *y : 0, m ? *m : 0, d ? *d : 0);
if(intent) fprintf(stderr, "(%s)\n", intent); if(intent) fprintf(stderr, "(%s)\n", intent);
recatch: recatch:
journal_(&j); journal_(&j);
finally: finally:
if(dir) { if(closedir(dir)) { dir = 0; goto recatch; } dir = 0; } if(dir) { if(closedir(dir)) { dir = 0; goto recatch; } dir = 0; }
if(is_dir_journal) { /*if(is_dir_journal) {
if(chdir(cwd) == -1) { is_dir_journal = 0; goto recatch; } if(chdir(cwd) == -1) { is_dir_journal = 0; goto recatch; }
is_dir_journal = 0; is_dir_journal = 0;
} }*/
int_array_(&years), int_array_(&months), int_array_(&days); /* Temporary. */ int_array_(&years), int_array_(&months), int_array_(&days); /* Temporary. */
return j; return j;
} }

View File

@ -173,7 +173,7 @@ static int scan_day(struct scan *const scan, union date32 date,
no content. */ no content. */
size_t line = 1; size_t line = 1;
char datestr[12] = {0}; char datestr[12] = {0};
const char *fail = "perhaps a bat?"; const char *fail = "perhaps a bat?", *lastnl = buffer;
struct { struct {
enum YYCONDTYPE future; enum YYCONDTYPE future;
union { union {
@ -235,7 +235,8 @@ static int scan_day(struct scan *const scan, union date32 date,
<newline> * { fail = "newline expected"; goto catch; } <newline> * { fail = "newline expected"; goto catch; }
<skip> "\x00" { fail = "no newline at end of file"; goto catch; } <skip> "\x00" { fail = "no newline at end of file"; goto catch; }
<line> "\x00" { return 1; } /* End of day. */ <line> "\x00" { return 1; } /* End of day. */
<line, skip, newline> "\n" => line { line++; continue; } <line, skip, newline> "\n" @s1 => line
{ line++; lastnl = s1; continue; }
<line> * :=> skip <line> * :=> skip
<line> "->" :=> place <line> "->" :=> place
<line> "--" / [^-] :=> source <line> "--" / [^-] :=> source
@ -277,8 +278,7 @@ static int scan_day(struct scan *const scan, union date32 date,
/* Whatsapp messages ignored. [11-03, 04:19] Contact: massage /* Whatsapp messages ignored. [11-03, 04:19] Contact: massage
Hopefully they don't have a ':' in their name. */ Hopefully they don't have a ':' in their name. */
<bracket> [0-1][0-9] "-" [0-3][0-9] ", " [0-2][0-9] ":" [0-5][0-9] "] " <bracket> [0-1][0-9] "-" [0-3][0-9] ", " [0-2][0-9] ":" [0-5][0-9] "] "
@s0 [^\x00\n:]+ @s1 ":" [^\x00\n]* "\n" => line { @s0 [^\x00\n:]+ @s1 ":" [^\x00\n]* / "\n" => skip {
line++;
fprintf(stderr, "Whatsapp message from %.*s ignored.\n", fprintf(stderr, "Whatsapp message from %.*s ignored.\n",
(int)(s1 - s0), s0); (int)(s1 - s0), s0);
continue; continue;
@ -730,12 +730,12 @@ static int scan_day(struct scan *const scan, union date32 date,
&new_glider->instr_min)) { fail = "glider instr time"; goto catch; } &new_glider->instr_min)) { fail = "glider instr time"; goto catch; }
continue; } continue; }
<glider_remarks> * { fail = "glider remarks"; goto catch; } <glider_remarks> * { fail = "glider remarks"; goto catch; }
<glider_remarks> "\n" => line <glider_remarks> "\n" @s1 => line
{ new_glider->remarks.a = new_glider->remarks.b = 0; { new_glider->remarks.a = new_glider->remarks.b = 0;
new_glider = 0; line++; continue; } new_glider = 0; line++; lastnl = s1; continue; }
<glider_remarks> ws* @s0 anylabel @s1 "\n" => line <glider_remarks> ws* @s0 anylabel @s1 "\n" @t1 => line
{ new_glider->remarks.a = s0, new_glider->remarks.b = s1; { new_glider->remarks.a = s0, new_glider->remarks.b = s1;
new_glider = 0; line++; continue; } new_glider = 0; line++; lastnl = t1; continue; }
/* type; registration; launch -- landing; pic; sic; /* type; registration; launch -- landing; pic; sic;
@ -802,12 +802,12 @@ static int scan_day(struct scan *const scan, union date32 date,
&new_flight->ifr_min)) { fail = "flight ifr time"; goto catch; } &new_flight->ifr_min)) { fail = "flight ifr time"; goto catch; }
continue; } continue; }
<flight_remarks> * { fail = "flight remarks"; goto catch; } <flight_remarks> * { fail = "flight remarks"; goto catch; }
<flight_remarks> "\n" => line <flight_remarks> "\n" @s1 => line
{ new_flight->remarks.a = new_flight->remarks.b = 0; { new_flight->remarks.a = new_flight->remarks.b = 0;
new_flight = 0; line++; continue; } new_flight = 0; line++; lastnl = s1; continue; }
<flight_remarks> ws* @s0 anylabel @s1 "\n" => line <flight_remarks> ws* @s0 anylabel @s1 "\n" @t1 => line
{ new_flight->remarks.a = s0, new_flight->remarks.b = s1; { new_flight->remarks.a = s0, new_flight->remarks.b = s1;
new_flight = 0; line++; continue; } new_flight = 0; line++; lastnl = t1; continue; }
/* Books in KJV. */ /* Books in KJV. */
@ -929,24 +929,24 @@ static int scan_day(struct scan *const scan, union date32 date,
<input_text> ws { continue; } <input_text> ws { continue; }
<input_text> "<<\n" @s0 => input_text_multi { // multi-line <input_text> "<<\n" @s0 => input_text_multi { // multi-line
input.pair->a = s0; input.pair->a = s0;
line++; line++; lastnl = s0;
continue; continue;
} }
<input_text> @s0 anylabel? @s1 "\n" => line { // one line; last one <input_text> @s0 anylabel? @s1 "\n" @t0 => line { // one line; last one
fprintf(stderr, "text: <<%.*s>>\n", (int)(s1 - s0), s0); /*fprintf(stderr, "text: <<%.*s>>\n", (int)(s1 - s0), s0);*/
input.pair->a = s0, input.pair->b = s1; input.pair->a = s0, input.pair->b = s1;
if(input.future != yycnewline) if(input.future != yycnewline)
{ fail = "use <<text>>"; goto catch; } { fail = "use <<text>>"; goto catch; }
line++; line++; lastnl = t0;
continue; continue;
} }
<input_text_multi> [^\x00\n] { continue; } <input_text_multi> [^\x00\n] { continue; }
<input_text_multi> [\x00] <input_text_multi> [\x00]
{ fail = "missing closing \">>\""; goto catch; } { fail = "missing closing \">>\""; goto catch; }
<input_text_multi> "\n" { line++; continue; } <input_text_multi> "\n" @s1 { line++; lastnl = s1; continue; }
<input_text_multi> @s1 ">>" / [^>] /*future*/ { <input_text_multi> @s1 ">>" / [^>] /*future*/ {
fprintf(stderr, "text: <<\n%.*s>>\n", /*fprintf(stderr, "text: <<\n%.*s>>\n",
(int)(s1 - input.pair->a), input.pair->a); (int)(s1 - input.pair->a), input.pair->a);*/
input.pair->b = s1; input.pair->b = s1;
condition = input.future; condition = input.future;
continue; continue;
@ -971,7 +971,11 @@ static int scan_day(struct scan *const scan, union date32 date,
catch: catch:
if(!errno) errno = EILSEQ; if(!errno) errno = EILSEQ;
date32_to_string(date, &datestr); date32_to_string(date, &datestr);
fprintf(stderr, "%s line %zu fail: %s" /*" condition %d"*/ ".\n", if(s1 = strchr(lastnl, '\n')) {
fprintf(stderr, "%.*s\n", (int)(s1 - lastnl), lastnl);
}
fprintf(stderr,
"%s line %zu fail: %s" /*" condition %d"*/ ".\n",
datestr, line, fail/*, condition*/); datestr, line, fail/*, condition*/);
return 0; return 0;
} }
@ -1266,7 +1270,7 @@ void scan_flight_graph(struct scan *const scan) {
flight_tree_to_string(&scan->flights)); flight_tree_to_string(&scan->flights));
printf("set terminal pngcairo dashed transparent truecolor" printf("set terminal pngcairo dashed transparent truecolor"
" size 840, 480 fontscale 1\n" " size 840, 480 fontscale 1\n"
"set output \"glider.png\"\n"); "set output \"flight.png\"\n");
printf("$Data <<EOD\n" printf("$Data <<EOD\n"
"# date, reg, sic, pic, source\n"); "# date, reg, sic, pic, source\n");
struct flight_tree_iterator it = flight_tree_iterator(&scan->flights); struct flight_tree_iterator it = flight_tree_iterator(&scan->flights);