PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/llstreamtools.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 525 lines | 397 code | 20 blank | 108 comment | 96 complexity | d099df39103d2f8fac46d278dfd0fd34 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llstreamtools.cpp
  3. * @brief some helper functions for parsing legacy simstate and asset files.
  4. *
  5. * $LicenseInfo:firstyear=2005&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. #include "linden_common.h"
  27. #include <iostream>
  28. #include <string>
  29. #include "llstreamtools.h"
  30. // ----------------------------------------------------------------------------
  31. // some std::istream helper functions
  32. // ----------------------------------------------------------------------------
  33. // skips spaces and tabs
  34. bool skip_whitespace(std::istream& input_stream)
  35. {
  36. int c = input_stream.peek();
  37. while (('\t' == c || ' ' == c) && input_stream.good())
  38. {
  39. input_stream.get();
  40. c = input_stream.peek();
  41. }
  42. return input_stream.good();
  43. }
  44. // skips whitespace, newlines, and carriage returns
  45. bool skip_emptyspace(std::istream& input_stream)
  46. {
  47. int c = input_stream.peek();
  48. while ( input_stream.good()
  49. && ('\t' == c || ' ' == c || '\n' == c || '\r' == c) )
  50. {
  51. input_stream.get();
  52. c = input_stream.peek();
  53. }
  54. return input_stream.good();
  55. }
  56. // skips emptyspace and lines that start with a #
  57. bool skip_comments_and_emptyspace(std::istream& input_stream)
  58. {
  59. while (skip_emptyspace(input_stream))
  60. {
  61. int c = input_stream.peek();
  62. if ('#' == c )
  63. {
  64. while ('\n' != c && input_stream.good())
  65. {
  66. c = input_stream.get();
  67. }
  68. }
  69. else
  70. {
  71. break;
  72. }
  73. }
  74. return input_stream.good();
  75. }
  76. bool skip_line(std::istream& input_stream)
  77. {
  78. int c;
  79. do
  80. {
  81. c = input_stream.get();
  82. } while ('\n' != c && input_stream.good());
  83. return input_stream.good();
  84. }
  85. bool skip_to_next_word(std::istream& input_stream)
  86. {
  87. int c = input_stream.peek();
  88. while ( input_stream.good()
  89. && ( (c >= 'a' && c <= 'z')
  90. || (c >= 'A' && c <= 'Z')
  91. || (c >= '0' && c <= '9')
  92. || '_' == c ) )
  93. {
  94. input_stream.get();
  95. c = input_stream.peek();
  96. }
  97. while ( input_stream.good()
  98. && !( (c >= 'a' && c <= 'z')
  99. || (c >= 'A' && c <= 'Z')
  100. || (c >= '0' && c <= '9')
  101. || '_' == c ) )
  102. {
  103. input_stream.get();
  104. c = input_stream.peek();
  105. }
  106. return input_stream.good();
  107. }
  108. bool skip_to_end_of_next_keyword(const char* keyword, std::istream& input_stream)
  109. {
  110. int key_length = strlen(keyword); /*Flawfinder: ignore*/
  111. if (0 == key_length)
  112. {
  113. return false;
  114. }
  115. while (input_stream.good())
  116. {
  117. skip_emptyspace(input_stream);
  118. int c = input_stream.get();
  119. if (keyword[0] != c)
  120. {
  121. skip_line(input_stream);
  122. }
  123. else
  124. {
  125. int key_index = 1;
  126. while ( key_index < key_length
  127. && keyword[key_index - 1] == c
  128. && input_stream.good())
  129. {
  130. key_index++;
  131. c = input_stream.get();
  132. }
  133. if (key_index == key_length
  134. && keyword[key_index-1] == c)
  135. {
  136. c = input_stream.peek();
  137. if (' ' == c || '\t' == c || '\r' == c || '\n' == c)
  138. {
  139. return true;
  140. }
  141. else
  142. {
  143. skip_line(input_stream);
  144. }
  145. }
  146. else
  147. {
  148. skip_line(input_stream);
  149. }
  150. }
  151. }
  152. return false;
  153. }
  154. /* skip_to_start_of_next_keyword() is disabled -- might tickle corruption bug in windows iostream
  155. bool skip_to_start_of_next_keyword(const char* keyword, std::istream& input_stream)
  156. {
  157. int key_length = strlen(keyword);
  158. if (0 == key_length)
  159. {
  160. return false;
  161. }
  162. while (input_stream.good())
  163. {
  164. skip_emptyspace(input_stream);
  165. int c = input_stream.get();
  166. if (keyword[0] != c)
  167. {
  168. skip_line(input_stream);
  169. }
  170. else
  171. {
  172. int key_index = 1;
  173. while ( key_index < key_length
  174. && keyword[key_index - 1] == c
  175. && input_stream.good())
  176. {
  177. key_index++;
  178. c = input_stream.get();
  179. }
  180. if (key_index == key_length
  181. && keyword[key_index-1] == c)
  182. {
  183. c = input_stream.peek();
  184. if (' ' == c || '\t' == c || '\r' == c || '\n' == c)
  185. {
  186. // put the keyword back onto the stream
  187. for (int index = key_length - 1; index >= 0; index--)
  188. {
  189. input_stream.putback(keyword[index]);
  190. }
  191. return true;
  192. }
  193. else
  194. {
  195. skip_line(input_stream);
  196. break;
  197. }
  198. }
  199. else
  200. {
  201. skip_line(input_stream);
  202. }
  203. }
  204. }
  205. return false;
  206. }
  207. */
  208. bool get_word(std::string& output_string, std::istream& input_stream)
  209. {
  210. skip_emptyspace(input_stream);
  211. int c = input_stream.peek();
  212. while ( !isspace(c)
  213. && '\n' != c
  214. && '\r' != c
  215. && input_stream.good() )
  216. {
  217. output_string += c;
  218. input_stream.get();
  219. c = input_stream.peek();
  220. }
  221. return input_stream.good();
  222. }
  223. bool get_word(std::string& output_string, std::istream& input_stream, int n)
  224. {
  225. skip_emptyspace(input_stream);
  226. int char_count = 0;
  227. int c = input_stream.peek();
  228. while (!isspace(c)
  229. && '\n' != c
  230. && '\r' != c
  231. && input_stream.good()
  232. && char_count < n)
  233. {
  234. char_count++;
  235. output_string += c;
  236. input_stream.get();
  237. c = input_stream.peek();
  238. }
  239. return input_stream.good();
  240. }
  241. // get everything up to and including the next newline
  242. bool get_line(std::string& output_string, std::istream& input_stream)
  243. {
  244. output_string.clear();
  245. int c = input_stream.get();
  246. while (input_stream.good())
  247. {
  248. output_string += c;
  249. if ('\n' == c)
  250. {
  251. break;
  252. }
  253. c = input_stream.get();
  254. }
  255. return input_stream.good();
  256. }
  257. // get everything up to and including the next newline
  258. // up to the next n characters.
  259. // add a newline on the end if bail before actual line ending
  260. bool get_line(std::string& output_string, std::istream& input_stream, int n)
  261. {
  262. output_string.clear();
  263. int char_count = 0;
  264. int c = input_stream.get();
  265. while (input_stream.good() && char_count < n)
  266. {
  267. char_count++;
  268. output_string += c;
  269. if ('\n' == c)
  270. {
  271. break;
  272. }
  273. if (char_count >= n)
  274. {
  275. output_string.append("\n");
  276. break;
  277. }
  278. c = input_stream.get();
  279. }
  280. return input_stream.good();
  281. }
  282. /* disabled -- might tickle bug in windows iostream
  283. // backs up the input_stream by line_size + 1 characters
  284. bool unget_line(const std::string& line, std::istream& input_stream)
  285. {
  286. input_stream.putback('\n'); // unget the newline
  287. for (int line_index = line.size()-1; line_index >= 0; line_index--)
  288. {
  289. input_stream.putback(line[line_index]);
  290. }
  291. return input_stream.good();
  292. }
  293. */
  294. // removes the last char in 'line' if it matches 'c'
  295. // returns true if removed last char
  296. bool remove_last_char(char c, std::string& line)
  297. {
  298. int line_size = line.size();
  299. if (line_size > 1
  300. && c == line[line_size - 1])
  301. {
  302. line.replace(line_size - 1, 1, "");
  303. return true;
  304. }
  305. return false;
  306. }
  307. // replaces escaped characters with the correct characters from left to right
  308. // "\\\\" ---> '\\' (two backslahes become one)
  309. // "\\n" ---> '\n' (backslash n becomes carriage return)
  310. void unescape_string(std::string& line)
  311. {
  312. int line_size = line.size();
  313. int index = 0;
  314. while (index < line_size - 1)
  315. {
  316. if ('\\' == line[index])
  317. {
  318. if ('\\' == line[index + 1])
  319. {
  320. line.replace(index, 2, "\\");
  321. line_size--;
  322. }
  323. else if ('n' == line[index + 1])
  324. {
  325. line.replace(index, 2, "\n");
  326. line_size--;
  327. }
  328. }
  329. index++;
  330. }
  331. }
  332. // replaces unescaped characters with expanded equivalents from left to right
  333. // '\\' ---> "\\\\" (one backslash becomes two)
  334. // '\n' ---> "\\n" (carriage return becomes backslash n)
  335. void escape_string(std::string& line)
  336. {
  337. int line_size = line.size();
  338. int index = 0;
  339. while (index < line_size)
  340. {
  341. if ('\\' == line[index])
  342. {
  343. line.replace(index, 1, "\\\\");
  344. line_size++;
  345. index++;
  346. }
  347. else if ('\n' == line[index])
  348. {
  349. line.replace(index, 1, "\\n");
  350. line_size++;
  351. index++;
  352. }
  353. index++;
  354. }
  355. }
  356. // removes '\n' characters
  357. void replace_newlines_with_whitespace(std::string& line)
  358. {
  359. int line_size = line.size();
  360. int index = 0;
  361. while (index < line_size)
  362. {
  363. if ('\n' == line[index])
  364. {
  365. line.replace(index, 1, " ");
  366. }
  367. index++;
  368. }
  369. }
  370. // erases any double-quote characters in 'line'
  371. void remove_double_quotes(std::string& line)
  372. {
  373. int index = 0;
  374. int line_size = line.size();
  375. while (index < line_size)
  376. {
  377. if ('"' == line[index])
  378. {
  379. int count = 1;
  380. while (index + count < line_size
  381. && '"' == line[index + count])
  382. {
  383. count++;
  384. }
  385. line.replace(index, count, "");
  386. line_size -= count;
  387. }
  388. else
  389. {
  390. index++;
  391. }
  392. }
  393. }
  394. // the 'keyword' is defined as the first word on a line
  395. // the 'value' is everything after the keyword on the same line
  396. // starting at the first non-whitespace and ending right before the newline
  397. void get_keyword_and_value(std::string& keyword,
  398. std::string& value,
  399. const std::string& line)
  400. {
  401. // skip initial whitespace
  402. int line_size = line.size();
  403. int line_index = 0;
  404. char c;
  405. while (line_index < line_size)
  406. {
  407. c = line[line_index];
  408. if (!LLStringOps::isSpace(c))
  409. {
  410. break;
  411. }
  412. line_index++;
  413. }
  414. // get the keyword
  415. keyword.clear();
  416. while (line_index < line_size)
  417. {
  418. c = line[line_index];
  419. if (LLStringOps::isSpace(c) || '\r' == c || '\n' == c)
  420. {
  421. break;
  422. }
  423. keyword += c;
  424. line_index++;
  425. }
  426. // get the value
  427. value.clear();
  428. if (keyword.size() > 0
  429. && '\r' != line[line_index]
  430. && '\n' != line[line_index])
  431. {
  432. // discard initial white spaces
  433. while (line_index < line_size
  434. && (' ' == line[line_index]
  435. || '\t' == line[line_index]) )
  436. {
  437. line_index++;
  438. }
  439. while (line_index < line_size)
  440. {
  441. c = line[line_index];
  442. if ('\r' == c || '\n' == c)
  443. {
  444. break;
  445. }
  446. value += c;
  447. line_index++;
  448. }
  449. }
  450. }
  451. std::streamsize fullread(
  452. std::istream& istr,
  453. char* buf,
  454. std::streamsize requested)
  455. {
  456. std::streamsize got;
  457. std::streamsize total = 0;
  458. istr.read(buf, requested); /*Flawfinder: ignore*/
  459. got = istr.gcount();
  460. total += got;
  461. while(got && total < requested)
  462. {
  463. if(istr.fail())
  464. {
  465. // If bad is true, not much we can doo -- it implies loss
  466. // of stream integrity. Bail in that case, and otherwise
  467. // clear and attempt to continue.
  468. if(istr.bad()) return total;
  469. istr.clear();
  470. }
  471. istr.read(buf + total, requested - total); /*Flawfinder: ignore*/
  472. got = istr.gcount();
  473. total += got;
  474. }
  475. return total;
  476. }
  477. std::istream& operator>>(std::istream& str, const char *tocheck)
  478. {
  479. char c = '\0';
  480. const char *p;
  481. p = tocheck;
  482. while (*p && !str.bad())
  483. {
  484. str.get(c);
  485. if (c != *p)
  486. {
  487. str.setstate(std::ios::failbit); /*Flawfinder: ignore*/
  488. break;
  489. }
  490. p++;
  491. }
  492. return str;
  493. }