From ecf9e2e104a854a4fff6b502e2902de91355536a Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Fri, 18 Aug 2006 18:04:26 +0000 Subject: author db --- math/math.c | 6 ++-- math/metadb.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- math/metadb.h | 10 ++++++ math/mkdb.c | 31 ++++++++++++++++--- 4 files changed, 134 insertions(+), 10 deletions(-) diff --git a/math/math.c b/math/math.c index ba0309d..0388ee5 100644 --- a/math/math.c +++ b/math/math.c @@ -208,6 +208,7 @@ pr_record(struct meta *mp, size_t n, enum sorder curso) { int i = 0; char *uri, *p; + struct author a; uri = getenv("REQUEST_URI"); @@ -236,7 +237,7 @@ pr_record(struct meta *mp, size_t n, enum sorder curso) pr_opts(mp); puts(""); printf("%s\n", ctime(&mp->date)); - printf("%s\n", mp->author); + printf("%s\n", db_getauthor(&a, mp->author) ? a.name : mp->author); } } @@ -331,6 +332,7 @@ main(int argc, char **argv) enum jflag jf; enum sorder so; char *q, *srchq, *lang; + struct author a; sname = getenv("SCRIPT_NAME"); @@ -472,7 +474,7 @@ main(int argc, char **argv) printf("

%s

\n", curr->abstract); puts("

"); - printf("Author: %s
\n", curr->author); + printf("Author: %s
\n", db_getauthor(&a, curr->author) ? a.name : curr->author); printf("Date: %s
\n", ctime(&curr->date)); printf("Language: "); lang = pr_lang(curr->language); diff --git a/math/metadb.c b/math/metadb.c index 7e50959..ce910e3 100644 --- a/math/metadb.c +++ b/math/metadb.c @@ -36,9 +36,11 @@ #define P(f1,f2) printf( #f1 " = '%s'\t" #f2 " = '%s'\n", (f1), (f2)) -DB *idx, *meta, *srch; +DB *idx, *meta, *srch, *auth; + +static char *encode_meta(struct meta *, size_t *); +static struct meta *decode_meta(struct meta *, char *); -struct meta *decode_meta(struct meta *, char *); int cmptitle_i(const void *, const void *); int cmptitle_d(const void *, const void *); int cmpdate_i(const void *, const void *); @@ -46,13 +48,17 @@ int cmpdate_d(const void *, const void *); int cmpauthor_i(const void *, const void *); int cmpauthor_d(const void *, const void *); +static char *encode_author(struct author *, size_t *); +static struct author *decode_author(struct author *, char *); + int db_open(int flags) { idx = dbopen(INDEXDB, flags, DB_MODE, DB_HASH, NULL); meta = dbopen(METADB, flags, DB_MODE, DB_HASH, NULL); srch = dbopen(SEARCHDB, flags, DB_MODE, DB_HASH, NULL); - if (idx == NULL || meta == NULL || srch == NULL) { + auth = dbopen(AUTHORDB, flags, DB_MODE, DB_HASH, NULL); + if (idx == NULL || meta == NULL || srch == NULL || auth == NULL) { db_close(); return -1; } @@ -68,6 +74,8 @@ db_close(void) (meta->close)(meta); if (srch != NULL) (srch->close)(srch); + if (auth != NULL) + (auth->close)(auth); } static int @@ -201,7 +209,7 @@ encode_meta(struct meta *mp, size_t *siz) (l) += strlen(d) + 1; \ } while (0) -struct meta * +static struct meta * decode_meta(struct meta *mp, char *buf) { size_t blen; @@ -578,4 +586,85 @@ db_wipe(void) while ((srch->seq)(srch, &k, &d, 0) != 0) (srch->del)(srch, &k, 0); + + while ((auth->seq)(auth, &k, &d, 0) != 0) + (auth->del)(auth, &k, 0); +} + +static char * +encode_author(struct author *ap, size_t *siz) +{ + char *buf; + size_t len, blen; + + *siz = LENGTH(ap->name); + *siz += LENGTH(ap->mail); + + buf = malloc(*siz); + assert(buf); + + blen = 0; + PUTSTR(buf, ap->name, blen, len); + PUTSTR(buf, ap->mail, blen, len); + + return buf; +} + +static struct author * +decode_author(struct author *ap, char *buf) +{ + size_t blen; + + blen = 0; + GETSTR(ap->name, buf, blen); + GETSTR(ap->mail, buf, blen); + + return ap; +} + +int +db_putauthor(struct author *ap, char *id) +{ + DBT k, d; + char *buf; + size_t siz; + int ret; + + memset(&k, 0, sizeof(DBT)); + memset(&d, 0, sizeof(DBT)); + + buf = encode_author(ap, &siz); + + k.data = id; + k.size = strlen(id); + d.data = buf; + d.size = siz; + + ret = (auth->put)(auth, &k, &d, 0); + free(buf); + + return ret; +} + +struct author * +db_getauthor(struct author *ap, char *id) +{ + DBT k, d; + + memset(&k, 0, sizeof(DBT)); + memset(&d, 0, sizeof(DBT)); + + k.data = id; + k.size = strlen(id); + + if ((auth->get)(auth, &k, &d, 0) == 0) { + if (ap == NULL) { + ap = malloc(sizeof(struct author)); + assert(ap); + } + + return decode_author(ap, d.data); + } + + return NULL; } diff --git a/math/metadb.h b/math/metadb.h index d00f34a..c2eb069 100644 --- a/math/metadb.h +++ b/math/metadb.h @@ -22,13 +22,20 @@ #define INDEXDB "_index.db" #define METADB "_meta.db" #define SEARCHDB "_search.db" +#define AUTHORDB "_author.db" #define METADATA "meta.txt" #define SECTION "section.txt" +#define AUTHORDATA "author.txt" enum mtype { M_SET, M_RECORD }; enum sorder { TITLEI, TITLED, DATEI, DATED, AUTHORI, AUTHORD }; +struct author { + char *name; + char *mail; +}; + struct meta { enum mtype type; time_t date; @@ -63,6 +70,9 @@ struct meta *db_children(unsigned int, size_t *); struct meta *db_path(unsigned int, size_t *); struct meta *db_find(char *, size_t *); void db_sort(struct meta *, size_t, enum sorder); + +int db_putauthor(struct author *, char *); +struct author *db_getauthor(struct author *, char *); __END_DECLS #endif /* not _METADB_H_ */ diff --git a/math/mkdb.c b/math/mkdb.c index f4a6469..759a012 100644 --- a/math/mkdb.c +++ b/math/mkdb.c @@ -30,6 +30,7 @@ void twiddle(void); void walk(char **); +void fillauthor(char *); void twiddle(void) @@ -55,8 +56,6 @@ walk(char **fname) printf("Scanning: "); - db_open(O_CREAT|O_RDWR); - // db_wipe(); while ((ftp = fts_read(fts)) != NULL) { @@ -119,20 +118,44 @@ walk(char **fname) } } - db_close(); fts_close(fts); printf("done\n"); } +void +fillauthor(char *fname) +{ + FILE *fd; + struct author a; + char buf[BUFSIZ], *p, *key; + char sep[] = ":\n"; + + fd = fopen(fname, "r"); + + while ((p = fgets(buf, sizeof(buf), fd)) != NULL) { + if (*p == '#') + continue; + key = strsep(&p, sep); + a.name = strsep(&p, sep); + a.mail = strsep(&p, sep); + db_putauthor(&a, key); + } + + fclose(fd); +} + + int main(int argc, char **argv) { - if (argc != 2) return -1; + db_open(O_CREAT|O_RDWR); + fillauthor(AUTHORDATA); walk(&argv[1]); + db_close(); return 0; } -- cgit v1.2.3