lang.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //////////////////////////////////////////////////////////////////////////
  23. /// \file lang.h
  24. /// \brief Header for language support
  25. //////////////////////////////////////////////////////////////////////////
  26. #include "sim/simBase.h"
  27. #include "collection/vector.h"
  28. #ifndef _LANG_H_
  29. #define _LANG_H_
  30. #define LANG_INVALID_ID 0xffffffff ///!< Invalid ID. Used for returning failure
  31. //////////////////////////////////////////////////////////////////////////
  32. /// \brief Class for working with language files
  33. //////////////////////////////////////////////////////////////////////////
  34. class LangFile
  35. {
  36. protected:
  37. Vector<UTF8 *> mStringTable;
  38. UTF8 * mLangName;
  39. UTF8 * mLangFile;
  40. void freeTable();
  41. public:
  42. LangFile(const UTF8 *langName = NULL);
  43. virtual ~LangFile();
  44. bool load(const UTF8 *filename);
  45. bool save(const UTF8 *filename);
  46. bool load(Stream *s);
  47. bool save(Stream *s);
  48. const UTF8 * getString(U32 id);
  49. U32 addString(const UTF8 *str);
  50. // [tom, 4/22/2005] setString() added to help the language compiler a bit
  51. void setString(U32 id, const UTF8 *str);
  52. void setLangName(const UTF8 *newName);
  53. const UTF8 *getLangName(void) { return mLangName; }
  54. const UTF8 *getLangFile(void) { return mLangFile; }
  55. void setLangFile(const UTF8 *langFile);
  56. bool activateLanguage(void);
  57. void deactivateLanguage(void);
  58. bool isLoaded(void) { return mStringTable.size() > 0; }
  59. S32 getNumStrings(void) { return mStringTable.size(); }
  60. };
  61. //////////////////////////////////////////////////////////////////////////
  62. /// \brief Language file table
  63. //////////////////////////////////////////////////////////////////////////
  64. class LangTable : public SimObject
  65. {
  66. typedef SimObject Parent;
  67. protected:
  68. Vector<LangFile *> mLangTable;
  69. S32 mDefaultLang;
  70. S32 mCurrentLang;
  71. public:
  72. DECLARE_CONOBJECT(LangTable);
  73. LangTable();
  74. virtual ~LangTable();
  75. S32 addLanguage(LangFile *lang, const UTF8 *name = NULL);
  76. S32 addLanguage(const UTF8 *filename, const UTF8 *name = NULL);
  77. void setDefaultLanguage(S32 langid);
  78. void setCurrentLanguage(S32 langid);
  79. S32 getCurrentLanguage(void) { return mCurrentLang; }
  80. const UTF8 * getLangName(const S32 langid) const
  81. {
  82. if(langid < 0 || langid >= mLangTable.size())
  83. return NULL;
  84. return mLangTable[langid]->getLangName();
  85. }
  86. const S32 getNumLang(void) const { return mLangTable.size(); }
  87. const UTF8 * getString(const U32 id) const;
  88. const U32 getStringLength(const U32 id) const;
  89. };
  90. extern UTF8 *sanitiseVarName(const UTF8 *varName, UTF8 *buffer, U32 bufsize);
  91. extern UTF8 *getCurrentModVarName(UTF8 *buffer, U32 bufsize);
  92. extern const LangTable *getCurrentModLangTable();
  93. extern const LangTable *getModLangTable(const UTF8 *mod);
  94. #endif // _LANG_H_