lluistring.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @file lluistring.h
  3. * @author: Steve Bennetts
  4. * @brief A fancy wrapper for std::string supporting argument substitutions.
  5. *
  6. * $LicenseInfo:firstyear=2006&license=viewergpl$
  7. *
  8. * Copyright (c) 2006-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LL_LLUISTRING_H
  34. #define LL_LLUISTRING_H
  35. #include "hbfastset.h"
  36. #include "llstring.h"
  37. // Use this class to store translated text that may have arguments
  38. // e.g. "Welcome [USERNAME] to [SECONDLIFE]!"
  39. // Adding or changing an argument will update the result string, preserving the
  40. // origianl. Thus, subsequent changes to arguments or even the original string
  41. // will produce the correct result
  42. // Example Usage:
  43. // LLUIString mMessage("Welcome [USERNAME] to [SECONDLIFE]!");
  44. // mMessage.setArg("[USERNAME]", "Steve");
  45. // mMessage.setArg("[SECONDLIFE]", "Second Life");
  46. // llinfos << mMessage.getString() << llendl; // outputs "Welcome Steve to Second Life"
  47. // mMessage.setArg("[USERNAME]", "Joe");
  48. // llinfos << mMessage.getString() << llendl; // outputs "Welcome Joe to Second Life"
  49. // mMessage = "Recepci￳n a la [SECONDLIFE] [USERNAME]"
  50. // mMessage.setArg("[SECONDLIFE]", "Segunda Vida");
  51. // llinfos << mMessage.getString() << llendl; // outputs "Recepci￳n a la Segunda Vida Joe"
  52. // Implementation note:
  53. // Attempting to have operator[](const std::string& s) return mArgs[s] fails
  54. // because we have to call format() after the assignment happens.
  55. class LLUIString
  56. {
  57. public:
  58. // All these functions perform appropriate argument substitution and modify
  59. // mOrig where appropriate.
  60. LLUIString();
  61. LLUIString(const std::string& instring,
  62. const LLStringUtil::format_map_t& args);
  63. LLUIString(const std::string& instring);
  64. ~LLUIString();
  65. void assign(const std::string& instring);
  66. LLUIString& operator=(const std::string& s) { assign(s); return *this; }
  67. void setArgList(const LLStringUtil::format_map_t& args);
  68. LL_INLINE void setArgs(const LLStringUtil::format_map_t& args)
  69. {
  70. setArgList(args);
  71. }
  72. void setArgs(const class LLSD& sd);
  73. void setArg(const std::string& key, const std::string& replacement);
  74. // Accessors
  75. LL_INLINE const std::string& getString() const { return mResult; }
  76. LL_INLINE operator std::string() const { return mResult; }
  77. LL_INLINE const LLWString& getWString() const { return mWResult; }
  78. LL_INLINE operator LLWString() const { return mWResult; }
  79. LL_INLINE bool empty() const { return mWResult.empty(); }
  80. LL_INLINE S32 length() const { return mWResult.size(); }
  81. void clear();
  82. LL_INLINE void clearArgs() { mArgs.clear(); }
  83. // These utility functions are included for text editing. They do not
  84. // affect mOrig and do not perform argument substitution.
  85. void truncate(S32 maxchars);
  86. void erase(S32 charidx, S32 len);
  87. void insert(S32 charidx, const LLWString& wchars);
  88. void replace(S32 charidx, llwchar wc);
  89. // Currency "translation" functions for OpenSim grids:
  90. static void setGridCurrency(const std::string& str);
  91. static void setRealCurrency(const std::string& str);
  92. static void translateCurrency(std::string& text);
  93. // To be called once, after grid and real currency symbols have been set.
  94. // Called in indra/newview/llstartup.cpp after login to the grid.
  95. static void translatePendingCurrency();
  96. private:
  97. void format();
  98. public:
  99. static const LLStringUtil::format_map_t sNullArgs;
  100. private:
  101. std::string mOrig;
  102. std::string mResult;
  103. LLWString mWResult; // For displaying
  104. LLStringUtil::format_map_t mArgs;
  105. static bool sCurrencyKnown;
  106. static std::string sGridCurrency;
  107. static std::string sRealCurrency;
  108. typedef fast_hset<LLUIString*> pending_currency_t;
  109. static pending_currency_t sPendingCurrencyUIStrings;
  110. };
  111. #endif // LL_LLUISTRING_H