FormatStr changed to string

This commit is contained in:
James Turk 2003-11-24 02:02:25 +00:00
parent 7a4ba04e94
commit 6709e83d94
2 changed files with 139 additions and 138 deletions

View File

@ -1,95 +1,95 @@
/*******************************************************************************
This file is Part of the ZEngine Library for 2D game development.
Copyright (C) 2002, 2003 James Turk
Licensed under a BSD-style license.
The maintainer of this library is James Turk (james@conceptofzero.net)
and the home of this Library is http://www.zengine.sourceforge.net
*******************************************************************************/
/*!
\file ZE_Utility.h
\brief Definition file for ZEngine Utilities.
Definition file for ZEngine Utilities which are used throughout the engine and can be used in
conjunction with ZEngine.
<br>$Id: ZE_Utility.h,v 1.9 2003/10/13 21:48:12 cozman Exp $<br>
\author James Turk
**/
#ifndef __ze_utility_h__
#define __ze_utility_h__
#include "ZE_Includes.h"
namespace ZE
{
/*!
\brief Parses a std::string and interprets variable arguments, similar to sprintf.
Takes % identifiers out of fmtstr and parses them, replacing them with cooresponding values
in the variable arguments list. For more detail view stdarg documentation.
\param fmtstr defines format of resulting std::string
\param ... variable number of arguments after fmtstr
\return std::string of parsed and combined std::string
**/
std::string FormatStr(const char *fmtstr, ...);
/*!
\brief Extracts a SDL_RWops memory structure from a zip archive.
Attempts to open a file from within a zipfile and return a SDL_RWops which can be used
to load a resource from memory.
Used internally, generally shouldn't be called by users.
\param zipname Name of zip-format archive to open.
\param filename Name of file within archive to access.
\return On success, pointer to SDL_RWops, on failure, NULL.
\since 0.8.5
**/
SDL_RWops* RWFromZip(std::string zipname, std::string filename);
/*!
\brief Properly free SDL_Surface.
Safely free an SDL_Surface* and set it to NULL.
\param image Image to free and set to NULL.
**/
void FreeImage(SDL_Surface *&image);
#ifdef USE_SDL_MIXER
/*!
\brief Properly free Mix_Chunk.
Safely free a Mix_Chunk* and set it to NULL.
\param chunk Chunk to free and set to NULL.
**/
void FreeSound(Mix_Chunk *&chunk);
/*!
\brief Properly free Mix_Music.
Safely free a Mix_Music* and set it to NULL.
\param music Music to free and set to NULL.
**/
void FreeMusic(Mix_Music *&music);
#endif
#ifdef USE_SDL_TTF
/*!
\brief Properly free TTF_Font.
Safely free a TTF_Font* and set it to NULL.
\param font Font to free and set to NULL.
**/
void FreeFont(TTF_Font *&font);
#endif
}
#endif //__ze_utility_h__
/*******************************************************************************
This file is Part of the ZEngine Library for 2D game development.
Copyright (C) 2002, 2003 James Turk
Licensed under a BSD-style license.
The maintainer of this library is James Turk (james@conceptofzero.net)
and the home of this Library is http://www.zengine.sourceforge.net
*******************************************************************************/
/*!
\file ZE_Utility.h
\brief Definition file for ZEngine Utilities.
Definition file for ZEngine Utilities which are used throughout the engine and can be used in
conjunction with ZEngine.
<br>$Id: ZE_Utility.h,v 1.10 2003/11/24 02:02:25 cozman Exp $<br>
\author James Turk
**/
#ifndef __ze_utility_h__
#define __ze_utility_h__
#include "ZE_Includes.h"
namespace ZE
{
/*!
\brief Parses a std::string and interprets variable arguments, similar to sprintf.
Takes % identifiers out of fmtstr and parses them, replacing them with cooresponding values
in the variable arguments list. For more detail view stdarg documentation.
\param fmtstr defines format of resulting std::string
\param ... variable number of arguments after fmtstr
\return std::string of parsed and combined std::string
**/
std::string FormatStr(const std::string fmtstr, ...);
/*!
\brief Extracts a SDL_RWops memory structure from a zip archive.
Attempts to open a file from within a zipfile and return a SDL_RWops which can be used
to load a resource from memory.
Used internally, generally shouldn't be called by users.
\param zipname Name of zip-format archive to open.
\param filename Name of file within archive to access.
\return On success, pointer to SDL_RWops, on failure, NULL.
\since 0.8.5
**/
SDL_RWops* RWFromZip(std::string zipname, std::string filename);
/*!
\brief Properly free SDL_Surface.
Safely free an SDL_Surface* and set it to NULL.
\param image Image to free and set to NULL.
**/
void FreeImage(SDL_Surface *&image);
#ifdef USE_SDL_MIXER
/*!
\brief Properly free Mix_Chunk.
Safely free a Mix_Chunk* and set it to NULL.
\param chunk Chunk to free and set to NULL.
**/
void FreeSound(Mix_Chunk *&chunk);
/*!
\brief Properly free Mix_Music.
Safely free a Mix_Music* and set it to NULL.
\param music Music to free and set to NULL.
**/
void FreeMusic(Mix_Music *&music);
#endif
#ifdef USE_SDL_TTF
/*!
\brief Properly free TTF_Font.
Safely free a TTF_Font* and set it to NULL.
\param font Font to free and set to NULL.
**/
void FreeFont(TTF_Font *&font);
#endif
}
#endif //__ze_utility_h__

