root/trunk/freewrt/tools/nfotizer/main.c

Revision 2100, 2.4 kB (checked in by n0-1, 5 years ago)

added license information and (C)

Line 
1 /* nfotizer - split information given in info.nfo files into several places.
2  * Copyright (C) 2007  Phil Sutter <n0-1@freewrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 #include "nfotizer.h"
19 #include <stdlib.h>             /* exit */
20 #include <string.h>             /* strlen */
21 #include <libgen.h>             /* dirname */
22
23 int nfotizer(char *);
24 int writefile(char *, char *);
25
26 int
27 main(int argc, char **argv)
28 {
29         int i = 1, ret = 0;
30
31         if (argc < 2) {
32                 PINFO("Usage: %s filename [filename ...]", argv[0]);
33                 exit(1);
34         }
35
36         while (i < argc) {
37                 if (nfotizer(argv[i++]))
38                         ret = 1;
39         }
40
41         exit(ret);
42 }
43
44 int
45 nfotizer(char *nfofile)
46 {
47         FILE *fp;
48         struct htab *htable;
49         const char **fdef;
50         char *dname, *fname, *temp, *fcontent;
51         int i;
52
53         if ((fp = fopen(nfofile, "r")) == NULL) {
54                 PERR("file \'%s\' doesnt exist", nfofile);
55                 return 1;
56         }
57
58         htable = htab_create(100);
59         if (parse(fp, htable)) {
60                 PERR("parsing \'%s\' failed", nfofile);
61                 fclose(fp);
62                 return 2;
63         }
64
65         fclose(fp);
66 #ifdef DEBUG
67         PINFO("\nItems in table:");
68         htab_print(*htable);
69         puts("");
70 #endif
71         dname = dirname(nfofile);
72         fname = 0;
73         temp = 0;
74         fcontent = 0;
75         for (i = 0; filedefs[i] != NULL; i++) {
76                 fdef = filedefs[i];
77                 fname = interpret(fdef[0], fdef[1], htable);
78                 fcontent = interpret(fdef[0], fdef[2], htable);
79
80                 asprintf(&temp, "%s/%s", dname, fname);
81                 free(fname);
82                 fname = temp;
83
84                 writefile(fname, fcontent);
85                 free(fname);
86                 free(fcontent);
87         }
88         htab_destroy(htable);
89         return 0;
90 }
91
92 /* overwrite content of file at fname with content given */
93 int
94 writefile(char *fname, char *content)
95 {
96         FILE *fp;
97         int ret = 0;
98         if ((fp = fopen(fname, "w")) == NULL) {
99                 PERR("could not open file \'%s\' for writing", fname);
100                 return 1;
101         }
102         if (fputs(content, fp) == EOF) {
103                 PERR("error writing file %s", fname);
104                 ret = 2;
105         }
106         fclose(fp);
107         return ret;
108 }
Note: See TracBrowser for help on using the browser.