diff --git a/src/Afterburner.cc b/src/Afterburner.cc index 6d070c2a15388915f77dd2fb8977ec4f76833c54..cb5b2cfe40f64c10c76f087f9adc3131351b9620 100644 --- a/src/Afterburner.cc +++ b/src/Afterburner.cc @@ -1131,7 +1131,7 @@ after_parini(AfterControl &globs, struct Variable *vars) else if (c >= 'a' && c <= 'z') namelist[i++] = c; else if (c >= 'A' && c <= 'Z') - namelist[i++] = tolower(c); + namelist[i++] = std::tolower(c); else c = ' '; diff --git a/src/CMOR.cc b/src/CMOR.cc index 55612d2288cda6997104020da7ee91c3ac2d68f7..337cc28ee41af185f4dc23ee04c54ce789973187 100644 --- a/src/CMOR.cc +++ b/src/CMOR.cc @@ -58,7 +58,7 @@ get_ifilevalue_code(int vlistID, const std::string value, int nvars) { std::string codestring; get_stringcode(vlistID, varID, codestring); - if (std::strcmp(codestring.c_str(), newcode) == 0) return varID; + if (codestring == newcode) return varID; } return CDI_UNDEFID; } @@ -70,19 +70,19 @@ get_ifilevalue_code(int vlistID, const std::string value, int nvars) } static int -get_ifilevalue_name(int vlistID, const std::string value, int nvars) +get_ifilevalue_name(int vlistID, const std::string &value, int nvars) { char ifilevalue[CDI_MAX_NAME]; for (int varID = 0; varID < nvars; ++varID) { vlistInqVarName(vlistID, varID, ifilevalue); - if (std::strcmp(ifilevalue, value.c_str()) == 0) return varID; + if (value == ifilevalue) return varID; } return CDI_UNDEFID; } static int -getVarIDToMap(int vlistID, int nvars, const std::string key, const std::string value) +getVarIDToMap(int vlistID, int nvars, const std::string &key, const std::string &value) { if (key.compare("code") == 0) return get_ifilevalue_code(vlistID, value, nvars); @@ -309,7 +309,7 @@ parse_string_to_values(const std::string workfile, const std::string pppline, in else if (std::isspace(static_cast<unsigned char>(trimmedLine[i]))) { break; } else if (trimmedLine[i] == '=') { - cdo_warning("Value to parse '%s' contains '='. This will not be used as assignment.", trimmedLine.c_str()); + cdo_warning("Value to parse '%s' contains '='. This will not be used as assignment.", trimmedLine); i++; } else { i++; } @@ -482,13 +482,13 @@ map_it(KVList *kvl, int vlistID, int varID, const std::string var2map) } static int -change_name_via_name(int vlistID, std::string map_name, const std::string cmor_name) +change_name_via_name(int vlistID, const std::string &map_name, const std::string &cmor_name) { char name[CDI_MAX_NAME]; for (int varID = 0; varID < vlistNvars(vlistID); ++varID) { vlistInqVarName(vlistID, varID, name); - if (std::strcmp(name, map_name.c_str()) == 0) + if (map_name == name) { cdiDefKeyString(vlistID, varID, CDI_KEY_NAME, parameter_to_word(cmor_name.c_str())); return 1; @@ -777,7 +777,7 @@ maptab_via_cn_and_key(KVList *kvl_oname, int vlistID, int nvars, const std::stri "You try to define a variable '%s' that is already in the input stream.\n" "The already existing infile variable will be renamed.\n" "This may lead to errors. Choose the respective var with '-selname' before applying cmor.", - kvcn->values[0].c_str()); + kvcn->values[0]); cdiDefKeyString(vlistID, newvar, CDI_KEY_NAME, "overwritten"); } if (Options::cdoVerbose) cdo_print("Started mapping of variable via '%s'.", key); @@ -818,7 +818,7 @@ maptab_via_cmd(std::string tablename, PMList pml, const std::string origValue, i "You try to define a variable '%s' that is already in the input stream.\n" "The already existing infile variable will be renamed.\n" "This may lead to errors. Choose the respective var with '-selname' before applying cmor.", - kvcn->values[0].c_str()); + kvcn->values[0]); cdiDefKeyString(vlistID, newvar, CDI_KEY_NAME, "overwritten"); } if (!cmorVarLine.size()) @@ -1340,7 +1340,7 @@ check_mem(KVList *kvl, std::string project_id) indexvalues[indexint] = atol((const char *) indexstring.c_str()); if (!indexvalues[indexint]) { - cdo_warning("Could not parse value '%s' of attribute '%s' to integer.", indexstring, index->c_str()); + cdo_warning("Could not parse value '%s' of attribute '%s' to integer.", indexstring, index); lallindices = false; } else @@ -1422,7 +1422,7 @@ check_mem(KVList *kvl, std::string project_id) indexvalues[indexint] = atol((const char *) indexstring.c_str()); if (!indexvalues[indexint]) { - cdo_warning("Could not parse value '%s' of attribute '%s' to integer.", indexstring, index->c_str()); + cdo_warning("Could not parse value '%s' of attribute '%s' to integer.", indexstring, index); lallindices = false; } else @@ -3657,7 +3657,7 @@ register_projection(int *grid_ids, int projID, double *ycoord_vals, double *xcoo { if (projtype == CDI_PROJ_RLL) { - if (std::strcmp(attname, p_rll[i].c_str()) == 0) + if (p_rll[i] == attname) { parameter_values[i] = attflt[0]; break; @@ -3665,7 +3665,7 @@ register_projection(int *grid_ids, int projID, double *ycoord_vals, double *xcoo } else if (projtype == CDI_PROJ_LCC) { - if (std::strcmp(attname, p_lcc[i].c_str()) == 0) + if (p_lcc[i] == attname) { parameter_values[i] = attflt[0]; break; @@ -3673,7 +3673,7 @@ register_projection(int *grid_ids, int projID, double *ycoord_vals, double *xcoo } else if (projtype == CDI_PROJ_STERE) { - if (std::strcmp(attname, p_ps[i].c_str()) == 0) + if (p_ps[i] == attname) { parameter_values[i] = attflt[0]; break; diff --git a/src/Importcmsaf.cc b/src/Importcmsaf.cc index 2b8af0ab6576ba131f5fff73c2575271f0a38bab..3b49edc04fb4c2547c9f5c7dd6f31a8dd67deea7 100644 --- a/src/Importcmsaf.cc +++ b/src/Importcmsaf.cc @@ -16,7 +16,7 @@ #endif #include <cdi.h> -#include <stdlib.h> // realloc() +#include <cstdlib> // realloc() #include "cdo_options.h" #include "varray.h" @@ -802,7 +802,7 @@ read_dataset(hid_t loc_id, const char *name, void *opdata) atype_class = H5Tget_class(atype); len = strlen(attname); - for (int k = 0; k < len; ++k) attname[k] = tolower(attname[k]); + for (int k = 0; k < len; ++k) attname[k] = std::tolower(attname[k]); if (cdo_cmpstr(attname, "intercept") || cdo_cmpstr(attname, "offset")) { diff --git a/src/Setpartab.cc b/src/Setpartab.cc index d452f413046b2404bcad651637b8eb6ebc5c1cda..33543d50ea90c4b95d85bd84f35859e6abbdfaab 100644 --- a/src/Setpartab.cc +++ b/src/Setpartab.cc @@ -68,35 +68,35 @@ mapvar(int vlistID, int varID, const KeyValues &kv, CmorVar &cmorVar, bool &hasV } if (key == "standard_name") { - if (not lv1) cdo_abort("%s can only have one string value!", key.c_str()); + if (not lv1) cdo_abort("%s can only have one string value!", key); cdiDefKeyString(vlistID, varID, CDI_KEY_STDNAME, value.c_str()); } else if (key == "long_name") { - if (not lv1) cdo_abort("%s can only have one string value!", key.c_str()); + if (not lv1) cdo_abort("%s can only have one string value!", key); cdiDefKeyString(vlistID, varID, CDI_KEY_LONGNAME, value.c_str()); } else if (key == "units") { - if (not lv1) cdo_abort("%s can only have one string value!", key.c_str()); + if (not lv1) cdo_abort("%s can only have one string value!", key); cdiDefKeyString(vlistID, varID, CDI_KEY_UNITS, value.c_str()); } else if (key == "filterspec") { - if (not lv1) cdo_abort("%s can only have one string value!", key.c_str()); + if (not lv1) cdo_abort("%s can only have one string value!", key); cdiDefKeyString(vlistID, varID, CDI_KEY_FILTERSPEC, value.c_str()); } else if (key == "name") { if (isnPtmodeName) { - if (not lv1) cdo_abort("%s can only have one string value!", key.c_str()); + if (not lv1) cdo_abort("%s can only have one string value!", key); cdiDefKeyString(vlistID, varID, CDI_KEY_NAME, parameter_to_word(value.c_str())); } } else if (key == "out_name") { - if (not lv1) cdo_abort("%s can only have one string value!", key.c_str()); + if (not lv1) cdo_abort("%s can only have one string value!", key); auto outname = parameter_to_word(value); if (cmorVar.name != outname) { diff --git a/src/Specinfo.cc b/src/Specinfo.cc index a1783d411c61258bc4c0daba8eb32f401e262a5d..07622027e486ba6c2bc628c9009048755f271869 100644 --- a/src/Specinfo.cc +++ b/src/Specinfo.cc @@ -230,7 +230,7 @@ public: using Process::Process; inline static CdoModule module = { .name = "Specinfo", - .operators = { { "specinfo"} }, + .operators = { { "specinfo" } }, .aliases = {}, .mode = EXPOSED, // Module mode: 0:intern 1:extern .number = CDI_REAL, // Allowed number type @@ -480,14 +480,13 @@ public: void init() override { - operator_input_arg("Txx, TLxx, NLON=xx, NLAT=xx, NIxx or ICONRyyLxx"); long len = cdo_operator_argv(0).size(); if ((len + 1) >= 128) cdo_abort("Parameter string too large!"); - for (long i = 0; i < len; ++i) arg[i] = toupper(cdo_operator_argv(0)[i]); + for (long i = 0; i < len; ++i) arg[i] = std::toupper(cdo_operator_argv(0)[i]); arg[len] = 0; argument = std::string(cdo_operator_argv(0)); diff --git a/src/cdo.cc b/src/cdo.cc index 1cce6046186b7c9c3abf343a377f5bd6333c7a5d..5a3bb7d3315d03e10070727a419e1198c0d2f69b 100644 --- a/src/cdo.cc +++ b/src/cdo.cc @@ -104,7 +104,7 @@ cdo_stackframe() if (messages) { for (int i = 0; i < frames; ++i) fprintf(stderr, "[bt] %s\n", messages[i]); - free(messages); + std::free(messages); } #endif } @@ -355,7 +355,7 @@ set_default_datatype(const std::string &datatypeString) int dtype = -1; auto datatypestr = datatypeString.c_str(); - auto datatype = tolower(*datatypestr); + auto datatype = std::tolower(*datatypestr); // clang-format off if (datatype == 'i') { dtype = D_INT; datatypestr++; } else if (datatype == 'u') { dtype = D_UINT; datatypestr++; } @@ -498,10 +498,10 @@ alignof_malloc_data(const std::vector<int> &tsize) -> int for (size_t i = 0; i < n; ++i) { - ptr[i] = (double *) malloc(tsize[i] * sizeof(double)); + ptr[i] = (double *) std::malloc(tsize[i] * sizeof(double)); align = std::min(align, alignof_address(ptr[i])); } - for (auto &p : ptr) free(p); + for (auto &p : ptr) std::free(p); return align; } @@ -628,14 +628,17 @@ get_env_vars() ->add_default("true") ->add_help("'false' don't write information to the global history attribute [default: true]."); - cdo::File_Suffix[0] = 0; CLIOptions::envvar("CDO_FILE_SUFFIX") - ->add_effect([&](const std::string &envstr) { strncat(cdo::File_Suffix, envstr.c_str(), sizeof(cdo::File_Suffix) - 1); }) + ->add_effect([&](const std::string &envstr) { + if (envstr.size()) cdo::FileSuffix = envstr; + }) ->describe_argument("suffix") ->add_help("Default filename suffix."); - CLIOptions::envvar("CDO_DISABLE_FILESUFFIX") - ->add_effect([&]() { std::strcat(cdo::File_Suffix, "NULL"); }) + CLIOptions::envvar("CDO_DISABLE_FILE_SUFFIX") + ->add_effect([&](const std::string &envstr) { + if (parameter_to_bool(envstr)) cdo::FileSuffix = "NULL"; + }) ->describe_argument("true|false") ->add_help("'true' disables file suffix."); @@ -650,9 +653,9 @@ static void print_system_info() { fprintf(stderr, "\n"); - fprintf(stderr, "CDO_Color = %d\n", mpmo_get_color_mode()); - fprintf(stderr, "Options::CDO_Reset_History = %d\n", Options::CDO_Reset_History); - fprintf(stderr, "CDO_File_Suffix = %s\n", cdo::File_Suffix); + fprintf(stderr, "CDO_Color = %d\n", mpmo_get_color_mode()); + fprintf(stderr, "Options::CDO_Reset_History = %d\n", Options::CDO_Reset_History); + fprintf(stderr, "CDO_FileSuffix = %s\n", cdo::FileSuffix.c_str()); fprintf(stderr, "CdoDefault::FileType = %d\n", CdoDefault::FileType); fprintf(stderr, "CdoDefault::DataType = %d\n", CdoDefault::DataType); fprintf(stderr, "CdoDefault::Byteorder = %d\n", CdoDefault::Byteorder); @@ -897,7 +900,6 @@ set_external_proj_func(void) #ifdef HAVE_CDI_PROJ_FUNCS proj_lonlat_to_lcc_func = proj_lonlat_to_lcc; proj_lcc_to_lonlat_func = proj_lcc_to_lonlat; - proj_lonlat_to_stere_func = proj_lonlat_to_stere; proj_stere_to_lonlat_func = proj_stere_to_lonlat; #endif @@ -994,7 +996,7 @@ create_options_from_envvars() { CLIOptions::option_from_envvar("CDO_VERSION_INFO"); - CLIOptions::option_from_envvar("CDO_DISABLE_FILESUFFIX"); + CLIOptions::option_from_envvar("CDO_DISABLE_FILE_SUFFIX"); CLIOptions::option_from_envvar("CDO_FILE_SUFFIX"); CLIOptions::option_from_envvar("CDO_DISABLE_HISTORY")->set_category("History"); diff --git a/src/cdo_options.cc b/src/cdo_options.cc index 6035ee8fa2cd88034f165ebe135a4a496c7f4b13..96e04a5194d844fdf6b7baefaa7b8ec6fb2e82ff 100644 --- a/src/cdo_options.cc +++ b/src/cdo_options.cc @@ -25,8 +25,8 @@ iTimer writeTimer; namespace cdo { const char *progname; -char File_Suffix[32]; const char *Version = "Climate Data Operators version " VERSION " (https://mpimet.mpg.de/cdo)"; +std::string FileSuffix; std::string DownloadPath; std::string IconGrids; bool stdinIsTerminal = false; @@ -139,7 +139,7 @@ set_compression(int streamID, int filetype) if (Options::cdoCompType != CDI_COMPRESS_NONE) { -/* streamDefShuffle(streamID, Options::cdoShuffle);*/ + /* streamDefShuffle(streamID, Options::cdoShuffle);*/ streamDefCompType(streamID, Options::cdoCompType); streamDefCompLevel(streamID, Options::cdoCompLevel); diff --git a/src/cdo_options.h b/src/cdo_options.h index 0a911fa6518e9d53f97b9eac9e46e16767dbe551..43c3ade6e4f1165c2b04a92fb1675c26342b5abe 100644 --- a/src/cdo_options.h +++ b/src/cdo_options.h @@ -14,8 +14,8 @@ namespace cdo { extern const char *progname; -extern char File_Suffix[32]; extern const char *Version; +extern std::string FileSuffix; extern std::string DownloadPath; extern std::string IconGrids; extern bool stdinIsTerminal; diff --git a/src/namelistdump.cc b/src/namelistdump.cc index 888fff6dd31b4d8348e6cf75bba20e931afe68c8..1ca4f6474030479f2156126785850da14bbdaa17 100644 --- a/src/namelistdump.cc +++ b/src/namelistdump.cc @@ -5,10 +5,10 @@ */ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <errno.h> +#include <cstdio> +#include <cstdlib> +#include <cstring> +#include <cerrno> #include <sys/stat.h> #include "namelist.h" @@ -41,7 +41,7 @@ main(int argc, char *argv[]) return -1; } - char *buffer = (char *) malloc(filesize); + char *buffer = (char *) std::malloc(filesize); size_t nitems = fread(buffer, 1, filesize, fp); std::fclose(fp); @@ -49,7 +49,7 @@ main(int argc, char *argv[]) if (nitems != filesize) { fprintf(stderr, "Read failed on %s!\n", filename); - free(buffer); + std::free(buffer); return -1; } @@ -82,7 +82,7 @@ main(int argc, char *argv[]) p.dump(buffer); - free(buffer); + std::free(buffer); return 0; } diff --git a/src/percentiles_hist.cc b/src/percentiles_hist.cc index 82138f35a63237ad7df7ddfde96373c72e78456e..5cf10485460ed7cb26bebb5cab1a1d902c1c3697 100644 --- a/src/percentiles_hist.cc +++ b/src/percentiles_hist.cc @@ -6,9 +6,9 @@ */ +#include <cstdlib> #include <climits> #include <algorithm> -#include <stdlib.h> #include <cdi.h> @@ -278,7 +278,7 @@ HistogramSet::createVarLevels(int varID, int nlevels, size_t nhists) auto vsize = isUint32 ? sizeof(unsigned int) : sizeof(unsigned short); hists[histID].isUint32 = isUint32; hists[histID].capacity = FLT_CAPACITY(nbins, vsize); - hists[histID].ptr = malloc(nbins * vsize); + hists[histID].ptr = std::malloc(nbins * vsize); if (hists[histID].ptr == nullptr) cdo_abort("Not enough memory (%s)", __func__); } } diff --git a/src/percentiles_hist.h b/src/percentiles_hist.h index 6c1027dce7235add59e97a3735e902d4909361a7..a63c023d925df41eaa17ffda5fbfabd615ee3024 100644 --- a/src/percentiles_hist.h +++ b/src/percentiles_hist.h @@ -1,6 +1,7 @@ #ifndef PERCENTILES_HIST_H_ #define PERCENTILES_HIST_H_ +#include <cstdlib> #include <cassert> #include "field.h" @@ -68,10 +69,10 @@ public: { for (auto varID = nvars; varID-- > 0;) { - const auto nhists = this->var_nhists[varID]; + auto nhists = this->var_nhists[varID]; for (auto levelID = this->var_nlevels[varID]; levelID-- > 0;) { - for (auto histID = nhists; histID-- > 0;) free(this->histograms[varID][levelID][histID].ptr); + for (auto histID = nhists; histID-- > 0;) std::free(this->histograms[varID][levelID][histID].ptr); } } } diff --git a/src/pmlist.cc b/src/pmlist.cc index 37eb39dcb7f90ba38b85c73550ed58be386752b8..ed2491fb02aa82139402205bd5ce55966cfb1f4d 100644 --- a/src/pmlist.cc +++ b/src/pmlist.cc @@ -203,7 +203,7 @@ get_number_of_values(const unsigned long ntok, const NamelistToken *tokens) static void replace_name(char *name) { - for (size_t pos = 0; pos < strlen(name); pos++) name[pos] = tolower(name[pos]); + for (size_t pos = 0; pos < strlen(name); pos++) name[pos] = std::tolower(name[pos]); if (cdo_cmpstr(name, "conventions")) std::strcpy(name, "Conventions"); if (cdo_cmpstr(name, "cn")) std::strcpy(name, "cmor_name"); diff --git a/src/process.cc b/src/process.cc index 2e7a48f8e074a53dc3d35333e3af1a798ea8f4d8..f9b21fc8f128fae4be557d2429c62a4b8362fe6c 100644 --- a/src/process.cc +++ b/src/process.cc @@ -183,9 +183,9 @@ Process::start_thread() Debug(PROCESS, "starting new thread for process %d", m_ID); pthread_attr_t attr; auto status = pthread_attr_init(&attr); - if (status) cdo_sys_error("pthread_attr_init failed for '%s'", operatorName.c_str()); + if (status) cdo_sys_error("pthread_attr_init failed for '%s'", operatorName); status = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - if (status) cdo_sys_error("pthread_attr_setdetachstate failed for '%s'", operatorName.c_str()); + if (status) cdo_sys_error("pthread_attr_setdetachstate failed for '%s'", operatorName); /* param.sched_priority = 0; status = pthread_attr_setschedparam(&attr, ¶m); @@ -215,7 +215,7 @@ Process::start_thread() if (rval != 0) { errno = rval; - cdo_sys_error("pthread_create failed for '%s'", operatorName.c_str()); + cdo_sys_error("pthread_create failed for '%s'", operatorName); } return thrID; diff --git a/src/util_files.cc b/src/util_files.cc index 4790156226166facb864a24f85c18f64d7e99b7a..8b692bf16d5317ba695bb43bd5930cabb6ab7c3d 100644 --- a/src/util_files.cc +++ b/src/util_files.cc @@ -1,9 +1,9 @@ #include <cdi.h> -#include <stdlib.h> -#include <string.h> -#include <stdio.h> -#include <ctype.h> +#include <cstdlib> +#include <cstring> +#include <cstdio> +#include <cctype> #include <sys/stat.h> #include "util_files.h" @@ -31,11 +31,11 @@ FileUtils::file_exists(const std::string &fileName) } else */ - { - struct stat buf; - auto status = stat(fileName.c_str(), &buf); - return (status == 0) && (S_ISREG(buf.st_mode) && buf.st_size > 0); - } + { + struct stat buf; + auto status = stat(fileName.c_str(), &buf); + return (status == 0) && (S_ISREG(buf.st_mode) && buf.st_size > 0); + } } bool @@ -77,7 +77,7 @@ FileUtils::size(const char *filename) } static std::string -gen_file_suffix(int filetype, const std::string &referenceName, int vlistID) +gen_filesuffix(int filetype, const std::string &referenceName, int vlistID) { std::string suffix; auto foundSuffix = false; @@ -94,7 +94,7 @@ gen_file_suffix(int filetype, const std::string &referenceName, int vlistID) if (pos > 1 && pos < (referenceName.size() - 1)) { auto result = referenceName.substr(pos + 1); - auto firstchar = tolower(result[1]); + auto firstchar = std::tolower(result[1]); switch (firstchar) { case 'g': @@ -121,7 +121,7 @@ gen_file_suffix(int filetype, const std::string &referenceName, int vlistID) { for (int i = 0, n = result.size(); i < n; ++i) { - if (result[i] == '.' || isalnum(result[i])) suffix += result[i]; + if (result[i] == '.' || std::isalnum(result[i])) suffix += result[i]; } } } @@ -144,10 +144,10 @@ std::string FileUtils::gen_suffix(int filetype, int vlistID, const std::string &referenceName) { std::string suffix; - if (std::strncmp(cdo::File_Suffix, "NULL", 4) != 0) + if (cdo::FileSuffix != "NULL") { - if (cdo::File_Suffix[0] != 0) { suffix = cdo::File_Suffix; } - else { suffix = gen_file_suffix(filetype, referenceName, vlistID); } + if (cdo::FileSuffix.size()) { suffix = cdo::FileSuffix; } + else { suffix = gen_filesuffix(filetype, referenceName, vlistID); } } return suffix; } diff --git a/src/util_string.cc b/src/util_string.cc index b72c163cb5dceeddb48022332be4f4756051c3c3..2ba753c66961d98c6ca34e98a6b71e528596404e 100644 --- a/src/util_string.cc +++ b/src/util_string.cc @@ -112,14 +112,14 @@ void cstr_to_lower(char *cstr) { if (cstr) - for (size_t i = 0; cstr[i]; ++i) cstr[i] = (char) tolower((int) cstr[i]); + for (size_t i = 0; cstr[i]; ++i) cstr[i] = (char) std::tolower((int) cstr[i]); } void cstr_to_upper(char *cstr) { if (cstr) - for (size_t i = 0; cstr[i]; ++i) cstr[i] = (char) toupper((int) cstr[i]); + for (size_t i = 0; cstr[i]; ++i) cstr[i] = (char) std::toupper((int) cstr[i]); } static void