source: app/src/common/common.h @ 0e3df7e

help
Last change on this file since 0e3df7e was 0e3df7e, checked in by obrebski <obrebski@…>, 16 years ago

process_seg() naprawione, 'lem -s kor -I kor' nie zawiesza sie

git-svn-id: svn://atos.wmid.amu.edu.pl/utt@46 e293616e-ec6a-49c2-aa92-f4a8b91c5d16

  • Property mode set to 100644
File size: 9.3 KB
Line 
1#ifndef __COMMON_H
2#define __COMMON_H
3
4#include <stdio.h>
5#include <ctype.h>
6#include <string.h>
7#include <stdlib.h>
8
9#include "../lib/const.h"
10
11#ifndef _CMDLINE_FILE
12#error _CMDLINE_FILE constant not defined!
13#else
14#include _CMDLINE_FILE
15#endif
16
17
18/**************************************************
19 * Stale dotyczace wejscia/wyjscia
20 */
21
22#define EMPTYFORM '*'
23#define INFIELD_SEP ':'
24#define MAXAUX 16
25#define FIELD_SEP " \t\n"
26#define FIELD_PREFIX_MAXLEN 32
27
28
29// katalogi z plikami konfiguracyjnymi
30// nowe
31// stare - do wyrzucenia
32// #define CONFIG_DIR ".utt/conf"
33
34// nazwa zmiennej okreslajaca sciezke do danych
35
36// #define UTT_DIR_VAR "UTT_DIR"
37
38// sciezka do plikow z danymi (np UTT_DIR/pliki) wzgledem $HOME!
39
40// #define UTT_DIR_DEFAULT ".utt/pl/"
41
42/**************************************************/
43
44
45extern FILE* inputf;
46extern FILE* outputf;
47extern FILE* failedf;
48
49extern char* input_filename;
50extern char* output_filename;
51extern char* failed_filename;
52extern bool one_line;
53extern bool one_field;
54
55extern char input_field_prefix[];
56extern char output_field_prefix[];
57
58extern bool copy_processed;
59extern bool append_output;
60extern bool append_failed;
61
62//sciezka do katalogu z danymi
63extern char utt_dir[];
64
65extern void process_common_options(gengetopt_args_info* args, char* argv0);
66extern void process_config_files(gengetopt_args_info* args, char* argv0);
67
68extern int expand_path(char* inpath, char* outpath);
69
70/**************************************************
71 * problems with casing                           */
72// sprawdzenie wielkosci liter
73// warto¶Ê zwracana:
74// 0 - wszystkie ma³e litery
75// 1 - pierwsza wielka, reszta male
76// 2 - wszystkie wielkie
77// 3 - inne
78inline int casing(char* s)
79{
80  int ret = isupper(*s) ? 1 : 0;
81  while(*++s != '\0')
82  {
83    if(isupper(*s))
84    {
85      if(ret==1) ret=2;
86      else if(ret==0) ret=3;
87    }
88    else
89    {
90      if(ret==2) ret=3;
91    }
92  }
93  return ret;
94}
95
96//
97inline void tolowers(char* s, char* d)
98{
99  *d=tolower(*s);
100  while(*s != '\0') * ++d = tolower(* ++s);
101}
102
103
104// przepisuje s do d
105// nadajac wielko¶Ê liter zgodnie z warto¶ci± casing
106// casing - warto¶Ê zwracana przez casing()
107// je¶li casing==3 przepisuje bez zmian (za ma³o informacji)
108inline void restorecasing(char *s, char *d, int casing)
109{
110  switch(casing)
111  {
112  case 0:
113  case 3:
114    *d=*s;
115    while(*s != '\0') * ++d = * ++s;
116    break;
117  case 1:
118    *d=toupper(*s);
119    while(*s != '\0') * ++d = * ++s;
120    break;
121  case 2:
122    *d=toupper(*s);
123    while(*s != '\0') * ++d = toupper(* ++s);
124    break;
125  }
126}
127
128/**************************************************/
129
130/*
131parameters:
132  -seg  - segment
133  -pref - field name or "1", "2", "3", "4" for the first four fields
134  +val  - field contents
135return value:
136  1 if specified field exists, 0 otherwise
137*/
138
139inline int getfield(char* seg, const char* pref, char* val)
140{
141
142  char* p=seg;
143  char* p0;
144
145  while(isspace(*p)) ++p;
146
147  // field "1"
148  p0=p; while(isdigit(*p)) ++p;
149  if(*pref=='1') if(p!=p0) { strncpy(val,p0,p-p0); val[p-p0]='\0'; return 1; } else return 0;
150 
151  while(isspace(*p)) ++p;
152
153  // field "2"
154  p0=p; while(isdigit(*p)) ++p;
155  if(*pref=='2') if(p!=p0) { strncpy(val,p0,p-p0); val[p-p0]='\0'; return 1; } else return 0;
156
157  while(isspace(*p)) ++p;
158
159  // field "3"
160  p0=p; while(isgraph(*p)) ++p; 
161  if(*pref=='3') if(p!=p0) { strncpy(val,p0,p-p0); val[p-p0]='\0'; return 1; } else return 0;
162
163  while(isspace(*p)) ++p;
164
165  // field "4"
166  p0=p; while(isgraph(*p)) ++p;
167  if(*pref=='4') if(p!=p0) { strncpy(val,p0,p-p0); val[p-p0]='\0'; return 1; } else return 0;
168
169  while(isspace(*p)) ++p;
170
171  // annotation fields
172  do p=strstr(p,pref); while(p!=NULL && *(p-1)!=' ' && *(p-1)!='\t');
173 
174  if(p==NULL) return 0;
175  else
176  {
177    p+=strlen(pref);
178    int len=strcspn(p,FIELD_SEP "\n\r\f\0");
179    strncpy(val,p,len);
180    val[len]='\0';
181    return 1;
182  }
183}
184
185
186/*
187parameters:
188        -name - field name, long or short
189        +prefix - field name with ':' appended if long name
190return value:
191        1 if correct field name, 0 otherwise
192examples:
193name    prefix  r.v.
194lem     lem:    1
195@       @       1
196::      'undef' 0
197a,b     'undef' 0
198*/
199inline
200int fieldprefix(char *name, char *prefix)
201{
202  if (ispunct(name[0]) && name[1]=='\0') // correct short name
203  {
204    strcpy(prefix, name); return 1;
205  }
206
207  int i=0;
208  while(name[i]!='\0' && isalnum(name[i])) ++i;
209 
210  if(name[i]=='\0' && i>0) // correct long name
211  {
212    sprintf(prefix,"%s:",name); return 1;
213  }
214
215  // incorrect
216  return 0;
217}
218
219inline
220bool process_seg(char* seg, gengetopt_args_info& args)
221{
222  char buf[256];
223  char pref[FIELD_PREFIX_MAXLEN];
224  bool ret = !args.process_given;
225  if(args.process_given)
226    {
227      getfield(seg,"3",buf);
228      for(int i=0; i<args.process_given; ++i)
229        if(strcmp(args.process_arg[i],buf)==0)
230          {
231            ret=true;
232            break;
233          }
234    }
235
236  if(ret==false) return false; 
237
238  for(int i=0; i<args.select_given; ++i)
239    {
240      fieldprefix(args.select_arg[i],pref); // !!! ŁATKA - ZOPTYMALIZOWAĆ !!!
241      if(! getfield(seg,pref,buf))
242        return false;
243    }
244  for(int i=0; i<args.ignore_given; ++i)
245    {
246      fieldprefix(args.ignore_arg[i],pref);  // !!! ŁATKA - ZOPTYMALIZOWAĆ !!!
247      if(getfield(seg,pref,buf))
248        return false;
249    }
250 
251  if(args.input_field_given & !getfield(seg,input_field_prefix,buf))
252    return false;
253
254  return true;
255}
256
257
258/*
259parameters:
260  -+seg - segment
261  -pref - prefix of the new field
262  -val  - contents of the new field
263return value:
264  1 - success, 0 - fail (limit on segment length exceeded)
265 */
266inline
267int addfield(char *seg, const char *pref, const char *val)
268     // zalozenie, ze seg konczy sie znakiem \n
269{
270  if(strlen(seg)+strlen(pref)+strlen(val) >= MAX_LINE) return 0; // bezpieczniej, ale wolniej
271
272  int seglen=strlen(seg);
273  sprintf(seg+(seglen-1)," %s%s\n",pref,val);
274  return 1;
275}
276
277/**************************************************/
278
279struct Seg
280{
281  int filepos, len;
282  char* tag;
283  char* form;
284  char* aux[MAXAUX];
285  int auxn;
286
287  bool parse(char* line);
288  char* getfield(char* fieldname);
289  void print(char* line);
290  bool addfield(char* s);
291  bool clearfields();
292};
293
294/**************************************************/
295
296/* definicja struktury wejscia/wyjscia
297 */
298struct Segment
299{
300  int filepos, len;
301  char* tag;
302  char* form;
303  char* aux[MAXAUX];
304  int auxn;
305
306  bool parse(char* line);
307  char* getfield(char* fieldname);
308  void print(char* line);
309  bool addfield(char* s);
310  bool clearfields();
311};
312
313/*
314 * Sprawdza czy nalezy przetwarzac dany segment.
315 */
316
317inline
318bool process_seg(Segment& s, gengetopt_args_info& args)
319{
320  bool ret = !args.process_given;
321
322  for(int i=0; i<args.process_given; ++i)
323    if(strcmp(args.process_arg[i],s.tag)==0)
324      {
325        ret=true;
326        break;
327      }
328
329  for(int i=0; i<args.select_given; ++i)
330    if(! s.getfield(args.select_arg[i]))
331      ret=false;
332
333  for(int i=0; i<args.ignore_given; ++i)
334    if(s.getfield(args.ignore_arg[i]))
335      ret=false;
336
337  return ret;
338}
339
340
341/*
342 * FUNKCJE OBSLUGUJACE WEJSCIE/WYJSCIE
343 */
344// napisy zostaj na miejscu (w line), tylko wskazniki sa ustawian
345// i zara dopisywane zera s dopisywane
346
347inline
348bool Segment::parse(char* line)
349{
350  auxn=0;
351  char* field;
352  if((field=strtok(line,FIELD_SEP))!=NULL)
353    filepos=atoi(field); // nie sprawdzana poprawnosc
354  else
355    return false;
356  if((field=strtok(NULL,FIELD_SEP))!=NULL)
357    len=atoi(field); // nie sprawdzana poprawnosc
358  else return false;
359  if((tag=strtok(NULL,FIELD_SEP))==NULL) return false;
360  if((form=strtok(NULL,FIELD_SEP))==NULL)
361    return true;
362  else
363    if(form[0] == EMPTYFORM && form[1] =='\0')
364      form=NULL;
365
366  while((aux[auxn]=strtok(NULL,FIELD_SEP))!=NULL) ++auxn;
367
368  return true;
369}
370
371
372inline char* Segment::getfield(char* f)
373{
374  int flen=strlen(f);
375  if(isalnum(*f))
376  {
377    for(int i=0; i<auxn; ++i)
378      if(strncmp(aux[i],f,flen)==0 && aux[i][flen]==INFIELD_SEP)
379        return aux[i]+flen+1;
380  } else
381  {
382    for(int i=0; i<auxn; ++i)
383    {
384      if(*f==*(aux[i]))
385        return aux[i]+1;
386    }
387  }
388  return NULL;
389}
390
391inline bool Segment::clearfields() {
392  for (int i=0; i<auxn; ++i) {
393    //    free(aux[i]);
394    aux[i] = NULL;
395  }
396  auxn=0;
397  return true;
398}
399
400inline // NIEEFEKTYWNE
401void Segment::print(char* line)
402{
403  sprintf(line,"%04d %02d %s", filepos, len, tag);
404  if(form)
405    {
406      strcat(line," ");
407      strcat(line,form);
408    }
409  else
410    if(auxn)
411      strcat(line," *");
412
413  for(int i=0; i<auxn; ++i)
414    {
415      strcat(line," ");
416      strcat(line,aux[i]);
417    }
418
419  strcat(line,"\n");
420}
421
422
423inline
424bool Segment::addfield(char* s)
425{
426  if(auxn<MAXAUX)
427    {
428      aux[auxn++]=s;
429      return true;
430    }
431  else
432    return false;
433}
434
435/**************************************************
436 * funkcje pomocne w operacjach na plikach        *
437 *  konfiguracyjnych                              *
438 **************************************************/
439
440// sprawdza istnienie pliku
441int file_accessible(const char* path);
442
443// sprawdza istnienie pliku konfiguracyjnego
444int config_file(const char* dir, const char* filename);
445
446/**************************************************/
447
448/* Pobiera wejscie
449 * parametry:
450 * - args - tablica stringow okresnajacych pola wejsciowe
451 * - args_len - rozmiar args
452 * - seg - segment
453 * wartosc - wskaznik do wejscia
454 */
455inline char*  getInput(char** args, int args_len, Segment seg) {
456  char* formp = NULL;
457  for (int i=0; i<args_len; ++i) {
458    if ('4' == args[i][0])
459      return seg.form;
460    if ((formp = seg.getfield(args[i])) != NULL) {
461      return formp;
462    }
463  }
464  return formp;
465}
466
467#endif
Note: See TracBrowser for help on using the repository browser.