lang.cpp File Reference


Detailed Description

Supports multiple languages.

Definition in file lang.cpp.

#include <string>
#include <stdlib.h>
#include <vector>
#include <stdio.h>
#include "log.h"
#include "lang.h"
#include "params.h"

Include dependency graph for lang.cpp:

Go to the source code of this file.

Classes

struct  s_lang
 Stores language entry (string in specific language) from lang/lang##.txt. More...

Defines

#define FILE_SLASH   "/"

Functions

string lang_01_init (string file_name)
 Initializes lang01 array from file lang/lang01.txt and sets lang01_init to true.
void lang_get_langs (int file_id, vector< string > &langs)
 Enumerates available languages.
string lang_get_string (int file_id, string lang, int id)
 Gets language-specific string, and initializes an array if needed.
void lang_rehash ()
 Rehashses the language files.
string lang_subst (string str, string substr)
 Routine for "%s" substitution.

Variables

vector< s_langlang01
 If lang01_init == true: stores language entries from file lang/lang01.txt.
bool lang01_init = false
 Set to true if lang01 contains entries (had been initialized).


Define Documentation

#define FILE_SLASH   "/"
 

Definition at line 57 of file lang.cpp.


Function Documentation

string lang_01_init string  file_name  ) 
 

Initializes lang01 array from file lang/lang01.txt and sets lang01_init to true.

Author:
VooDooMan
Version:
1
Date:
2004
Parameters:
file_name File name (should be "lang/lang01.txt")
Returns:
Returns an empty string

Definition at line 100 of file lang.cpp.

References s_lang::id, s_lang::lang, lang01, log_debug(), and s_lang::text.

Referenced by lang_get_langs(), lang_get_string(), and lang_rehash().

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 }

Here is the call graph for this function:

void lang_get_langs int  file_id,
vector< string > &  langs
 

Enumerates available languages.

Author:
VooDooMan
Version:
1
Date:
2004
Parameters:
file_id ID of language file ("lang/langID.txt")
langs Returns array of available languages

Definition at line 267 of file lang.cpp.

References FILE_SLASH, lang01, lang01_init, and lang_01_init().

Referenced by dcc_loop_language().

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 }

Here is the call graph for this function:

string lang_get_string int  file_id,
string  lang,
int  id
 

Gets language-specific string, and initializes an array if needed.

Author:
VooDooMan
Version:
1
Date:
2004
Parameters:
file_id ID of language file ("lang/langID.txt")
lang Language identification string
id ID of message
Returns:
Returns language-specific string, if not found, returns an empty string

Definition at line 169 of file lang.cpp.

References FILE_SLASH, lang01, lang01_init, lang_01_init(), and log_debug().

Referenced by botnet_showbots(), dcc_apply(), dcc_broadcast(), dcc_check_for_filesystem(), dcc_check_limit(), dcc_loop(), dcc_loop_edit_chan(), dcc_loop_edit_dynbans(), dcc_loop_edit_proc(), dcc_loop_edit_rproc(), dcc_loop_edit_user1(), dcc_loop_edit_user2(), dcc_loop_filesystem(), dcc_loop_filesystem_disp_rights(), dcc_loop_language(), dcc_loop_msg(), dcc_loop_new_user(), dcc_loop_private(), dcc_loop_replication(), dcc_loop_terminator(), dcc_notify(), dcc_rehashed(), dcc_send_file(), dcc_upgrade(), dcc_upgrade_bot_join(), dcc_upgrade_bot_mode(), dcc_upgrade_bot_nick(), dcc_user_pong(), filesys_build_getfile_message(), filesys_dcc_check_for_notifies(), irc_check_for_filesystem(), irc_loop_process_input(), irc_quoted_callback(), logic_partyline_whois(), logic_rehash(), logic_rollback(), and stats_display().

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 }

Here is the call graph for this function:

void lang_rehash  ) 
 

Rehashses the language files.

Author:
VooDooMan
Version:
1
Date:
2004

Definition at line 297 of file lang.cpp.

References FILE_SLASH, lang01_init, and lang_01_init().

Referenced by dcc_loop().

00298 {
00299     lang01_init=false;
00300     lang_01_init("lang" FILE_SLASH "lang01.txt");
00301     lang01_init=true;
00302 }

Here is the call graph for this function:

string lang_subst string  str,
string  substr
 

Routine for "%s" substitution.

Author:
VooDooMan
Version:
2
Date:
2004, 2005
Parameters:
str Message containing at least one "%s" to substitute
substr Substring, wich will be inserted instead of FIRST "%s" found in str
Returns:
Returns resulting string

Definition at line 204 of file lang.cpp.

References log_debug().

Referenced by dcc_apply(), dcc_broadcast(), dcc_loop(), dcc_loop_edit_proc(), dcc_loop_edit_rproc(), dcc_loop_edit_user1(), dcc_loop_edit_user2(), dcc_loop_filesystem(), dcc_loop_filesystem_disp_rights(), dcc_loop_language(), dcc_loop_new_user(), dcc_rehashed(), dcc_user_pong(), and irc_quoted_callback().

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 }

Here is the call graph for this function:


Variable Documentation

vector<s_lang> lang01
 

If lang01_init == true: stores language entries from file lang/lang01.txt.

Definition at line 89 of file lang.cpp.

Referenced by lang_01_init(), lang_get_langs(), and lang_get_string().

bool lang01_init = false
 

Set to true if lang01 contains entries (had been initialized).

Definition at line 90 of file lang.cpp.

Referenced by lang_get_langs(), lang_get_string(), and lang_rehash().


Generated on Sun Jul 10 04:46:32 2005 for VooDoo cIRCle by doxygen 1.4.3

Hosted by SourceForge.net Logo