Replace some snprintf by std::to_string and modernize more code

Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
Stefan Weil 2021-03-24 07:32:35 +01:00
parent 6ee69db22c
commit 595346d548
7 changed files with 25 additions and 35 deletions

View File

@ -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) {

View File

@ -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;

View File

@ -139,9 +139,7 @@ bool ParamUtils::GetParamAsString(const char *name, const ParamsVectors *member_
// Look for the parameter among int parameters.
auto *ip = FindParam<IntParam>(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 &param : vec->int_params) {
param->ResetToDefault();
}
for (i = 0; i < vec->bool_params.size(); ++i) {
vec->bool_params[i]->ResetToDefault();
for (auto &param : vec->bool_params) {
param->ResetToDefault();
}
for (auto &string_param : vec->string_params) {
string_param->ResetToDefault();
for (auto &param : vec->string_params) {
param->ResetToDefault();
}
for (auto &double_param : vec->double_params) {
double_param->ResetToDefault();
for (auto &param : vec->double_params) {
param->ResetToDefault();
}
}
}

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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()) {