// emit_icon() will attempt to copy the icon from a PXML directory, or from a pnd file if appended,
// to the given directory; returns 1 on sucess, otherwise is a fail.
unsigned char pnd_emit_icon ( char *targetpath, pnd_disco_t *p );
+unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ); // returns length of malloc in r_buflen if !NULL
// pnd_map_dotdesktop_categories() will attempt to find an appropriate standard .desktop category(s) based
// on the provided PXML-style category.
return ( ret );
}
+
+unsigned char *pnd_emit_icon_to_buffer ( pnd_disco_t *p, unsigned int *r_buflen ) {
+ // this is shamefully mostly a copy of emit_icon() above; really, need to refactor that to use this routine
+ // with a fwrite at the end...
+ char from [ FILENAME_MAX ]; // source filename
+ char bits [ 8 * 1024 ];
+ unsigned int bitlen;
+ FILE *pnd = NULL;
+ unsigned char *target = NULL, *targiter = NULL;
+
+ // prelim .. if a pnd file, and no offset found, discovery code didn't locate icon.. so bail.
+ if ( ( p -> object_type == pnd_object_type_pnd ) &&
+ ( ! p -> pnd_icon_pos ) )
+ {
+ return ( NULL ); // discover code didn't find it, so FAIL
+ }
+
+ /* first.. open the source file, by type of application:
+ * are we looking through a pnd file or a dir?
+ */
+ if ( p -> object_type == pnd_object_type_directory ) {
+ sprintf ( from, "%s/%s", p -> object_path, p -> icon );
+ } else if ( p -> object_type == pnd_object_type_pnd ) {
+ sprintf ( from, "%s/%s", p -> object_path, p -> object_filename );
+ }
+
+ pnd = fopen ( from, "r" );
+
+ if ( ! pnd ) {
+ return ( NULL );
+ }
+
+ // determine length of file, then adjust by icon position to find begin of icon
+ unsigned int len;
+
+ fseek ( pnd, 0, SEEK_END );
+ len = ftell ( pnd );
+ //fseek ( pnd, 0, SEEK_SET );
+
+ fseek ( pnd, p -> pnd_icon_pos, SEEK_SET );
+
+ len -= p -> pnd_icon_pos;
+
+ // create target buffer
+ target = malloc ( len );
+
+ if ( ! target ) {
+ fclose ( pnd );
+ return ( 0 );
+ }
+
+ targiter = target;
+
+ if ( r_buflen ) {
+ *r_buflen = len;
+ }
+
+ // copy over icon to target
+ while ( len ) {
+
+ if ( len > (8*1024) ) {
+ bitlen = (8*1024);
+ } else {
+ bitlen = len;
+ }
+
+ if ( fread ( bits, bitlen, 1, pnd ) != 1 ) {
+ fclose ( pnd );
+ free ( target );
+ return ( NULL );
+ }
+
+ memmove ( targiter, bits, bitlen );
+ targiter += bitlen;
+
+ len -= bitlen;
+ } // while
+
+ fclose ( pnd );
+
+ return ( target );
+}