Skip to content

Commit

Permalink
Add BinFile abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
attah committed Aug 19, 2023
1 parent 7ff0699 commit 5cc9e05
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 99 deletions.
88 changes: 88 additions & 0 deletions lib/binfile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#ifndef BINFILE_H
#define BINFILE_H

#include <string>
#include <iostream>
#include <fstream>

class InBinFile
{
public:
InBinFile() = delete;
InBinFile(const InBinFile&) = delete;
InBinFile& operator=(const InBinFile&) = delete;

InBinFile(std::string name)
{
if(name == "-")
{
in = &std::cin;
}
else
{
ifs = std::ifstream(name, std::ios::in | std::ios::binary);
in = &ifs;
}
}
operator std::istream&()
{
return *in;
}
std::istream& operator*()
{
return *in;
}
std::istream* operator->()
{
return in;
}
template <typename T>
std::istream& operator>>(T& t)
{
*in >> t;
return *in;
}
private:
std::ifstream ifs;
std::istream* in;
};

class OutBinFile
{
public:
OutBinFile() = delete;
OutBinFile(const InBinFile&) = delete;
OutBinFile& operator=(const OutBinFile&) = delete;

using value_type = std::ostream&;

OutBinFile(std::string name)
{
if(name == "-")
{
out = &std::cout;
}
else
{
ofs = std::ofstream(name, std::ios::out | std::ios::binary);
out = &ofs;
}
}
operator std::ostream&()
{
return *out;
}
std::ostream& operator*()
{
return *out;
}
std::ostream* operator->()
{
return out;
}
private:
std::ofstream ofs;
std::ostream* out;
};

#endif //BINFILE_H
35 changes: 5 additions & 30 deletions utils/baselinify_main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <fstream>
#include <iostream>
#include <baselinify.h>
#include <binfile.h>

int main(int argc, char** argv)
{
Expand All @@ -10,39 +11,13 @@ int main(int argc, char** argv)
return 1;
}

std::string inFile(argv[1]);
std::string outFile(argv[2]);
InBinFile inFile(argv[1]);
OutBinFile outFile(argv[2]);

std::ifstream ifs;
std::istream* in;
std::ofstream ofs;
std::ostream* out;

if(inFile == "-")
{
in = &std::cin;
std::ios_base::sync_with_stdio(false);
}
else
{
ifs = std::ifstream(inFile, std::ios::in | std::ios::binary);
in = &ifs;
}

if(outFile == "-")
{
out = &std::cout;
}
else
{
ofs = std::ofstream(outFile, std::ios::out | std::ios::binary);
out = &ofs;
}

Bytestream inBts(*in);
Bytestream inBts(inFile);
Bytestream outBts;

baselinify(inBts, outBts);

*out << outBts;
outFile << outBts;
}
40 changes: 15 additions & 25 deletions utils/pdf2printable_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "pdf2printable.h"
#include "ppm2pwg.h"
#include "argget.h"
#include "binfile.h"
#include "mediaposition.h"

#define HELPTEXT "Options from 'resolution' and onwards only affect raster output formats.\n" \
Expand Down Expand Up @@ -36,8 +37,8 @@ int main(int argc, char** argv)
int hwResY;
bool duplex = false;
bool tumble = false;
std::string infile;
std::string outfile;
std::string inFileName;
std::string outFileName;

SwitchArg<bool> helpOpt(help, {"-h", "--help"}, "Print this help text");
SwitchArg<bool> verboseOpt(verbose, {"-v", "--verbose"}, "Be verbose, print headers and progress");
Expand Down Expand Up @@ -90,8 +91,8 @@ int main(int argc, char** argv)
"Media position, e.g.: main, top, left, roll-2 etc.");
SwitchArg<std::string> mediaTypeOpt(params.mediaType, {"-mt", "--media-type"}, "Media type, e.g.: stationery, cardstock etc.");

PosArg pdfArg(infile, "PDF-file");
PosArg outArg(outfile, "out-file");
PosArg pdfArg(inFileName, "PDF-file");
PosArg outArg(outFileName, "out-file");

