root/libswish3/trunk/src/swish_header.c

Revision 2129, 3.4 kB (checked in by karpet, 3 months ago)

use our string_to_int instead of strtol() directly

Line 
1 /*
2  * This file is part of libswish3
3  * Copyright (C) 2008 Peter Karman
4  *
5  *  libswish3 is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  libswish3 is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with libswish3; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 /* check a swish3 header file for correct syntax */
21
22 #include <ctype.h>
23 #include <sys/param.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <locale.h>
29 #include <err.h>
30 #include <getopt.h>
31
32 #include "libswish3.h"
33
34 extern int SWISH_DEBUG;
35
36 static struct option longopts[] =
37 {
38     {"debug",           required_argument,  0, 'd'},
39     {"help",            no_argument,        0, 'h'},
40     {0, 0, 0, 0}
41 };
42
43 int 
44 usage()
45 {
46
47     char * descr = "swish_header reads and writes swish3 header/config files\n";
48     printf("swish_header [opts] file\n");
49     printf("opts:\n --debug [lvl]\n --help\n");
50     printf("\n%s\n", descr);
51     exit(0);
52 }
53
54
55 int 
56 main(int argc, char **argv)
57 {
58 #ifdef LIBXML_READER_ENABLED
59     int i, ch;
60     int             option_index;
61     extern char    *optarg;
62     extern int      optind;
63     swish_Config   *config;
64    
65     option_index = 0;
66    
67     swish_init();   
68
69     while ((ch = getopt_long(argc, argv, "d:h", longopts, &option_index)) != -1)
70     {
71         switch (ch)
72         {
73         case 0:    /* If this option set a flag, do nothing else now. */
74             if (longopts[option_index].flag != 0)
75                 break;
76             printf("option %s", longopts[option_index].name);
77             if (optarg)
78                 printf(" with arg %s", optarg);
79             printf("\n");
80             break;           
81
82         case 'd':
83             printf("turning on debug mode: %s\n", optarg);
84
85             if (!isdigit(optarg[0]))
86                 err(1, "-d option requires a positive integer as argument\n");
87
88             SWISH_DEBUG = swish_string_to_int(optarg);
89             break;
90
91         case '?':
92         case 'h':
93         default:
94             usage();
95
96         }
97
98     }
99    
100     i = optind;
101
102     for (; i < argc; i++) {
103         printf("config file %s\n", argv[i]);
104         config = swish_init_config();
105         if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) {
106             SWISH_DEBUG_MSG("init_config");
107         }
108         swish_config_set_default(config);
109         if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) {
110             SWISH_DEBUG_MSG("set default");
111         }
112         if (!swish_merge_config_with_header( (char*)argv[i], config )) {
113             SWISH_CROAK("failed to merge header %s with defaulf config", argv[i]);
114         }               
115         swish_write_header("swish_header.xml", config);
116         if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) {
117             SWISH_DEBUG_MSG("header written");
118         }
119         swish_free_config(config);
120     }
121        
122    
123     /*
124      * this is to debug memory for regression tests
125      */
126     xmlMemoryDump();
127     return 0;
128 #else
129
130     SWISH_CROAK("Your version of libxml2 is too old");
131     return 1;
132 #endif
133
134 }
Note: See TracBrowser for help on using the browser.