source: src/common/common.cc

Last change on this file was e0cd003, checked in by Tomasz Obrebski <to@…>, 11 years ago

wsp�lny parametr -e usuni�ty
wyg�adzone teksty help

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