From 39ed3c5708440101fd7ad6574e4f535f49dcd360 Mon Sep 17 00:00:00 2001 From: Peter De Wachter Date: Fri, 26 Jan 2018 23:51:10 +0100 Subject: [PATCH] Cleanups --- src/job.cc | 4 ++-- src/job.h | 3 ++- src/main.cc | 24 ++++++++++++------------ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/job.cc b/src/job.cc index 206bb11..440fc6d 100644 --- a/src/job.cc +++ b/src/job.cc @@ -95,7 +95,7 @@ void job::encode_page(const page_params &page_params, std::vector reference(linesize); block block; - if (!nextline(line.data())) { + if (!nextline(line)) { return; } block.add_line(encode_line(line)); @@ -103,7 +103,7 @@ void job::encode_page(const page_params &page_params, fputs("\033*b1030m", out_); - for (int i = 1; i < lines && nextline(line.data()); ++i) { + for (int i = 1; i < lines && nextline(line); ++i) { std::vector encoded = encode_line(line, reference); if (!block.line_fits(encoded.size())) { block.flush(out_); diff --git a/src/job.h b/src/job.h index f26a78c..2ae7ea5 100644 --- a/src/job.h +++ b/src/job.h @@ -21,6 +21,7 @@ #include #include #include +#include struct page_params { int num_copies; @@ -44,7 +45,7 @@ struct page_params { class job { public: - typedef bool (*nextline_fn)(uint8_t *buf); + typedef bool (*nextline_fn)(std::vector &buf); explicit job(FILE *out, const std::string &job_name); ~job(); diff --git a/src/main.cc b/src/main.cc index 828fc61..259a708 100644 --- a/src/main.cc +++ b/src/main.cc @@ -29,26 +29,26 @@ #include "job.h" #include "debug.h" + namespace { +cups_raster_t *ras; volatile sig_atomic_t interrupted = 0; + void sigterm_handler(int sig) { interrupted = 1; } -cups_raster_t *ras; -cups_page_header2_t header; - -bool next_line(uint8_t *buf) { +bool next_line(std::vector &buf) { if (interrupted) { return false; } - unsigned bytes = header.cupsBytesPerLine; - return cupsRasterReadPixels(ras, buf, bytes) == bytes; + return cupsRasterReadPixels(ras, buf.data(), buf.size()) == buf.size(); } + bool plain_ascii_string(const char *str) { bool result = true; for (; result && *str; str++) { @@ -82,7 +82,7 @@ std::string ascii_job_name(const char *job_id, const char *job_user, const char return result; } -page_params build_page_params() { +page_params build_page_params(const cups_page_header2_t &header) { static const std::array sources = {{ "AUTO", "T1", "T2", "T3", "MP", "MANUAL" }}; @@ -125,12 +125,11 @@ page_params build_page_params() { } // namespace - int main(int argc, char *argv[]) { fprintf(stderr, "INFO: %s version %s\n", PACKAGE, VERSION); if (argc != 6 && argc != 7) { - fprintf(stderr, "ERROR: %s job-id user title copies options [file]\n", argv[0]); + fprintf(stderr, "ERROR: rastertobrlaser job-id user title copies options [file]\n"); fprintf(stderr, "INFO: This program is a CUPS filter. It is not intended to be run manually.\n"); return 1; } @@ -145,7 +144,7 @@ int main(int argc, char *argv[]) { signal(SIGTERM, sigterm_handler); signal(SIGPIPE, SIG_IGN); - int fd = 0; + int fd = STDIN_FILENO; if (job_filename) { fd = open(job_filename, O_RDONLY); if (fd < 0) { @@ -163,6 +162,7 @@ int main(int argc, char *argv[]) { int pages = 0; { job job(stdout, ascii_job_name(job_id, job_user, job_name)); + cups_page_header2_t header; while (!interrupted && cupsRasterReadHeader2(ras, &header)) { if (header.cupsBitsPerPixel != 1 || header.cupsBitsPerColor != 1 @@ -176,7 +176,7 @@ int main(int argc, char *argv[]) { fprintf(stderr, "DEBUG: " PACKAGE ": Page header of first page\n"); dump_page_header(header); } - job.encode_page(build_page_params(), + job.encode_page(build_page_params(header), header.cupsHeight, header.cupsBytesPerLine, next_line); @@ -191,7 +191,7 @@ int main(int argc, char *argv[]) { fflush(stdout); if (ferror(stdout)) { - fprintf(stderr, "ERROR: " PACKAGE ": Could not write print data\n"); + fprintf(stderr, "DEBUG: " PACKAGE ": Could not write print data. Most likely the CUPS backend failed.\n"); return 1; } return 0;