ArgGet args({&helpOpt, &verboseOpt, &formatOpt, &pagesOpt,
&copiesOpt, /*&pageCopiesOpt,*/ &paperSizeOpt, &resolutionOpt,
Expand All @@ -114,19 +115,19 @@ int main(int argc, char** argv)

if(!formatOpt.isSet())
{
if(ends_with(outfile, ".ps"))
if(ends_with(outFileName, ".ps"))
{
params.format = PrintParameters::Postscript;
}
else if(ends_with(outfile, ".pwg"))
else if(ends_with(outFileName, ".pwg"))
{
params.format = PrintParameters::PWG;
}
else if(ends_with(outfile, ".urf"))
else if(ends_with(outFileName, ".urf"))
{
params.format = PrintParameters::URF;
}
else if(ends_with(outfile, ".pdf"))
else if(ends_with(outFileName, ".pdf"))
{
params.format = PrintParameters::PDF;
}
Expand Down Expand Up @@ -191,23 +192,12 @@ int main(int argc, char** argv)
}
}

std::ofstream ofs;
std::ostream* out;
OutBinFile outFile(outFileName);

if(outfile == "-")
{
out = &std::cout;
}
else
{
ofs = std::ofstream(outfile, std::ios::out | std::ios::binary);
out = &ofs;
}

WriteFun writeFun([out](unsigned char const* buf, unsigned int len) -> bool
WriteFun writeFun([&outFile](unsigned char const* buf, unsigned int len) -> bool
{
out->write((const char*)buf, len);
return out->exceptions() == std::ostream::goodbit;
outFile->write((const char*)buf, len);
return outFile->exceptions() == std::ostream::goodbit;
});

if(verbose)
Expand All @@ -216,11 +206,11 @@ int main(int argc, char** argv)
{
std::cerr << "Progress: " << page << "/" << total << "\n\n";
});
return pdf_to_printable(infile, writeFun, params, progressFun, true);
return pdf_to_printable(inFileName, writeFun, params, progressFun, true);
}
else
{
return pdf_to_printable(infile, writeFun, params);
return pdf_to_printable(inFileName, writeFun, params);
}

}
67 changes: 23 additions & 44 deletions utils/ppm2pwg_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "ppm2pwg.h"
#include "argget.h"
#include "binfile.h"
#include "mediaposition.h"

#define HELPTEXT "Use \"-\" as filename for stdin/stdout."
Expand All @@ -16,15 +17,15 @@ inline void print_error(std::string hint, std::string argHelp)
std::cerr << hint << std::endl << std::endl << argHelp << std::endl << HELPTEXT << std::endl;
}

inline void ignore_comments(std::istream* in)
inline void ignore_comments(std::istream& in)
{
if(in->peek() == '\n')
if(in.peek() == '\n')
{
in->ignore(1);
in.ignore(1);
}
while(in->peek() == '#')
while(in.peek() == '#')
{
in->ignore(std::numeric_limits<std::streamsize>::max(), '\n');
in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}

Expand All @@ -41,8 +42,8 @@ int main(int argc, char** argv)
int hwResY;
bool duplex = false;
bool tumble = false;
std::string inFile;
std::string outFile;
std::string inFileName;
std::string outFileName;

SwitchArg<bool> helpOpt(help, {"-h", "--help"}, "Print this help text");
SwitchArg<bool> verboseOpt(verbose, {"-v", "--verbose"}, "Be verbose, print headers");
Expand Down Expand Up @@ -71,8 +72,8 @@ int main(int argc, char** argv)
"Media position, e.g.: main, top, left, roll-2 etc.");
SwitchArg<std::string> mediaTypeOpt(params.mediaType, {"-mt", "--media-type"}, "Media type, e.g.: stationery, cardstock etc.");

PosArg inArg(inFile, "in-file");
PosArg outArg(outFile, "out-file");
PosArg inArg(inFileName, "in-file");
PosArg outArg(outFileName, "out-file");

ArgGet args({&helpOpt, &verboseOpt, &urfOpt, &pagesOpt, &paperSizeOpt,
&resolutionOpt, &resolutionXOpt, &resolutionYOpt,
Expand Down Expand Up @@ -149,53 +150,31 @@ int main(int argc, char** argv)
Bytestream outBts;
Bytestream bmpBts;

std::ifstream ifs;
std::istream* in;
std::ofstream ofs;
std::ostream* out;
InBinFile inFile(inFileName);
OutBinFile outFile(outFileName);

if(inFile == "-")
{
in = &std::cin;
}
else
{
ifs = std::ifstream(inFile, std::ios::in | std::ios::binary);
in = &ifs;
}

if(outFile == "-")
{
out = &std::cout;
}
else
{
ofs = std::ofstream(outFile, std::ios::out | std::ios::binary);
out = &ofs;
}

*out << fileHdr;
outFile << fileHdr;

while(!in->eof())
while(!inFile->eof())
{
outBts.reset();
page++;

std::string p, xs, ys, r;
*in >> p;
inFile >> p;

ignore_comments(in);
ignore_comments(inFile);

*in >> xs >> ys;
inFile >> xs >> ys;

if(p == "P6")
{
*in >> r;
inFile >> r;
params.colorMode = PrintParameters::sRGB24;
}
else if(p == "P5")
{
*in >> r;
inFile >> r;
params.colorMode = PrintParameters::Gray8;
}
else if(p == "P4")
Expand All @@ -215,7 +194,7 @@ int main(int argc, char** argv)
return 1;
}

ignore_comments(in);
ignore_comments(inFile);

if(verbose)
{
Expand All @@ -226,12 +205,12 @@ int main(int argc, char** argv)
params.paperSizeH = stoi(ys);

size_t size = params.paperSizeH*params.getPaperSizeWInBytes();
bmpBts = Bytestream(*in, size);
bmpBts = Bytestream(inFile, size);

bmp_to_pwg(bmpBts, outBts, page, params, verbose);

*out << outBts;
in->peek(); // maybe trigger eof
outFile << outBts;
inFile->peek(); // maybe trigger eof
}
return 0;
}

0 comments on commit 5cc9e05

Please sign in to comment.