From d263b812c0367889962a2da2f4f4ea333c31fa0d Mon Sep 17 00:00:00 2001 From: Unbewohnte Date: Sun, 6 Mar 2022 09:38:34 +0300 Subject: [PATCH] Remove "#" from comments when read --- README.txt | 3 ++- src/pnm.cpp | 48 +++++++++++++++++++++++++++++++++++++++++++----- tests/test.cpp | 13 +++++++++---- 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/README.txt b/README.txt index c3b5aae..3f6f9b1 100644 --- a/README.txt +++ b/README.txt @@ -18,7 +18,8 @@ or Current version: -v0.3 +v0.4 +Remove '#' from comments when read Status: v0.3 diff --git a/src/pnm.cpp b/src/pnm.cpp index 0606859..198d2d0 100644 --- a/src/pnm.cpp +++ b/src/pnm.cpp @@ -15,7 +15,7 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I #define RWPNM /* -RWPNM v0.3 +RWPNM v0.4 A drop-in library to work with PNM images */ @@ -67,6 +67,30 @@ public: }; +// TODO +// template +// class PNM_image { +// protected: +// std::string m_MAGIC_NUMBER; +// const char m_COMMENT_CHAR = '#'; + +// // image width and height +// uint32_t m_width; +// uint32_t m_height; + +// // stored comments +// std::vector m_comments; + +// // actual pixel data +// std::vector m_pixel_data; + +// // returns 1-dimensional array index as if it was 2-dimensional array +// uint64_t index_at(uint32_t x, uint32_t y) { +// return m_width * y + x; +// } +// public: +// }; + // PPM image file format reader/writer class PPM { protected: @@ -126,12 +150,18 @@ public: m_comments.push_back(comment); } + // Removes the last read/added comment if present void remove_last_comment() { - m_comments.pop_back(); + if (m_comments.size() > 0) { + m_comments.pop_back(); + } } + // Removes all comments if present void remove_all_comments() { - m_comments.clear(); + if (m_comments.size() > 0) { + m_comments.clear(); + } } // Return all captured comments of an image @@ -161,7 +191,7 @@ public: ppm_image_file >> entity; if (entity[0] == m_COMMENT_CHAR) { // this is a comment - std::string comment = entity; + std::string comment = entity.erase(0, 1); getline(ppm_image_file, entity); comment += entity; m_comments.push_back(comment); @@ -195,7 +225,7 @@ public: if (one[0] == m_COMMENT_CHAR) { // ah, comment again... getline(ppm_image_file, entity); - m_comments.push_back(entity); + m_comments.push_back(entity.erase(0, 1)); } else { break; } @@ -254,6 +284,14 @@ public: } }; +// TODO +// class PBM { +// protected: +// public: +// PBM() {} +// ~PBM() {} +// }; + } #endif \ No newline at end of file diff --git a/tests/test.cpp b/tests/test.cpp index 5241216..2c46652 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -95,7 +95,7 @@ void remove_comments() { } } -void read_comment() { +void read_comment(bool print) { try { write_comment(); @@ -103,11 +103,16 @@ void read_comment() { image.read("result_image.ppm"); std::vector comments = image.get_comments(); - if (comments.size() == 0) { throw std::runtime_error("No comments found, though there should be"); } - + + if (print) { + for (const std::string& comment : comments) { + std::cout << comment << "\n"; + } + } + } catch(const std::exception& e) { std::string error_message("read_comment:", e.what()); throw std::runtime_error(error_message); @@ -120,7 +125,7 @@ int main() { green_rectangle_on_top_of_existing_image(); no_pixel_assign(); write_comment(); - read_comment(); + read_comment(false); remove_comments(); } catch(const std::exception& e) {