File: array-show.c

package info (click to toggle)
sufary 2.1.1-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,236 kB
  • ctags: 782
  • sloc: ansic: 4,122; perl: 1,378; makefile: 736; sh: 664; tcl: 441; cpp: 192
file content (344 lines) | stat: -rw-r--r-- 8,886 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*--------------------------------------------------------------*
 *                                                              *
 *           = array  - Suffix Array Υƥȸ =       *
 *                                                              *
 *  array-show.c - show ޥɥϥɥ                        *
 *                 ߤϰϤ                         *
 *                                                              *
 *--------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sufary.h"
#include "array.h"

#include <assert.h>

#define MAX_SHOW_BUFFER_SIZE 400000
static char buffer[MAX_SHOW_BUFFER_SIZE]; /* ѥХåե */

static void show_sort(long show_from, long show_to);
static void output(long);
static void output_kwic(long);
static void output_simple(long);
static int long_cmp(const void *, const void *);

SUFARY *cur_ary;

/**********************************************
 *            eresult com_show(char *cmd);
 *
 * purpose
 *   ̤ɽ
 *
 * parameters
 *   cmd : 
 *
 * return value
 *   ץ³⡼
 *
 * description
 *   ߤΥȥ⡼ɤ˽äʬ
 **********************************************/
eresult com_show(char *cmd)
{
  int i;
  char *s, *t;
  long show_from, show_to;

  s = strtok(cmd, ct); /* 1ܤΰ */
  if (s == NULL) show_from = -1;
  else show_from = atol(s);
  t = strtok(NULL, ct); /* 2ܤΰ */
  if (t == NULL) show_to = -1;
  else show_to = atol(t);

  if (arrays == 0){
    printf ("specify target files first.\n");
    return CONT;
  }

  for (i = 0; i < arrays; i++){
    cur_ary = sufary[i];
    show_sort(show_from, show_to);
  }

  return CONT;
}


/**********************************************
 *          void output(long idx);
 *
 * purpose
 *   ǥ 1 Ǥ򤤤ʥեޥåȤǽ
 *
 * parameters
 *   idx : ǥ
 *
 **********************************************/
static void output(long idx)
{
  char *s;
  switch (display_mode){
  case DISP_KWIC:    output_kwic(idx);      break;
  case DISP_INDEX:
    printf("%s:%ld\n", keyword,idx);
    break;
  case DISP_LINE:
    s = sa_getline(cur_ary,idx);
    printf("%s\n",s);
    free(s);
    break;
  case DISP_SIMPLE:
  default:           output_simple(idx);    break;
  }
}


/**********************************************
 * int long_cmp(const void *data1, const void *data2);
 *
 * purpose
 *   ʸ
 *
 * parameters
 *   data1 : ʸ1
 *   data2 : ʸ2
 *
 * return value
 *   -1 ʲ : *data1 < *data2
 *    0      : *data1 == *data2
 *    1 ʾ : *data1 > *data2
 *
 * description
 *   sort ѤӥХåؿ
 **********************************************/
static int long_cmp(const void *data1, const void *data2)
{
  long *l1, *l2;
  long ret;

  l1 = (long*) data1;
  l2 = (long*) data2;
  ret = *l1 - *l2;
  return (ret>0)? 1: (ret<0)? -1: 0;
}


/**********************************************
 *      int suffix_compare(long *a, long *b)
 *
 * purpose
 *   ƥȤ򥳡ɽ˥
 *
 * parameters
 *   ƥΥǥ
 *
 * return value
 *   strcmpƱ
 *
 * description
 *   show_sortqsortƤФ롣
 *   (near 黻νϤǥեȤindexˤʤ뤿)
 **********************************************/
static int suffix_compare(long *a, long *b)
{
  return strcmp(cur_ary->txtmap + *a, cur_ary->txtmap + *b);
}


/**********************************************
 *      void show_sort(long show_from, long show_to)
 *
 * purpose
 *   ɽ礫ǥå˥Ȥ줿
 *
 * parameters
 *   showޥɤǻꤷɽϰ
 *
 * description
 *   show() ƤФ롣̤Ф output() Ƥ
 **********************************************/
static void show_sort(long show_from, long show_to)
{
  unsigned char *mapptr;
  static long now;
  long l, bottom, top, tmp, from, to, size;
  long *buf;

  bottom = sa_bottom(cur_ary);
  top    = sa_top(cur_ary);
  size = top - bottom + 1; /* 980327 for NEW sa_sel */

  if(show_from == -1) show_from = 1; /* show άν */
  if(show_to == -1) show_to = size; /* show άν */
  printf("from %ld to %ld (%ld)\n", show_from, show_to, size);

  from = bottom + show_from - 1; to = bottom + show_to;

  /* nearѤǥǥåˤʤäƤꤷʤ */
  if (cur_ary->arymap == cur_ary->aryorig){
    /*** ̤θ ***/
    if(sort_mode == 1){ /* 祽 */
      for (tmp = from; tmp < to ; tmp++){
	l = sa_aryidx2txtidx(cur_ary, tmp);
	output(l);
      }
    }else{ /* ǥå祽  󥽡ȤƤޤΤԷк */
      buf = (long *)malloc(size * sizeof(long));
      for (tmp = 0; tmp < size ; tmp++)
	buf[tmp] = sa_aryidx2txtidx(cur_ary, tmp + bottom);
      qsort(buf, size, sizeof(long), (int(*)(const void*,const void*))(long_cmp)); 
      for (tmp = from; tmp < to; tmp++)	output(buf[tmp-(bottom)]);
      free(buf);
    }
  } else {
    /*** near ***/
    if(sort_mode == 1){ /* 祽  󥽡ȤƤޤΤԷк */
      buf = (long *)malloc(size * sizeof(long));
      for (tmp = 0; tmp < size ; tmp++)
	buf[tmp] = sa_aryidx2txtidx(cur_ary, tmp);
      qsort(buf, size, sizeof(long), (int(*)(const void*,const void*))(suffix_compare));
      for (tmp = from; tmp < to; tmp++)	output(buf[tmp-bottom]);
      free(buf);
    }else{ /* ǥå祽 */
      for (tmp = from; tmp < to ; tmp++){
	l = sa_aryidx2txtidx(cur_ary,tmp);
	output(l);
      }
    }
  }
}


