File: sass.c

package info (click to toggle)
sufary 2.1.1-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,236 kB
  • ctags: 782
  • sloc: ansic: 4,122; perl: 1,378; makefile: 726; sh: 664; tcl: 441; cpp: 192
file content (105 lines) | stat: -rw-r--r-- 2,949 bytes parent folder | download | duplicates (5)
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
/******************************************************************************
   SUFARYڸץ sass
   
   USAGE:
     sass KEYWORD FILE1 FILE2 ...
     sass '' FILE1 FILE2 ...

   оݤ˻ꤷեˤ FILE1.ary, FILE2.ary Ȥä
     arrayե뤬ɬפǤ     

   (KEYWORD)  '' ꤹȡɸϤ饭
     ϤƸԤʤޤ

 *****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "sufary.h"

long pre_scan(SUFARY *ary, long pos);
void search_and_print(SUFARY *ary, char *key, char *fname);

main(int argc, char *argv[])
{
  SUFARY *ary[256];
  int i;
  int num_of_files = argc - 2; /* եο */

  if(argc < 3){ /* ­ʤ */
    fprintf(stderr,
	    "\n"
	    "sass --- SUFARY Simpler Search\n\n"
	    "Version 1.0.1 981115\n\n"
	    "USAGE\n"
	    "  sass KEYWORD FILE1 FILE2 ...\n"
	    "  sass '' FILE1 FILE2 ...\n\n"
	    "  оݤ˻ꤷեˤarrayե뤬ɬפǤ\n"
	    "    (arrayե̾ɬ ե̾ + \".ary\")\n"
	    "  (KEYWORD)  '' ꤹɸϤ\n"
	    "    ɤϤƸԤʤޤ\n"
	    "\n"
	    );
    exit(1);
  }

  /** ե򳫤 **/
  for (i = 0; i < num_of_files; i++)
    if ((ary[i] = sa_openfiles(argv[i+2],NULL)) == NULL) exit(1);

  if(argv[1][0] == '\0'){
    /*** ɸϤ饭 ***/
    char cmd[1000];
    while(fgets(cmd, (int)sizeof(cmd), stdin)){
      cmd[strlen(cmd)-1] = '\0'; /* Ϥ줿ɤβ٤ */
      for (i = 0; i < num_of_files; i++){
	if(num_of_files == 1) search_and_print(ary[i], cmd, NULL);
	else search_and_print(ary[i], cmd, argv[i+2]);
	sa_reset(ary[i]); /*  */
      }
    }
  } else {
    /*** (argv[1]) ***/
    for (i = 0; i < num_of_files; i++){
      if(num_of_files == 1) search_and_print(ary[i], argv[1], NULL);
      else search_and_print(ary[i], argv[1], argv[i+2]);
    }
  }

  /** եĤ **/
  for (i = 0; i < num_of_files; i++) sa_closefiles(ary[i]);
}


/*
   void search_and_print(SUFARY *ary, char *key, char *fname)
   ɽ
*/
void search_and_print(SUFARY *ary, char *key, char *fname)
{
  int i = 0;
  long ai, pos, lpos;
  char *s;
  if (sa_sel(ary, key) == CONT){
    for (ai = sa_left(ary); ai <= sa_right(ary) ; ai++){
      pos = sa_aryidx2txtidx(ary, ai);
      lpos = pre_scan(ary, pos); /* ƬΥǥå */
      s = sa_getline(ary, pos);
      if(fname != NULL) /* ʣեΤȤϥե̾ɽ */
	printf("%s:",fname);
      printf("%ld:%ld:%s\n", lpos, pos-lpos, s);
      free(s);
    }
  }
}


/*
   long pre_scan(SUFARY *ary, long pos)
   ֤߰Ƭޤǥ󤷤ơƬΥǥåĴ٤
 */
long pre_scan(SUFARY *ary, long pos)
{
  while(*(sa_txtidx2txtptr(ary, pos)) != '\n' && pos > 0) pos--;
  if(pos == 0)return 0;
  return pos+1;
}