Example #1
0
static bool tcddbput(TCDDB *ddb, const void *kbuf, int ksiz, const void *vbuf, int vsiz){
  if(!ddb->name) return false;
  bool err = false;
  char *path = makepath(ddb, kbuf, ksiz);
  if(!tcwritefile(path, vbuf, vsiz)) err = true;
  tcfree(path);
  return !err;
}
Example #2
0
/* perform export command */
static int procexport(const char *dbpath, int64_t id, const char *dirpath){
  TCTDB *tdb = tctdbnew();
  if(!tctdbopen(tdb, dbpath, TDBOREADER)){
    printdberr(tdb);
    tctdbdel(tdb);
    return 1;
  }
  bool err = false;
  if(id > 0){
    char pkbuf[NUMBUFSIZ];
    int pksiz = sprintf(pkbuf, "%lld", (long long)id);
    TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);
    if(cols){
      TCXSTR *rbuf = tcxstrnew3(IOBUFSIZ);
      wikidump(rbuf, cols);
      fwrite(tcxstrptr(rbuf), 1, tcxstrsize(rbuf), stdout);
      tcxstrdel(rbuf);
      tcmapdel(cols);
    } else {
      printdberr(tdb);
      err = true;
    }
  } else {
    if(!dirpath) dirpath = ".";
    if(!tctdbiterinit(tdb)){
      printdberr(tdb);
      err = true;
    }
    char *pkbuf;
    int pksiz;
    while((pkbuf = tctdbiternext(tdb, &pksiz)) != NULL){
      TCMAP *cols = tctdbget(tdb, pkbuf, pksiz);
      if(cols){
        char *name = tcstrdup(tcmapget4(cols, "name", ""));
        tcstrcututf(name, 32);
        char *enc = pathencode(name);
        char *path = tcsprintf("%s/%s-%s.tpw", dirpath, pkbuf, enc);
        TCXSTR *rbuf = tcxstrnew3(IOBUFSIZ);
        wikidump(rbuf, cols);
        if(tcwritefile(path, tcxstrptr(rbuf), tcxstrsize(rbuf))){
          printf("%s: exported: id=%s name=%s\n", path, pkbuf, name);
        } else {
          printf("%s: writing failed\n", path);
          err = true;
        }
        tcxstrdel(rbuf);
        tcfree(path);
        tcfree(enc);
        tcfree(name);
        tcmapdel(cols);
      } else {
        printdberr(tdb);
        err = true;
      }
      tcfree(pkbuf);
    }
  }
  if(!tctdbclose(tdb)){
    printdberr(tdb);
    err = true;
  }
  tctdbdel(tdb);
  return err ? 1 : 0;
}