source: app/src/common/common.h @ 7562131

help
Last change on this file since 7562131 was 13a8a67, checked in by pawelk <pawelk@…>, 16 years ago

Przejrzalem narzedzia pod katem includow CMDLINE_FILE.
Gdzie mozna bylo, usunalem.
Jest pewien balagan w Makefile'ach, bo nadmiarowo dodalem -D ....
Calosc sie kompiluje i generuje sie tarball.

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

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