2 #include <stdio.h> /* for FILE */
3 #include <stdlib.h> /* for malloc */
4 #include <string.h> /* for string ops */
6 #include <sys/types.h> /* for stat */
7 #include <sys/stat.h> /* for stat */
8 #include <unistd.h> /* for stat */
11 #include "pnd_pathiter.h"
12 #include "pnd_tinyxml.h"
13 #include "pnd_logger.h"
15 pnd_pxml_handle *pnd_pxml_fetch ( char *fullpath ) {
20 f = fopen ( fullpath, "r" );
26 fseek ( f, 0, SEEK_END );
30 fseek ( f, 0, SEEK_SET );
32 b = (char*) malloc ( len );
39 fread ( b, 1, len, f );
43 return ( pnd_pxml_fetch_buffer ( fullpath, b ) );
46 pnd_pxml_handle *pnd_pxml_fetch_buffer ( char *filename, char *buffer ) {
48 pnd_pxml_t **p = malloc ( sizeof(pnd_pxml_t*) * PXML_MAXAPPS );
49 memset ( p, '\0', sizeof(pnd_pxml_t*) * PXML_MAXAPPS );
51 if ( ! pnd_pxml_parse ( filename, buffer, strlen ( buffer ), p ) ) {
55 return ( (pnd_pxml_handle*) p );
58 void pnd_pxml_delete ( pnd_pxml_handle h ) {
59 pnd_pxml_t *p = (pnd_pxml_t*) h;
63 for (i = 0; i < p->titles_c; i++)
65 free(p->titles[i].language);
66 free(p->titles[i].string);
71 if (p->descriptions) {
72 for (i = 0; i < p->descriptions_c; i++)
74 free(p->descriptions[i].language);
75 free(p->descriptions[i].string);
77 free(p->descriptions);
80 if ( p -> standalone ) {
81 free ( p -> standalone );
86 if ( p -> previewpic1 ) {
87 free ( p -> previewpic1 );
89 if ( p -> previewpic2 ) {
90 free ( p -> previewpic2 );
92 if ( p -> author_name ) {
93 free ( p -> author_name );
95 if ( p -> author_website ) {
96 free ( p -> author_website );
98 if ( p -> version_major ) {
99 free ( p -> version_major );
101 if ( p -> version_minor ) {
102 free ( p -> version_minor );
104 if ( p -> version_release ) {
105 free ( p -> version_release );
107 if ( p -> version_build ) {
108 free ( p -> version_build );
113 if ( p -> main_category ) {
114 free ( p -> main_category );
116 if ( p -> subcategory1 ) {
117 free ( p -> subcategory1 );
119 if ( p -> subcategory2 ) {
120 free ( p -> subcategory2 );
122 if ( p -> altcategory ) {
123 free ( p -> altcategory );
125 if ( p -> altsubcategory1 ) {
126 free ( p -> altsubcategory1 );
128 if ( p -> altsubcategory2 ) {
129 free ( p -> altsubcategory2 );
131 if ( p -> osversion_major ) {
132 free ( p -> osversion_major );
134 if ( p -> osversion_minor ) {
135 free ( p -> osversion_minor );
137 if ( p -> osversion_release ) {
138 free ( p -> osversion_release );
140 if ( p -> osversion_build ) {
141 free ( p -> osversion_build );
143 if ( p -> associationitem1_name ) {
144 free ( p -> associationitem1_name );
146 if ( p -> associationitem1_filetype ) {
147 free ( p -> associationitem1_filetype );
149 if ( p -> associationitem1_parameter ) {
150 free ( p -> associationitem1_parameter );
152 if ( p -> associationitem2_name ) {
153 free ( p -> associationitem2_name );
155 if ( p -> associationitem2_filetype ) {
156 free ( p -> associationitem2_filetype );
158 if ( p -> associationitem2_parameter ) {
159 free ( p -> associationitem2_parameter );
161 if ( p -> associationitem3_name ) {
162 free ( p -> associationitem3_name );
164 if ( p -> associationitem1_filetype ) {
165 free ( p -> associationitem3_filetype );
167 if ( p -> associationitem1_parameter ) {
168 free ( p -> associationitem3_parameter );
170 if ( p -> clockspeed ) {
171 free ( p -> clockspeed );
173 if ( p -> background ) {
174 free ( p -> background );
176 if ( p -> startdir ) {
177 free ( p -> startdir );
180 free(p); /*very important!*/
185 void pnd_pxml_set_app_name ( pnd_pxml_handle h, char *v ) {
187 * Please do not use this function if it can be avoided; it is only here for compatibility.
188 * The function might fail on low memory, and there's no way for the user to know when this happens.
190 pnd_pxml_t *p = (pnd_pxml_t*) h;
191 char has_en_field = 0;
194 if (!v) return; /*TODO: add error information? Make it possible to set the string to NULL?*/
196 for (i = 0; i < p->titles_c; i++)
198 if (strncmp("en", p->titles[i].language, 2) == 0) /*strict comparison; match "en_US", "en_GB" etc... All these are set.*/
200 free(p->titles[i].string);
201 p->titles[i].string = strdup(v);
209 if (p->titles_c > p->titles_alloc_c) //we don't have enough strings allocated
211 p->titles_alloc_c <<= 1;
212 p->titles = (pnd_localized_string_t *)realloc((void*)p->titles, p->titles_alloc_c);
213 if (!p->titles) return; //errno = ENOMEM
215 p->titles[p->titles_c - 1].language = "en_US";
216 p->titles[p->titles_c - 1].string = strdup(v);
222 unsigned char pnd_is_pxml_valid_app ( pnd_pxml_handle h ) {
223 //pnd_pxml_t *p = (pnd_pxml_t*) h; //unused atm
225 // for now, lets just verify the exec-path is valid
227 //printf ( "exec is '%s'\n", p -> exec );
231 // even this is complicated by pnd_run.sh semantics .. can't check if it exists
232 // during discovery, since it is not mounted yet..
235 if ( stat ( p -> exec, &buf ) == 0 ) {
236 return ( 1 ); // path is present
243 signed char pnd_pxml_merge_override ( pnd_pxml_handle h, char *searchpath ) {
244 // the pxml includes a unique-id; use this value to attempt to find an
245 // override in the given searchpath
246 signed char retval = 0;
248 #if 0 // TODO: Unfinished entirely now
249 pnd_pxml_handle mergeh;
251 if ( ! pnd_pxml_get_unique_id ( h ) ) {
252 return ( -1 ); // no unique-id present, so can't use it to name potential override files
259 strncat ( buffer, "/", FILENAME_MAX );
260 strncat ( buffer, pnd_pxml_get_unique_id ( h ), FILENAME_MAX );
261 strncat ( buffer, ".xml", FILENAME_MAX );
262 //printf ( " Path to seek merges: '%s'\n", buffer );
264 // TODO: handle multiple subapps!
265 mergeh = pnd_pxml_fetch ( buffer );
269 // TODO: handle all the various data bits
270 if ( pnd_pxml_get_app_name_en ( mergeh ) ) {
271 pnd_pxml_set_app_name ( h, pnd_pxml_get_app_name_en ( mergeh ) );
274 pnd_pxml_delete ( mergeh );
284 char *pnd_pxml_get_best_localized_string(pnd_localized_string_t strings[], int strings_c, char *iso_lang)
287 int similarity_weight = 0xffff; /*Set to something Really Bad in the beginning*/
288 char *best_match = NULL;
290 for(i = 0; i < strings_c; i++)
292 // factor in the length -- if we're given 'en' and have a string 'en_US', thats better than 'de_something'; if we don't
293 // use length, then en_US and de_FO are same to 'en'.
294 int maxcount = strlen ( strings[i].language ) < strlen ( iso_lang ) ? strlen ( strings[i].language ) : strlen ( iso_lang );
295 int new_weight = abs(strncmp(strings[i].language, iso_lang, maxcount));
296 //pnd_log ( PND_LOG_DEFAULT, "looking for lang %s, looking at lang %s (weight %d, old %d): %s\n",
297 // iso_lang, strings [ i ].language, new_weight, similarity_weight, strings [ i ].string );
298 if (new_weight < similarity_weight)
300 similarity_weight = new_weight;
301 best_match = strings[i].string;
306 //pnd_log ( PND_LOG_DEFAULT, "best match: %s\n", best_match );
307 return strdup(best_match);
310 //pnd_log ( PND_LOG_DEFAULT, "best match: FAIL\n" );
315 char *pnd_pxml_get_app_name ( pnd_pxml_handle h, char *iso_lang ) {
316 pnd_pxml_t *p = (pnd_pxml_t *) h;
317 return pnd_pxml_get_best_localized_string(p->titles, p->titles_c, iso_lang);
320 char *pnd_pxml_get_app_name_en ( pnd_pxml_handle h ) {
321 return pnd_pxml_get_app_name(h, "en");
324 char *pnd_pxml_get_app_name_de ( pnd_pxml_handle h ) {
325 return pnd_pxml_get_app_name(h, "de");
328 char *pnd_pxml_get_app_name_it ( pnd_pxml_handle h ) {
329 return pnd_pxml_get_app_name(h, "it");
332 char *pnd_pxml_get_app_name_fr ( pnd_pxml_handle h ) {
333 return pnd_pxml_get_app_name(h, "fr");
336 char *pnd_pxml_get_unique_id ( pnd_pxml_handle h ) {
337 pnd_pxml_t *p = (pnd_pxml_t*) h;
338 return ( p -> unique_id );
341 char *pnd_pxml_get_standalone ( pnd_pxml_handle h ) {
342 pnd_pxml_t *p = (pnd_pxml_t*) h;
343 return ( p -> standalone );
346 char *pnd_pxml_get_icon ( pnd_pxml_handle h ) {
347 pnd_pxml_t *p = (pnd_pxml_t*) h;
348 return ( p -> icon );
351 char *pnd_pxml_get_app_description ( pnd_pxml_handle h, char *iso_lang ) {
352 pnd_pxml_t *p = (pnd_pxml_t *) h;
353 return pnd_pxml_get_best_localized_string(p->descriptions, p->descriptions_c, iso_lang);
356 char *pnd_pxml_get_description_en ( pnd_pxml_handle h ) {
357 return pnd_pxml_get_app_description(h, "en");
360 char *pnd_pxml_get_description_de ( pnd_pxml_handle h ) {
361 return pnd_pxml_get_app_description(h, "de");
364 char *pnd_pxml_get_description_it ( pnd_pxml_handle h ) {
365 return pnd_pxml_get_app_description(h, "it");
368 char *pnd_pxml_get_description_fr ( pnd_pxml_handle h ) {
369 return pnd_pxml_get_app_description(h, "fr");
372 char *pnd_pxml_get_previewpic1 ( pnd_pxml_handle h ) {
373 pnd_pxml_t *p = (pnd_pxml_t*) h;
374 return ( p -> previewpic1 );
377 char *pnd_pxml_get_previewpic2 ( pnd_pxml_handle h ) {
378 pnd_pxml_t *p = (pnd_pxml_t*) h;
379 return ( p -> previewpic2 );
382 char *pnd_pxml_get_author_name ( pnd_pxml_handle h ) {
383 pnd_pxml_t *p = (pnd_pxml_t*) h;
384 return ( p -> author_name );
387 char *pnd_pxml_get_author_website ( pnd_pxml_handle h ) {
388 pnd_pxml_t *p = (pnd_pxml_t*) h;
389 return ( p -> author_website );
392 char *pnd_pxml_get_version_major ( pnd_pxml_handle h ) {
393 pnd_pxml_t *p = (pnd_pxml_t*) h;
394 return ( p -> version_major );
397 char *pnd_pxml_get_version_minor ( pnd_pxml_handle h ) {
398 pnd_pxml_t *p = (pnd_pxml_t*) h;
399 return ( p -> version_minor );
402 char *pnd_pxml_get_version_release ( pnd_pxml_handle h ) {
403 pnd_pxml_t *p = (pnd_pxml_t*) h;
404 return ( p -> version_release );
407 char *pnd_pxml_get_version_build ( pnd_pxml_handle h ) {
408 pnd_pxml_t *p = (pnd_pxml_t*) h;
409 return ( p -> version_build );
412 char *pnd_pxml_get_exec ( pnd_pxml_handle h ) {
413 pnd_pxml_t *p = (pnd_pxml_t*) h;
414 return ( p -> exec );
417 char *pnd_pxml_get_execargs ( pnd_pxml_handle h ) {
418 pnd_pxml_t *p = (pnd_pxml_t*) h;
419 return ( p -> execargs );
422 char *pnd_pxml_get_exec_option_no_x11 ( pnd_pxml_handle h ) {
423 pnd_pxml_t *p = (pnd_pxml_t*) h;
424 return ( p -> exec_no_x11 );
427 char *pnd_pxml_get_main_category ( pnd_pxml_handle h ) {
428 pnd_pxml_t *p = (pnd_pxml_t*) h;
429 return ( p -> main_category );
432 char *pnd_pxml_get_subcategory1 ( pnd_pxml_handle h ) {
433 pnd_pxml_t *p = (pnd_pxml_t*) h;
434 return ( p -> subcategory1 );
437 char *pnd_pxml_get_subcategory2 ( pnd_pxml_handle h ) {
438 pnd_pxml_t *p = (pnd_pxml_t*) h;
439 return ( p -> subcategory2 );
442 char *pnd_pxml_get_altcategory ( pnd_pxml_handle h ) {
443 pnd_pxml_t *p = (pnd_pxml_t*) h;
444 return ( p -> altcategory );
447 char *pnd_pxml_get_altsubcategory1 ( pnd_pxml_handle h ) {
448 pnd_pxml_t *p = (pnd_pxml_t*) h;
449 return ( p -> altsubcategory1 );
452 char *pnd_pxml_get_altsubcategory2 ( pnd_pxml_handle h ) {
453 pnd_pxml_t *p = (pnd_pxml_t*) h;
454 return ( p -> altsubcategory2 );
457 char *pnd_pxml_get_osversion_major ( pnd_pxml_handle h ) {
458 pnd_pxml_t *p = (pnd_pxml_t*) h;
459 return ( p -> osversion_major );
462 char *pnd_pxml_get_osversion_minor ( pnd_pxml_handle h ) {
463 pnd_pxml_t *p = (pnd_pxml_t*) h;
464 return ( p -> osversion_minor );
467 char *pnd_pxml_get_osversion_release ( pnd_pxml_handle h ) {
468 pnd_pxml_t *p = (pnd_pxml_t*) h;
469 return ( p -> osversion_release );
472 char *pnd_pxml_get_osversion_build ( pnd_pxml_handle h ) {
473 pnd_pxml_t *p = (pnd_pxml_t*) h;
474 return ( p -> osversion_build );
477 char *pnd_pxml_get_associationitem1_name ( pnd_pxml_handle h ) {
478 pnd_pxml_t *p = (pnd_pxml_t*) h;
479 return ( p -> associationitem1_name );
482 char *pnd_pxml_get_associationitem1_filetype ( pnd_pxml_handle h ) {
483 pnd_pxml_t *p = (pnd_pxml_t*) h;
484 return ( p -> associationitem1_filetype );
487 char *pnd_pxml_get_associationitem1_parameter ( pnd_pxml_handle h ) {
488 pnd_pxml_t *p = (pnd_pxml_t*) h;
489 return ( p -> associationitem1_parameter );
492 char *pnd_pxml_get_associationitem2_name ( pnd_pxml_handle h ) {
493 pnd_pxml_t *p = (pnd_pxml_t*) h;
494 return ( p -> associationitem2_name );
497 char *pnd_pxml_get_associationitem2_filetype ( pnd_pxml_handle h ) {
498 pnd_pxml_t *p = (pnd_pxml_t*) h;
499 return ( p -> associationitem2_filetype );
502 char *pnd_pxml_get_associationitem2_parameter ( pnd_pxml_handle h ) {
503 pnd_pxml_t *p = (pnd_pxml_t*) h;
504 return ( p -> associationitem2_parameter );
507 char *pnd_pxml_get_associationitem3_name ( pnd_pxml_handle h ) {
508 pnd_pxml_t *p = (pnd_pxml_t*) h;
509 return ( p -> associationitem3_name );
512 char *pnd_pxml_get_associationitem3_filetype ( pnd_pxml_handle h ) {
513 pnd_pxml_t *p = (pnd_pxml_t*) h;
514 return ( p -> associationitem3_filetype );
517 char *pnd_pxml_get_associationitem3_parameter ( pnd_pxml_handle h ) {
518 pnd_pxml_t *p = (pnd_pxml_t*) h;
519 return ( p -> associationitem3_parameter );
522 char *pnd_pxml_get_clockspeed ( pnd_pxml_handle h ) {
523 pnd_pxml_t *p = (pnd_pxml_t*) h;
524 return ( p -> clockspeed );
527 char *pnd_pxml_get_background ( pnd_pxml_handle h ) {
528 pnd_pxml_t *p = (pnd_pxml_t*) h;
529 return ( p -> background );
532 char *pnd_pxml_get_startdir ( pnd_pxml_handle h ) {
533 pnd_pxml_t *p = (pnd_pxml_t*) h;
534 return ( p -> startdir );
537 char *pnd_pxml_get_mkdir ( pnd_pxml_handle h ) {
538 pnd_pxml_t *p = (pnd_pxml_t*) h;
539 return ( p -> mkdir_sp );
542 unsigned char pnd_pxml_is_affirmative ( char *v ) {
548 if ( ( v [ 0 ] == 'Y' ) ||
549 ( v [ 0 ] == 'y' ) ||