From baa33a9a0837d84de2752fef006c1dd859f29d71 Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Thu, 1 Feb 2024 22:01:13 +0100 Subject: [PATCH] Ensure \r\n newlines in plaintext --- lib/ippprintjob.h | 26 +++++++++++++++++++++++++- lib/stringsplit.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 lib/stringsplit.h diff --git a/lib/ippprintjob.h b/lib/ippprintjob.h index 8f790e7..ebf3a67 100644 --- a/lib/ippprintjob.h +++ b/lib/ippprintjob.h @@ -9,6 +9,7 @@ #include "setting.h" #include "error.h" #include "minimime.h" +#include "stringsplit.h" class IppPrintJob { @@ -86,11 +87,34 @@ class IppPrintJob return Error(); }; + ConvertFun FixupText = [](std::string inFileName, WriteFun writeFun, const IppPrintJob&, ProgressFun progressFun, bool) + { + InBinFile in(inFileName); + if(!in) + { + return Error("Failed to open input"); + } + Bytestream inBts(in); + std::string allText = inBts.getString(inBts.size()); + + List lines; + for(const std::string& rnline : split_string(allText, "\r\n")) + { + lines += split_string(rnline, "\n"); + } + std::string outString = join_string(lines, "\r\n"); + + writeFun((uint8_t*)outString.c_str(), outString.length()); + progressFun(1, 1); + return Error(); + }; + std::map Pipelines {{{MiniMime::PDF, MiniMime::PDF}, Pdf2Printable}, {{MiniMime::PDF, MiniMime::Postscript}, Pdf2Printable}, {{MiniMime::PDF, MiniMime::PWG}, Pdf2Printable}, {{MiniMime::PDF, MiniMime::URF}, Pdf2Printable}, - {{MiniMime::JPEG, MiniMime::JPEG}, Baselinify}}; + {{MiniMime::JPEG, MiniMime::JPEG}, Baselinify}, + {{"text/plain", "text/plain"}, FixupText}}; IppAttrs opAttrs; IppAttrs jobAttrs; diff --git a/lib/stringsplit.h b/lib/stringsplit.h new file mode 100644 index 0000000..da9e408 --- /dev/null +++ b/lib/stringsplit.h @@ -0,0 +1,34 @@ +#ifndef STRINGSPLIT_H +#define STRINGSPLIT_H + +#include +#include + +inline List split_string(const std::string& s, const std::string& tok) +{ + size_t pos = 0; + List res; + while(pos <= s.length()) + { + size_t found = std::min(s.length(), s.find(tok, pos)); + res.push_back(s.substr(pos, (found - pos))); + pos = found + 1; + } + return res; +} + +inline std::string join_string(List list, const std::string& sep) +{ + std::string res; + if(!list.empty()) + { + res = list.takeFront(); + for(const std::string& e : list) + { + res += sep + e; + } + } + return res; +} + +#endif //STRINGSPLIT_H