source: app/src/common/common.cc @ 25ae32e

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

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

  • Property mode set to 100644
File size: 6.5 KB
Line 
1#include <stdlib.h>
2#include <string.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <unistd.h>
6#include "common.h"
7
8#include <stdio.h>
9#include <locale.h>
10
11FILE* inputf=stdin;
12FILE* outputf=stdout;
13FILE* failedf=stdout;
14bool copy_processed=0;
15bool one_field=false;
16bool one_line=false;
17char output_field_prefix[32];
18char input_field_prefix[32];
19
20extern int argc;
21extern char **argv;
22
23
24// tilde (home dir) expansion in path
25int expand_path(char* inpath, char* outpath)
26{
27  if(inpath[0]=='~')
28    sprintf(outpath,"%s%s",getenv("HOME"),inpath+1);
29  else
30    strcpy(outpath,inpath);
31  return 0; // no problem
32}
33
34
35
36
37/*
38parameters:
39        -name - field name, long or short
40        +prefix - field name with ':' appended if long name
41return value:
42        1 if correct field name, 0 otherwise
43examples:
44name    prefix  r.v.
45lem     lem:    1
46@       @       1
47::      'undef' 0
48a,b     'undef' 0
49*/
50int fieldprefix(char *name, char *prefix)
51{
52  if (ispunct(name[0]) && name[1]=='\0') // correct short name
53  {
54    strcpy(prefix, name); return 1;
55  }
56
57  int i=0;
58  while(name[i]!='\0' && isalnum(name[i])) ++i;
59 
60  if(name[i]=='\0' && i>0) // correct long name
61  {
62    sprintf(prefix,"%s:",name); return 1;
63  }
64
65  // incorrect
66  return 0;
67}
68
69
70
71void set_program_name(char program_name[], char* argv0)
72{
73  if (char* p_name = strrchr(argv0, '/'))
74    strcpy(program_name,p_name+1);
75  else
76    strcpy(program_name,argv0);
77}
78
79
80
81extern void process_config_files(gengetopt_args_info* args, char* argv0)
82{
83
84  char program_name[256];
85  char config_file[256];
86  char config_file_tmp[256];
87
88  set_program_name(program_name,argv0);
89
90  // obsługa pliku konfiguracyjnego podanego w linii komend
91  if (args->config_given) {
92    if (file_accessible(args->config_arg) == 0) {
93      if (cmdline_parser_configfile(args->config_arg,
94                                    args,
95                                    0, // 0 - nie nadpisuj wartości parametrów
96                                    0, // 0 - nie inicjuj
97                                    0) != 0) {
98        fprintf(stderr, "Error in config file (%s)\n", args->config_arg);
99        exit(1);
100      }
101    }
102  }
103
104  if(args->one_line_given && !one_line) one_line=true, one_field=false;
105  if(args->one_field_given && !one_field) one_line=false, one_field=true;
106
107  // obsluga pliku konfiguracyjnego uzytkownika dla programu
108  sprintf(config_file_tmp, "%s/%s.conf", USER_CONFIG_DIR, program_name);
109  expand_path(config_file_tmp, config_file);
110  if (file_accessible(config_file) == 0) {
111    if (cmdline_parser_configfile(config_file,
112                                  args,
113                                  0, // 0 - nie nadpisuj danych
114                                  0, // 0 - nie inicjuj struktury
115                                  0) != 0) {
116      fprintf(stderr, "Error in config file (%s)\n", config_file);
117      exit(1);
118    }
119  }
120
121  if(args->one_line_given && !one_line) one_line=true, one_field=false;
122  if(args->one_field_given && !one_field) one_line=false, one_field=true;
123
124
125  // obsluga pliku konfiguracyjnego uzytkownika globalnego
126  sprintf(config_file_tmp, "%s/utt.conf", USER_CONFIG_DIR);
127  expand_path(config_file_tmp, config_file);
128  if (file_accessible(config_file) == 0) {
129    if (cmdline_parser_configfile(config_file,
130                                  args,
131                                  0, // 0 - nie nadpisuj danych
132                                  0, // 0 - nie inicjuj struktury
133                                  0) != 0) {
134      fprintf(stderr, "Error in config file (%s)\n", config_file);
135      exit(1);
136    }
137  }
138
139  if(args->one_line_given && !one_line) one_line=true, one_field=false;
140  if(args->one_field_given && !one_field) one_line=false, one_field=true;
141
142
143
144  // obsluga systemowego pliku konfiguracyjnego dla programu
145  sprintf(config_file, "%s/%s.conf", SYSTEM_CONFIG_DIR, program_name);
146  if (file_accessible(config_file) == 0) {
147    if (cmdline_parser_configfile(config_file,
148                                  args,
149                                  0, // 0 - nie zmieniaj danych wczesniejszych
150                                  0, // 0 - nie inicjuj struktury
151                                  0  // 0 - nie sprawdzaj wymaganych parametrow
152                                  ) != 0) {
153      fprintf(stderr, "Error in config file (%s)\n", config_file);
154      exit(1);
155    }
156  }
157
158  if(args->one_line_given && !one_line) one_line=true, one_field=false;
159  if(args->one_field_given && !one_field) one_line=false, one_field=true;
160
161
162  // obsluga systemowego pliku konfiguracyjnego globalnego
163  sprintf(config_file, "%s/utt.conf", SYSTEM_CONFIG_DIR);
164  if (file_accessible(config_file) == 0) {
165    if (cmdline_parser_configfile(config_file,
166                                  args,
167                                  0, // 0 - nie zmieniaj danych wczesniejszych
168                                  0, // 0 - nie inicjuj struktury
169                                  0  // 0 - nie sprawdzaj wymaganych parametrow
170                                  ) != 0) {
171      fprintf(stderr, "Error in config file (%s)\n", config_file);
172      exit(1);
173    }
174  }
175
176  if(args->one_line_given && !one_line) one_line=true, one_field=false;
177  if(args->one_field_given && !one_field) one_line=false, one_field=true;
178
179}
180
181
182void process_common_options(gengetopt_args_info* args, char* argv0)
183{
184  char program_name[256];
185
186  set_program_name(program_name,argv0);
187
188  setlocale(LC_CTYPE,"");
189  setlocale(LC_COLLATE, "");
190
191  if(args->help_given)
192      cmdline_parser_print_help ();
193
194  if(args->input_given)
195    if(!(inputf=fopen(args->input_arg,"r")))
196    {
197      fprintf(stderr,"No such file: %s.\n", args->input_arg);
198      exit(1);
199    }
200 
201  if(args->output_given)
202    if(!(outputf=fopen(args->output_arg,"w")))
203    {
204      fprintf(stderr,"Cannot open output file: %s.\n", args->output_arg);
205      exit(1);
206    }
207 
208  if(args->fail_given)
209      if(!(failedf=fopen(args->fail_arg,"w")))
210      {
211        fprintf(stderr,"Cannot open output file: %s.\n", args->fail_arg);
212        exit(1);
213      }
214
215  if(args->input_field_given)
216    fieldprefix(args->input_field_arg[0],input_field_prefix);
217  else
218    strcpy(input_field_prefix, "4");
219
220  if(args->output_field_given)
221    fieldprefix(args->output_field_arg,output_field_prefix);
222  else
223    sprintf(output_field_prefix, "%s%c", program_name, INFIELD_SEP);
224
225  if ((args->copy_given))
226    copy_processed=true;
227}
228
229// sprawdza istnienie pliku
230int file_accessible(const char* path) {
231    return access(path, R_OK);
232}
233
234// sprawdza istnienie pliku konfiguracyjnego
235int config_file_exists(const char* dir, const char* filename) {
236    struct stat dir_stat;
237    struct stat file_stat;
238
239    char* path = (char*)malloc(strlen(dir) + strlen(filename) + 2); // + '\0' + '/'
240
241    sprintf(path, "%s/%s", dir, filename);
242
243    if (stat(dir, &dir_stat) != 0)
244        return -1;
245
246    if (stat(path, &file_stat) != 0)
247        return -1;
248
249    if (!S_ISDIR(dir_stat.st_mode))
250        return -1; // katalog nie jest katalogiem
251
252    if (!S_ISREG(file_stat.st_mode))
253        return -1; // plik konfiguracyjny nie jest plikiem
254
255    if (access(dir, X_OK) != 0)
256        return -1; // nie mamy prawa zmienic katalogu
257
258    if (access(path, R_OK) != 0)
259        return -1; // nie mamy prawa odczytu pliku
260
261    free(path);
262
263    return 0;
264}
Note: See TracBrowser for help on using the repository browser.