libpnd, mmenu: better handling of 'NoSubcategory' pseudo-subcategory in ovr files
[pandora-libraries.git] / lib / pnd_discovery.c
index 1e41ead..8afbb38 100644 (file)
@@ -129,18 +129,21 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
 #if 1 // icon
     // for convenience, lets skip along past trailing newlines/CR's in hopes of finding icon data?
     {
-      unsigned int pos = ftell ( f );
       char pngbuffer [ 16 ]; // \211 P N G \r \n \032 \n
       pngbuffer [ 0 ] = 137;      pngbuffer [ 1 ] = 80;      pngbuffer [ 2 ] = 78;      pngbuffer [ 3 ] = 71;
       pngbuffer [ 4 ] = 13;       pngbuffer [ 5 ] = 10;       pngbuffer [ 6 ] = 26;      pngbuffer [ 7 ] = 10;
 
-      unsigned char padtests = 10;
+      unsigned char padtests = 20;
       unsigned int padstart = ftell ( f );
+
+      // seek back 10 (should be back into the /PXML> part) to catch any appending-icon-no-line-endings funny business
+      fseek ( f, -10, SEEK_CUR );
+
       while ( padtests ) {
 
        if ( fread ( pngbuffer + 8, 8, 1, f ) == 1 ) {
          if ( memcmp ( pngbuffer, pngbuffer + 8, 8 ) == 0 ) {
-           pxml_close_pos = pos;
+           pxml_close_pos = ftell ( f ) - 8;
            break;
          } // if
          fseek ( f, -7, SEEK_CUR ); // seek back 7 (so we're 1 further than we started, since PNG header is 8b)
@@ -363,8 +366,11 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
        if ( ( v = pnd_conf_get_as_char ( ovrh, key ) ) ) {
          if ( p -> main_category1 ) {
            free ( p -> main_category1 );
+           p -> main_category1 = NULL;
+         }
+         if ( strcasecmp ( v, "NoSubcategory" ) != 0 ) {
+           p -> main_category1 = strdup ( v );
          }
-         p -> main_category1 = strdup ( v );
        }
 
       } // got ovr conf loaded?
@@ -420,3 +426,34 @@ pnd_box_handle pnd_disco_search ( char *searchpath, char *overridespath ) {
 
   return ( disco_box );
 }
+
+pnd_box_handle pnd_disco_file ( char *path, char *filename ) {
+  struct stat statbuf;
+
+  // set up container
+  disco_overrides = NULL;
+  disco_box = pnd_box_new ( "discovery" );
+
+  // path
+  char fullpath [ PATH_MAX ];
+  sprintf ( fullpath, "%s/%s", path, filename );
+
+  // fake it
+  if ( stat ( fullpath, &statbuf ) < 0 ) {
+    return ( 0 );
+  }
+
+  struct FTW ftw;
+  ftw.base = strlen ( path );
+  ftw.level = 0;
+
+  pnd_disco_callback ( fullpath, &statbuf, FTW_F, &ftw );
+
+  // return whatever we found, or NULL if nada
+  if ( ! pnd_box_get_head ( disco_box ) ) {
+    pnd_box_delete ( disco_box );
+    disco_box = NULL;
+  }
+
+  return ( disco_box );
+}