@@ -122,7 +122,7 @@ struct MULTIBITMAPHEADER {
122122 BOOL changed;
123123 int page_count;
124124 BlockList m_blocks;
125- std::string m_filename;
125+ MultiBitmapPath m_filename;
126126 BOOL read_only;
127127 FREE_IMAGE_FORMAT cache_fif;
128128 int load_flags;
@@ -138,20 +138,6 @@ struct MULTIBITMAPHEADER {
138138// Helper functions
139139// =====================================================================
140140
141- inline void
142- ReplaceExtension (std::string& dst_filename, const std::string& src_filename, const std::string& dst_extension) {
143- size_t lastDot = src_filename.find_last_of (' .' );
144- if (lastDot == std::string::npos) {
145- dst_filename = src_filename;
146- dst_filename += " ." ;
147- dst_filename += dst_extension;
148- }
149- else {
150- dst_filename = src_filename.substr (0 , lastDot + 1 );
151- dst_filename += dst_extension;
152- }
153- }
154-
155141} // < ns
156142
157143
@@ -254,12 +240,13 @@ FreeImage_InternalGetPageCount(FIMULTIBITMAP *bitmap) {
254240// Multipage functions
255241// =====================================================================
256242
257- FIMULTIBITMAP * DLL_CALLCONV
258- FreeImage_OpenMultiBitmap (FREE_IMAGE_FORMAT fif, const char * filename, BOOL create_new, BOOL read_only, BOOL keep_cache_in_memory, int flags) {
243+ static FIMULTIBITMAP *
244+ OpenMultiBitmapPath (FREE_IMAGE_FORMAT fif, const MultiBitmapPath& filename, BOOL create_new, BOOL read_only, BOOL keep_cache_in_memory, int flags) {
259245
260246 FILE *handle = NULL ;
261247 try {
262248 // sanity check on the parameters
249+ if (filename.empty ()) return NULL ;
263250
264251 if (create_new) {
265252 read_only = FALSE ;
@@ -274,7 +261,7 @@ FreeImage_OpenMultiBitmap(FREE_IMAGE_FORMAT fif, const char *filename, BOOL crea
274261
275262 if (node) {
276263 if (!create_new) {
277- handle = fopen ( filename, " rb " );
264+ handle = filename. open ( false );
278265 if (handle == NULL ) {
279266 return NULL ;
280267 }
@@ -308,8 +295,7 @@ FreeImage_OpenMultiBitmap(FREE_IMAGE_FORMAT fif, const char *filename, BOOL crea
308295 // set up the cache
309296
310297 if (!read_only) {
311- std::string cache_name;
312- ReplaceExtension (cache_name, filename, " ficache" );
298+ MultiBitmapPath cache_name = filename.withExtension (" ficache" );
313299
314300 if (!header->m_cachefile .open (cache_name, keep_cache_in_memory)) {
315301 // an error occured ...
@@ -334,6 +320,28 @@ FreeImage_OpenMultiBitmap(FREE_IMAGE_FORMAT fif, const char *filename, BOOL crea
334320 return NULL ;
335321}
336322
323+ FIMULTIBITMAP * DLL_CALLCONV
324+ FreeImage_OpenMultiBitmap (FREE_IMAGE_FORMAT fif, const char *filename, BOOL create_new, BOOL read_only, BOOL keep_cache_in_memory, int flags) {
325+ try {
326+ return OpenMultiBitmapPath (fif, MultiBitmapPath (filename), create_new, read_only, keep_cache_in_memory, flags);
327+ } catch (std::bad_alloc &) {
328+ return NULL ;
329+ }
330+ }
331+
332+ FIMULTIBITMAP * DLL_CALLCONV
333+ FreeImage_OpenMultiBitmapU (FREE_IMAGE_FORMAT fif, const wchar_t *filename, BOOL create_new, BOOL read_only, BOOL keep_cache_in_memory, int flags) {
334+ #ifdef _WIN32
335+ try {
336+ return OpenMultiBitmapPath (fif, MultiBitmapPath (filename), create_new, read_only, keep_cache_in_memory, flags);
337+ } catch (std::bad_alloc &) {
338+ return NULL ;
339+ }
340+ #else
341+ return NULL ;
342+ #endif
343+ }
344+
337345FIMULTIBITMAP * DLL_CALLCONV
338346FreeImage_OpenMultiBitmapFromHandle (FREE_IMAGE_FORMAT fif, FreeImageIO *io, fi_handle handle, int flags) {
339347 try {
@@ -502,17 +510,15 @@ FreeImage_CloseMultiBitmap(FIMULTIBITMAP *bitmap, int flags) {
502510 try {
503511 // open a temp file
504512
505- std::string spool_name;
506-
507- ReplaceExtension (spool_name, header->m_filename , " fispool" );
513+ MultiBitmapPath spool_name = header->m_filename .withExtension (" fispool" );
508514
509515 // open the spool file and the source file
510516
511- FILE *f = fopen ( spool_name.c_str (), " w+b " );
517+ FILE *f = spool_name.open ( true );
512518
513519 // saves changes
514520 if (f == NULL ) {
515- FreeImage_OutputMessageProc (header->fif , " Failed to open %s, %s" , spool_name. c_str () , strerror (errno));
521+ FreeImage_OutputMessageProc (header->fif , " Failed to open multipage spool file: %s" , strerror (errno));
516522 success = FALSE ;
517523 } else {
518524 success = FreeImage_SaveMultiBitmapToHandle (header->fif , bitmap, &header->io , (fi_handle)f, flags);
@@ -521,7 +527,7 @@ FreeImage_CloseMultiBitmap(FIMULTIBITMAP *bitmap, int flags) {
521527
522528 if (fclose (f) != 0 ) {
523529 success = FALSE ;
524- FreeImage_OutputMessageProc (header->fif , " Failed to close %s, %s" , spool_name. c_str () , strerror (errno));
530+ FreeImage_OutputMessageProc (header->fif , " Failed to close multipage spool file: %s" , strerror (errno));
525531 }
526532 }
527533 if (header->handle ) {
@@ -531,13 +537,13 @@ FreeImage_CloseMultiBitmap(FIMULTIBITMAP *bitmap, int flags) {
531537 // applies changes to the destination file
532538
533539 if (success) {
534- remove ( header->m_filename .c_str () );
535- success = (rename ( spool_name.c_str (), header->m_filename . c_str () ) == 0 ) ? TRUE :FALSE ;
540+ header->m_filename .remove ( );
541+ success = (spool_name.renameTo ( header->m_filename ) == 0 ) ? TRUE :FALSE ;
536542 if (!success) {
537- FreeImage_OutputMessageProc (header->fif , " Failed to rename %s to %s" , spool_name. c_str (), header-> m_filename . c_str ( ));
543+ FreeImage_OutputMessageProc (header->fif , " Failed to rename multipage spool file: %s" , strerror (errno ));
538544 }
539545 } else {
540- remove ( spool_name.c_str () );
546+ spool_name.remove ( );
541547 }
542548 } catch (std::bad_alloc &) {
543549 success = FALSE ;
0 commit comments