English Language flag
// Log In
// CVSweb
Project: FreeWRT
// Summary // Activity // Search // Tracker // Lists // News // SCM // Wiki

SCM Repository

ViewVC logotype

Contents of /branches/common-nfo/tools/nfotiser/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3443 - (show annotations) (download)
Wed Aug 8 12:45:20 2007 UTC (6 years, 6 months ago) by tg
File MIME type: text/plain
File size: 3894 byte(s)
• use libdl (required on GNU/Lunox)
• fix categories decl (oops…)
• insert categories into DB

1 /* $FreeWRT: src/share/misc/licence.template,v 1.20 2006/12/11 21:04:56 tg Rel $ */
2
3 /*-
4 * Copyright (c) 2007
5 * Thorsten Glaser <tg@mirbsd.de>
6 *
7 * Provided that these terms and disclaimer and all copyright notices
8 * are retained or reproduced in an accompanying document, permission
9 * is granted to deal in this work without restriction, including un-
10 * limited rights to use, publicly perform, distribute, sell, modify,
11 * merge, give away, or sublicence.
12 *
13 * Advertising materials mentioning features or use of this work must
14 * display the following acknowledgement:
15 * This product includes material provided by Thorsten Glaser.
16 * This acknowledgement does not need to be reprinted if this work is
17 * linked into a bigger work whose licence does not allow such clause
18 * and the author of this work is given due credit in the bigger work
19 * or its accompanying documents, where such information is generally
20 * kept, provided that said credits are retained.
21 *
22 * This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
23 * the utmost extent permitted by applicable law, neither express nor
24 * implied; without malicious intent or gross negligence. In no event
25 * may a licensor, author or contributor be held liable for indirect,
26 * direct, other damage, loss, or other issues arising in any way out
27 * of dealing in the work, even if advised of the possibility of such
28 * damage or existence of a defect, except proven that it results out
29 * of said person's immediate fault when using the work as intended.
30 */
31
32 #include <sys/param.h>
33
34 #include <err.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <locale.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 #define EXTERN
45 #include "extern.h"
46
47 extern const char *__progname;
48
49 static void usage(void) __attribute__((noreturn));
50
51 int
52 main(int argc, char *argv[])
53 {
54 int ch;
55 const char *ofile;
56
57 /* set a UTF-8 locale (which is required for SQLite3) */
58 ofile = "en_US.UTF-8";
59 errno = 0;
60 if (strcmp(ofile, setlocale(LC_CTYPE, "en_US.UTF-8")))
61 warn("cannot set the UTF-8 locale");
62
63 /* make stdin unbuffered */
64 if (setvbuf(stdin, NULL, _IONBF, 0))
65 warn("cannot set stdin to unbuffered I/O");
66
67 ofile = "nfo_.db"; /* default output file */
68 /* parse arguments */
69 while ((ch = getopt(argc, argv, "do:")) != -1)
70 switch (ch) {
71 case 'd':
72 ++debug;
73 break;
74 case 'o':
75 ofile = optarg;
76 break;
77 case '?':
78 default:
79 usage();
80 break;
81 }
82 argc -= optind;
83 argv += optind;
84
85 D(1, "debugging: on, level %d\n", debug);
86 D(1, "writing to output database %s from %d files\n", ofile, argc);
87 if (debug >= 2) {
88 int i;
89 for (i = 0; i < argc; ++i)
90 fprintf(stderr, "- %s\n", argv[i]);
91 }
92
93 /* need at least one .nfo file to parse */
94 // if (!argc)
95 // usage();
96
97 /* if the output file exists, remove it */
98 if (unlink(ofile) && (errno != ENOENT))
99 err(1, "cannot unlink output file \"%s\"", ofile);
100
101 /* create the output database */
102 D(2, "creating database\n");
103 if (sqlite3_open(ofile, &db) != SQLITE_OK)
104 dberr(1, "cannot create output file \"%s\"", ofile);
105 sqlite3_extended_result_codes(db, 1);
106
107 if (dbexecv(NULL, NULL, "%s", dbscheme))
108 dberr(1, "error processing the database scheme");
109 D(1, "database created\n");
110
111 for (const struct categories *ctp = categories; ctp->catname != NULL;
112 ++ctp) {
113 xdbexecv(NULL, NULL, NULL,
114 "INSERT INTO categories (name, desc) VALUES ('%s', '%s');",
115 ctp->catname, ctp->catdesc);
116 }
117
118 /* … */
119
120 D(1, "finishing...\n");
121 if (sqlite3_close(db) != SQLITE_OK)
122 dberr(255, "cannot close database");
123 return (0);
124 }
125
126 static void
127 usage(void)
128 {
129 fprintf(stderr,
130 "Usage: %s [-d] [-o ofile] package/FOO/info.nfo ...\n",
131 __progname);
132 if (db) {
133 fprintf(stderr,
134 "Warning, usage() called too late.\n"
135 "Please report this to the author!\n");
136 sqlite3_close(db);
137 }
138 exit(1);
139 }

root@freewrt.org:443
ViewVC Help
Powered by ViewVC 1.1.20