From 69c9b0423b43a81cf60827a77c1644bb5d74ff74 Mon Sep 17 00:00:00 2001 From: skeezix Date: Tue, 15 Dec 2009 10:50:59 -0500 Subject: [PATCH] Added basic emit_icon_to_buffer() routine for cpasjuste; you must free its return on your own --- include/pnd_desktop.h | 1 + lib/pnd_desktop.c | 82 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/include/pnd_desktop.h b/include/pnd_desktop.h index a6aba00..550b03e 100644 --- a/include/pnd_desktop.h +++ b/include/pnd_desktop.h @@ -19,6 +19,7 @@ unsigned char pnd_emit_dotdesktop ( char *targetpath, char *pndrun, pnd_disco_t // 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. diff --git a/lib/pnd_desktop.c b/lib/pnd_desktop.c index c2cffec..3760fba 100644 --- a/lib/pnd_desktop.c +++ b/lib/pnd_desktop.c @@ -393,3 +393,85 @@ char *pnd_map_dotdesktop_category ( pnd_conf_handle c, char *single_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 ); +} -- 2.39.5