src/tool/i18n.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Function used to format string.
00020  * Eg. : Format("Hello %s", "world") returns "Hello World".
00021  *****************************************************************************/
00022 
00023 #include "i18n.h"
00024 #include <string>
00025 #include <stdio.h>
00026 #include <stdarg.h>
00027 #include "debug.h"
00028 #include "../config.h"
00029 #include "../game/config.h"
00030 
00031 // Package is defined by autotools
00032 #ifdef WIN32
00033 #define PACKAGE "Wormux"
00034 #endif
00035 
00036 #define GETTEXT_DOMAIN PACKAGE
00037 
00038 std::string Format(const char *format, ...){
00039         const int bufferSize = 256;
00040         char buffer[bufferSize];
00041         va_list argp;
00042         std::string result;
00043 
00044         va_start(argp, format);
00045 
00046         int size = vsnprintf(buffer, bufferSize, format, argp);
00047 
00048         if( size < 0 )
00049                 Error( "Error formating string...");
00050 
00051         if( size < bufferSize)
00052                 result = std::string(buffer);
00053         else{
00054                 char *bigBuffer = (char *)malloc( (size + 1) * sizeof(char) );
00055                 if( bigBuffer == NULL)
00056                         Error( "Out of memory !");
00057 
00058                 size = vsnprintf(bigBuffer, size + 1, format, argp);
00059                 if( size < 0 )
00060                         Error( "Error formating string...");
00061 
00062                 result = std::string(bigBuffer);
00063                 free(bigBuffer);
00064         }
00065 
00066         va_end(argp);
00067 
00068         return result;
00069 }
00070 
00071 void I18N_SetDir(const std::string &dir){
00072   bindtextdomain(GETTEXT_DOMAIN, dir.c_str());
00073   bind_textdomain_codeset (GETTEXT_DOMAIN, "UTF-8");
00074 }
00075 
00076 void InitI18N(const std::string &dir){
00077   setlocale (LC_ALL, "");
00078   I18N_SetDir (dir);
00079   textdomain(GETTEXT_DOMAIN);
00080 }
00081 

Generated on Mon Jan 1 13:10:59 2007 for Wormux by  doxygen 1.4.7