File: tsin-char.cpp

package info (click to toggle)
gcin 2.9.4%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,796 kB
  • sloc: cpp: 34,326; ansic: 9,319; makefile: 658; sh: 556
file content (153 lines) | stat: -rw-r--r-- 2,709 bytes parent folder | download | duplicates (8)
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
#include "gcin.h"
#include "pho.h"
#include "tsin.h"

extern int ts_gtabN;
typedef struct {
  char ch[CH_SZ];
  u_int bits;
} CH_ENT;

static CH_ENT *chs;
static int chsN;

static int qcmp_ch(const void *aa, const void *bb)
{
  CH_ENT *a = (CH_ENT *)aa;
  CH_ENT *b = (CH_ENT *)bb;

  return memcmp(a->ch, b->ch, sizeof(CH_SZ));
}

static CH_ENT *find(char *ch)
{
  char t[CH_SZ];
  bzero(t, CH_SZ);

  u8cpy(t, ch);
  int bottom = 0;
  int top = chsN-1;

  do {
    int mid = (bottom + top) /2;
    int r = memcmp(t, chs[mid].ch, CH_SZ);

    if (r < 0)
      top = mid - 1;
    else
    if (r > 0)
      bottom = mid + 1;
    else
      return &chs[mid];
  } while (bottom <= top);

  return NULL;
}

#include <sys/stat.h>

void build_ts_gtab(int rebuild);
void get_gcin_user_or_sys_fname(char *name, char fname[]);
int load_ts_gtab(int idx, char *tstr, usecount_t *usecount);

static void build_chs()
{
  if (!ts_gtabN)
    build_ts_gtab(0);

  char fname[256];
  get_gcin_user_or_sys_fname("tsin-ch-idx", fname);
  struct stat st_gtab, st_tsin32;
  FILE *fp;
  extern char tsfname[];

#if 1
  if (!stat(fname, &st_gtab) && !stat(tsfname, &st_tsin32) &&
      st_tsin32.st_mtime < st_gtab.st_mtime) {

    if (fp=fopen(fname, "rb")) {
      printf("............... from %s\n", fname);
      fread(&chsN, sizeof(chsN), 1, fp);
      chs = tmalloc(CH_ENT, chsN);
      fread(chs, sizeof(CH_ENT), chsN, fp);
      fclose(fp);
      return;
    }
  }
#endif

  int i;
  char str[MAX_CIN_PHR];
  usecount_t uc;

  for(i=0;i < ts_gtabN; i++) {
    load_ts_gtab(i, str, &uc);
    char *p=str;

    while (*p) {
      chs = trealloc(chs, CH_ENT, chsN+1);
      bzero(&chs[chsN], sizeof(CH_ENT));
      int sz = u8cpy(chs[chsN].ch, p);
      p+=sz;
      chsN++;
    }
  }

  qsort(chs, chsN, sizeof(CH_ENT), qcmp_ch);
#if 0
  printf("chsN:%d\n", chsN);
#endif
  int nchsN=1;
  for(i=1; i<chsN; i++)
    if (qcmp_ch(&chs[i], &chs[i-1]))
      chs[nchsN++]=chs[i];

  chsN = nchsN;
  chs = trealloc(chs, CH_ENT, chsN);
#if 0
  printf("chsN:%d\n", chsN);
#endif
  for(i=0;i < ts_gtabN; i++) {
    load_ts_gtab(i, str, &uc);
    char *p=str;

    int cidx = 0;
    while (*p) {
      int sz = utf8_sz(p);
      CH_ENT *ce = find(p);
      if (!ce)
        p_err("err found %s", p);

      ce->bits |= 1<<cidx;

      cidx++;
      p+=sz;
    }
  }

  if (fp=fopen(fname, "wb")) {
    fwrite(&chsN, sizeof(chsN), 1, fp);
    fwrite(chs, sizeof(CH_ENT), chsN, fp);
    fclose(fp);
  }
}


int ch_pos_find(char *ch, int pos)
{
//  utf8_putchar(ch);

  if (!chsN)
    build_chs();

  CH_ENT *p = find(ch);

  if (!p) {
//    puts("0");
    return 0;
  }

  int v = p->bits & (1<<pos);
//  printf("%d\n", v);
  return v;
}