File: tsiyindump.c

package info (click to toggle)
libtabe 0.2.6-1.2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 5,576 kB
  • ctags: 1,777
  • sloc: ansic: 17,458; sh: 9,848; makefile: 273
file content (149 lines) | stat: -rw-r--r-- 3,260 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright 1999, TaBE Project, All Rights Reserved.
 * Copyright 1999, Pai-Hsiang Hsiao, All Rights Reserved.
 *
 * $Id: tsiyindump.c,v 1.7 2003/05/13 10:01:07 kcwu Exp $
 *
 */
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

#ifdef HPUX
#  define _INCLUDE_POSIX_SOURCE
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "../src/version.h"

#include <db.h>
#include <tabe.h>

void
usage(void)
{
  printf("tsiyindump: libtabe-%s\n", RELEASE_VER);
  printf("Usage: tsiyindump -d <TsiDB> -y <TsiYinDB>\n");
  printf("   -d <TsiDB>     \t path to TsiDB\n");
  printf("   -y <TsiYinDB>  \t path to TsiYinDB\n");
  exit(0);
}

void
dump(struct TsiDB *db, struct TsiYinDB *ydb)
{
  struct TsiInfo *tsi;
  struct TsiYinInfo *tsiyin;
  int rval, i, j, len;

  rval = db->RecordNumber(db);
  if (rval < 0) {
    fprintf(stderr, "tsiyindump: wrong DB format.\n");
    usage();
  }

  tsi = (struct TsiInfo *)malloc(sizeof(struct TsiInfo));
  memset(tsi, 0, sizeof(struct TsiInfo));
  tsi->tsi = (ZhiStr)malloc(sizeof(unsigned char)*80);
  memset(tsi->tsi, 0, 80);

  tsiyin = (struct TsiYinInfo *)malloc(sizeof(struct TsiYinInfo));
  memset(tsiyin, 0, sizeof(struct TsiYinInfo));

  i = 0;
  while (1) {
    if (i == 0) {
      db->CursorSet(db, tsi, 0);
    }
    else {
      rval = db->CursorNext(db, tsi);
      if (rval < 0) {
        break;
      }
    }
    i++;
    if (!tsi->yinnum) {
      tabeTsiInfoLookupPossibleTsiYin(db, tsi);
    }
    len = strlen((char *)tsi->tsi)/2;
    for (j = 0; j < tsi->yinnum; j++) {
      tsiyin->yinlen = len;
      tsiyin->yin = (Yin *)malloc(sizeof(Yin)*len);
      memcpy(tsiyin->yin, tsi->yindata+j*len, sizeof(Yin)*len);
      rval = ydb->Get(ydb, tsiyin);
      if (rval < 0) { /* no such tsiyin */
        tsiyin->tsinum = 1;
        tsiyin->tsidata = (ZhiStr)malloc(sizeof(unsigned char)*len*2);
        memcpy(tsiyin->tsidata, tsi->tsi, sizeof(unsigned char)*len*2);
        ydb->Put(ydb, tsiyin);
      }
      else {
        tsiyin->tsidata =
          (ZhiStr)realloc(tsiyin->tsidata,
                          sizeof(unsigned char)*((tsiyin->tsinum+1)*len*2));
        memcpy(tsiyin->tsidata+(tsiyin->tsinum*len*2), tsi->tsi,
               sizeof(unsigned char)*len*2);
        tsiyin->tsinum++;
        ydb->Put(ydb, tsiyin);
      }
    }
  }
}

int
main(int argc, char **argv)
{
  int ch;
  struct TsiDB *db;
  struct TsiYinDB *ydb;
extern char *optarg;
extern int optind, opterr, optopt;

  char *db_name, *op_name;

  db_name = op_name = (char *)NULL;

  while ((ch = getopt(argc, argv, "d:y:")) != -1) {
    switch(ch) {
      case 'd':
        db_name = (char *)strdup(optarg);
        break;
      case 'y':
        op_name = (char *)strdup(optarg);
        break;
      default:
        usage();
        break;
    }
  }
  argc -= optind;
  argv += optind;

  if (!db_name) {
    usage();
  }

  db = tabeTsiDBOpen(DB_TYPE_DB, db_name, DB_FLAG_READONLY);
  if (!db) {
    usage();
  }

  if (!op_name) {
    usage();
  }

  ydb = tabeTsiYinDBOpen(DB_TYPE_DB, op_name,
                         DB_FLAG_CREATEDB | DB_FLAG_OVERWRITE | DB_FLAG_NOSYNC);
  if (!ydb) {
    usage();
  }

  dump(db, ydb);

  db->Close(db);
  ydb->Close(ydb);

  return(0);
}