View File

@ -13,7 +13,7 @@
\brief Source file for ZEngine utility functions.
Source file containing open utilities for use inside and alongside ZEngine.
<br>$Id: ZE_Utility.cpp,v 1.13 2003/10/13 21:48:13 cozman Exp $<br>
<br>$Id: ZE_Utility.cpp,v 1.14 2003/11/24 02:07:03 cozman Exp $<br>
\author James Turk
**/
@ -22,57 +22,58 @@
namespace ZE
{
std::string FormatStr(const char *fmtstr, ...)
std::string FormatStr(const std::string fmtstr, ...)
{
char buf[512];
va_list args;
//simply puts args into the buffer using standard parsing rules
va_start(args,fmtstr);
vsprintf(buf, fmtstr, args);
vsprintf(buf, fmtstr.c_str(), args);
va_end(args);
return buf;
}
SDL_RWops* RWFromZip(std::string zipname, std::string filename)
{
unzFile zip = unzOpen(zipname.c_str());
unz_file_info info;
void *buffer;
if(!zip) //failed to open zip
{
//log error
return NULL;
}
//locate the file and open it (last param means case sensitive comparison)
unzLocateFile(zip,filename.c_str(),0);
if(unzOpenCurrentFile(zip) != UNZ_OK) //failed to open file within zip
{
return NULL;
}
//find current file info (we are looking for uncompressed file size)
unzGetCurrentFileInfo(zip,&info,NULL,0,NULL,0,NULL,0);
//create a buffer big enough to hold uncompressed file in memory
buffer = (void*)new char[info.uncompressed_size];
if(!buffer)
{
unzCloseCurrentFile(zip);
unzClose(zip);
//log error (failed to allocate memory?!)
return NULL;
}
//load into memory
unzReadCurrentFile(zip, buffer, info.uncompressed_size);
//close archive
unzCloseCurrentFile(zip);
unzClose(zip);
return SDL_RWFromMem(buffer, info.uncompressed_size); //return buffer in RW form
SDL_RWops* RWFromZip(std::string zipname, std::string filename)
{
unzFile zip = unzOpen(zipname.c_str());
unz_file_info info;
void *buffer;
if(!zip) //failed to open zip
{
//log error
return NULL;
}
//locate the file and open it (last param means case sensitive comparison)
unzLocateFile(zip,filename.c_str(),0);
if(unzOpenCurrentFile(zip) != UNZ_OK) //failed to open file within zip
{
return NULL;
}
//find current file info (we are looking for uncompressed file size)
unzGetCurrentFileInfo(zip,&info,NULL,0,NULL,0,NULL,0);
//create a buffer big enough to hold uncompressed file in memory
buffer = (void*)new char[info.uncompressed_size];
if(!buffer)
{
unzCloseCurrentFile(zip);
unzClose(zip);
//log error (failed to allocate memory?!)
return NULL;
}
//load into memory
unzReadCurrentFile(zip, buffer, info.uncompressed_size);
//close archive
unzCloseCurrentFile(zip);
unzClose(zip);
return SDL_RWFromMem(buffer, info.uncompressed_size); //return buffer in RW form
}
//Each of the Free*s safely frees & NULLs the pointer