Skip to content

Commit

Permalink
Ensure \r\n newlines in plaintext
Browse files Browse the repository at this point in the history
  • Loading branch information
attah committed Feb 1, 2024
1 parent a4f9114 commit baa33a9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
26 changes: 25 additions & 1 deletion lib/ippprintjob.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "setting.h"
#include "error.h"
#include "minimime.h"
#include "stringsplit.h"

class IppPrintJob
{
Expand Down Expand Up @@ -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<std::string> 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<ConvertKey, ConvertFun> 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;
Expand Down
34 changes: 34 additions & 0 deletions lib/stringsplit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#ifndef STRINGSPLIT_H
#define STRINGSPLIT_H

#include <string>
#include <list.h>

inline List<std::string> split_string(const std::string& s, const std::string& tok)
{
size_t pos = 0;
List<std::string> 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<std::string> 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

0 comments on commit baa33a9

Please sign in to comment.