Ejemplo n.º 1
0
/*#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
*/
int main(int argc, char **argv){

TCHDB *hdb;
char *key, *value; 

/* create the object */
hdb = tchdbnew();

/* open the database */
if(!tchdbopen(hdb, "teloon.hdb", HDBOWRITER|HDBOTRUNC)){
	fprintf(stderr, "open error\n");
}

/* store records */
if(!tchdbput2(hdb, "foo", "hop") ||
!tchdbput2(hdb, "barz", "step") ||
!tchdbput2(hdb, "ssss", "jump")){
fprintf(stderr, "put error\n");
}

/* retrieve records */
value = tchdbget2(hdb, "foo");
if(value){
printf("%s\n", value);
free(value);
} else {
fprintf(stderr, "get error\n");
}

/* traverse records */
tchdbiterinit(hdb);
while((key = tchdbiternext2(hdb)) != NULL){
value = tchdbget2(hdb, key);
if(value){
printf("%s:%s\n", key, value);
free(value);
}
free(key);
}
tchdbiterinit(hdb);
while((key = tchdbiternext2(hdb)) != NULL){
value = tchdbget2(hdb, key);
if(value){
printf("%s:%s\n", key, value);
free(value);
}
free(key);
}

/* close the database */
if(!tchdbclose(hdb)){
fprintf(stderr, "close error\n");
}

/* delete the object */
tchdbdel(hdb);

return 0;
} 
Ejemplo n.º 2
0
/* perform importtsv command */
static int procimporttsv(const char *path, const char *file, int omode, bool sc){
  FILE *ifp = file ? fopen(file, "rb") : stdin;
  if(!ifp){
    fprintf(stderr, "%s: could not open\n", file ? file : "(stdin)");
    return 1;
  }
  TCHDB *hdb = tchdbnew();
  if(g_dbgfd >= 0) tchdbsetdbgfd(hdb, g_dbgfd);
  if(!tchdbsetcodecfunc(hdb, _tc_recencode, NULL, _tc_recdecode, NULL)) printerr(hdb);
  if(!tchdbopen(hdb, path, HDBOWRITER | HDBOCREAT | omode)){
    printerr(hdb);
    tchdbdel(hdb);
    if(ifp != stdin) fclose(ifp);
    return 1;
  }
  bool err = false;
  char *line;
  int cnt = 0;
  while(!err && (line = mygetline(ifp)) != NULL){
    char *pv = strchr(line, '\t');
    if(!pv){
      tcfree(line);
      continue;
    }
    *pv = '\0';
    if(sc) tcstrutfnorm(line, TCUNSPACE | TCUNLOWER | TCUNNOACC | TCUNWIDTH);
    if(!tchdbput2(hdb, line, pv + 1)){
      printerr(hdb);
      err = true;
    }
    tcfree(line);
    if(cnt > 0 && cnt % 100 == 0){
      putchar('.');
      fflush(stdout);
      if(cnt % 5000 == 0) printf(" (%08d)\n", cnt);
    }
    cnt++;
  }
  printf(" (%08d)\n", cnt);
  if(!tchdbclose(hdb)){
    if(!err) printerr(hdb);
    err = true;
  }
  tchdbdel(hdb);
  if(ifp != stdin) fclose(ifp);
  return err ? 1 : 0;
}