src/tools/tLocale.h

Go to the documentation of this file.
00001 /*
00002 
00003 *************************************************************************
00004 
00005 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00006 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00007 
00008 **************************************************************************
00009 
00010 This program is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU General Public License
00012 as published by the Free Software Foundation; either version 2
00013 of the License, or (at your option) any later version.
00014 
00015 This program is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 GNU General Public License for more details.
00019 
00020 You should have received a copy of the GNU General Public License
00021 along with this program; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023 
00024 **************************************************************************
00025 */
00026 
00027 #ifndef ArmageTron_tLocale_H
00028 #define ArmageTron_tLocale_H
00029 
00030 #include "tMemManager.h"
00031 #include "tString.h"
00032 #include "tLinkedList.h"
00033 #include "tError.h"
00034 
00035 class tLocaleItem;
00036 class tOutputItemBase;
00037 
00038 class tLanguage: public tListItem<tLanguage>      // identifies a language
00039 {
00040     tString name;           
00041     mutable tString file;   
00042 public:
00043     tLanguage(const tString& n);
00044 
00045     static tLanguage* FirstLanguage();
00046 
00047     void SetFirstLanguage()  const;  // set this language as the favorite choice
00048     void SetSecondLanguage() const;
00049 
00050     void Load() const; 
00051     void LoadLater( char const * file ) const; 
00052 
00053     static tLanguage* Find(tString const & name); 
00054     static tLanguage* FindStrict(tString const & name); 
00055     static tLanguage* FindSloppy(tString const & name); 
00056 
00057     const tString& Name(){
00058         return name;
00059     }
00060 };
00061 
00062 
00063 // flexible output unit: the output string is generated on the fly from
00064 // the components.
00065 
00066 /* usage: like a normal string:
00067    int i=5;
00068 
00069    tOutput x;
00070    x << "menu_exit";  // lets x print the TEXT ITEM menu_exit from the language files
00071    x << i;            // concatenate i to it
00072    x << tString("menu_exit"); // lets x print the LITERAL string "menu_exit".
00073    x.Literal("menu_exit");   // the same.
00074 
00075    std::cout << x;
00076    // prints Menu Exit5menu_exitmenu_exit
00077 */
00078 
00079 class tOutput{
00080     friend class tOutputItemBase;
00081 
00082     tOutputItemBase *anchor;
00083 
00084     tOutput& operator << (const tOutput &o);
00085 public:
00086     tOutput();
00087     ~tOutput();
00088 
00089     operator const char *() const;   // creates the output string
00090     //  operator tString() const;    // creates the output string
00091     void AddLiteral(const char *);       // adds a language independent string
00092     void AddLocale(const char *);        // adds a language dependant string
00093     void AddSpace();                     // adds a simple space
00094 
00095     // set a template parameter at this position of the output string
00096     tOutput & SetTemplateParameter(int num, const char *parameter);
00097     tOutput & SetTemplateParameter(int num, int         parameter);
00098     tOutput & SetTemplateParameter(int num, float       parameter);
00099 
00100     // delete all elements
00101     void Clear();
00102 
00103     tOutput(const std::string& identifier);
00104     tOutput(const tString& identifier);
00105     tOutput(const char * identifier);
00106     tOutput(const tLocaleItem &locale);
00107 
00108     // convenience template constructors: fill in template parameters with passed value
00109     template< class T1>
00110     tOutput( char const * identifier, T1 const & template1 )
00111             :anchor(NULL)
00112     {
00113         tASSERT( identifier && identifier[0] == '$' );
00114 
00115         SetTemplateParameter(1, template1);
00116 
00117         *this << identifier;
00118     }
00119 
00120     template< class T1, class T2 >
00121     tOutput( char const * identifier, T1 const & template1, T2 const & template2 )
00122             :anchor(NULL)
00123     {
00124         tASSERT( identifier && identifier[0] == '$' );
00125 
00126         SetTemplateParameter(1, template1);
00127         SetTemplateParameter(2, template2);
00128 
00129         *this << identifier;
00130     }
00131 
00132     template< class T1, class T2, class T3 >
00133     tOutput( char const * identifier, T1 const & template1, T2 const & template2, T3 const & template3 )
00134             :anchor(NULL)
00135     {
00136         tASSERT( identifier && identifier[0] == '$' );
00137 
00138         SetTemplateParameter(1, template1);
00139         SetTemplateParameter(2, template2);
00140         SetTemplateParameter(3, template3);
00141 
00142         *this << identifier;
00143     }
00144 
00145     template< class T1, class T2, class T3, class T4 >
00146     tOutput( char const * identifier, T1 const & template1, T2 const & template2, T3 const & template3, T4 const & template4 )
00147             :anchor(NULL)
00148     {
00149         tASSERT( identifier && identifier[0] == '$' );
00150 
00151         SetTemplateParameter(1, template1);
00152         SetTemplateParameter(2, template2);
00153         SetTemplateParameter(3, template3);
00154         SetTemplateParameter(4, template4);
00155 
00156         *this << identifier;
00157     }
00158 
00159     tOutput(const tOutput &o); // copy constructor
00160     tOutput& operator=(const tOutput &o); // copy operator
00161 
00162     void Append(const tOutput &o);
00163 
00164     bool IsEmpty()const {
00165         return !anchor;
00166     }
00167 };
00168 
00169 class tOutputItemBase: public tListItem<tOutputItemBase>
00170 {
00171 public:
00172     tOutputItemBase(tOutput& o);
00173     virtual ~tOutputItemBase();
00174     virtual void Print(tString& target) const = 0;
00175     virtual void Clone(tOutput& o)      const = 0;
00176 };
00177 
00178 
00179 template <class T>class tOutputItem: public tOutputItemBase
00180 {
00181     T element;
00182     public:
00183 tOutputItem(tOutput& o, const T& e): tOutputItemBase(o), element(e){};
00184 virtual void Print(tString& target) const {
00185     target << element;
00186 }
00187 virtual void Clone(tOutput& o)      const {
00188     tNEW(tOutputItem<T>)(o, element);
00189 }
00190 };
00191 
00192 
00193 
00194 // now the print template:
00195 template<class T> tOutput&  operator<< (tOutput& o, const T& e)
00196 {
00197     tNEW(tOutputItem<T>)(o, e);
00198     return o;
00199 }
00200 
00201 // and a special implementation for the locales and strings:
00202 tOutput& operator << (tOutput &o, const char *locale);
00203 tOutput& operator << (tOutput &o, char *locale);
00204 
00205 // output operators
00206 std::ostream& operator<< (std::ostream& s, const tOutput& o);
00207 //std::stringstream& operator<< (std::stringstream& s, const tOutput& o);
00208 tString& operator<< (tString& s, const tOutput& o);
00209 
00210 
00211 
00212 // interface class for locale items
00213 class tLocale
00214 {
00215 public:
00216     static const tLocaleItem& Find(const char* id);
00217     static void               Load(const char* filename);
00218     static void               Clear();           // clear all locale data on program exit
00219 };
00220 
00221 std::ostream& operator<< (std::ostream& s, const tLocaleItem& o);
00222 //std::stringstream& operator<<(std::stringstream& s, const tLocaleItem &t);
00223 tString& operator<< (tString& s, const tLocaleItem& o);
00224 
00225 
00226 #endif

Generated on Sat Mar 15 22:56:00 2008 for Armagetron Advanced by  doxygen 1.5.4