💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
2.4 KiB

  1. /*
  2. * Copyright (C) 2017-2019 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "resource-loader.hpp"
  20. #include "csv-table.hpp"
  21. static CSVRow parseRow(const std::string& line)
  22. {
  23. std::vector<std::string> row;
  24. std::string column;
  25. bool quoted = false;
  26. bool escape = false;
  27. for (char c: line)
  28. {
  29. if (escape)
  30. {
  31. switch (c)
  32. {
  33. case 'n':
  34. column.push_back('\n');
  35. break;
  36. case 't':
  37. column.push_back('\t');
  38. break;
  39. default:
  40. column.push_back(c);
  41. break;
  42. }
  43. escape = false;
  44. }
  45. else
  46. {
  47. switch (c)
  48. {
  49. case '\\':
  50. escape = true;
  51. break;
  52. case ',':
  53. if (quoted)
  54. {
  55. column.push_back(c);
  56. }
  57. else
  58. {
  59. row.push_back(column);
  60. column.clear();
  61. }
  62. break;
  63. case '"':
  64. if (!quoted)
  65. {
  66. quoted = true;
  67. }
  68. else
  69. {
  70. quoted = false;
  71. }
  72. break;
  73. default:
  74. column.push_back(c);
  75. break;
  76. }
  77. }
  78. }
  79. row.push_back(column);
  80. return row;
  81. }
  82. template <>
  83. CSVTable* ResourceLoader<CSVTable>::load(ResourceManager* resourceManager, std::istream* is)
  84. {
  85. CSVTable* table = new CSVTable();
  86. std::string line;
  87. while (!is->eof())
  88. {
  89. std::getline(*is, line);
  90. if (is->bad() || is->fail())
  91. {
  92. break;
  93. }
  94. table->push_back(parseRow(line));
  95. }
  96. return table;
  97. }
  98. template <>
  99. void ResourceLoader<CSVTable>::save(ResourceManager* resourceManager, std::ostream* os, const CSVTable* table)
  100. {
  101. for (std::size_t i = 0; i < table->size(); ++i)
  102. {
  103. const CSVRow& row = (*table)[i];
  104. for (std::size_t j = 0; j < row.size(); ++j)
  105. {
  106. const CSVEntry& column = row[j];
  107. (*os) << column;
  108. if (j < row.size() - 1)
  109. {
  110. (*os) << ",";
  111. }
  112. }
  113. if (i < table->size() - 1)
  114. {
  115. (*os) << std::endl;
  116. }
  117. }
  118. }