got rid of lorem

This commit is contained in:
Neil 2023-02-03 11:50:10 -08:00
parent ffcb1ce6aa
commit e963765d30
13 changed files with 67 additions and 147 deletions

View File

@ -30,19 +30,18 @@ else
CF += -g
endif
projects := bin/test-text bin/test-journal bin/test-source bin/test-kjv bin/test-lorem bin/kjv bin/flight
projects := bin/test-text bin/test-journal bin/test-source bin/test-kjv bin/kjv bin/flight
#docs := $(patsubst test/test_%.c, doc/%.md, $(wildcard test/test_*.c))
default: $(projects)
# success
bin/test-text: build/text.o build/test_text.o
bin/test-lorem: build/lorem.o build/test_lorem.o
bin/test-journal: build/text.o build/journal.o build/test_journal.o
bin/test-source: build/text.o build/journal.o build/lorem.o build/source.o build/test_source.o
bin/test-kjv: build/text.o build/kjv.o build/test_kjv.o
bin/kjv: build/text.o build/journal.o build/kjv.o build/scan_kjv.o
bin/flight: build/text.o build/journal.o build/flight.o build/flighthours.o
bin/test-source: build/text.o build/pair.o build/journal.o build/source.o build/test_source.o
bin/test-kjv: build/text.o build/pair.o build/kjv.o build/test_kjv.o
bin/kjv: build/text.o build/pair.o build/journal.o build/kjv.o build/scan_kjv.o
bin/flight: build/text.o build/pair.o build/journal.o build/flight.o build/flighthours.o
bin/%:
@echo "\033[1;36mlinking $@\033[0m"

View File

