💿🐜 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.

79 lines
1.8 KiB

  1. /*
  2. * Copyright (C) 2023 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 "resources/resource-loader.hpp"
  20. #include "i18n/string-table.hpp"
  21. #include "resources/deserialize-error.hpp"
  22. #include <physfs.h>
  23. template <>
  24. i18n::string_table* resource_loader<i18n::string_table>::load(resource_manager* resource_manager, PHYSFS_File* file, const std::filesystem::path& path)
  25. {
  26. i18n::string_table* table = new i18n::string_table();
  27. i18n::string_table_row row;
  28. std::string entry;
  29. for (;;)
  30. {
  31. char c;
  32. const PHYSFS_sint64 status = PHYSFS_readBytes(file, &c, 1);
  33. if (status == 1)
  34. {
  35. if (c == '\t')
  36. {
  37. row.push_back(entry);
  38. entry.clear();
  39. }
  40. else if (c == '\n')
  41. {
  42. row.push_back(entry);
  43. entry.clear();
  44. table->push_back(row);
  45. row.clear();
  46. }
  47. else if (c != '\r')
  48. {
  49. entry.push_back(c);
  50. }
  51. }
  52. else
  53. {
  54. if (PHYSFS_eof(file))
  55. {
  56. if (!entry.empty())
  57. {
  58. row.push_back(entry);
  59. }
  60. if (!row.empty())
  61. {
  62. table->push_back(row);
  63. }
  64. break;
  65. }
  66. else
  67. {
  68. throw deserialize_error(PHYSFS_getLastError());
  69. }
  70. }
  71. }
  72. return table;
  73. }