/* parse arguments of update command */ static int runupdate(int argc, char **argv){ char *dbpath = NULL; char *idstr = NULL; char *file = NULL; for(int i = 2; i < argc; i++){ if(!dbpath && argv[i][0] == '-'){ usage(); } else if(!dbpath){ dbpath = argv[i]; } else if(!idstr){ idstr = argv[i]; } else if(!file){ file = argv[i]; } else { usage(); } } if(!dbpath || !idstr) usage(); int64_t id = tcatoi(idstr); char *ibuf; int isiz; if(file && file[0] == '@'){ isiz = strlen(file) - 1; ibuf = tcmemdup(file + 1, isiz); } else { ibuf = tcreadfile(file, -1, &isiz); } if(!ibuf){ eprintf("%s: cannot open", file ? file : "(stdin)"); return 1; } int rv = procupdate(dbpath, id, ibuf); tcfree(ibuf); return rv; }
static void *tcddbget(TCDDB *ddb, const void *kbuf, int ksiz, int *sp){ if(!ddb->name) return false; void *vbuf; char *path = makepath(ddb, kbuf, ksiz); vbuf = tcreadfile(path, -1, sp); tcfree(path); return vbuf; }
/* parse arguments of convert command */ static int runconvert(int argc, char **argv){ char *path = NULL; int fmt = FMTHTML; char *buri = "promenade.cgi"; char *duri = NULL; bool page = false; for(int i = 2; i < argc; i++){ if(!path && argv[i][0] == '-'){ if(!strcmp(argv[i], "-fw")){ fmt = FMTWIKI; } else if(!strcmp(argv[i], "-ft")){ fmt = FMTTEXT; } else if(!strcmp(argv[i], "-buri")){ if(++i >= argc) usage(); buri = argv[i]; } else if(!strcmp(argv[i], "-duri")){ if(++i >= argc) usage(); duri = argv[i]; } else if(!strcmp(argv[i], "-page")){ page = true; } else { usage(); } } else if(!path){ path = argv[i]; } else { usage(); } } char *ibuf; int isiz; if(path && path[0] == '@'){ isiz = strlen(path) - 1; ibuf = tcmemdup(path + 1, isiz); } else { ibuf = tcreadfile(path, IOMAXSIZ, &isiz); } if(!ibuf){ eprintf("%s: cannot open", path ? path : "(stdin)"); return 1; } int rv = procconvert(ibuf, isiz, fmt, buri, duri, page); if(path && path[0] == '@') printf("\n"); tcfree(ibuf); return rv; }
/* perform import command */ static int procimport(const char *dbpath, TCLIST *files, TCLIST *sufs){ TCTDB *tdb = tctdbnew(); if(!tctdbtune(tdb, TUNEBNUM, TUNEAPOW, TUNEFPOW, 0)){ printdberr(tdb); tctdbdel(tdb); return 1; } if(!tctdbopen(tdb, dbpath, TDBOWRITER | TDBOCREAT)){ printdberr(tdb); tctdbdel(tdb); return 1; } bool err = false; if(!tctdbsetindex(tdb, "name", TDBITLEXICAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbsetindex(tdb, "cdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbsetindex(tdb, "mdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } if(!tctdbsetindex(tdb, "xdate", TDBITDECIMAL | TDBITKEEP) && tctdbecode(tdb) != TCEKEEP){ printdberr(tdb); err = true; } tclistinvert(files); char *fpath; while((fpath = tclistpop2(files)) != NULL){ TCLIST *cfiles = tcreaddir(fpath); if(cfiles){ tclistsort(cfiles); for(int i = tclistnum(cfiles) - 1; i >= 0; i--){ const char *cfile = tclistval2(cfiles, i); bool hit = false; for(int j = 0; j < tclistnum(sufs); j++){ if(tcstribwm(cfile, tclistval2(sufs, j))){ hit = true; break; } } if(!hit) continue; char *lpath = tcsprintf("%s/%s", fpath, cfile); tclistpush2(files, lpath); tcfree(lpath); } tclistdel(cfiles); } else { int isiz; char *ibuf = tcreadfile(fpath, IOMAXSIZ, &isiz); if(ibuf){ TCMAP *cols = tcmapnew2(TINYBNUM); wikiload(cols, ibuf); const char *name = tcmapget2(cols, "name"); if(name && *name != '\0'){ int64_t id = tcatoi(tcmapget4(cols, "id", "")); if(dbputart(tdb, id, cols)){ id = tcatoi(tcmapget4(cols, "id", "")); printf("%s: imported: id=%lld name=%s\n", fpath, (long long)id, name); } else { printdberr(tdb); err = true; } } else { printf("%s: ignored because there is no name\n", fpath); } tcmapdel(cols); tcfree(ibuf); } } tcfree(fpath); } if(!tctdbclose(tdb)){ printdberr(tdb); err = true; } tctdbdel(tdb); return err ? 1 : 0; }