source: app/src/dgp/common.h @ 14afb6e

help
Last change on this file since 14afb6e was 0214596, checked in by pawelk <pawelk@…>, 16 years ago

Dodalismy do pakietu utt komponent dgp (brak configow i innych bajerow).

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

  • Property mode set to 100644
File size: 5.1 KB
Line 
1#ifndef __COMMON_H
2#define __COMMON_H
3
4#include <stdio.h>
5#include <ctype.h>
6
7#include "cmdline.h"
8#include "const.hh"
9
10
11/**************************************************
12 * Stale dotyczace wejscia/wyjscia
13 */
14
15#define EMPTYFORM '*'
16#define INFIELD_SEP ':'
17#define MAXAUX 64
18#define FIELD_SEP " \t\n"
19
20/**************************************************/
21
22
23extern FILE* inputf;
24extern FILE* outputf;
25extern FILE* failedf;
26
27extern char* input_filename;
28extern char* output_filename;
29extern char* failed_filename;
30
31extern bool copy_processed;
32extern bool append_output;
33extern bool append_failed;
34
35extern void process_common_options(gengetopt_args_info args);
36
37
38
39/**************************************************/
40/*
41parameters:
42  -seg  - segment
43  -name - field name
44  +val  - field contents
45return value:
46  1 if specified field exists, 0 otherwise
47*/
48
49inline int getfield(const char* seg, const char* pref, char* val)
50{
51  const char* p=seg;
52
53  while(*p==' ') ++p;
54 
55 pos:
56  if(isdigit(*p) or *p=='*')
57    if(*pref=='1') return sscanf(p,"%s",val); else while(*p!=' ') ++p; 
58  else
59    if(*pref=='1') return 0; else goto type;
60
61  while(*p==' ') ++p;
62 
63 len:
64  if(isdigit(*p) or *p=='*')
65    if(*pref=='2') return sscanf(p,"%s",val); else while(*p!=' ') ++p; 
66  else
67    if(*pref=='2') return 0; else goto type;
68
69  while(*p==' ') ++p;
70 
71 type:
72
73  if(*pref=='3') return sscanf(p,"%s",val); else while(*p!=' ') ++p;
74
75  while(*p==' ') ++p;
76
77 form:
78  if(*pref=='4') return sscanf(p,"%s",val); else while(*p!=' ') ++p;
79
80  while(*p==' ') ++p;
81
82 annotation:
83  do p=strstr(p,pref); while(p!=NULL && *(p-1)!=' ' && *(p-1)!='\t');
84 
85  if(p==NULL) return 0;
86  else
87  {
88    p+=strlen(pref);
89    int len=strcspn(p,FIELD_SEP "\n\r\f\0");
90    strncpy(val,p,len);
91    val[len]='\0';
92    return 1;
93  }
94}
95
96
97/*
98parameters:
99  +seg - segment
100  -pref - prefix of the new field
101  -val  - contents of the new field
102return value:
103  1 - success, 0 - fail (limit on segment length exceeded)
104*/
105inline int addfield(char *seg, const char *pref, const char *val)
106     // zalozenie, ze seg konczy sie znakiem \n
107{
108  if(strlen(seg)+strlen(pref)+strlen(val) >= MAXLINE) return 0; // bezpieczniej, ale wolniej
109
110  int seglen=strlen(seg);
111  sprintf(seg+(seglen-1)," %s%s\n",pref,val);
112  return 1;
113}
114
115
116inline
117bool processseg(const char* s, gengetopt_args_info& args)
118{
119  bool ret = !args.process_given;
120  char field[MAXAUX];
121   
122  if(args.process_given)
123  {
124    getfield(s,"3",field);
125    for(int i=0; i<args.process_given; ++i)
126      if(strcmp(args.process_arg[i],field)==0)
127      {
128        ret=true;
129        break;
130      }
131  }
132
133  for(int i=0; i<args.select_given; ++i)
134    if(! getfield(s,args.select_arg[i],field))
135      ret=false;
136 
137  for(int i=0; i<args.ignore_given; ++i)
138    if(getfield(s,args.ignore_arg[i],field))
139      ret=false;
140
141  return ret;
142}
143
144
145/* DEPRICATED */
146
147
148/* definicja struktury wejscia/wyjscia
149 */
150struct Segment
151{
152  int filepos, len;
153  char* tag;
154  char* form;
155  char* aux[MAXAUX];
156  int auxn;
157
158  bool parse(char* line);
159  char* getfield(char* fieldname);
160  void print(char* line);
161  bool addfield(char* s);
162  bool clearfields();
163};
164
165/*
166 * Sprawdza czy nalezy przetwarzac dany segment.
167 */
168
169inline
170bool process_seg(Segment& s, gengetopt_args_info& args)
171{
172  bool ret = !args.process_given;
173
174  for(int i=0; i<args.process_given; ++i)
175    if(strcmp(args.process_arg[i],s.tag)==0)
176      {
177        ret=true;
178        break;
179      }
180
181  for(int i=0; i<args.select_given; ++i)
182    if(! s.getfield(args.select_arg[i]))
183      ret=false;
184
185  for(int i=0; i<args.ignore_given; ++i)
186    if(s.getfield(args.ignore_arg[i]))
187      ret=false;
188
189  return ret;
190}
191
192
193/*
194 * FUNKCJE OBSLUGUJACE WEJSCIE/WYJSCIE
195 */
196// napisy zostaj na miejscu (w line), tylko wskazniki sa ustawian
197// i zara dopisywane zera s dopisywane
198
199inline
200bool Segment::parse(char* line)
201{
202  auxn=0;
203  char* field;
204  if((field=strtok(line,FIELD_SEP))!=NULL)
205    filepos=atoi(field); // nie sprawdzana poprawnosc
206  else
207    return false;
208  if((field=strtok(NULL,FIELD_SEP))!=NULL)
209    len=atoi(field); // nie sprawdzana poprawnosc
210  else return false;
211  if((tag=strtok(NULL,FIELD_SEP))==NULL) return false;
212  if((form=strtok(NULL,FIELD_SEP))==NULL)
213    return false;
214/*   else */
215/*     if(form[0] == EMPTYFORM && form[1] =='\0') */
216/*       form=NULL; */
217
218  while((aux[auxn]=strtok(NULL,FIELD_SEP))!=NULL) ++auxn;
219
220  return true;
221}
222
223
224inline char* Segment::getfield(char* f)
225{
226  int flen=strlen(f);
227  for(int i=0; i<auxn; ++i)
228    if(strncmp(aux[i],f,flen)==0 && aux[i][flen]==INFIELD_SEP)
229      return aux[i]+flen+1;
230  return NULL;
231}
232
233inline bool Segment::clearfields() {
234  for (int i=0; i<auxn; ++i) {
235    //    free(aux[i]);
236    aux[i] = NULL;
237  }
238  auxn=0;
239  return true;
240}
241
242inline // NIEEFEKTYWNE
243void Segment::print(char* line)
244{
245  sprintf(line,"%04d %02d %s", filepos, len, tag);
246  if(form)
247    {
248      strcat(line," ");
249      strcat(line,form);
250    }
251  else
252    if(auxn)
253      strcat(line," *");
254
255  for(int i=0; i<auxn; ++i)
256    {
257      strcat(line," ");
258      strcat(line,aux[i]);
259    }
260
261  strcat(line,"\n");
262}
263
264
265inline
266bool Segment::addfield(char* s)
267{
268  if(auxn<MAXAUX)
269    {
270      aux[auxn++]=s;
271      return true;
272    }
273  else
274    return false;
275}
276
277#endif
Note: See TracBrowser for help on using the repository browser.