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 );
36 b = (char*) malloc ( len + 1 );
43 fread ( b, 1, len, f );
47 return ( pnd_pxml_fetch_buffer ( fullpath, b ) );
50 pnd_pxml_handle *pnd_pxml_fetch_buffer ( char *filename, char *buffer ) {
52 pnd_pxml_t **p = malloc ( sizeof(pnd_pxml_t*) * PXML_MAXAPPS );
53 memset ( p, '\0', sizeof(pnd_pxml_t*) * PXML_MAXAPPS );
55 if ( ! pnd_pxml_parse ( filename, buffer, strlen ( buffer ), p ) ) {
59 return ( (pnd_pxml_handle*) p );
62 void pnd_pxml_delete ( pnd_pxml_handle h ) {
63 pnd_pxml_t *p = (pnd_pxml_t*) h;
67 for (i = 0; i < p->titles_c; i++)
69 free(p->titles[i].language);
70 free(p->titles[i].string);
75 if (p->descriptions) {
76 for (i = 0; i < p->descriptions_c; i++)
78 free(p->descriptions[i].language);
79 free(p->descriptions[i].string);
81 free(p->descriptions);
84 if ( p -> standalone ) {
85 free ( p -> standalone );
90 if ( p -> previewpic1 ) {
91 free ( p -> previewpic1 );
93 if ( p -> previewpic2 ) {
94 free ( p -> previewpic2 );
96 if ( p -> author_name ) {
97 free ( p -> author_name );
99 if ( p -> author_website ) {
100 free ( p -> author_website );
102 if ( p -> version_major ) {
103 free ( p -> version_major );
105 if ( p -> version_minor ) {
106 free ( p -> version_minor );
108 if ( p -> version_release ) {
109 free ( p -> version_release );
111 if ( p -> version_build ) {
112 free ( p -> version_build );
117 if ( p -> main_category ) {
118 free ( p -> main_category );
120 if ( p -> subcategory1 ) {
121 free ( p -> subcategory1 );
123 if ( p -> subcategory2 ) {
124 free ( p -> subcategory2 );
126 if ( p -> altcategory ) {
127 free ( p -> altcategory );
129 if ( p -> altsubcategory1 ) {
130 free ( p -> altsubcategory1 );
132 if ( p -> altsubcategory2 ) {
133 free ( p -> altsubcategory2 );
135 if ( p -> osversion_major ) {
136 free ( p -> osversion_major );
138 if ( p -> osversion_minor ) {
139 free ( p -> osversion_minor );
141 if ( p -> osversion_release ) {
142 free ( p -> osversion_release );
144 if ( p -> osversion_build ) {
145 free ( p -> osversion_build );
147 if ( p -> associationitem1_name ) {
148 free ( p -> associationitem1_name );
150 if ( p -> associationitem1_filetype ) {
151 free ( p -> associationitem1_filetype );
153 if ( p -> associationitem1_parameter ) {
154 free ( p -> associationitem1_parameter );
156 if ( p -> associationitem2_name ) {
157 free ( p -> associationitem2_name );
159 if ( p -> associationitem2_filetype ) {
160 free ( p -> associationitem2_filetype );
162 if ( p -> associationitem2_parameter ) {
163 free ( p -> associationitem2_parameter );
165 if ( p -> associationitem3_name ) {
166 free ( p -> associationitem3_name );
168 if ( p -> associationitem1_filetype ) {
169 free ( p -> associationitem3_filetype );
171 if ( p -> associationitem1_parameter ) {
172 free ( p -> associationitem3_parameter );
174 if ( p -> clockspeed ) {
175 free ( p -> clockspeed );
177 if ( p -> background ) {
178 free ( p -> background );
180 if ( p -> startdir ) {
181 free ( p -> startdir );
183 if ( p -> appdata_dirname ) {
184 free ( p -> appdata_dirname );
187 free(p); /*very important!*/
192 void pnd_pxml_set_app_name ( pnd_pxml_handle h, char *v ) {
194 * Please do not use this function if it can be avoided; it is only here for compatibility.
195 * The function might fail on low memory, and there's no way for the user to know when this happens.
197 pnd_pxml_t *p = (pnd_pxml_t*) h;
198 char has_en_field = 0;
201 if (!v) return; /*TODO: add error information? Make it possible to set the string to NULL?*/
203 for (i = 0; i < p->titles_c; i++)
205 if (strncmp("en", p->titles[i].language, 2) == 0) /*strict comparison; match "en_US", "en_GB" etc... All these are set.*/
207 free(p->titles[i].string);
208 p->titles[i].string = strdup(v);
216 if (p->titles_c > p->titles_alloc_c) //we don't have enough strings allocated
218 p->titles_alloc_c <<= 1;
219 p->titles = (pnd_localized_string_t *)realloc((void*)p->titles, p->titles_alloc_c);
220 if (!p->titles) return; //errno = ENOMEM
222 p->titles[p->titles_c - 1].language = "en_US";
223 p->titles[p->titles_c - 1].string = strdup(v);
229 unsigned char pnd_is_pxml_valid_app ( pnd_pxml_handle h ) {
230 //pnd_pxml_t *p = (pnd_pxml_t*) h; //unused atm
232 // for now, lets just verify the exec-path is valid
234 //printf ( "exec is '%s'\n", p -> exec );
238 // even this is complicated by pnd_run.sh semantics .. can't check if it exists
239 // during discovery, since it is not mounted yet..
242 if ( stat ( p -> exec, &buf ) == 0 ) {
243 return ( 1 ); // path is present
250 signed char pnd_pxml_merge_override ( pnd_pxml_handle h, char *searchpath ) {
251 // the pxml includes a unique-id; use this value to attempt to find an
252 // override in the given searchpath
253 signed char retval = 0;
255 #if 0 // TODO: Unfinished entirely now
256 pnd_pxml_handle mergeh;
258 if ( ! pnd_pxml_get_unique_id ( h ) ) {
259 return ( -1 ); // no unique-id present, so can't use it to name potential override files
266 strncat ( buffer, "/", FILENAME_MAX );
267 strncat ( buffer, pnd_pxml_get_unique_id ( h ), FILENAME_MAX );
268 strncat ( buffer, ".xml", FILENAME_MAX );
269 //printf ( " Path to seek merges: '%s'\n", buffer );
271 // TODO: handle multiple subapps!
272 mergeh = pnd_pxml_fetch ( buffer );
276 // TODO: handle all the various data bits
277 if ( pnd_pxml_get_app_name_en ( mergeh ) ) {
278 pnd_pxml_set_app_name ( h, pnd_pxml_get_app_name_en ( mergeh ) );
281 pnd_pxml_delete ( mergeh );
291 char *pnd_pxml_get_best_localized_string(pnd_localized_string_t strings[], int strings_c, char *iso_lang)
294 int similarity_weight = 0xffff; /*Set to something Really Bad in the beginning*/
295 char *best_match = NULL;
297 for(i = 0; i < strings_c; i++)
299 // factor in the length -- if we're given 'en' and have a string 'en_US', thats better than 'de_something'; if we don't
300 // use length, then en_US and de_FO are same to 'en'.
301 int maxcount = strlen ( strings[i].language ) < strlen ( iso_lang ) ? strlen ( strings[i].language ) : strlen ( iso_lang );
302 int new_weight = abs(strncmp(strings[i].language, iso_lang, maxcount));
303 //pnd_log ( PND_LOG_DEFAULT, "looking for lang %s, looking at lang %s (weight %d, old %d): %s\n",
304 // iso_lang, strings [ i ].language, new_weight, similarity_weight, strings [ i ].string );
305 if (new_weight < similarity_weight)
307 similarity_weight = new_weight;
308 best_match = strings[i].string;
313 //pnd_log ( PND_LOG_DEFAULT, "best match: %s\n", best_match );
314 return strdup(best_match);
317 //pnd_log ( PND_LOG_DEFAULT, "best match: FAIL\n" );
322 char *pnd_pxml_get_package_id ( pnd_pxml_handle h ) {
323 pnd_pxml_t *p = (pnd_pxml_t*) h;
324 return ( p -> package_id );
327 char *pnd_pxml_get_app_name ( pnd_pxml_handle h, char *iso_lang ) {
328 pnd_pxml_t *p = (pnd_pxml_t *) h;
329 return pnd_pxml_get_best_localized_string(p->titles, p->titles_c, iso_lang);
332 char *pnd_pxml_get_app_name_en ( pnd_pxml_handle h ) {
333 return pnd_pxml_get_app_name(h, "en");
336 char *pnd_pxml_get_app_name_de ( pnd_pxml_handle h ) {
337 return pnd_pxml_get_app_name(h, "de");
340 char *pnd_pxml_get_app_name_it ( pnd_pxml_handle h ) {
341 return pnd_pxml_get_app_name(h, "it");
344 char *pnd_pxml_get_app_name_fr ( pnd_pxml_handle h ) {
345 return pnd_pxml_get_app_name(h, "fr");
347 char *pnd_pxml_get_unique_id ( pnd_pxml_handle h ) {
348 pnd_pxml_t *p = (pnd_pxml_t*) h;
349 return ( p -> unique_id );
352 char *pnd_pxml_get_appdata_dirname ( pnd_pxml_handle h ) {
353 pnd_pxml_t *p = (pnd_pxml_t*) h;
354 return ( p -> appdata_dirname );
357 char *pnd_pxml_get_standalone ( pnd_pxml_handle h ) {
358 pnd_pxml_t *p = (pnd_pxml_t*) h;
359 return ( p -> standalone );
362 char *pnd_pxml_get_icon ( pnd_pxml_handle h ) {
363 pnd_pxml_t *p = (pnd_pxml_t*) h;
364 return ( p -> icon );
367 // this guy's func name is 'out of sync' with the family of functions below; but since it
368 // exists, rather than just remove it and break someones code, will add in the appropriate
369 // function wrapper; the header only specifies the other guy (always did), so the header
370 // was already on the right path.
371 char *pnd_pxml_get_app_description ( pnd_pxml_handle h, char *iso_lang ) {
372 pnd_pxml_t *p = (pnd_pxml_t *) h;
373 return pnd_pxml_get_best_localized_string(p->descriptions, p->descriptions_c, iso_lang);
376 char *pnd_pxml_get_description_en ( pnd_pxml_handle h ) {
377 return pnd_pxml_get_app_description(h, "en");
380 char *pnd_pxml_get_description_de ( pnd_pxml_handle h ) {
381 return pnd_pxml_get_app_description(h, "de");
384 char *pnd_pxml_get_description_it ( pnd_pxml_handle h ) {
385 return pnd_pxml_get_app_description(h, "it");
388 char *pnd_pxml_get_description_fr ( pnd_pxml_handle h ) {
389 return pnd_pxml_get_app_description(h, "fr");
392 // wrapper added so family of function names is consistent; see comment for pnd_pxml_get_app_description() above
393 char *pnd_pxml_get_description ( pnd_pxml_handle h, char *iso_lang) {
394 return ( pnd_pxml_get_app_description ( h, iso_lang ) );
397 char *pnd_pxml_get_previewpic1 ( pnd_pxml_handle h ) {
398 pnd_pxml_t *p = (pnd_pxml_t*) h;
399 return ( p -> previewpic1 );
402 char *pnd_pxml_get_previewpic2 ( pnd_pxml_handle h ) {
403 pnd_pxml_t *p = (pnd_pxml_t*) h;
404 return ( p -> previewpic2 );
407 char *pnd_pxml_get_author_name ( pnd_pxml_handle h ) {
408 pnd_pxml_t *p = (pnd_pxml_t*) h;
409 return ( p -> author_name );
412 char *pnd_pxml_get_author_website ( pnd_pxml_handle h ) {
413 pnd_pxml_t *p = (pnd_pxml_t*) h;
414 return ( p -> author_website );
417 char *pnd_pxml_get_version_major ( pnd_pxml_handle h ) {
418 pnd_pxml_t *p = (pnd_pxml_t*) h;
419 return ( p -> version_major );
422 char *pnd_pxml_get_version_minor ( pnd_pxml_handle h ) {
423 pnd_pxml_t *p = (pnd_pxml_t*) h;
424 return ( p -> version_minor );
427 char *pnd_pxml_get_version_release ( pnd_pxml_handle h ) {
428 pnd_pxml_t *p = (pnd_pxml_t*) h;
429 return ( p -> version_release );
432 char *pnd_pxml_get_version_build ( pnd_pxml_handle h ) {
433 pnd_pxml_t *p = (pnd_pxml_t*) h;
434 return ( p -> version_build );
437 char *pnd_pxml_get_exec ( pnd_pxml_handle h ) {
438 pnd_pxml_t *p = (pnd_pxml_t*) h;
439 return ( p -> exec );
442 char *pnd_pxml_get_execargs ( pnd_pxml_handle h ) {
443 pnd_pxml_t *p = (pnd_pxml_t*) h;
444 return ( p -> execargs );
447 char *pnd_pxml_get_exec_option_no_x11 ( pnd_pxml_handle h ) {
448 pnd_pxml_t *p = (pnd_pxml_t*) h;
449 return ( p -> exec_no_x11 );
452 char *pnd_pxml_get_main_category ( pnd_pxml_handle h ) {
453 pnd_pxml_t *p = (pnd_pxml_t*) h;
454 return ( p -> main_category );
457 char *pnd_pxml_get_subcategory1 ( pnd_pxml_handle h ) {
458 pnd_pxml_t *p = (pnd_pxml_t*) h;
459 return ( p -> subcategory1 );
462 char *pnd_pxml_get_subcategory2 ( pnd_pxml_handle h ) {
463 pnd_pxml_t *p = (pnd_pxml_t*) h;
464 return ( p -> subcategory2 );
467 char *pnd_pxml_get_altcategory ( pnd_pxml_handle h ) {
468 pnd_pxml_t *p = (pnd_pxml_t*) h;
469 return ( p -> altcategory );
472 char *pnd_pxml_get_altsubcategory1 ( pnd_pxml_handle h ) {
473 pnd_pxml_t *p = (pnd_pxml_t*) h;
474 return ( p -> altsubcategory1 );
477 char *pnd_pxml_get_altsubcategory2 ( pnd_pxml_handle h ) {
478 pnd_pxml_t *p = (pnd_pxml_t*) h;
479 return ( p -> altsubcategory2 );
482 char *pnd_pxml_get_osversion_major ( pnd_pxml_handle h ) {
483 pnd_pxml_t *p = (pnd_pxml_t*) h;
484 return ( p -> osversion_major );
487 char *pnd_pxml_get_osversion_minor ( pnd_pxml_handle h ) {
488 pnd_pxml_t *p = (pnd_pxml_t*) h;
489 return ( p -> osversion_minor );
492 char *pnd_pxml_get_osversion_release ( pnd_pxml_handle h ) {
493 pnd_pxml_t *p = (pnd_pxml_t*) h;
494 return ( p -> osversion_release );
497 char *pnd_pxml_get_osversion_build ( pnd_pxml_handle h ) {
498 pnd_pxml_t *p = (pnd_pxml_t*) h;
499 return ( p -> osversion_build );
502 char *pnd_pxml_get_associationitem1_name ( pnd_pxml_handle h ) {
503 pnd_pxml_t *p = (pnd_pxml_t*) h;
504 return ( p -> associationitem1_name );
507 char *pnd_pxml_get_associationitem1_filetype ( pnd_pxml_handle h ) {
508 pnd_pxml_t *p = (pnd_pxml_t*) h;
509 return ( p -> associationitem1_filetype );
512 char *pnd_pxml_get_associationitem1_parameter ( pnd_pxml_handle h ) {
513 pnd_pxml_t *p = (pnd_pxml_t*) h;
514 return ( p -> associationitem1_parameter );
517 char *pnd_pxml_get_associationitem2_name ( pnd_pxml_handle h ) {
518 pnd_pxml_t *p = (pnd_pxml_t*) h;
519 return ( p -> associationitem2_name );
522 char *pnd_pxml_get_associationitem2_filetype ( pnd_pxml_handle h ) {
523 pnd_pxml_t *p = (pnd_pxml_t*) h;
524 return ( p -> associationitem2_filetype );
527 char *pnd_pxml_get_associationitem2_parameter ( pnd_pxml_handle h ) {
528 pnd_pxml_t *p = (pnd_pxml_t*) h;
529 return ( p -> associationitem2_parameter );
532 char *pnd_pxml_get_associationitem3_name ( pnd_pxml_handle h ) {
533 pnd_pxml_t *p = (pnd_pxml_t*) h;
534 return ( p -> associationitem3_name );
537 char *pnd_pxml_get_associationitem3_filetype ( pnd_pxml_handle h ) {
538 pnd_pxml_t *p = (pnd_pxml_t*) h;
539 return ( p -> associationitem3_filetype );
542 char *pnd_pxml_get_associationitem3_parameter ( pnd_pxml_handle h ) {
543 pnd_pxml_t *p = (pnd_pxml_t*) h;
544 return ( p -> associationitem3_parameter );
547 char *pnd_pxml_get_clockspeed ( pnd_pxml_handle h ) {
548 pnd_pxml_t *p = (pnd_pxml_t*) h;
549 return ( p -> clockspeed );
552 char *pnd_pxml_get_background ( pnd_pxml_handle h ) {
553 pnd_pxml_t *p = (pnd_pxml_t*) h;
554 return ( p -> background );
557 char *pnd_pxml_get_startdir ( pnd_pxml_handle h ) {
558 pnd_pxml_t *p = (pnd_pxml_t*) h;
559 return ( p -> startdir );
562 char *pnd_pxml_get_mkdir ( pnd_pxml_handle h ) {
563 pnd_pxml_t *p = (pnd_pxml_t*) h;
564 return ( p -> mkdir_sp );
567 unsigned char pnd_pxml_is_affirmative ( char *v ) {
573 if ( ( v [ 0 ] == 'Y' ) ||
574 ( v [ 0 ] == 'y' ) ||
583 pnd_pxml_x11_req_e pnd_pxml_get_x11 ( char *pxmlvalue ) {
586 return ( pnd_pxml_x11_ignored );
587 } else if ( strcasecmp ( pxmlvalue, "req" ) == 0 ) {
588 return ( pnd_pxml_x11_required );
589 } else if ( strcasecmp ( pxmlvalue, "stop" ) == 0 ) {
590 return ( pnd_pxml_x11_stop );
591 } else if ( strcasecmp ( pxmlvalue, "ignore" ) == 0 ) {
592 return ( pnd_pxml_x11_ignored );
595 return ( pnd_pxml_x11_ignored ); // default
598 char *pnd_pxml_get_info_name ( pnd_pxml_handle h ) {
599 pnd_pxml_t *p = (pnd_pxml_t*) h;
600 return ( p -> info_name );
603 char *pnd_pxml_get_info_type ( pnd_pxml_handle h ) {
604 pnd_pxml_t *p = (pnd_pxml_t*) h;
605 return ( p -> info_type );
608 char *pnd_pxml_get_info_src ( pnd_pxml_handle h ) {
609 pnd_pxml_t *p = (pnd_pxml_t*) h;
610 return ( p -> info_filename );