summaryrefslogtreecommitdiff
path: root/biblio/tib/sk/swortcp.c
blob: 32c10ae69a5678709c74d553b0a3b94ee475ee2f (plain)
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
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define DEBUG
#undef DEBUG

#define MAXLINE 1024		/* max. no. of characters per line */
#define MAXREC (20*MAXLINE)	/* max. no. of characters per record */

#define DELIMITER ','		/* delimiter character */

char *progname;
char *infilename;
int linenumber;

char record_buffer[MAXREC];	/* buffer for a record consisting of
				   several lines */
char *rec_ptr;

char wstring[MAXLINE];		/* holds the line of key words */

main(argc, argv)
     int argc;
     char *argv[];
{
  FILE *fp, *fopen();

  rec_ptr = record_buffer;
  progname = argv[0];
  if (argc == 1) {		/* without arguments: */
    infilename = NULL;
    filebylines(stdin);		/* hack standard input */
  }
  else
    while (--argc > 0)
      if ((fp = fopen(*++argv, "r")) == NULL) {
	fprintf(stderr,
		"%s: can't open %s\n", progname, *argv);
	exit(1);
      } else {
	infilename = *argv;
	filebylines(fp);
	fclose(fp);
      }
  exit(0);
}
	
filebylines(fp)			/* pass each line of file fp to a function */
     FILE *fp;
{
  char s[MAXLINE];		/* buffer for one line of input */

  linenumber = 0;
  while (fgets(s, MAXLINE, fp) != NULL) {
    linenumber++;
#ifdef DEBUG
    printf("##filebylines: line %d: ``%s''\n", linenumber, s);
#endif
    linehack(s);
  }
  write_records();	/* to write out the last record, if the file */
  			/* does not end with an empty line */
}

linehack(s)		/* accumulate line s of a record into rec_ptr */
     char *s;
{
  char tibfield();

#ifdef DEBUG
  printf("##tibfield(%s) = %c\n", s, tibfield(s));
#endif  
  switch (tibfield(s)){
  case 'W':			/* a key word line */
#ifdef DEBUG
      printf("Saving `%s' in wstring.\n", s);
#endif
    strcpy(wstring, s);		/* save it for write_records */
    break;
  case '0':			/* an empty line means end of this record */
    write_records();
    break;
  case '1':			/* other lines, maybe continuation lines */
    append_to_record_buffer(s);
    break;
  default:
    fprintf(stderr, "switch fall through in linehack: internal error!\n");
    break;
  }
}

char tibfield(s)			/* return type (tib field tag) of s */
     char *s;
{
  if (strcmp(s,"\n") == 0)	/* empty line got by fgets contains \n only */
    return('0');
  if (strlen(s) >= 2 && s[0] == '%' & s[1] == 'W')
      return('W');
  return('1');			/* neither empty nor %W line*/
}

write_records()	/* split line wstring into key words and output as
			   many copies of record_buffer as there are key
			   words. */
{
  char *s;
  char *swort;
  char c;

#ifdef DEBUG
  printf("##write_records: splitting `%s'\n", wstring);
#endif
  s = wstring;
  if (*s == '\0'){		/* s is empty if there was no %W line in this
				   record */
    reset_rec_ptr();		/* Throw away records without key word.
				   BUG FIX 89/3/9 sk
				   [left overs got into the next record] */
    return;
  }
  
  if (*s != '%') {
    fprintf(stderr,
	    "Wrong call to write_records - in line %d `%%' expected.\n",
	    linenumber);
  }
  s++;				/* advance over `%' ... */
  while (*s){
    while (isspace(*++s))	/* ... over W or DELIMITER from last iteration
				   and over white space */
      ;
    swort = s;			/* start of key word in s */
    while ((c = *s) != DELIMITER && c != '\n' && c != '\0') {
      s++;
    }
    *s = '\0';			/* put end of key where DELIMITER or \n was */
    if (strlen(swort)!= 0) {	/* the last key word is always empty */
#ifdef DEBUG
      printf("##write_records: outputting one copy for key `%s'\n", swort);
#endif
      printf("%%W %s\n", swort);
      printf("%s\n", record_buffer);
    }
    *s = c;			/* ready for next key word */
  }				/* all key words processed */
  wstring[0] = '\0';		/* so that succesive empty lines are
				   suppressed */
  reset_rec_ptr();
}

append_to_record_buffer(s)
     char *s;
{

#ifdef DEBUG
      printf("##append_to_record_buffer: `%s'\n", s);
#endif
  strcpy(rec_ptr, s);	/* append this line to the record buffer */
  rec_ptr += strlen(s);	/* ready for next append */

}

reset_rec_ptr()
{
#ifdef DEBUG
  printf("write_records: resetting rec_ptr.\n");
#endif
  rec_ptr = record_buffer; /* ready for next record */
}