summaryrefslogtreecommitdiff
path: root/includes/image_io/utils
diff options
context:
space:
mode:
Diffstat (limited to 'includes/image_io/utils')
-rw-r--r--includes/image_io/utils/file_utils.h19
-rw-r--r--includes/image_io/utils/message_stats_writer.h47
-rw-r--r--includes/image_io/utils/string_outputter.h19
-rw-r--r--includes/image_io/utils/string_outputter_message_writer.h28
4 files changed, 104 insertions, 9 deletions
diff --git a/includes/image_io/utils/file_utils.h b/includes/image_io/utils/file_utils.h
index d1a469d..3a8d2c6 100644
--- a/includes/image_io/utils/file_utils.h
+++ b/includes/image_io/utils/file_utils.h
@@ -6,34 +6,35 @@
#include <string>
#include "image_io/base/data_segment.h"
+#include "image_io/base/message_handler.h"
namespace photos_editing_formats {
namespace image_io {
-/// A policy that controls whether an error is reported or not.
-enum class ReportErrorPolicy { kDontReportError, kReportError };
-
/// @param file_name The name of the file to get the size in bytes of.
/// @param size A pointer to a variable to receive the size.
/// @return Whether file size was obtained properly.
bool GetFileSize(const std::string& file_name, size_t* size);
/// @param file_name The name of the file to open for output.
+/// @param message_handler Optional message handler to write messages to.
/// @return An ostream pointer or nullptr if the open failed.
-std::unique_ptr<std::ostream> OpenOutputFile(
- const std::string& file_name, ReportErrorPolicy report_error_policy);
+std::unique_ptr<std::ostream> OpenOutputFile(const std::string& file_name,
+ MessageHandler* message_handler);
/// @param file_name The name of the file to open for input.
+/// @param message_handler Optional message handler to write messages to.
/// @return An istream pointer or nullptr if the open failed.
-std::unique_ptr<std::istream> OpenInputFile(
- const std::string& file_name, ReportErrorPolicy report_error_policy);
+std::unique_ptr<std::istream> OpenInputFile(const std::string& file_name,
+ MessageHandler* message_handler);
/// Opens the named file for input, gets its size, and reads the entire contents
/// into a data segment that is returned to the caller.
/// @param file_name The name of the file to open for input.
+/// @param message_handler Optional message handler to write messages to.
/// @return A DataSegment pointer or nullptr if the open and reading failed.
-std::shared_ptr<DataSegment> ReadEntireFile(
- const std::string& file_name, ReportErrorPolicy report_error_policy);
+std::shared_ptr<DataSegment> ReadEntireFile(const std::string& file_name,
+ MessageHandler* message_handler);
} // namespace image_io
} // namespace photos_editing_formats
diff --git a/includes/image_io/utils/message_stats_writer.h b/includes/image_io/utils/message_stats_writer.h
new file mode 100644
index 0000000..24dd6b1
--- /dev/null
+++ b/includes/image_io/utils/message_stats_writer.h
@@ -0,0 +1,47 @@
+#ifndef IMAGE_IO_UTILS_MESSAGE_STATS_WRITER_H_ // NOLINT
+#define IMAGE_IO_UTILS_MESSAGE_STATS_WRITER_H_ // NOLINT
+
+#include <memory>
+#include <sstream>
+#include <string>
+
+#include "image_io/base/message_stats.h"
+#include "image_io/utils/string_outputter.h"
+
+namespace photos_editing_formats {
+namespace image_io {
+
+/// A class to write the message stats for error and warning counts. The output
+/// is written when the writer object is destroyed, making this a conveneient
+/// class to use in functions that have multiple return points and for which
+/// such output is desired at all return points.
+class MessageStatsWriter {
+ public:
+ /// @param message_stats The message stats object holding the counts.
+ /// @param outputter The outputter function to write the stats to.
+ /// @param name The name of the tool or function that is "finished".
+ MessageStatsWriter(const std::shared_ptr<MessageStats>& message_stats,
+ const std::string& name, const StringOutputter& outputter)
+ : stats_(message_stats), outputter_(outputter), name_(name) {}
+
+ /// Writes the finished message with the stats to the outputter function.
+ ~MessageStatsWriter() {
+ const string kError = stats_->error_count == 1 ? "error" : "errors";
+ const string kWarning = stats_->warning_count == 1 ? "warning" : "warnings";
+ std::stringstream ss;
+ ss << std::endl
+ << name_ << " finished, " << stats_->error_count << " " << kError << ", "
+ << stats_->warning_count << " " << kWarning << std::endl;
+ outputter_(ss.str());
+ }
+
+ private:
+ std::shared_ptr<MessageStats> stats_;
+ StringOutputter outputter_;
+ std::string name_;
+};
+
+} // namespace image_io
+} // namespace photos_editing_formats
+
+#endif // IMAGE_IO_UTILS_MESSAGE_STATS_WRITER_H_ // NOLINT
diff --git a/includes/image_io/utils/string_outputter.h b/includes/image_io/utils/string_outputter.h
new file mode 100644
index 0000000..b6ea013
--- /dev/null
+++ b/includes/image_io/utils/string_outputter.h
@@ -0,0 +1,19 @@
+#ifndef IMAGE_IO_UTILS_STRING_OUTPUTTER_H_ // NOLINT
+#define IMAGE_IO_UTILS_STRING_OUTPUTTER_H_ // NOLINT
+
+#include <functional>
+#include <string>
+
+namespace photos_editing_formats {
+namespace image_io {
+
+/// A typedef for a function that accepts a string and writes it somewhere.
+/// These types of functions are typically used in command line tools to write
+/// the output of the tool to stdout or some other location. The function
+/// should not write its own new line at the end of the str.
+using StringOutputter = std::function<void(const std::string& str)>;
+
+} // namespace image_io
+} // namespace photos_editing_formats
+
+#endif // IMAGE_IO_UTILS_STRING_OUTPUTTER_H_ // NOLINT
diff --git a/includes/image_io/utils/string_outputter_message_writer.h b/includes/image_io/utils/string_outputter_message_writer.h
new file mode 100644
index 0000000..f34d975
--- /dev/null
+++ b/includes/image_io/utils/string_outputter_message_writer.h
@@ -0,0 +1,28 @@
+#ifndef IMAGE_IO_UTILS_STRING_OUTPUTTER_MESSAGE_WRITER_H_ // NOLINT
+#define IMAGE_IO_UTILS_STRING_OUTPUTTER_MESSAGE_WRITER_H_ // NOLINT
+
+#include "image_io/base/message_writer.h"
+#include "image_io/utils/string_outputter.h"
+
+namespace photos_editing_formats {
+namespace image_io {
+
+/// A MessageWriter that writes the messages to the StringOutputter function.
+class StringOutputterMessageWriter : public MessageWriter {
+ public:
+ /// @param outputter The outputter function to write messages to.
+ explicit StringOutputterMessageWriter(const StringOutputter& outputter)
+ : outputter_(outputter) {}
+ void WriteMessage(const Message& message) override {
+ outputter_(GetFormattedMessage(message));
+ outputter_("\n");
+ }
+
+ private:
+ StringOutputter outputter_;
+};
+
+} // namespace image_io
+} // namespace photos_editing_formats
+
+#endif // IMAGE_IO_UTILS_STRING_OUTPUTTER_MESSAGE_WRITER_H_ // NOLINT