From 595346d5482f099d78607d4b31efc84d8dc62b2e Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Wed, 24 Mar 2021 07:32:35 +0100 Subject: [PATCH] Replace some snprintf by std::to_string and modernize more code Signed-off-by: Stefan Weil --- src/api/baseapi.cpp | 7 ++----- src/api/tesseractmain.cpp | 5 ++--- src/ccutil/params.cpp | 25 +++++++++++-------------- src/textord/tabvector.cpp | 5 ++--- src/training/common/mastertrainer.cpp | 4 ++-- src/viewer/svutil.cpp | 12 +++++------- unittest/heap_test.cc | 2 +- 7 files changed, 25 insertions(+), 35 deletions(-) diff --git a/src/api/baseapi.cpp b/src/api/baseapi.cpp index 9546b227..bcba45c0 100644 --- a/src/api/baseapi.cpp +++ b/src/api/baseapi.cpp @@ -119,8 +119,6 @@ static const char *kInputFile = "noname.tif"; * Temp file used for storing current parameters before applying retry values. */ static const char *kOldVarsFile = "failed_vars.txt"; -/** Max string length of an int. */ -const int kMaxIntSize = 22; #ifndef DISABLED_LEGACY_ENGINE static const char kUnknownFontName[] = "UnknownFont"; @@ -1063,9 +1061,8 @@ bool TessBaseAPI::ProcessPagesMultipageTiff(const l_uint8 *data, size_t size, co // Only print page number for multipage TIFF file. tprintf("Page %d\n", page + 1); } - char page_str[kMaxIntSize]; - snprintf(page_str, kMaxIntSize - 1, "%d", page); - SetVariable("applybox_page", page_str); + auto page_string = std::to_string(page); + SetVariable("applybox_page", page_string.c_str()); bool r = ProcessPage(pix, page, filename, retry_config, timeout_millisec, renderer); pixDestroy(&pix); if (!r) { diff --git a/src/api/tesseractmain.cpp b/src/api/tesseractmain.cpp index f28cb5c4..7cc33e0f 100644 --- a/src/api/tesseractmain.cpp +++ b/src/api/tesseractmain.cpp @@ -686,9 +686,8 @@ int main(int argc, char **argv) { FixPageSegMode(api, pagesegmode); if (dpi) { - char dpi_string[255]; - snprintf(dpi_string, 254, "%d", dpi); - api.SetVariable("user_defined_dpi", dpi_string); + auto dpi_string = std::to_string(dpi); + api.SetVariable("user_defined_dpi", dpi_string.c_str()); } int ret_val = EXIT_SUCCESS; diff --git a/src/ccutil/params.cpp b/src/ccutil/params.cpp index e1c6a7d6..c4e087f0 100644 --- a/src/ccutil/params.cpp +++ b/src/ccutil/params.cpp @@ -139,9 +139,7 @@ bool ParamUtils::GetParamAsString(const char *name, const ParamsVectors *member_ // Look for the parameter among int parameters. auto *ip = FindParam(name, GlobalParams()->int_params, member_params->int_params); if (ip) { - char buf[128]; - snprintf(buf, sizeof(buf), "%d", int32_t(*ip)); - *value = buf; + *value = std::to_string(int32_t(*ip)); return true; } // Look for the parameter among bool parameters. @@ -157,7 +155,7 @@ bool ParamUtils::GetParamAsString(const char *name, const ParamsVectors *member_ std::ostringstream stream; stream.imbue(std::locale::classic()); stream << double(*dp); - *value = stream.str().c_str(); + *value = stream.str(); return true; } return false; @@ -191,21 +189,20 @@ void ParamUtils::PrintParams(FILE *fp, const ParamsVectors *member_params) { // Resets all parameters back to default values; void ParamUtils::ResetToDefaults(ParamsVectors *member_params) { - int v, i; int num_iterations = (member_params == nullptr) ? 1 : 2; - for (v = 0; v < num_iterations; ++v) { + for (int v = 0; v < num_iterations; ++v) { ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params; - for (i = 0; i < vec->int_params.size(); ++i) { - vec->int_params[i]->ResetToDefault(); + for (auto ¶m : vec->int_params) { + param->ResetToDefault(); } - for (i = 0; i < vec->bool_params.size(); ++i) { - vec->bool_params[i]->ResetToDefault(); + for (auto ¶m : vec->bool_params) { + param->ResetToDefault(); } - for (auto &string_param : vec->string_params) { - string_param->ResetToDefault(); + for (auto ¶m : vec->string_params) { + param->ResetToDefault(); } - for (auto &double_param : vec->double_params) { - double_param->ResetToDefault(); + for (auto ¶m : vec->double_params) { + param->ResetToDefault(); } } } diff --git a/src/textord/tabvector.cpp b/src/textord/tabvector.cpp index 483c0e62..cdafbd94 100644 --- a/src/textord/tabvector.cpp +++ b/src/textord/tabvector.cpp @@ -560,10 +560,9 @@ void TabVector::Display(ScrollView *tab_win) { tab_win->Pen(ScrollView::GREY); tab_win->Line(startpt_.x(), startpt_.y(), startpt_.x(), extended_ymin_); tab_win->Line(endpt_.x(), extended_ymax_, endpt_.x(), endpt_.y()); - char score_buf[64]; - snprintf(score_buf, sizeof(score_buf), "%d", percent_score_); + auto score_string = std::to_string(percent_score_); tab_win->TextAttributes("Times", 50, false, false, false); - tab_win->Text(startpt_.x(), startpt_.y(), score_buf); + tab_win->Text(startpt_.x(), startpt_.y(), score_string.c_str()); } #endif diff --git a/src/training/common/mastertrainer.cpp b/src/training/common/mastertrainer.cpp index c39df732..fe12d070 100644 --- a/src/training/common/mastertrainer.cpp +++ b/src/training/common/mastertrainer.cpp @@ -545,7 +545,7 @@ void MasterTrainer::SetupFlatShapeTable(ShapeTable *shape_table) { int num_shapes = flat_shapes_.NumShapes(); for (int s = 0; s < num_shapes; ++s) { int font = flat_shapes_.GetShape(s)[0].font_ids[0]; - int f = 0; + unsigned f = 0; for (f = 0; f < active_fonts.size(); ++f) { if (active_fonts[f] == font) { break; @@ -994,7 +994,7 @@ void MasterTrainer::ClusterShapes(int min_shapes, int max_shape_unichars, float } min_dist = kInfiniteDist; for (int s1 = 0; s1 < num_shapes; ++s1) { - for (int i = 0; i < shape_dists[s1].size(); ++i) { + for (unsigned i = 0; i < shape_dists[s1].size(); ++i) { if (shape_dists[s1][i].distance < min_dist) { min_dist = shape_dists[s1][i].distance; min_s1 = s1; diff --git a/src/viewer/svutil.cpp b/src/viewer/svutil.cpp index 184e96cc..e6482188 100644 --- a/src/viewer/svutil.cpp +++ b/src/viewer/svutil.cpp @@ -120,10 +120,9 @@ SVSemaphore::SVSemaphore() { # ifdef _WIN32 semaphore_ = CreateSemaphore(0, 0, 10, 0); # elif defined(__APPLE__) - char name[50]; - snprintf(name, sizeof(name), "%ld", random()); - sem_unlink(name); - semaphore_ = sem_open(name, O_CREAT, S_IWUSR, 0); + auto name = std::to_string(random()); + sem_unlink(name.c_str()); + semaphore_ = sem_open(name.c_str(), O_CREAT, S_IWUSR, 0); if (semaphore_ == SEM_FAILED) { perror("sem_open"); } @@ -270,8 +269,7 @@ SVNetwork::SVNetwork(const char *hostname, int port) { buffer_ptr_ = nullptr; struct addrinfo *addr_info = nullptr; - char port_str[40]; - snprintf(port_str, 40, "%d", port); + auto port_string = std::to_string(port); # ifdef _WIN32 // Initialize Winsock WSADATA wsaData; @@ -281,7 +279,7 @@ SVNetwork::SVNetwork(const char *hostname, int port) { } # endif // _WIN32 - if (getaddrinfo(hostname, port_str, nullptr, &addr_info) != 0) { + if (getaddrinfo(hostname, port_string.c_str(), nullptr, &addr_info) != 0) { std::cerr << "Error resolving name for ScrollView host " << std::string(hostname) << ":" << port << std::endl; # ifdef _WIN32 diff --git a/unittest/heap_test.cc b/unittest/heap_test.cc index 4d7cd2ed..1aa31fa5 100644 --- a/unittest/heap_test.cc +++ b/unittest/heap_test.cc @@ -50,7 +50,7 @@ public: std::sort(v->begin(), v->end()); // Check that we have increasing order. EXPECT_LT((*v)[0].key(), v->back().key()); - for (int i = 0; i < v->size(); ++i) { + for (unsigned i = 0; i < v->size(); ++i) { EXPECT_EQ((*v)[i].key(), heap->PeekTop().key()); // Indices don't necessarily match for equal keys, so don't test them. if (i + 1 < v->size() && (*v)[i + 1].key() == (*v)[i].key()) {