discovery code now recognizes pnd files
[pandora-libraries.git] / lib / pnd_discovery.c
index 01f4c9a..9513087 100644 (file)
@@ -1,6 +1,8 @@
 
 #include <stdio.h> /* for FILE etc */
 #include <stdlib.h> /* for malloc */
+
+#define __USE_GNU /* for strcasestr */
 #include <string.h> /* for making ftw.h happy */
 
 #define _XOPEN_SOURCE 500
@@ -51,68 +53,112 @@ static int pnd_disco_callback ( const char *fpath, const struct stat *sb,
                                int typeflag, struct FTW *ftwbuf )
 {
   unsigned char valid = 0; // 1 for plaintext PXML, 2 for PND...
+  pnd_pxml_handle pxmlh = 0;
+
+  //printf ( "disco root callback encountered '%s'\n", fpath );
 
   // PXML.xml is a possible application candidate (and not a dir named PXML.xml :)
   if ( typeflag & FTW_D ) {
+    //printf ( " .. is dir, skipping\n" );
     return ( 0 ); // skip directories and other non-regular files
   }
 
   // PND/PNZ file and others may be valid as well .. but lets leave that for now
+  //   printf ( "%s %s\n", fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT );
   if ( strcasecmp ( fpath + ftwbuf -> base, PXML_FILENAME ) == 0 ) {
     valid = 1;
-  } else if ( strcasecmp ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) == 0 ) {
+  } else if ( strcasestr ( fpath + ftwbuf -> base, PND_PACKAGE_FILEEXT "\0" ) ) {
     valid = 2;
   }
 
   // if not a file of interest, just keep looking until we run out
   if ( ! valid ) {
+    //printf ( " .. bad filename, skipping\n" );
     return ( 0 );
   }
 
   // potentially a valid application
   if ( valid == 1 ) {
     // Plaintext PXML file
-    //printf ( "disco callback encountered '%s'\n", fpath );
-
-    pnd_pxml_handle pxmlh;
+    //printf ( "PXML: disco callback encountered '%s'\n", fpath );
 
     // pick up the PXML if we can
-
     pxmlh = pnd_pxml_fetch ( (char*) fpath );
 
-    if ( ! pxmlh ) {
-      return ( 0 ); // continue the scan
+  } else if ( valid == 2 ) {
+    // PND ... ??
+    FILE *f;
+    char pxmlbuf [ 32 * 1024 ]; // TBD: assuming 32k pxml accrual buffer is a little lame
+
+    //printf ( "PND: disco callback encountered '%s'\n", fpath );
+
+    // is this a valid .pnd file? The filename is a candidate already, but verify..
+    // .. presence of PXML appended, or at least contained within?
+    // .. presence of an icon appended after PXML?
+
+    // open it up..
+    f = fopen ( fpath, "r" );
+
+    // try to locate the PXML portion
+    if ( ! pnd_pnd_seek_pxml ( f ) ) {
+      fclose ( f );
+      return ( 0 ); // pnd or not, but not to spec. Pwn'd the pnd?
+    }
+
+    // accrue it into a buffer
+    if ( ! pnd_pnd_accrue_pxml ( f, pxmlbuf, 32 * 1024 ) ) {
+      fclose ( f );
+      return ( 0 );
     }
 
+    //printf ( "buffer is %s\n", pxmlbuf );
+    //fflush ( stdout );
+
+    // by now, we have <PXML> .. </PXML>, try to parse..
+    pxmlh = pnd_pxml_fetch_buffer ( (char*) fpath, pxmlbuf );
+
+  }
+
+  // pxmlh is useful?
+  if ( pxmlh ) {
+
     // look for any overrides, if requested
     pnd_pxml_merge_override ( pxmlh, disco_overrides );
 
     // check for validity and add to resultset if it looks executable
     if ( pnd_is_pxml_valid_app ( pxmlh ) ) {
+      char b [ 1024 ]; // TBD: also lame
       pnd_disco_t *p;
 
       p = pnd_box_allocinsert ( disco_box, (char*) fpath, sizeof(pnd_disco_t) );
-      p -> title_en = strdup ( pnd_pxml_get_app_name ( pxmlh ) );
-      p -> icon = strdup ( pnd_pxml_get_icon_path ( pxmlh ) );
-      p -> exec = strdup ( pnd_pxml_get_exec_path ( pxmlh ) );
-      p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
-      p -> main_category = strdup ( pnd_pxml_get_primary_category ( pxmlh ) );
-      p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) ); 
-
+      if ( pnd_pxml_get_app_name ( pxmlh ) ) {
+       p -> title_en = strdup ( pnd_pxml_get_app_name ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_icon_path ( pxmlh ) ) {
+       p -> icon = strdup ( pnd_pxml_get_icon_path ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_exec_path ( pxmlh ) ) {
+       snprintf ( b, 1024, "pnd_run_magic %s", pnd_pxml_get_exec_path ( pxmlh ) );
+       p -> exec = strdup ( b );
+      }
+      if ( pnd_pxml_get_unique_id ( pxmlh ) ) {
+       p -> unique_id = strdup ( pnd_pxml_get_unique_id ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_primary_category ( pxmlh ) ) {
+       p -> main_category = strdup ( pnd_pxml_get_primary_category ( pxmlh ) );
+      }
+      if ( pnd_pxml_get_clockspeed ( pxmlh ) ) {
+       p -> clockspeed = strdup ( pnd_pxml_get_clockspeed ( pxmlh ) ); 
+      }
+
+    } else {
+      //printf ( "Invalid PXML; skipping.\n" );
     }
 
     // ditch pxml
     pnd_pxml_delete ( pxmlh );
 
-  } else if ( valid == 2 ) {
-    // PND ... ??
-    printf ( "PND: disco callback encountered '%s'\n", fpath );
-
-    // is this a valid .pnd file? The filename is a candidate already, but verify..
-    // .. presence of PXML appeneded, or at least contained within?
-    // .. presence of an icon appended after PXML?
-
-  }
+  } // got a pxmlh
 
   return ( 0 ); // continue the tree walk
 }