File: tsidump.c

package info (click to toggle)
libtabe 0.2.6-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,568 kB
  • ctags: 1,782
  • sloc: ansic: 17,454; sh: 9,848; makefile: 268
file content (157 lines) | stat: -rw-r--r-- 3,141 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
150
151
152
153
154
155
156
157
/*
 * Copyright 1999, TaBE Project, All Rights Reserved.
 * Copyright 1999, Pai-Hsiang Hsiao, All Rights Reserved.
 *
 * $Id: tsidump.c,v 1.6 2003/05/11 07:25:25 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 <db.h>
#include <tabe.h>
#include "../src/version.h"

void
usage(void)
{
  printf("tsidump: libtabe-%s\n", RELEASE_VER);
  printf("Usage: tsidump -d <TsiDB> [-f output file -ry]\n");
  printf("   -d <TsiDB>     \t path to TsiDB\n");
  printf("   -f <output file>\t output file in plain text (default: stdout)\n");
  printf("   -r             \t include reference count (default: not)\n");
  printf("   -y             \t include yin data (default: not)\n");
  exit(0);
}

void
dump(struct TsiDB *db, FILE *fp, int ref, int tsiyin)
{
  struct TsiInfo *tsi;
  int rval, i, j, k, len;

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

  tsi = (struct TsiInfo *)malloc(sizeof(struct TsiInfo));
  tsi->tsi = (ZhiStr)malloc(sizeof(unsigned char)*80);
  memset(tsi->tsi, 0, 80);
  tsi->refcount = -1;
  tsi->yinnum = -1;
  tsi->yindata = (Yin *)NULL;

  i = 0;
  while (1) {
    if (i == 0) {
      db->CursorSet(db, tsi, 0);
    }
    else {
      rval = db->CursorNext(db, tsi);
      if (rval < 0) {
        break;
      }
    }
    i++;
    len = strlen((char *)tsi->tsi)/2;
    fprintf(fp, "%s", tsi->tsi);
    if (ref) {
      fprintf(fp, " %ld", tsi->refcount);
    }
    if (tsiyin) {
      ZuYinSymbolSequence zs = NULL;
      int begin = 0;

      fprintf(fp, " ");
      for (j = 0; j < tsi->yinnum; j++) {
        for (k = 0; k < len; k++) {
          zs = tabeYinToZuYinSymbolSequence(tsi->yindata[j*len+k]);
          if (zs) {
            if (begin) {
              fprintf(fp, "@");
            }
            else {
              begin = 1;
            }
            fprintf(fp, "%s", zs);
            free(zs);
          }
        }
      }
    }
    fprintf(fp, "\n");
  }

  db->Close(db);
}

int
main(int argc, char **argv)
{
  int ch;
  int ref, tsiyin;
  FILE *fp;
  struct TsiDB *db;
extern char *optarg;
extern int optind, opterr, optopt;

  char *db_name, *op_name;

  db_name = op_name = (char *)NULL;
  ref = 0;
  tsiyin = 0;

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

  if (!db_name) {
    usage();
  }

  db = tabeTsiDBOpen(DB_TYPE_DB, db_name, 
      tsiyin?DB_FLAG_READONLY:DB_FLAG_READONLY|DB_FLAG_NOUNPACK_YIN);
  if (!db) {
    usage();
  }

  if (op_name) {
    fp = fopen(op_name, "w");
    dump(db, fp, ref, tsiyin);
    fclose(fp);
  }
  else {
    dump(db, stdout, ref, tsiyin);
  }

  return(0);
}