source: app/src/common/common.cc @ 0e3df7e

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

process_seg() naprawione, 'lem -s kor -I kor' nie zawiesza sie

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

  • Property mode set to 100644
File size: 5.9 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[FIELD_PREFIX_MAXLEN];
18char input_field_prefix[FIELD_PREFIX_MAXLEN];
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
37void set_program_name(char program_name[], char* argv0)
38{
39  if (char* p_name = strrchr(argv0, '/'))
40    strcpy(program_name,p_name+1);
41  else
42    strcpy(program_name,argv0);
43}
44
45
46
47extern void process_config_files(gengetopt_args_info* args, char* argv0)
48{
49
50  char program_name[256];
51  char config_file[256];
52  char config_file_tmp[256];
53
54  set_program_name(program_name,argv0);
55
56  // obsługa pliku konfiguracyjnego podanego w linii komend
57  if (args->config_given) {
58    if (file_accessible(args->config_arg) == 0) {
59      if (cmdline_parser_configfile(args->config_arg,
60                                    args,
61                                    0, // 0 - nie nadpisuj wartości parametrów
62                                    0, // 0 - nie inicjuj
63                                    0) != 0) {
64        fprintf(stderr, "Error in config file (%s)\n", args->config_arg);
65        exit(1);
66      }
67    }
68  }
69
70  if(args->one_line_given && !one_line) one_line=true, one_field=false;
71  if(args->one_field_given && !one_field) one_line=false, one_field=true;
72
73  // obsluga pliku konfiguracyjnego uzytkownika dla programu
74  sprintf(config_file_tmp, "%s/%s.conf", USER_CONFIG_DIR, program_name);
75  expand_path(config_file_tmp, config_file);
76  if (file_accessible(config_file) == 0) {
77    if (cmdline_parser_configfile(config_file,
78                                  args,
79                                  0, // 0 - nie nadpisuj danych
80                                  0, // 0 - nie inicjuj struktury
81                                  0) != 0) {
82      fprintf(stderr, "Error in config file (%s)\n", config_file);
83      exit(1);
84    }
85  }
86
87  if(args->one_line_given && !one_line) one_line=true, one_field=false;
88  if(args->one_field_given && !one_field) one_line=false, one_field=true;
89
90
91  // obsluga pliku konfiguracyjnego uzytkownika globalnego
92  sprintf(config_file_tmp, "%s/utt.conf", USER_CONFIG_DIR);
93  expand_path(config_file_tmp, config_file);
94  if (file_accessible(config_file) == 0) {
95    if (cmdline_parser_configfile(config_file,
96                                  args,
97                                  0, // 0 - nie nadpisuj danych
98                                  0, // 0 - nie inicjuj struktury
99                                  0) != 0) {
100      fprintf(stderr, "Error in config file (%s)\n", config_file);
101      exit(1);
102    }
103  }
104
105  if(args->one_line_given && !one_line) one_line=true, one_field=false;
106  if(args->one_field_given && !one_field) one_line=false, one_field=true;
107
108
109
110  // obsluga systemowego pliku konfiguracyjnego dla programu
111  sprintf(config_file, "%s/%s.conf", SYSTEM_CONFIG_DIR, program_name);
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.