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