source: src/common/common.cc @ ac25afd

Last change on this file since ac25afd was cfdf333, checked in by Tomasz Obrebski <to@…>, 12 years ago

Naprawy dotyczace oblugi plikow konfiguracyjnych.

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