Browse Source

Remove "#" from comments when read

master
Unbewohnte 3 years ago
parent
commit
d263b812c0
  1. 3
      README.txt
  2. 48
      src/pnm.cpp
  3. 13
      tests/test.cpp

3
README.txt

@ -18,7 +18,8 @@ or
Current version: Current version:
v0.3 v0.4
Remove '#' from comments when read
Status: Status:
v0.3 v0.3

48
src/pnm.cpp

@ -15,7 +15,7 @@ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR I
#define RWPNM #define RWPNM
/* /*
RWPNM v0.3 RWPNM v0.4
A drop-in library to work with PNM images A drop-in library to work with PNM images
*/ */
@ -67,6 +67,30 @@ public:
}; };
// TODO
// template<class T>
// 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<std::string> m_comments;
// // actual pixel data
// std::vector<T> 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 // PPM image file format reader/writer
class PPM { class PPM {
protected: protected:
@ -126,12 +150,18 @@ public:
m_comments.push_back(comment); m_comments.push_back(comment);
} }
// Removes the last read/added comment if present
void remove_last_comment() { 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() { void remove_all_comments() {
m_comments.clear(); if (m_comments.size() > 0) {
m_comments.clear();
}
} }
// Return all captured comments of an image // Return all captured comments of an image
@ -161,7 +191,7 @@ public:
ppm_image_file >> entity; ppm_image_file >> entity;
if (entity[0] == m_COMMENT_CHAR) { if (entity[0] == m_COMMENT_CHAR) {
// this is a comment // this is a comment
std::string comment = entity; std::string comment = entity.erase(0, 1);
getline(ppm_image_file, entity); getline(ppm_image_file, entity);
comment += entity; comment += entity;
m_comments.push_back(comment); m_comments.push_back(comment);
@ -195,7 +225,7 @@ public:
if (one[0] == m_COMMENT_CHAR) { if (one[0] == m_COMMENT_CHAR) {
// ah, comment again... // ah, comment again...
getline(ppm_image_file, entity); getline(ppm_image_file, entity);
m_comments.push_back(entity); m_comments.push_back(entity.erase(0, 1));
} else { } else {
break; break;
} }
@ -254,6 +284,14 @@ public:
} }
}; };
// TODO
// class PBM {
// protected:
// public:
// PBM() {}
// ~PBM() {}
// };
} }
#endif #endif

13
tests/test.cpp

@ -95,7 +95,7 @@ void remove_comments() {
} }
} }
void read_comment() { void read_comment(bool print) {
try { try {
write_comment(); write_comment();
@ -103,11 +103,16 @@ void read_comment() {
image.read("result_image.ppm"); image.read("result_image.ppm");
std::vector<std::string> comments = image.get_comments(); std::vector<std::string> comments = image.get_comments();
if (comments.size() == 0) { if (comments.size() == 0) {
throw std::runtime_error("No comments found, though there should be"); 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) { } catch(const std::exception& e) {
std::string error_message("read_comment:", e.what()); std::string error_message("read_comment:", e.what());
throw std::runtime_error(error_message); throw std::runtime_error(error_message);
@ -120,7 +125,7 @@ int main() {
green_rectangle_on_top_of_existing_image(); green_rectangle_on_top_of_existing_image();
no_pixel_assign(); no_pixel_assign();
write_comment(); write_comment();
read_comment(); read_comment(false);
remove_comments(); remove_comments();
} catch(const std::exception& e) { } catch(const std::exception& e) {

Loading…
Cancel
Save