lluistring.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * @file lluistring.cpp
  3. * @brief LLUIString implementation.
  4. *
  5. * $LicenseInfo:firstyear=2006&license=viewergpl$
  6. *
  7. * Copyright (c) 2006-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "lluistring.h"
  34. #include "llsd.h"
  35. const LLStringUtil::format_map_t LLUIString::sNullArgs;
  36. // Statics
  37. bool LLUIString::sCurrencyKnown = false;
  38. std::string LLUIString::sGridCurrency;
  39. std::string LLUIString::sRealCurrency;
  40. LLUIString::pending_currency_t LLUIString::sPendingCurrencyUIStrings;
  41. LLUIString::LLUIString()
  42. : mOrig(),
  43. mArgs()
  44. {
  45. }
  46. LLUIString::LLUIString(const std::string& instring,
  47. const LLStringUtil::format_map_t& args)
  48. : mArgs(args)
  49. {
  50. assign(instring);
  51. }
  52. LLUIString::LLUIString(const std::string& instring)
  53. : mArgs()
  54. {
  55. assign(instring);
  56. }
  57. LLUIString::~LLUIString()
  58. {
  59. if (!sPendingCurrencyUIStrings.empty())
  60. {
  61. // In case an UI string containing pending currency translations gets
  62. // destroyed before we perform the said translation after login...
  63. sPendingCurrencyUIStrings.erase(this);
  64. }
  65. }
  66. void LLUIString::assign(const std::string& s)
  67. {
  68. mOrig = s;
  69. if (sCurrencyKnown)
  70. {
  71. // in-lined (instead of calling translateCurrency()) for speed
  72. if (!sGridCurrency.empty())
  73. {
  74. LLStringUtil::replaceString(mOrig, "L$", sGridCurrency);
  75. }
  76. if (!sRealCurrency.empty())
  77. {
  78. LLStringUtil::replaceString(mOrig, "US$", sRealCurrency);
  79. }
  80. }
  81. else if (mOrig.find("L$") != std::string::npos ||
  82. mOrig.find("US$") != std::string::npos)
  83. {
  84. sPendingCurrencyUIStrings.insert(this);
  85. }
  86. format();
  87. }
  88. void LLUIString::setArgList(const LLStringUtil::format_map_t& args)
  89. {
  90. mArgs = args;
  91. format();
  92. }
  93. void LLUIString::setArgs(const LLSD& sd)
  94. {
  95. if (sd.isMap())
  96. {
  97. for (LLSD::map_const_iterator sd_it = sd.beginMap(), end = sd.endMap();
  98. sd_it != end; ++sd_it)
  99. {
  100. setArg(sd_it->first, sd_it->second.asString());
  101. }
  102. format();
  103. }
  104. }
  105. void LLUIString::setArg(const std::string& key, const std::string& replacement)
  106. {
  107. mArgs[key] = replacement;
  108. format();
  109. }
  110. void LLUIString::truncate(S32 maxchars)
  111. {
  112. if (maxchars >= 0 && (size_t)maxchars < mWResult.size())
  113. {
  114. LLWStringUtil::truncate(mWResult, maxchars);
  115. mResult = wstring_to_utf8str(mWResult);
  116. }
  117. }
  118. void LLUIString::erase(S32 charidx, S32 len)
  119. {
  120. if (charidx >= 0 && (size_t)charidx < mWResult.size())
  121. {
  122. mWResult.erase(charidx, len);
  123. mResult = wstring_to_utf8str(mWResult);
  124. }
  125. }
  126. void LLUIString::insert(S32 charidx, const LLWString& wchars)
  127. {
  128. if (charidx >= 0 && (size_t)charidx <= mWResult.size())
  129. {
  130. mWResult.insert(charidx, wchars);
  131. mResult = wstring_to_utf8str(mWResult);
  132. }
  133. }
  134. void LLUIString::replace(S32 charidx, llwchar wc)
  135. {
  136. if (charidx >= 0 && (size_t)charidx < mWResult.size())
  137. {
  138. mWResult[charidx] = wc;
  139. mResult = wstring_to_utf8str(mWResult);
  140. }
  141. }
  142. void LLUIString::clear()
  143. {
  144. // Keep Args
  145. mOrig.clear();
  146. mResult.clear();
  147. mWResult.clear();
  148. }
  149. void LLUIString::format()
  150. {
  151. if (mOrig.empty())
  152. {
  153. mResult.clear();
  154. mWResult.clear();
  155. return;
  156. }
  157. mResult = mOrig;
  158. if (!mArgs.empty())
  159. {
  160. LLStringUtil::format(mResult, mArgs);
  161. }
  162. mWResult = utf8str_to_wstring(mResult);
  163. }
  164. //static
  165. void LLUIString::setGridCurrency(const std::string& str)
  166. {
  167. if (str != "L$")
  168. {
  169. sGridCurrency = str;
  170. }
  171. else
  172. {
  173. sGridCurrency.clear();
  174. }
  175. }
  176. //static
  177. void LLUIString::setRealCurrency(const std::string& str)
  178. {
  179. if (str != "US$")
  180. {
  181. sRealCurrency = str;
  182. }
  183. else
  184. {
  185. sRealCurrency.clear();
  186. }
  187. }
  188. //static
  189. void LLUIString::translateCurrency(std::string& text)
  190. {
  191. if (!sGridCurrency.empty())
  192. {
  193. LLStringUtil::replaceString(text, "L$", sGridCurrency);
  194. }
  195. if (!sRealCurrency.empty())
  196. {
  197. LLStringUtil::replaceString(text, "US$", sRealCurrency);
  198. }
  199. }
  200. //static
  201. void LLUIString::translatePendingCurrency()
  202. {
  203. sCurrencyKnown = true;
  204. for (pending_currency_t::iterator it = sPendingCurrencyUIStrings.begin(),
  205. end = sPendingCurrencyUIStrings.end();
  206. it != end; ++it)
  207. {
  208. LLUIString* uistr = *it;
  209. translateCurrency(uistr->mOrig);
  210. uistr->format();
  211. }
  212. sPendingCurrencyUIStrings.clear();
  213. }