/**********************************************
 * void output_simple(long idx);
 *
 * purpose
 *   index Ȥ60ʸ
 *
 * parameters
 *   idx : ǥ
 *
 **********************************************/
static void output_simple(long idx)
{
  strncpy(buffer,cur_ary->txtmap+idx,60);
  if(mojibake_proc_flag){ /* ʸɻ߽ */
    sa_mojibakebousi(buffer);
    sa_ctrl_code_tubusi(buffer);
  }
  printf("%ld : %s\n", idx, buffer);
}


/****************************************************
 * output_kwic: ̤ Key Word in Context ɽ
 * 
 * parameters
 *   idx --- ɤؤΥǥå
 *  970126
 *  ޤǤKWIC˥Ĺ(Ⱦʬ)ޤƤϤᡣ
 *****************************************************/
static void output_kwic(long idx)
{
  long kwic_from,kwic_to;
  char *pos;
  char kwicpre[1500],kwicpost[1500];
  long kwlen = strlen(keyword); /* ɤʸĹ */

  if (cur_ary->arymap != cur_ary->aryorig) kwlen = 0; /* nearΤȤ */

  /* ɤʬФ */
/*  kwic_from = idx - kwic_width + kwlen/2;*/
  kwic_from = idx - kwic_width;
  if(kwic_from < 0) kwic_from = 0;

  strncpy(kwicpre,cur_ary->txtmap+kwic_from,idx-kwic_from);
  kwicpre[idx-kwic_from] = '\0';

  /* ɤʬФ */  
  kwic_to = idx + kwic_width + kwlen;
  if(kwic_to > (long)cur_ary->txtsz) kwic_to = (long)cur_ary->txtsz;

  strncpy(kwicpost,cur_ary->txtmap+idx+kwlen,kwic_to-idx);  
  kwicpost[kwic_to-(idx+kwlen)] = '\0';

  if (cur_ary->arymap == cur_ary->aryorig){
    /*** ̤θ ***/
    /* Ĥ */
    sprintf(buffer,"%s<SUFARYKEY>%s</SUFARYKEY>%s",kwicpre,keyword,kwicpost);
  }else{
    /*** near ***/

    sprintf(buffer,"%s<SUFARYKEY>%s",kwicpre,kwicpost);
/*    kwic_from = idx - kwic_width;
    if(kwic_width % 2 == 1) kwic_from++;
    if(kwic_from < 0) kwic_from = 0;
    kwic_to = idx + kwic_width;
    if(kwic_to > (long)cur_ary->txtsz) kwic_to = (long)cur_ary->txtsz;

    strncpy(buffer, cur_ary->txtmap+kwic_from, kwic_to-kwic_from);

    buffer[kwic_to-kwic_from] = '\0';
*/
  }
  
  if(mojibake_proc_flag){ /* ʸɻ߽ */
    sa_mojibakebousi(buffer);
    sa_ctrl_code_tubusi(buffer);
  }

  printf("%s\t%ld\n",buffer,idx); /*  */
}


/**********************************************
 * com_line(char *cmd);
 **********************************************/
eresult com_line(char *cmd){
  long pos;
  char *s;

  pos = atol(cmd);
  s = sa_getline(sufary[0],pos);
  printf("%s\n",s);
/*  if(s[strlen(s)-1] != '\n') printf("\n");*/
  free(s);
  return CONT;
}


/**********************************************
 * com_get(char *cmd);
 **********************************************/
eresult com_get(char *cmd)
{
  long pos;
  char *s,d1[100],d2[100];

  if(sscanf(cmd,"%ld %s %s",&pos,d1,d2) < 3) return ERROR;
  s = sa_getblock(sufary[0],pos,d1,d2);
  printf("%s",s);
  if(s[strlen(s)-1] != '\n') printf("\n");
  free(s);
  return CONT;
}


/**********************************************
 * com_get_str(char *cmd);
 **********************************************/
eresult com_get_str(char *cmd)
{
  long idx;
  int num;
  char buf[1000];
  if(sscanf(cmd,"%ld %d",&idx,&num) < 2) return ERROR;
  strncpy(buf,(sufary[0]->txtmap+idx),num);
  buf[num] = '\0';
  printf("%s\n",buf);
  return CONT;
}