From b0b8dfbc816b60f8e0a2872e2fb343c60a25989b Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Thu, 18 Oct 2018 19:18:20 +0200 Subject: [PATCH 1/2] TessResultRenderer: Extend API to access status of renderer Signed-off-by: Stefan Weil --- src/api/renderer.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/api/renderer.h b/src/api/renderer.h index 55f1a2fa3..6c753403c 100644 --- a/src/api/renderer.h +++ b/src/api/renderer.h @@ -80,6 +80,9 @@ class TESS_API TessResultRenderer { const char* file_extension() const { return file_extension_; } const char* title() const { return title_.c_str(); } + // Is everything fine? Otherwise something went wrong. + bool happy() { return happy_; } + /** * Returns the index of the last image given to AddImage * (i.e. images are incremented whether the image succeeded or not) From 49d7df6dc3efc61861c783021e06567d3d4780f0 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Thu, 18 Oct 2018 19:22:13 +0200 Subject: [PATCH 2/2] tesseractmain: Show error message when output file could not be created Signed-off-by: Stefan Weil --- src/api/tesseractmain.cpp | 61 ++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 10 deletions(-) diff --git a/src/api/tesseractmain.cpp b/src/api/tesseractmain.cpp index d22f7d9a3..bcf156ffe 100644 --- a/src/api/tesseractmain.cpp +++ b/src/api/tesseractmain.cpp @@ -22,6 +22,7 @@ #include "config_auto.h" #endif +#include // for errno #include #include "allheaders.h" @@ -33,7 +34,7 @@ #include "renderer.h" #include "simddetect.h" #include "strngs.h" -#include "tprintf.h" +#include "tprintf.h" // for tprintf #if defined(_WIN32) #include @@ -407,16 +408,28 @@ static void PreloadRenderers( if (b) { bool font_info; api->GetBoolVariable("hocr_font_info", &font_info); - renderers->push_back( - new tesseract::TessHOcrRenderer(outputbase, font_info)); + tesseract::TessHOcrRenderer* renderer = + new tesseract::TessHOcrRenderer(outputbase, font_info); + if (renderer->happy()) { + renderers->push_back(renderer); + } else { + tprintf("Error, could not create hOCR output file: %s\n", + strerror(errno)); + } } api->GetBoolVariable("tessedit_create_tsv", &b); if (b) { bool font_info; api->GetBoolVariable("hocr_font_info", &font_info); - renderers->push_back( - new tesseract::TessTsvRenderer(outputbase, font_info)); + tesseract::TessTsvRenderer* renderer = + new tesseract::TessTsvRenderer(outputbase, font_info); + if (renderer->happy()) { + renderers->push_back(renderer); + } else { + tprintf("Error, could not create TSV output file: %s\n", + strerror(errno)); + } } api->GetBoolVariable("tessedit_create_pdf", &b); @@ -427,23 +440,51 @@ static void PreloadRenderers( #endif // WIN32 bool textonly; api->GetBoolVariable("textonly_pdf", &textonly); - renderers->push_back(new tesseract::TessPDFRenderer( - outputbase, api->GetDatapath(), textonly)); + tesseract::TessPDFRenderer* renderer = + new tesseract::TessPDFRenderer(outputbase, api->GetDatapath(), + textonly); + if (renderer->happy()) { + renderers->push_back(renderer); + } else { + tprintf("Error, could not create PDF output file: %s\n", + strerror(errno)); + } } api->GetBoolVariable("tessedit_write_unlv", &b); if (b) { - renderers->push_back(new tesseract::TessUnlvRenderer(outputbase)); + tesseract::TessUnlvRenderer* renderer = + new tesseract::TessUnlvRenderer(outputbase); + if (renderer->happy()) { + renderers->push_back(renderer); + } else { + tprintf("Error, could not create UNLV output file: %s\n", + strerror(errno)); + } } api->GetBoolVariable("tessedit_create_boxfile", &b); if (b) { - renderers->push_back(new tesseract::TessBoxTextRenderer(outputbase)); + tesseract::TessBoxTextRenderer* renderer = + new tesseract::TessBoxTextRenderer(outputbase); + if (renderer->happy()) { + renderers->push_back(renderer); + } else { + tprintf("Error, could not create BOX output file: %s\n", + strerror(errno)); + } } api->GetBoolVariable("tessedit_create_txt", &b); if (b || renderers->empty()) { - renderers->push_back(new tesseract::TessTextRenderer(outputbase)); + tesseract::TessTextRenderer* renderer = + new tesseract::TessTextRenderer(outputbase); + if (renderer->happy()) { + renderers->push_back(renderer); + } else { + tprintf("Error, could not create TXT output file: %s\n", + strerror(errno)); + } } }