@ -23,19 +23,19 @@ static const char *flight_type_string[] = { FLIGHT_TYPE };
#undef X
#undef FLIGHT_TYPE
#include "lorem.h"
#include "pair.h"
struct glider {
struct lorem type, reg, launch;
struct pair type, reg, launch;
enum launch_type how;
unsigned height_ft, pilot_min, dual_min, instr_min;
struct lorem remarks;
struct pair remarks;
};
struct power {
struct lorem type, reg, launch, landing, pilot, copilot;
struct pair type, reg, launch, landing, pilot, copilot;
unsigned dual_min, pilot_min, ifrsim_min, ifr_min;
struct lorem remarks;
struct pair remarks;
};
struct flight {

9
src/hash.h Normal file
View File

@ -0,0 +1,9 @@
#include <stdint.h>
/** djb2 <http://www.cse.yorku.ca/~oz/hash.html> */
static uint32_t djb2(const char *s) {
const unsigned char *str = (const unsigned char *)s;
uint32_t hash = 5381, c;
while(c = *str++) hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}

View File

@ -10,7 +10,7 @@
#define BASE
#include "../src/kjv.h" /* Just the base data. */
#include "../src/text.h"
#include "../src/helper.h"
#include "../src/pair.h"
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
@ -90,7 +90,7 @@ static int looks_like_book_fn(const char *fn, unsigned *const book_no) {
*
{ return 0; }
@s0 natural @s1 [^.\x00]* ".txt" "\x00"
{ return helper_natural(s0, s1, book_no); }
{ return pair_to_natural(s0, s1, book_no); }
*/
}
@ -129,8 +129,8 @@ scan:
<line> [^[\]\n\x00]* "\n" { lex->line++; goto scan; }
<line> "\x00" { return 0; }
<line> "[" @s0 natural @s1 ":" @t0 natural @t1 "]" => verse {
if(!helper_natural(s0, s1, &lex->chapter)
|| !helper_natural(t0, t1, &lex->verse))
if(!pair_to_natural(s0, s1, &lex->chapter)
|| !pair_to_natural(t0, t1, &lex->verse))
return errno = EILSEQ, lex->error = 1, 0;
lex->words = 0;
/*printf("%u:%u", lex->chapter, lex->verse);*/

View File

@ -1,71 +0,0 @@
/** @license 2023 Neil Edelman, distributed under the terms of the
[MIT License](https://opensource.org/licenses/MIT).
Global stable pool of temporary strings duplicated from substrings. Doesn't
support deletion, only growing.
@std C89 */
#include "lorem.h"
#include <assert.h>
#include <errno.h>
#define ARRAY_NAME block
#define ARRAY_TYPE char *
#include "array.h"
static struct {
size_t capacity, size; /* Lead block. */
struct block_array blocks;
} buffer;
struct lorem lorem(const char *const a, const char *const b) {
struct lorem l;
assert(a && a <= b);
l.sub = a;
l.size = (size_t)(b - a);
return l;
}
const char *lorem_dup(const struct lorem sub) {
char *string;
size_t size;
if(!sub.sub) { errno = EDOM; return 0; } /* Null substring. */
if(sub.size == ~(size_t)0) { errno = ERANGE; return 0; } /* Unlikely. */
if((size = sub.size + 1) > buffer.capacity - buffer.size) {
size_t c0 = buffer.capacity < 8 ? 8 : buffer.capacity;
char *block = 0, **record = 0;
while(c0 < size) { /* Grow. */
size_t c1 = c0 + (c0 >> 1) + (c0 >> 3);
if(c0 >= c1) { c0 = ~(size_t)0; break; } /* Unlikely. */
c0 = c1;
}
if(!(block = malloc(c0))
|| !(record = block_array_new(&buffer.blocks))) { /* Error. */
free(block);
if(!errno) errno = ERANGE;
return 0;
}
*record = block;
buffer.capacity = c0;
buffer.size = 0;
/*printf("%s size %zu\n", orcify(block), c0);*/
}
assert(buffer.blocks.size);
string = buffer.blocks.data[buffer.blocks.size - 1] + buffer.size;
memcpy(string, sub.sub, sub.size);
string[sub.size] = '\0';
buffer.size += size;
return string;
}
void lorem_(void) {
size_t i;
for(i = 0; i < buffer.blocks.size; i++) {
char *block = buffer.blocks.data[i];
/*printf("free %s\n", orcify(block));*/
free(block);
}
buffer.capacity = buffer.size = 0;
block_array_(&buffer.blocks);
}

View File

@ -1,7 +0,0 @@
#include <stddef.h>
struct lorem { const char *sub; size_t size; };
struct lorem lorem(const char *const a, const char *const b);
const char *lorem_dup(const struct lorem);
void lorem_(void);

24
src/pair.c Normal file
View File

@ -0,0 +1,24 @@
#include "pair.h"
#include <stdint.h>
#include <errno.h>
/** @return Constructs `a` and `b` as a pair. */
struct pair pair(const char *const a, const char *const b) {
struct pair p;
p.a = a, p.b = b;
return p;
}
/** Doesn't check anything.
@return Whether it was able to parse unsigned `p` to `n`. */
int pair_to_natural(const char *s, const char *const e, uint32_t *const n) {
uint32_t accum = 0;
while(s < e) {
unsigned next = accum * 10 + (unsigned)(*s - '0');
if(accum >= next) return errno = ERANGE, 0;
accum = next;
s++;
}
*n = accum;
return 1;
}

5
src/pair.h Normal file
View File

@ -0,0 +1,5 @@
#include <stdint.h>
struct pair { const char *a, *b; };
struct pair pair(const char *const a, const char *const b);
int pair_to_natural(const char *, const char *, uint32_t *);

View File

@ -5,10 +5,11 @@
#include "../src/journal.h"
#include "../src/kjv.h"
#include "../src/helper.h"
#include "../src/pair.h"
#include <inttypes.h> /* C99 */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <limits.h>
@ -136,15 +137,15 @@ static int scan(union date32 date, const char *const buffer,
<book> ws+ @s0 natural @s1 ":" @t0 natural @t1 [ab]? {
if(chapter || verse || verse_end)
{ why = "reference unrecognized"; goto catch; }
if(!helper_natural(s0, s1, &chapter)
|| !helper_natural(t0, t1, &verse))
if(!pair_to_natural(s0, s1, &chapter)
|| !pair_to_natural(t0, t1, &verse))
{ why = "reference numerical error"; goto catch; }
continue;
}
<book> "-" @s0 natural @s1 [ab]? { /* Verse range. */
if(!chapter || !verse || verse_end)
{ why = "range unrecognized"; goto catch; }
if(!helper_natural(s0, s1, &verse_end))
if(!pair_to_natural(s0, s1, &verse_end))
{ why = "range numerical error"; goto catch; }
continue;
}

View File

@ -1,7 +1,6 @@
#if defined BASE \
|| !defined BASE && !defined GENERIC && !defined PROTO /* <!-- base */
#include "helper.h"
#include "lorem.h"
//#include "lorem.h"
/*void kjvcite_to_string(const union kjvcite, char (*)[12]);*/
#endif /* base --> */

View File

@ -4,29 +4,27 @@
#define BASE
#include "../src/source.h" /* base */
#include "../src/journal.h"
#include "../src/hash.h" /* djb2 */
#include "../src/pair.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
/* Temporary buffer to lookup. */
#define ARRAY_NAME char
#define ARRAY_TYPE char
#include "array.h"
/* ...wait, why would you copy? modify the table to use substring. */
/* fixme: ...wait, why would you copy? modify the table to use substring. */
/* This is a lookup table for source strings ("2000glider") to substring the
first description ("Glider pilot log book"). */
static int lookup_is_equal(const char *const x, const char *const y)
{ return !strcmp(x, y); }
static uint32_t lookup_hash(const char *const x) { return djb2(x); }
static void lookup_to_string(const char *x, const struct lorem desc,
static void lookup_to_string(const char *x, const struct pair desc,
char (*const a)[12]) { (void)desc; sprintf(*a, "%.11s", x); }
static struct lorem lookup_default = { 0, 0 };
static struct pair lookup_default = { 0, 0 };
#define TABLE_NAME lookup
#define TABLE_KEY char *
#define TABLE_UINT uint32_t
#define TABLE_VALUE struct lorem
#define TABLE_VALUE struct pair
#define TABLE_DEFAULT lookup_default
#define TABLE_TO_STRING
#include "../src/table.h"
@ -84,7 +82,7 @@ static int scan(union date32 date, const char *const buffer,
<source> * { why = "default source unrecognized"; goto catch; }
<source> @s0 keyword @s1 / "\n" => skip {
struct lorem lor = lorem(s0, s1);
struct pair p = pair(s0, s1);
const char *label;
printf("extracted <%.*s>\n", (int)(s1 - s0), s0);
continue;
@ -123,6 +121,6 @@ struct sources sources(struct journal *const j) {
catch:
sources_(&s);
finally:
char_array_(&temp); /* Erase the temporary buffer. */
//char_array_(&temp); /* Erase the temporary buffer. */
return s;
}

View File

@ -1,37 +0,0 @@
#include "../src/lorem.h"
#include <stdlib.h>
#include <stdio.h>
int main(void) {
int success = EXIT_SUCCESS;
const char lorem[] = "Lorem ipsum dolor sit amet, consectetur adipiscing "
"elit. Aenean tincidunt leo neque. Integer vel bibendum lectus, a "
"vulputate dolor. Vivamus vestibulum quam ut euismod aliquet. Vivamus "
"vel pulvinar felis, eu dictum lorem. Integer scelerisque lobortis "
"orci nec tincidunt. Mauris vulputate ipsum non tempus tincidunt. "
"Pellentesque nec iaculis dolor. Curabitur bibendum pretium dui "
"euismod tincidunt. In cursus, libero et porta placerat, ante ante "
"accumsan lacus, nec sollicitudin ex elit nec lectus. Sed nisi sem, "
"rhoncus sed nulla et, faucibus feugiat eros.";
struct lorem s;
const char *a, *b, *c, *d;
s.sub = lorem + 6, s.size = 5;
if(!(a = lorem_dup(s))) goto catch;
printf("a: <%s>\n", a);
s.sub = lorem + 40, s.size = 20;
if(!(b = lorem_dup(s))) goto catch;
printf("a: <%s>, b: <%s>\n", a, b);
s.sub = lorem + 80, s.size = 60;
if(!(c = lorem_dup(s))) goto catch;
printf("a: <%s>, b: <%s>, c: <%s>\n", a, b, c);
s.sub = lorem + 200, s.size = 10;
if(!(d = lorem_dup(s))) goto catch;
printf("a: <%s>, b: <%s>, c: <%s>, d: <%s>\n", a, b, c, d);
goto finally;
catch:
success = EXIT_FAILURE;
perror("text");
finally:
lorem_();
return success;
}

View File

@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
int main(void) {
int success = EXIT_SUCCESS;
@ -17,6 +18,5 @@ catch:
finally:
sources_(&s);
journal_(&j);
lorem_();
return success;
}