lang.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           lang.cpp  -  description
00003                              -------------------
00004     begin                : Thu Dec 9 2004
00005     copyright            : (C) 2004 by VooDooMan
00006     email                : vdmfun@hotmail.com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010 
00011 VooDoo cIRCle - an IRC (ro)bot
00012 Copyright (C) 2004 by Marian VooDooMan Meravy (vdmfun@hotmail.com)
00013 
00014 This program is free software; you can redistribute it and/or
00015 modify it under the terms of the GNU General Public License
00016 as published by the Free Software Foundation; either version 2
00017 of the License, or (at your option) any later version.
00018 
00019 This program is distributed in the hope that it will be useful,
00020 but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License
00025 along with this program; if not, write to the Free Software
00026 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028 ****************************************************************************/
00029 
00030 /*!
00031     \file
00032     \brief Supports multiple languages
00033 */
00034 
00035 //---------------------------------------------------------------------------
00036 
00037 #include <string>
00038 #include <stdlib.h>
00039 #include <vector>
00040 #include <stdio.h>
00041 #include "log.h"
00042 
00043 #pragma hdrstop
00044 
00045 #include "lang.h"
00046 
00047 #include "params.h"
00048 
00049 //---------------------------------------------------------------------------
00050 #ifdef __BORLANDC__
00051 #pragma package(smart_init)
00052 #endif
00053 
00054 #ifdef _WIN32
00055 #   define FILE_SLASH "\\"
00056 #else
00057 #   define FILE_SLASH "/"
00058 #endif
00059 
00060 #ifdef _MSC_VER
00061 #   define __FUNC__ __FUNCTION__
00062 #endif
00063 
00064 #ifdef __GNUC__
00065 #define __FUNC__ "<unknown>"
00066 #endif
00067 
00068 using namespace std;
00069 
00070 /*!
00071     \brief Stores language entry (string in specific language) from lang/lang\#\#.txt
00072     \author VooDooMan
00073     \version 1
00074     \date 2004
00075 */
00076 struct s_lang {
00077     int id;         //!< ID of message
00078     string lang;    //!< Language-identification string
00079     string text;    //!< Text of message in specific language
00080 
00081     s_lang()
00082     {
00083         id=0;
00084         lang="";
00085         text="";
00086     }
00087 };
00088 
00089 vector<s_lang> lang01;  //!< If lang01_init == true: stores language entries from file lang/lang01.txt
00090 bool lang01_init=false; //!< Set to true if lang01 contains entries (had been initialized)
00091 
00092 /*!
00093     \brief Initializes lang01 array from file lang/lang01.txt and sets lang01_init to true
00094     \author VooDooMan
00095     \version 1
00096     \date 2004
00097     \param file_name File name (should be "lang/lang01.txt")
00098     \return Returns an empty string
00099 */
00100 string lang_01_init(string file_name)
00101 {
00102     lang01.clear();
00103 
00104     FILE* f=fopen(file_name.c_str(),"r");
00105     if(!f)
00106         return "";
00107     char ln[10240+1];
00108     while(!feof(f)) {
00109         if(fgets(ln,1024*10-1,f)==NULL)
00110             break;
00111         ln[10240]=0;
00112         if(ln[strlen(ln)-1]=='\r')
00113             ln[strlen(ln)-1]=0;
00114         if(ln[strlen(ln)-1]=='\n')
00115             ln[strlen(ln)-1]=0;
00116         if(ln[strlen(ln)-1]=='\r')
00117             ln[strlen(ln)-1]=0;
00118         if(ln[strlen(ln)-1]=='\n')
00119             ln[strlen(ln)-1]=0;
00120         string id_, lang_, text_;
00121         int pos=0;
00122         for(unsigned int i1=0; i1<strlen(ln); i1++) {
00123             if(pos==0 && ln[i1]=='_') {
00124                 pos++;
00125                 continue;
00126             }
00127             if(pos==1 && ln[i1]=='=') {
00128                 pos++;
00129                 continue;
00130             }
00131             if(pos==0)
00132                 id_+=ln[i1];
00133             if(pos==1)
00134                 lang_+=ln[i1];
00135             if(pos==2)
00136                 text_+=ln[i1];
00137         }
00138         s_lang s;
00139         s.id=atol(id_.c_str());
00140         s.lang=lang_;
00141         s.text=text_;
00142         if(s.lang.empty() || s.text.empty())
00143             continue;
00144 
00145         vector<s_lang>::iterator i2;
00146         for(i2=lang01.begin(); i2!=lang01.end(); i2++)
00147             if((*i2).id==s.id && !(*i2).lang.compare(s.lang)) {
00148                 char tmp[1024];
00149                 sprintf(tmp,"%s%d%s%s%s%d","in file " __FILE__ " in function " __FUNC__ " occurred warning: duplicite language string entry for: file_id=",1," lang=",s.lang.c_str()," id=",s.id);
00150                 log_debug(tmp);
00151             }
00152 
00153         lang01.push_back(s);
00154     }
00155     fclose(f);
00156     return "";
00157 }
00158 
00159 /*!
00160     \brief Gets language-specific string, and initializes an array if needed
00161     \author VooDooMan
00162     \version 1
00163     \date 2004
00164     \param file_id ID of language file ("lang/langID.txt")
00165     \param lang Language identification string
00166     \param id ID of message
00167     \return Returns language-specific string, if not found, returns an empty string
00168 */
00169 string lang_get_string(int file_id, string lang, int id)
00170 {
00171     if(file_id==1 && !lang01_init) {
00172         lang_01_init("lang" FILE_SLASH "lang01.txt");
00173         lang01_init=true;
00174     }
00175     if(file_id==1) {
00176         vector<s_lang>::iterator i;
00177         for(i=lang01.begin(); i!=lang01.end(); i++)
00178             if((*i).id==id && !(*i).lang.compare(lang))
00179                 return (*i).text;
00180         char tmp[1024];
00181         sprintf(tmp,"%s%d%s%s%s%d%s","in file " __FILE__ " in function " __FUNC__ " occurred error: cannot get language string: file_id=",file_id," lang=",lang.c_str()," id=",id," - trying default language \"en\"");
00182         log_debug(tmp);
00183         // not found? try "en" as lang
00184         lang="en";
00185         for(i=lang01.begin(); i!=lang01.end(); i++)
00186             if((*i).id==id && !(*i).lang.compare(lang))
00187                 return (*i).text;
00188     }
00189     char tmp[1024];
00190     sprintf(tmp,"%s%d%s%s%s%d","in file " __FILE__ " in function " __FUNC__ " occurred error: cannot get language string: file_id=",file_id," lang=",lang.c_str()," id=",id);
00191     log_debug(tmp);
00192     return "<?>";
00193 }
00194 
00195 /*!
00196     \brief Routine for "%s" substitution
00197     \author VooDooMan
00198     \version 2
00199     \date 2004, 2005
00200     \param str Message containing at least one "%s" to substitute
00201     \param substr Substring, wich will be inserted instead of FIRST "%s" found in str
00202     \return Returns resulting string
00203 */
00204 string lang_subst(string str, string substr)
00205 {
00206     string res;
00207     int esc=0;
00208     bool got=false;
00209     for(unsigned int i1=0; i1<str.length(); i1++) {
00210         if(esc==0 && str[i1]=='%') {
00211             esc++;
00212             continue;
00213         }
00214         if(esc==1 && str[i1]=='s' /*&& substr.compare("")*/) {
00215             if(!got) {
00216                 got=true;
00217                 res+=substr;
00218                 substr="";
00219             } else {
00220                 res+="%s";
00221             }
00222             esc=0;
00223             continue;
00224         }
00225         if(esc==1 && str[i1]=='s') {
00226             res+="%s";
00227             esc=0;
00228             continue;
00229         }
00230         if((esc==1 || esc==2) && str[i1]=='%') {
00231             res+="%";
00232             continue;
00233         }
00234         res+=str[i1];
00235     }
00236 
00237     if(!got) {
00238         while(str.find("\r",0)!=string::npos)
00239             str.erase(str.find("\r",0),1);
00240         while(str.find("\n",0)!=string::npos)
00241             str.erase(str.find("\n",0),1);
00242 
00243         while(substr.find("\r",0)!=string::npos)
00244             substr.erase(substr.find("\r",0),1);
00245         while(substr.find("\n",0)!=string::npos)
00246             substr.erase(substr.find("\n",0),1);
00247 
00248         string log="Warning: In function " __FUNC__ " I was unable to find \"%s\" sequence to substitute it with sub-string. (following strings will have \\r and \\n stripped off). String was: \"";
00249         log+=str;
00250         log+="\", and sub-string as substituent was: \"";
00251         log+=substr;
00252         log+="\"";
00253         log_debug(log.c_str());
00254     }
00255 
00256     return res;
00257 }
00258 
00259 /*!
00260     \brief Enumerates available languages
00261     \author VooDooMan
00262     \version 1
00263     \date 2004
00264     \param file_id ID of language file ("lang/langID.txt")
00265     \param langs Returns array of available languages
00266 */
00267 void lang_get_langs(int file_id, vector<string>& langs)
00268 {
00269     langs.clear();
00270 
00271     if(file_id==1 && !lang01_init) {
00272         lang_01_init("lang" FILE_SLASH "lang01.txt");
00273         lang01_init=true;
00274     }
00275     if(file_id==1) {
00276         vector<s_lang>::iterator i;
00277         for(i=lang01.begin(); i!=lang01.end(); i++) {
00278             vector<string>::iterator i1;
00279             bool got=false;
00280             for(i1=langs.begin(); i1!=langs.end(); i1++)
00281                 if(!(*i1).compare((*i).lang)) {
00282                     got=true;
00283                     break;
00284                 }
00285             if(!got)
00286                 langs.push_back((*i).lang);
00287         }
00288     }
00289 }
00290 
00291 /*!
00292     \brief Rehashses the language files
00293     \author VooDooMan
00294     \version 1
00295     \date 2004
00296 */
00297 void lang_rehash()
00298 {
00299     lang01_init=false;
00300     lang_01_init("lang" FILE_SLASH "lang01.txt");
00301     lang01_init=true;
00302 }
00303 

Generated on Sun Jul 10 03:22:56 2005 for VooDoo cIRCle by doxygen 1.4.3

Hosted by SourceForge.net Logo