5471ff69c9ae0c844bb9bb1f68c66d6da6f925d5
[pandora-libraries.git] / lib / pnd_pxml.c
1
2 #include <stdio.h> /* for FILE */
3 #include <stdlib.h> /* for malloc */
4 #include <string.h> /* for string ops */
5
6 #include <sys/types.h> /* for stat */
7 #include <sys/stat.h> /* for stat */
8 #include <unistd.h> /* for stat */
9
10 #include "pnd_pxml.h"
11 #include "pnd_pathiter.h"
12 #include "pnd_tinyxml.h"
13
14 pnd_pxml_handle pnd_pxml_fetch ( char *fullpath ) {
15
16   pnd_pxml_t *p = malloc ( sizeof(pnd_pxml_t) );
17
18   memset ( p, '\0', sizeof(pnd_pxml_t) );
19
20   if ( ! pnd_pxml_load ( fullpath, p ) ) {
21     return ( 0 );
22   }
23
24   return ( p );
25 }
26
27 pnd_pxml_handle pnd_pxml_fetch_buffer ( char *filename, char *buffer ) {
28
29   pnd_pxml_t *p = malloc ( sizeof(pnd_pxml_t) );
30
31   memset ( p, '\0', sizeof(pnd_pxml_t) );
32
33   if ( ! pnd_pxml_parse ( filename, buffer, strlen ( buffer ), p ) ) {
34     return ( 0 );
35   }
36
37   return ( p );
38 }
39
40 void pnd_pxml_delete ( pnd_pxml_handle h ) {
41   pnd_pxml_t *p = (pnd_pxml_t*) h;
42
43   if ( p -> title_en ) {
44     free ( p -> title_en );
45   }
46
47   if ( p -> icon ) {
48     free ( p -> icon );
49   }
50
51   if ( p -> exec ) {
52     free ( p -> exec );
53   }
54   if ( p -> main_category ) {
55     free ( p -> main_category );
56   }
57   if ( p -> unique_id ) {
58     free ( p -> unique_id );
59   }
60   if ( p -> clockspeed ) {
61     free ( p -> clockspeed );
62   }
63
64   return;
65 }
66
67 char *pnd_pxml_get_app_name ( pnd_pxml_handle h ) {
68   pnd_pxml_t *p = (pnd_pxml_t*) h;
69   return ( p -> title_en );
70 }
71
72 char *pnd_pxml_get_icon_path ( pnd_pxml_handle h ) {
73   pnd_pxml_t *p = (pnd_pxml_t*) h;
74   return ( p -> icon );
75 }
76
77 char *pnd_pxml_get_clockspeed ( pnd_pxml_handle h ) {
78   pnd_pxml_t *p = (pnd_pxml_t*) h;
79   return ( p -> clockspeed );
80 }
81
82 void pnd_pxml_set_app_name ( pnd_pxml_handle h, char *v ) {
83   pnd_pxml_t *p = (pnd_pxml_t*) h;
84   if ( p -> title_en ) {
85     free ( p -> title_en );
86     p -> title_en = NULL;
87   }
88
89   if ( v ) {
90     p -> title_en = strdup ( v );
91   }
92
93   return;
94 }
95
96 char *pnd_pxml_get_unique_id ( pnd_pxml_handle h ) {
97   pnd_pxml_t *p = (pnd_pxml_t*) h;
98   return ( p -> unique_id );
99 }
100
101 char *pnd_pxml_get_primary_category ( pnd_pxml_handle h ) {
102   pnd_pxml_t *p = (pnd_pxml_t*) h;
103   return ( p -> main_category );
104 }
105
106 char *pnd_pxml_get_exec_path ( pnd_pxml_handle h ) {
107   pnd_pxml_t *p = (pnd_pxml_t*) h;
108   return ( p -> exec );
109 }
110
111 unsigned char pnd_is_pxml_valid_app ( pnd_pxml_handle h ) {
112   pnd_pxml_t *p = (pnd_pxml_t*) h;
113
114   // for now, lets just verify the exec-path is valid
115   //printf ( "exec is '%s'\n", p -> exec );
116
117   struct stat buf;
118   if ( stat ( p -> exec, &buf ) == 0 ) {
119     return ( 1 ); // path is present
120   }
121
122   return ( 0 );
123 }
124
125 signed char pnd_pxml_merge_override ( pnd_pxml_handle h, char *searchpath ) {
126   // the pxml includes a unique-id; use this value to attempt to find an
127   // override in the given searchpath
128   signed char retval = 0;
129   pnd_pxml_handle mergeh;
130
131   SEARCHPATH_PRE
132   {
133
134     // do it
135     strncat ( buffer, "/", FILENAME_MAX );
136     strncat ( buffer, pnd_pxml_get_unique_id ( h ), FILENAME_MAX );
137     strncat ( buffer, ".xml", FILENAME_MAX );
138     //printf ( "  Path to seek merges: '%s'\n", buffer );
139
140     mergeh = pnd_pxml_fetch ( buffer );
141
142     if ( mergeh ) {
143
144       if ( pnd_pxml_get_app_name ( mergeh ) ) {
145         pnd_pxml_set_app_name ( h, pnd_pxml_get_app_name ( mergeh ) );
146       }
147
148       pnd_pxml_delete ( mergeh );
149     }
150
151   }
152   SEARCHPATH_POST
153
154   return ( retval );
155 }