virtual_tfile.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. XCC Utilities and Library
  3. Copyright (C) 2000 Olaf van der Spek <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "stdafx.h"
  16. #include "virtual_tfile.h"
  17. void Cvirtual_tfile::load_data(const Cvirtual_binary s)
  18. {
  19. m_data = s;
  20. pos = 0;
  21. }
  22. string Cvirtual_tfile::read_line()
  23. {
  24. assert(data());
  25. const int first_non_ws = pos;
  26. int last_non_ws;
  27. while (pos < size())
  28. {
  29. switch (data()[pos++])
  30. {
  31. case '\r':
  32. last_non_ws = pos - 2;
  33. if (pos < size() && data()[pos] == '\n')
  34. pos++;
  35. return string(data() + first_non_ws, last_non_ws - first_non_ws + 1);
  36. case '\n':
  37. last_non_ws = pos - 2;
  38. return string(data() + first_non_ws, last_non_ws - first_non_ws + 1);
  39. }
  40. }
  41. last_non_ws = size() - 1;
  42. return string(data() + first_non_ws, last_non_ws - first_non_ws + 1);
  43. }
  44. string Cvirtual_tfile::read_line(bool remove_ws)
  45. {
  46. if (!remove_ws)
  47. return read_line();
  48. assert(data());
  49. int first_non_ws;
  50. int last_non_ws;
  51. while (pos < size())
  52. {
  53. switch (data()[pos++])
  54. {
  55. case '\r':
  56. if (pos < size() && data()[pos] == '\n')
  57. pos++;
  58. case '\n':
  59. return "";
  60. case '\t':
  61. case ' ':
  62. break;
  63. default:
  64. first_non_ws = pos - 1;
  65. last_non_ws = pos - 2;
  66. while (pos < size())
  67. {
  68. switch (data()[pos++])
  69. {
  70. case '\r':
  71. if (pos < size() && data()[pos] == '\n')
  72. pos++;
  73. case '\n':
  74. return string(data() + first_non_ws, last_non_ws - first_non_ws + 1);
  75. case '\t':
  76. case ' ':
  77. break;
  78. default:
  79. last_non_ws = pos - 1;
  80. }
  81. }
  82. return string(data() + first_non_ws, last_non_ws - first_non_ws + 1);
  83. }
  84. }
  85. return "";
  86. }