1 /*
2 * rarian-sk-get-cl.cpp
3 * This file is part of Rarian
4 *
5 * Copyright (C) 2006 - Don scorgie
6 *
7 * Rarian is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * Rarian is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Rarian; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301 USA
21 */
22
23 #define I_KNOW_RARIAN_0_8_IS_UNSTABLE
24 #include <rarian.h>
25 #include <rarian-utils.h>
26 #include <tinyxml.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29
30 static char *cat = NULL;
31 static TiXmlElement *docs = NULL;
32 static int id = 0;
33
34 void
print_usage(char * proc_name)35 print_usage (char *proc_name)
36 {
37 printf ("Usage: %s <LOCALE> <CAT TREE NAME>\n", proc_name);
38 exit (0);
39 }
40
41 void
get_attribute(TiXmlElement * elem)42 get_attribute (TiXmlElement *elem)
43 {
44 TiXmlAttribute* pAttrib=elem->FirstAttribute();
45 if (strcmp (pAttrib->Value(), "")) {
46 char *new_cat = NULL;
47 char *iter = NULL;
48 char *st_iter = NULL;
49 char *tmp = NULL;
50
51 cat = strdup (pAttrib->Value());
52 /* Fix category to mimic scrollkeeper */
53 new_cat = (char *) calloc (sizeof(char), strlen (cat));
54
55 st_iter = cat;
56 iter = cat;
57 while (iter && *iter) {
58 while (iter && *iter && *iter != '|') {
59 iter++;
60 }
61 tmp = rrn_strndup (st_iter, (iter-st_iter));
62 if (*new_cat) {
63 strcat (new_cat, tmp);
64 } else {
65 strcpy (new_cat, tmp);
66 }
67 free (tmp);
68 if (*iter == '|')
69 iter++;
70 else
71 break;
72 st_iter = iter;
73 }
74 elem->SetAttribute("categorycode", new_cat);
75
76 }
77 }
78
79 int
get_docs(RrnReg * reg,TiXmlNode * pParent)80 get_docs (RrnReg *reg, TiXmlNode *pParent)
81 {
82 if (reg->omf_location) {
83 TiXmlElement * doc = new TiXmlElement ("doc");
84 TiXmlElement * entry;
85 TiXmlElement * text;
86 char *tmp;
87
88 doc->SetAttribute ("id", id);
89 id++;
90
91 /* doctitle */
92 entry = new TiXmlElement ("doctitle");
93 entry->LinkEndChild (new TiXmlText (reg->name));
94 doc->LinkEndChild (entry);
95
96 /* docomf */
97 entry = new TiXmlElement ("docomf");
98 entry->LinkEndChild (new TiXmlText (reg->omf_location));
99 doc->LinkEndChild (entry);
100
101 /* docsource */
102 tmp = reg->uri + 7;// Remove file://
103 entry = new TiXmlElement ("docsource");
104 entry->LinkEndChild (new TiXmlText (tmp));
105 doc->LinkEndChild (entry);
106
107 /* docformat */
108 entry = new TiXmlElement ("docformat");
109 entry->LinkEndChild (new TiXmlText (reg->type));
110 doc->LinkEndChild (entry);
111
112 /* docseriesid */
113 tmp = reg->identifier + 17;// remove org.scrollkeeper.
114 entry = new TiXmlElement ("docseriesid");
115 entry->LinkEndChild (new TiXmlText (tmp));
116 doc->LinkEndChild (entry);
117 pParent->LinkEndChild (doc);
118
119 }
120
121 return 0;
122 }
123
124 void
process_node(TiXmlNode * pParent)125 process_node (TiXmlNode *pParent)
126 {
127 TiXmlNode* pChild;
128 TiXmlText* pText;
129 int t = pParent->Type();
130 int num;
131
132 switch ( t )
133 {
134 case TiXmlNode::DOCUMENT:
135 break;
136
137 case TiXmlNode::ELEMENT:
138 if (!strcmp (pParent->Value(), "sect")) {
139 get_attribute (pParent->ToElement());
140 rrn_for_each_in_category ((RrnForeachFunc) get_docs, cat, pParent);
141 free (cat);
142
143 }
144 default:
145 break;
146 }
147
148 for ( pChild = pParent->FirstChild(); pChild != 0; pChild = pChild->NextSibling())
149 {
150 process_node ( pChild);
151 }
152 }
153
154
155 /* Find a suitable file to dump out output to.
156 * for now, we're always going to the same file
157 */
158 char *
find_dump_name()159 find_dump_name ()
160 {
161 char *filename = NULL;
162 char *user = getenv ("USERNAME");
163 char *basepath = NULL;
164 int i=0;
165 int last = 0;
166 unsigned int lasttime = 0;
167 int init = 1;
168
169 if (!user) {
170 /* if USERNAME isn't set, we use the "default" username: UNKNOWN */
171 user = strdup ("UNKNOWN");
172 }
173
174 basepath = (char *) malloc (sizeof(char) * (strlen(user) + 18 /*/tmp/scrollkeeper.*/ + 1));
175 sprintf (basepath, "/tmp/scrollkeeper-%s", user);
176 mkdir(basepath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
177
178
179 filename = (char *) malloc (sizeof(char) * (strlen(user) + 18 /*/tmp/scrollkeeper.*/ + 10 /*contents.0*/) + 1);
180
181 for (i=0 ;i<5; i++) {
182 struct stat sbuf;
183 sprintf (filename, "/tmp/scrollkeeper-%s/contents.%d", user, i);
184 if (stat(filename,&sbuf) == -1) {
185 last = i;
186 break;
187 }
188 if (sbuf.st_mtime < lasttime || init) {
189 init = 0;
190 last = i;
191 lasttime = sbuf.st_mtime;
192 }
193 }
194 sprintf (filename, "/tmp/scrollkeeper-%s/contents.%d", user, last);
195
196 return filename;
197 }
198
main(int argc,char * argv[])199 int main (int argc, char * argv[])
200 {
201 int skip = 0;
202
203 if (argc < 3 || argc > 4) {
204 print_usage (argv[0]);
205 }
206 if (!strcmp(argv[1], "-v")) {
207 skip = 1;
208 }
209
210 if (strcmp (argv[2+skip], "scrollkeeper_extended_cl.xml") &&
211 strcmp (argv[2+skip], "scrollkeeper_cl.xml")) {
212 print_usage (argv[0]);
213 }
214 bool loadok;
215
216 rrn_set_language (argv[1+skip]);
217
218 TiXmlDocument doc (DATADIR"/librarian/rarian-sk-cl.xml");
219 loadok = doc.LoadFile(TIXML_ENCODING_UTF8);
220
221 if (!loadok) {
222 fprintf (stderr, "ERROR: Cannot parse template file. Aborting.\n");
223 exit (2);
224 }
225
226 TiXmlNode *pParent = doc.FirstChildElement();
227
228 process_node (pParent);
229
230 char *filename = find_dump_name ();
231 doc.SaveFile(filename);
232 printf ("%s\n", filename);
233
234 exit (0);
235
236 }
237