PageRenderTime 66ms CodeModel.GetById 41ms RepoModel.GetById 0ms app.codeStats 0ms

/linden/indra/llui/lltextvalidate.cpp

https://github.com/jimjesus/kittyviewer
C++ | 321 lines | 243 code | 28 blank | 50 comment | 58 complexity | 3b043479712bcd8f73e8ecb7d48a597a MD5 | raw file
  1. /**
  2. * @file lltextvalidate.cpp
  3. * @brief Text validation helper functions
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2010, 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://secondlife.com/developers/opensource/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://secondlife.com/developers/opensource/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. */
  33. // Text editor widget to let users enter a single line.
  34. #include "linden_common.h"
  35. #include "lltextvalidate.h"
  36. #include "llresmgr.h" // for LLLocale
  37. namespace LLTextValidate
  38. {
  39. void ValidateTextNamedFuncs::declareValues()
  40. {
  41. declare("ascii", validateASCII);
  42. declare("float", validateFloat);
  43. declare("int", validateInt);
  44. declare("positive_s32", validatePositiveS32);
  45. declare("non_negative_s32", validateNonNegativeS32);
  46. declare("alpha_num", validateAlphaNum);
  47. declare("alpha_num_space", validateAlphaNumSpace);
  48. declare("ascii_printable_no_pipe", validateASCIIPrintableNoPipe);
  49. declare("ascii_printable_no_space", validateASCIIPrintableNoSpace);
  50. declare("ascii_with_newline", validateASCIIWithNewLine);
  51. }
  52. // Limits what characters can be used to [1234567890.-] with [-] only valid in the first position.
  53. // Does NOT ensure that the string is a well-formed number--that's the job of post-validation--for
  54. // the simple reasons that intermediate states may be invalid even if the final result is valid.
  55. //
  56. bool validateFloat(const LLWString &str)
  57. {
  58. LLLocale locale(LLLocale::USER_LOCALE);
  59. bool success = TRUE;
  60. LLWString trimmed = str;
  61. LLWStringUtil::trim(trimmed);
  62. S32 len = trimmed.length();
  63. if( 0 < len )
  64. {
  65. // May be a comma or period, depending on the locale
  66. llwchar decimal_point = (llwchar)LLResMgr::getInstance()->getDecimalPoint();
  67. S32 i = 0;
  68. // First character can be a negative sign
  69. if( '-' == trimmed[0] )
  70. {
  71. i++;
  72. }
  73. for( ; i < len; i++ )
  74. {
  75. if( (decimal_point != trimmed[i] ) && !LLStringOps::isDigit( trimmed[i] ) )
  76. {
  77. success = FALSE;
  78. break;
  79. }
  80. }
  81. }
  82. return success;
  83. }
  84. // Limits what characters can be used to [1234567890-] with [-] only valid in the first position.
  85. // Does NOT ensure that the string is a well-formed number--that's the job of post-validation--for
  86. // the simple reasons that intermediate states may be invalid even if the final result is valid.
  87. //
  88. bool validateInt(const LLWString &str)
  89. {
  90. LLLocale locale(LLLocale::USER_LOCALE);
  91. bool success = TRUE;
  92. LLWString trimmed = str;
  93. LLWStringUtil::trim(trimmed);
  94. S32 len = trimmed.length();
  95. if( 0 < len )
  96. {
  97. S32 i = 0;
  98. // First character can be a negative sign
  99. if( '-' == trimmed[0] )
  100. {
  101. i++;
  102. }
  103. for( ; i < len; i++ )
  104. {
  105. if( !LLStringOps::isDigit( trimmed[i] ) )
  106. {
  107. success = FALSE;
  108. break;
  109. }
  110. }
  111. }
  112. return success;
  113. }
  114. bool validatePositiveS32(const LLWString &str)
  115. {
  116. LLLocale locale(LLLocale::USER_LOCALE);
  117. LLWString trimmed = str;
  118. LLWStringUtil::trim(trimmed);
  119. S32 len = trimmed.length();
  120. bool success = TRUE;
  121. if(0 < len)
  122. {
  123. if(('-' == trimmed[0]) || ('0' == trimmed[0]))
  124. {
  125. success = FALSE;
  126. }
  127. S32 i = 0;
  128. while(success && (i < len))
  129. {
  130. if(!LLStringOps::isDigit(trimmed[i++]))
  131. {
  132. success = FALSE;
  133. }
  134. }
  135. }
  136. if (success)
  137. {
  138. S32 val = strtol(wstring_to_utf8str(trimmed).c_str(), NULL, 10);
  139. if (val <= 0)
  140. {
  141. success = FALSE;
  142. }
  143. }
  144. return success;
  145. }
  146. bool validateNonNegativeS32(const LLWString &str)
  147. {
  148. LLLocale locale(LLLocale::USER_LOCALE);
  149. LLWString trimmed = str;
  150. LLWStringUtil::trim(trimmed);
  151. S32 len = trimmed.length();
  152. bool success = TRUE;
  153. if(0 < len)
  154. {
  155. if('-' == trimmed[0])
  156. {
  157. success = FALSE;
  158. }
  159. S32 i = 0;
  160. while(success && (i < len))
  161. {
  162. if(!LLStringOps::isDigit(trimmed[i++]))
  163. {
  164. success = FALSE;
  165. }
  166. }
  167. }
  168. if (success)
  169. {
  170. S32 val = strtol(wstring_to_utf8str(trimmed).c_str(), NULL, 10);
  171. if (val < 0)
  172. {
  173. success = FALSE;
  174. }
  175. }
  176. return success;
  177. }
  178. bool validateAlphaNum(const LLWString &str)
  179. {
  180. LLLocale locale(LLLocale::USER_LOCALE);
  181. bool rv = TRUE;
  182. S32 len = str.length();
  183. if(len == 0) return rv;
  184. while(len--)
  185. {
  186. if( !LLStringOps::isAlnum((char)str[len]) )
  187. {
  188. rv = FALSE;
  189. break;
  190. }
  191. }
  192. return rv;
  193. }
  194. bool validateAlphaNumSpace(const LLWString &str)
  195. {
  196. LLLocale locale(LLLocale::USER_LOCALE);
  197. bool rv = TRUE;
  198. S32 len = str.length();
  199. if(len == 0) return rv;
  200. while(len--)
  201. {
  202. if(!(LLStringOps::isAlnum((char)str[len]) || (' ' == str[len])))
  203. {
  204. rv = FALSE;
  205. break;
  206. }
  207. }
  208. return rv;
  209. }
  210. // Used for most names of things stored on the server, due to old file-formats
  211. // that used the pipe (|) for multiline text storage. Examples include
  212. // inventory item names, parcel names, object names, etc.
  213. bool validateASCIIPrintableNoPipe(const LLWString &str)
  214. {
  215. bool rv = TRUE;
  216. S32 len = str.length();
  217. if(len == 0) return rv;
  218. while(len--)
  219. {
  220. llwchar wc = str[len];
  221. if (wc < 0x20
  222. || wc > 0x7f
  223. || wc == '|')
  224. {
  225. rv = FALSE;
  226. break;
  227. }
  228. if(!(wc == ' '
  229. || LLStringOps::isAlnum((char)wc)
  230. || LLStringOps::isPunct((char)wc) ) )
  231. {
  232. rv = FALSE;
  233. break;
  234. }
  235. }
  236. return rv;
  237. }
  238. // Used for avatar names
  239. bool validateASCIIPrintableNoSpace(const LLWString &str)
  240. {
  241. bool rv = TRUE;
  242. S32 len = str.length();
  243. if(len == 0) return rv;
  244. while(len--)
  245. {
  246. llwchar wc = str[len];
  247. if (wc < 0x20
  248. || wc > 0x7f
  249. || LLStringOps::isSpace(wc))
  250. {
  251. rv = FALSE;
  252. break;
  253. }
  254. if( !(LLStringOps::isAlnum((char)str[len]) ||
  255. LLStringOps::isPunct((char)str[len]) ) )
  256. {
  257. rv = FALSE;
  258. break;
  259. }
  260. }
  261. return rv;
  262. }
  263. bool validateASCII(const LLWString &str)
  264. {
  265. bool rv = TRUE;
  266. S32 len = str.length();
  267. while(len--)
  268. {
  269. if (str[len] < 0x20 || str[len] > 0x7f)
  270. {
  271. rv = FALSE;
  272. break;
  273. }
  274. }
  275. return rv;
  276. }
  277. // Used for multiline text stored on the server.
  278. // Example is landmark description in Places SP.
  279. bool validateASCIIWithNewLine(const LLWString &str)
  280. {
  281. bool rv = TRUE;
  282. S32 len = str.length();
  283. while(len--)
  284. {
  285. if ((str[len] < 0x20 && str[len] != 0xA) || str[len] > 0x7f)
  286. {
  287. rv = FALSE;
  288. break;
  289. }
  290. }
  291. return rv;
  292. }
  293. }