/indra/llui/lltextvalidate.cpp

https://github.com/HyangZhao/NaCl-main · C++ · 296 lines · 228 code · 27 blank · 41 comment · 61 complexity · 24cadd98abae38dbd31eebb8f2dfa24b MD5 · raw file

  1. /**
  2. * @file lltextvalidate.cpp
  3. * @brief Text validation helper functions
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. // Text editor widget to let users enter a single line.
  27. #include "linden_common.h"
  28. #include "lltextvalidate.h"
  29. #include "llresmgr.h" // for LLLocale
  30. namespace LLTextValidate
  31. {
  32. void ValidateTextNamedFuncs::declareValues()
  33. {
  34. declare("ascii", validateASCII);
  35. declare("float", validateFloat);
  36. declare("int", validateInt);
  37. declare("positive_s32", validatePositiveS32);
  38. declare("non_negative_s32", validateNonNegativeS32);
  39. declare("alpha_num", validateAlphaNum);
  40. declare("alpha_num_space", validateAlphaNumSpace);
  41. declare("ascii_printable_no_pipe", validateASCIIPrintableNoPipe);
  42. declare("ascii_printable_no_space", validateASCIIPrintableNoSpace);
  43. }
  44. // Limits what characters can be used to [1234567890.-] with [-] only valid in the first position.
  45. // Does NOT ensure that the string is a well-formed number--that's the job of post-validation--for
  46. // the simple reasons that intermediate states may be invalid even if the final result is valid.
  47. //
  48. bool validateFloat(const LLWString &str)
  49. {
  50. LLLocale locale(LLLocale::USER_LOCALE);
  51. bool success = TRUE;
  52. LLWString trimmed = str;
  53. LLWStringUtil::trim(trimmed);
  54. S32 len = trimmed.length();
  55. if( 0 < len )
  56. {
  57. // May be a comma or period, depending on the locale
  58. llwchar decimal_point = (llwchar)LLResMgr::getInstance()->getDecimalPoint();
  59. S32 i = 0;
  60. // First character can be a negative sign
  61. if( '-' == trimmed[0] )
  62. {
  63. i++;
  64. }
  65. for( ; i < len; i++ )
  66. {
  67. if( (decimal_point != trimmed[i] ) && !LLStringOps::isDigit( trimmed[i] ) )
  68. {
  69. success = FALSE;
  70. break;
  71. }
  72. }
  73. }
  74. return success;
  75. }
  76. // Limits what characters can be used to [1234567890-] with [-] only valid in the first position.
  77. // Does NOT ensure that the string is a well-formed number--that's the job of post-validation--for
  78. // the simple reasons that intermediate states may be invalid even if the final result is valid.
  79. //
  80. bool validateInt(const LLWString &str)
  81. {
  82. LLLocale locale(LLLocale::USER_LOCALE);
  83. bool success = TRUE;
  84. LLWString trimmed = str;
  85. LLWStringUtil::trim(trimmed);
  86. S32 len = trimmed.length();
  87. if( 0 < len )
  88. {
  89. S32 i = 0;
  90. // First character can be a negative sign
  91. if( '-' == trimmed[0] )
  92. {
  93. i++;
  94. }
  95. for( ; i < len; i++ )
  96. {
  97. if( !LLStringOps::isDigit( trimmed[i] ) )
  98. {
  99. success = FALSE;
  100. break;
  101. }
  102. }
  103. }
  104. return success;
  105. }
  106. bool validatePositiveS32(const LLWString &str)
  107. {
  108. LLLocale locale(LLLocale::USER_LOCALE);
  109. LLWString trimmed = str;
  110. LLWStringUtil::trim(trimmed);
  111. S32 len = trimmed.length();
  112. bool success = TRUE;
  113. if(0 < len)
  114. {
  115. if(('-' == trimmed[0]) || ('0' == trimmed[0]))
  116. {
  117. success = FALSE;
  118. }
  119. S32 i = 0;
  120. while(success && (i < len))
  121. {
  122. if(!LLStringOps::isDigit(trimmed[i++]))
  123. {
  124. success = FALSE;
  125. }
  126. }
  127. }
  128. if (success)
  129. {
  130. S32 val = strtol(wstring_to_utf8str(trimmed).c_str(), NULL, 10);
  131. if (val <= 0)
  132. {
  133. success = FALSE;
  134. }
  135. }
  136. return success;
  137. }
  138. bool validateNonNegativeS32(const LLWString &str)
  139. {
  140. LLLocale locale(LLLocale::USER_LOCALE);
  141. LLWString trimmed = str;
  142. LLWStringUtil::trim(trimmed);
  143. S32 len = trimmed.length();
  144. bool success = TRUE;
  145. if(0 < len)
  146. {
  147. if('-' == trimmed[0])
  148. {
  149. success = FALSE;
  150. }
  151. S32 i = 0;
  152. while(success && (i < len))
  153. {
  154. if(!LLStringOps::isDigit(trimmed[i++]))
  155. {
  156. success = FALSE;
  157. }
  158. }
  159. }
  160. if (success)
  161. {
  162. S32 val = strtol(wstring_to_utf8str(trimmed).c_str(), NULL, 10);
  163. if (val < 0)
  164. {
  165. success = FALSE;
  166. }
  167. }
  168. return success;
  169. }
  170. bool validateAlphaNum(const LLWString &str)
  171. {
  172. LLLocale locale(LLLocale::USER_LOCALE);
  173. bool rv = TRUE;
  174. S32 len = str.length();
  175. if(len == 0) return rv;
  176. while(len--)
  177. {
  178. if( !LLStringOps::isAlnum((char)str[len]) )
  179. {
  180. rv = FALSE;
  181. break;
  182. }
  183. }
  184. return rv;
  185. }
  186. bool validateAlphaNumSpace(const LLWString &str)
  187. {
  188. LLLocale locale(LLLocale::USER_LOCALE);
  189. bool rv = TRUE;
  190. S32 len = str.length();
  191. if(len == 0) return rv;
  192. while(len--)
  193. {
  194. if(!(LLStringOps::isAlnum((char)str[len]) || (' ' == str[len])))
  195. {
  196. rv = FALSE;
  197. break;
  198. }
  199. }
  200. return rv;
  201. }
  202. // Used for most names of things stored on the server, due to old file-formats
  203. // that used the pipe (|) for multiline text storage. Examples include
  204. // inventory item names, parcel names, object names, etc.
  205. bool validateASCIIPrintableNoPipe(const LLWString &str)
  206. {
  207. bool rv = TRUE;
  208. S32 len = str.length();
  209. if(len == 0) return rv;
  210. while(len--)
  211. {
  212. llwchar wc = str[len];
  213. if (wc < 0x20
  214. || wc > 0x7f
  215. || wc == '|')
  216. {
  217. rv = FALSE;
  218. break;
  219. }
  220. if(!(wc == ' '
  221. || LLStringOps::isAlnum((char)wc)
  222. || LLStringOps::isPunct((char)wc) ) )
  223. {
  224. rv = FALSE;
  225. break;
  226. }
  227. }
  228. return rv;
  229. }
  230. // Used for avatar names
  231. bool validateASCIIPrintableNoSpace(const LLWString &str)
  232. {
  233. bool rv = TRUE;
  234. S32 len = str.length();
  235. if(len == 0) return rv;
  236. while(len--)
  237. {
  238. llwchar wc = str[len];
  239. if (wc < 0x20
  240. || wc > 0x7f
  241. || LLStringOps::isSpace(wc))
  242. {
  243. rv = FALSE;
  244. break;
  245. }
  246. if( !(LLStringOps::isAlnum((char)str[len]) ||
  247. LLStringOps::isPunct((char)str[len]) ) )
  248. {
  249. rv = FALSE;
  250. break;
  251. }
  252. }
  253. return rv;
  254. }
  255. bool validateASCII(const LLWString &str)
  256. {
  257. bool rv = TRUE;
  258. S32 len = str.length();
  259. while(len--)
  260. {
  261. if (str[len] < 0x20 || str[len] > 0x7f)
  262. {
  263. rv = FALSE;
  264. break;
  265. }
  266. }
  267. return rv;
  268. }
  269. }