File: skkdic-sort.c

package info (click to toggle)
skktools 1.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 924 kB
  • ctags: 143
  • sloc: sh: 7,211; ansic: 1,251; perl: 798; lisp: 269; ruby: 133; makefile: 98; awk: 94; cpp: 73; sed: 1
file content (190 lines) | stat: -rw-r--r-- 4,633 bytes parent folder | download | duplicates (2)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* SKK JISYO TOOLS (SKK dictionary handling tools)
Copyright (C) 1994, 1996, 1999, 2000
      Hironobu Takahashi, Masahiko Sato, Kiyotaka Sakai

Author: Hironobu Takahashi, Masahiko Sato, Kiyotaka Sakai, Kenji Yabuuchi
Maintainer: Mikio Nakajima <minakaji@osaka.email.ne.jp>
Version: $Id: skkdic-sort.c,v 1.3 2000/10/05 17:16:44 czkmt Exp $
Keywords: japanese
Last Modified: $Date: 2000/10/05 17:16:44 $

This file is part of Daredevil SKK.

SKK JISYO TOOLS are free software; you can redistribute them and/or modify
them under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

SKK JISYO TOOLS are distributed in the hope that they will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with SKK; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* skkdic-sort.c
   ���Υץ���� SKK �μ���������Ԥ��ޤ�
 */

#include <config.h>
#include <stdio.h>
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif

/* ���Υץ���Ǥϼ���򤹤٤ƥ��������ɤ߹��ߤޤ���
   �����ͤϤ��餫�����Ѱդ������ȡ���­���������ɲä�
   �������̤��ꤷ�ޤ������ޤ꾮���������ˤ� realloc()
   ��ȯ�����ޤ���*/

#define STEP (256*1024)

/* ����ϼ��Υݥ��󥿤ˤ�äƺ����줿����˽񤭹��ޤ�ޤ� */
static unsigned char *dict;
static unsigned int dictsize;

/* ����Υ���ǥ����ꥹ�ȤǤ� */
static unsigned int *index;

/* �����Ȥǻ��Ѥ���ݥ��󥿤Ǥ� */
static unsigned int *list;
static unsigned int n_line;

/* ������ɤ߹��ߥץ��� */
static void
readdict()
{
  unsigned int bufsize, incsize;

  dictsize = 0;

  /* �����ѤΥ���Ȥ��� STEP �Х��Ȥ��Ѱդ��ޤ� */
  dict = malloc(STEP);
  bufsize = STEP;
  incsize = fread(dict+dictsize, 1, bufsize-dictsize, stdin);
  dictsize += incsize;

  /* �⤷����դʤ�� STEP �Х��Ȥ������䤷���ɤ߹��ߤ򷫤��֤��ޤ� */
  while (dictsize == bufsize) {
    bufsize += STEP;
    dict = realloc(dict, bufsize);
    incsize = fread(dict+dictsize, 1, bufsize-dictsize, stdin);
    dictsize += incsize;
  }
}

/* ����ǥ�������� */
static void
make_index()
{
  int i, n;

  n = 0;
  for (i = 0; i < dictsize; ++ i)
    if (dict[i] == '\n')
      ++ n;
  n_line = n;

  index = malloc((sizeof *index)*(n_line+1));
  n = 0;
  index[0] = 0;
  for (i = 0; i < dictsize; ++ i)
    if (dict[i] == '\n')
      index[++n] = i+1;

  list = malloc((sizeof *list)*n_line);
  for(i = 0; i < n_line; ++ i)
    list[i] = i;
}

/* �����Ȥ����ʸ������ӥ롼����
   ����ʤ��Υ���ȥ�ǰ��� a ����ˤ���٤����� -1 ���֤���
   ����ʳ��λ��� 1 ���֤� */
static int
hexcomp(a, b)
     unsigned char *a;
     unsigned char *b;
{
  while(*a == *b) {
    ++a; ++ b;
  }
  if (*a > *b) return 1;
  return -1;
}

/* ���꤬�ʤ����뤫�ɤ���Ƚ�Ǥ���  0-�ʤ� 1-���� */
static int okuriari(p)
     unsigned char *p;
{
  if ((p[0] & 0x80) == 0) return 0;
  while(*p != ' ')
    if (*p == '\0') return 0; /* ���򤬤ʤ��Ԥ������ϰ۾�Ȥ��ư����٤� */
    else            ++ p;
  if (('a' <= p[-1]) && (p[-1] <= 'z')) return 1;
  return 0;
}

/* �����Ȥ������ӥ롼����
   ���� a ����ˤ���٤����� -1 ���֤�������ʳ��λ��� 1 ���֤� */
int skkcompar(a, b)
     int *a;
     int *b;
{
  unsigned char *ptra, *ptrb;
  ptra = dict+index[*a];
  ptrb = dict+index[*b];

  if (okuriari(ptra)) {
    if (okuriari(ptrb))
      /* ����������겾̾������Τǡ�a b ��򴹤�����Ӥ��� */
      return hexcomp(ptrb, ptra);
    else
      return -1;  /* a ���� */
  } else {
    if (okuriari(ptrb))
      return 1;  /* a ���� */
    else
      /* ����������겾̾���ʤ��Τǡ�a b �򤽤Τޤ���Ӥ�����̤��֤� */
      return hexcomp(ptra, ptrb);
  }
}

/* ��̤���� */
static void printout()
{
  int i;
  unsigned char *ptr, *line;
  int okuriflag;

  puts(";; okuri-ari entries.");
  okuriflag = 1;
  for(i = 0; i < n_line; ++ i) {
    line = dict+index[list[i]];
    if (line[0] == ';') continue; /* �����ȹԤϽ��Ϥ��ʤ� */
    if (okuriflag) {
      if (!okuriari(line)) {
	puts(";; okuri-nasi entries.");
	okuriflag = 0;
      }
    }
    for (ptr = line; *ptr != '\n'; ++ ptr)
      putchar(*ptr);
    putchar('\n');
  }
}

/* �ᥤ��ץ��� ����ϻ��Ѥ��ޤ��� */
int main(argc, argv)
     int argc;
     char **argv;
{
  readdict();
  make_index();
  qsort((char *)list, n_line, sizeof *list, skkcompar);
  printout();
  return 0;
}

/* end of skkdic-sort.c */