Workbook.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Workbook.h"
  27. #include <AK/JsonArray.h>
  28. #include <AK/JsonObject.h>
  29. #include <AK/JsonObjectSerializer.h>
  30. #include <AK/JsonParser.h>
  31. #include <LibCore/File.h>
  32. #include <string.h>
  33. namespace Spreadsheet {
  34. bool Workbook::set_filename(const String& filename)
  35. {
  36. if (m_current_filename == filename)
  37. return false;
  38. m_current_filename = filename;
  39. return true;
  40. }
  41. Result<bool, String> Workbook::load(const StringView& filename)
  42. {
  43. auto file_or_error = Core::File::open(filename, Core::IODevice::OpenMode::ReadOnly);
  44. if (file_or_error.is_error()) {
  45. StringBuilder sb;
  46. sb.append("Failed to open ");
  47. sb.append(filename);
  48. sb.append(" for reading. Error: ");
  49. sb.append(file_or_error.error());
  50. return sb.to_string();
  51. }
  52. auto json_value_option = JsonParser(file_or_error.value()->read_all()).parse();
  53. if (!json_value_option.has_value()) {
  54. StringBuilder sb;
  55. sb.append("Failed to parse ");
  56. sb.append(filename);
  57. return sb.to_string();
  58. }
  59. auto& json_value = json_value_option.value();
  60. if (!json_value.is_array()) {
  61. StringBuilder sb;
  62. sb.append("Did not find a spreadsheet in ");
  63. sb.append(filename);
  64. return sb.to_string();
  65. }
  66. NonnullRefPtrVector<Sheet> sheets;
  67. auto& json_array = json_value.as_array();
  68. json_array.for_each([&](auto& sheet_json) {
  69. if (!sheet_json.is_object())
  70. return IterationDecision::Continue;
  71. auto sheet = Sheet::from_json(sheet_json.as_object());
  72. if (!sheet)
  73. return IterationDecision::Continue;
  74. sheets.append(sheet.release_nonnull());
  75. return IterationDecision::Continue;
  76. });
  77. m_sheets.clear();
  78. m_sheets = move(sheets);
  79. set_filename(filename);
  80. return true;
  81. }
  82. Result<bool, String> Workbook::save(const StringView& filename)
  83. {
  84. JsonArray array;
  85. for (auto& sheet : m_sheets)
  86. array.append(sheet.to_json());
  87. auto file_content = array.to_string();
  88. auto file = Core::File::construct(filename);
  89. file->open(Core::IODevice::WriteOnly);
  90. if (!file->is_open()) {
  91. StringBuilder sb;
  92. sb.append("Failed to open ");
  93. sb.append(filename);
  94. sb.append(" for write. Error: ");
  95. sb.append(file->error_string());
  96. return sb.to_string();
  97. }
  98. bool result = file->write(file_content);
  99. if (!result) {
  100. int error_number = errno;
  101. StringBuilder sb;
  102. sb.append("Unable to save file. Error: ");
  103. sb.append(strerror(error_number));
  104. return sb.to_string();
  105. }
  106. set_filename(filename);
  107. return true;
  108. }
  109. }