diff --git a/src/ccmain/paramsd.cpp b/src/ccmain/paramsd.cpp index 60de457b32..85e596d100 100644 --- a/src/ccmain/paramsd.cpp +++ b/src/ccmain/paramsd.cpp @@ -32,7 +32,7 @@ # include "svmnode.h" // for SVMenuNode # include "tesseractclass.h" // for Tesseract -# include // for fclose, fopen, fprintf, sprintf, FILE +# include // for fclose, fopen, fprintf, FILE # include // for atoi # include // for strcmp, strcspn, strlen, strncpy # include // for std::locale::classic @@ -319,16 +319,12 @@ ParamsEditor::ParamsEditor(tesseract::Tesseract *tess, ScrollView *sv) { // Write all (changed_) parameters to a config file. void ParamsEditor::WriteParams(char *filename, bool changes_only) { FILE *fp; // input file - char msg_str[255]; // if file exists if ((fp = fopen(filename, "rb")) != nullptr) { fclose(fp); - sprintf(msg_str, - "Overwrite file " - "%s" - "? (Y/N)", - filename); - int a = sv_window_->ShowYesNoDialog(msg_str); + std::stringstream msg; + msg << "Overwrite file " << filename << "? (Y/N)"; + int a = sv_window_->ShowYesNoDialog(msg.str().c_str()); if (a == 'n') { return; } // don't write diff --git a/src/ccmain/pgedit.cpp b/src/ccmain/pgedit.cpp index 9e4902bbb4..dd239851f5 100644 --- a/src/ccmain/pgedit.cpp +++ b/src/ccmain/pgedit.cpp @@ -36,6 +36,9 @@ #include #include +#include // for std::setprecision +#include // for std::locale::classic +#include // for std::stringstream #ifndef GRAPHICS_DISABLED namespace tesseract { @@ -140,32 +143,30 @@ static void show_point(PAGE_RES *page_res, float x, float y) { FCOORD pt(x, y); PAGE_RES_IT pr_it(page_res); - const int kBufsize = 512; - char msg[kBufsize]; - char *msg_ptr = msg; - - msg_ptr += sprintf(msg_ptr, "Pt:(%0.3f, %0.3f) ", x, y); + std::stringstream msg; + msg.imbue(std::locale::classic()); + msg << std::fixed << std::setprecision(3) << "Pt:(" << x << ", " << y << ") "; for (WERD_RES *word = pr_it.word(); word != nullptr; word = pr_it.forward()) { if (pr_it.row() != pr_it.prev_row() && pr_it.row()->row->bounding_box().contains(pt)) { - msg_ptr += sprintf(msg_ptr, "BL(x)=%0.3f ", pr_it.row()->row->base_line(x)); + msg << "BL(x)=" << pr_it.row()->row->base_line(x) << ' '; } if (word->word->bounding_box().contains(pt)) { TBOX box = word->word->bounding_box(); - msg_ptr += sprintf(msg_ptr, "Wd(%d, %d)/(%d, %d) ", box.left(), box.bottom(), box.right(), - box.top()); + msg << "Wd(" << box.left() << ", " << box.bottom() << ")/(" + << box.right() << ", " << box.top() << ") "; C_BLOB_IT cblob_it(word->word->cblob_list()); for (cblob_it.mark_cycle_pt(); !cblob_it.cycled_list(); cblob_it.forward()) { C_BLOB *cblob = cblob_it.data(); box = cblob->bounding_box(); if (box.contains(pt)) { - msg_ptr += sprintf(msg_ptr, "CBlb(%d, %d)/(%d, %d) ", box.left(), box.bottom(), - box.right(), box.top()); + msg << "CBlb(" << box.left() << ", " << box.bottom() << ")/(" + << box.right() << ", " << box.top() << ") "; } } } } - image_win->AddMessage(msg); + image_win->AddMessage(msg.str().c_str()); } /** @@ -622,7 +623,7 @@ void Tesseract::process_image_event( // action in image win break; default: - sprintf(msg, "Mode %d not yet implemented", mode); + snprintf(msg, sizeof(msg), "Mode %d not yet implemented", mode); image_win->AddMessage(msg); break; } diff --git a/src/ccutil/errcode.cpp b/src/ccutil/errcode.cpp index dddc1231b8..e6b05c213c 100644 --- a/src/ccutil/errcode.cpp +++ b/src/ccutil/errcode.cpp @@ -22,6 +22,8 @@ #include #include #include +#include // for std::cerr +#include // for std::stringstream namespace tesseract { @@ -41,37 +43,26 @@ void ERRCODE::error( // handle error const char *format, ... // special message ) const { va_list args; // variable args - char msg[MAX_MSG]; - char *msgptr = msg; + std::stringstream msg; if (caller != nullptr) { // name of caller - msgptr += sprintf(msgptr, "%s:", caller); + msg << caller << ':'; } // actual message - msgptr += sprintf(msgptr, "Error:%s", message); + msg << "Error:" << message; if (format != nullptr) { - msgptr += sprintf(msgptr, ":"); + char str[MAX_MSG]; va_start(args, format); // variable list -#ifdef _WIN32 - // print remainder - msgptr += _vsnprintf(msgptr, MAX_MSG - 2 - (msgptr - msg), format, args); - msg[MAX_MSG - 2] = '\0'; // ensure termination - strcat(msg, "\n"); -#else - // print remainder - msgptr += vsprintf(msgptr, format, args); - // no specific - msgptr += sprintf(msgptr, "\n"); -#endif + // print remainder + std::vsnprintf(str, sizeof(str), format, args); + // ensure termination + str[sizeof(str) - 1] = '\0'; va_end(args); - } else { - // no specific - msgptr += sprintf(msgptr, "\n"); + msg << ':' << str; } - // %s is needed here so msg is printed correctly! - fprintf(stderr, "%s", msg); + std::cerr << msg.str() << '\n'; switch (action) { case DBG: diff --git a/src/ccutil/unicharset.cpp b/src/ccutil/unicharset.cpp index 7f06f7cdc8..828c5285ee 100644 --- a/src/ccutil/unicharset.cpp +++ b/src/ccutil/unicharset.cpp @@ -314,10 +314,10 @@ std::string UNICHARSET::debug_utf8_str(const char *str) { step = UNICHAR::utf8_step(str + i); if (step == 0) { step = 1; - sprintf(hex, "%x", str[i]); + snprintf(hex, sizeof(hex), "%x", str[i]); } else { UNICHAR ch(str + i, step); - sprintf(hex, "%x", ch.first_uni()); + snprintf(hex, sizeof(hex), "%x", ch.first_uni()); } result += hex; result += " "; diff --git a/src/opencl/openclwrapper.cpp b/src/opencl/openclwrapper.cpp index 79817f2d12..15c477cbc9 100644 --- a/src/opencl/openclwrapper.cpp +++ b/src/opencl/openclwrapper.cpp @@ -812,7 +812,8 @@ int OpenclDevice::BinaryGenerated(const char *clFileName, FILE **fhandle) { cl_int clStatus; int status = 0; FILE *fd = nullptr; - char fileName[256] = {0}, cl_name[128] = {0}; + char fileName[256]; + char cl_name[128]; char deviceName[1024]; clStatus = clGetDeviceInfo(gpuEnv.mpArryDevsID[i], CL_DEVICE_NAME, sizeof(deviceName), deviceName, nullptr); @@ -820,7 +821,7 @@ int OpenclDevice::BinaryGenerated(const char *clFileName, FILE **fhandle) { const char *str = strstr(clFileName, ".cl"); memcpy(cl_name, clFileName, str - clFileName); cl_name[str - clFileName] = '\0'; - sprintf(fileName, "%s-%s.bin", cl_name, deviceName); + snprintf(fileName, sizeof(fileName), "%s-%s.bin", cl_name, deviceName); legalizeFileName(fileName); fd = fopen(fileName, "rb"); status = (fd != nullptr) ? 1 : 0; @@ -894,9 +895,9 @@ int OpenclDevice::GeneratBinFromKernelSource(cl_program program, const char *clF /* dump out each binary into its own separate file. */ for (i = 0; i < numDevices; i++) { - char fileName[256] = {0}, cl_name[128] = {0}; - if (binarySizes[i] != 0) { + char fileName[256]; + char cl_name[128]; char deviceName[1024]; clStatus = clGetDeviceInfo(mpArryDevsID[i], CL_DEVICE_NAME, sizeof(deviceName), deviceName, nullptr); @@ -905,7 +906,7 @@ int OpenclDevice::GeneratBinFromKernelSource(cl_program program, const char *clF const char *str = strstr(clFileName, ".cl"); memcpy(cl_name, clFileName, str - clFileName); cl_name[str - clFileName] = '\0'; - sprintf(fileName, "%s-%s.bin", cl_name, deviceName); + snprintf(fileName, sizeof(fileName), "%s-%s.bin", cl_name, deviceName); legalizeFileName(fileName); if (!WriteBinaryToFile(fileName, binaries[i], binarySizes[i])) { tprintf("[OD] write binary[%s] failed\n", fileName); diff --git a/src/wordrec/findseam.cpp b/src/wordrec/findseam.cpp index 74a0578c11..fdc347a1cb 100644 --- a/src/wordrec/findseam.cpp +++ b/src/wordrec/findseam.cpp @@ -103,7 +103,6 @@ void Wordrec::add_seam_to_queue(float new_priority, SEAM *new_seam, SeamQueue *s void Wordrec::choose_best_seam(SeamQueue *seam_queue, const SPLIT *split, PRIORITY priority, SEAM **seam_result, TBLOB *blob, SeamPile *seam_pile) { SEAM *seam; - char str[80]; float my_priority; /* Add seam of split */ my_priority = priority; @@ -133,7 +132,8 @@ void Wordrec::choose_best_seam(SeamQueue *seam_queue, const SPLIT *split, PRIORI seam->FullPriority(bbox.left(), bbox.right(), chop_overlap_knob, chop_centered_maxwidth, chop_center_knob, chop_width_change_knob); if (chop_debug) { - sprintf(str, "Full my_priority %0.0f, ", my_priority); + char str[80]; + snprintf(str, sizeof(str), "Full my_priority %0.0f, ", my_priority); seam->Print(str); }