diff --git a/src/emc/ini/emcIniFile.hh b/src/emc/ini/emcIniFile.hh index edd55b90c18..535d387d66f 100644 --- a/src/emc/ini/emcIniFile.hh +++ b/src/emc/ini/emcIniFile.hh @@ -76,10 +76,9 @@ public: return(IniFile::Find(result, tag, section, num)); } - const char * Find(const char *tag, const char *section=NULL, + std::optional Find(const char *tag, const char *section=NULL, int num = 1){ - return(IniFile::Find(tag, section, num) - .value_or(nullptr)); + return(IniFile::Find(tag, section, num)); } private: diff --git a/src/emc/ini/inijoint.cc b/src/emc/ini/inijoint.cc index ae1dbb61386..c7647da40bd 100644 --- a/src/emc/ini/inijoint.cc +++ b/src/emc/ini/inijoint.cc @@ -73,7 +73,6 @@ extern value_inihal_data old_inihal_data; static int loadJoint(int joint, EmcIniFile *jointIniFile) { char jointString[16]; - const char *inistring; EmcJointType jointType; double units; double backlash; @@ -234,8 +233,9 @@ static int loadJoint(int joint, EmcIniFile *jointIniFile) comp_file_type = 0; // default jointIniFile->Find(&comp_file_type, "COMP_FILE_TYPE", jointString); - if (NULL != (inistring = jointIniFile->Find("COMP_FILE", jointString))) { - if (0 != emcJointLoadComp(joint, inistring, comp_file_type)) { + auto comp_file = jointIniFile->Find("COMP_FILE", jointString); + if (comp_file) { + if (0 != emcJointLoadComp(joint, comp_file->c_str(), comp_file_type)) { return -1; } } diff --git a/src/emc/ini/initraj.cc b/src/emc/ini/initraj.cc index f6389933e95..dade6236594 100644 --- a/src/emc/ini/initraj.cc +++ b/src/emc/ini/initraj.cc @@ -109,33 +109,33 @@ static int loadTraj(EmcIniFile *trajInifile) try{ int axismask = 0; - const char *coord = trajInifile->Find("COORDINATES", "TRAJ"); + auto coord = trajInifile->Find("COORDINATES", "TRAJ"); if(coord) { - if(strchr(coord, 'x') || strchr(coord, 'X')) { + if(coord->find_first_of("xX") != std::string::npos) { axismask |= 1; } - if(strchr(coord, 'y') || strchr(coord, 'Y')) { + if(coord->find_first_of("yY") != std::string::npos) { axismask |= 2; } - if(strchr(coord, 'z') || strchr(coord, 'Z')) { + if(coord->find_first_of("zZ") != std::string::npos) { axismask |= 4; } - if(strchr(coord, 'a') || strchr(coord, 'A')) { + if(coord->find_first_of("aA") != std::string::npos) { axismask |= 8; } - if(strchr(coord, 'b') || strchr(coord, 'B')) { + if(coord->find_first_of("bB") != std::string::npos) { axismask |= 16; } - if(strchr(coord, 'c') || strchr(coord, 'C')) { + if(coord->find_first_of("cC") != std::string::npos) { axismask |= 32; } - if(strchr(coord, 'u') || strchr(coord, 'U')) { + if(coord->find_first_of("uU") != std::string::npos) { axismask |= 64; } - if(strchr(coord, 'v') || strchr(coord, 'V')) { + if(coord->find_first_of("vV") != std::string::npos) { axismask |= 128; } - if(strchr(coord, 'w') || strchr(coord, 'W')) { + if(coord->find_first_of("wW") != std::string::npos) { axismask |= 256; } } else { @@ -295,7 +295,6 @@ static int loadTraj(EmcIniFile *trajInifile) return -1; } try{ - const char *inistring; unsigned char coordinateMark[6] = { 1, 1, 1, 0, 0, 0 }; int t; int len; @@ -303,14 +302,15 @@ static int loadTraj(EmcIniFile *trajInifile) char home[LINELEN]; EmcPose homePose = { {0.0, 0.0, 0.0}, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }; double d; - if (NULL != (inistring = trajInifile->Find("HOME", "TRAJ"))) { + auto inistring = trajInifile->Find("HOME", "TRAJ"); + if (inistring) { // [TRAJ]HOME is important for genhexkins.c kinetmaticsForward() // and probably other non-identity kins that solve the forward // kinematics with an iterative algorithm when the homePose // is not all zeros // found it, now interpret it according to coordinateMark[] - rtapi_strxcpy(homes, inistring); + rtapi_strxcpy(homes, inistring->c_str()); len = 0; for (t = 0; t < 6; t++) { if (!coordinateMark[t]) { @@ -354,7 +354,7 @@ static int loadTraj(EmcIniFile *trajInifile) } else { // badly formatted entry rcs_print("invalid INI file value for [TRAJ] HOME: %s\n", - inistring); + inistring->c_str()); return -1; } } // end of for-loop on coordinateMark[] diff --git a/src/emc/pythonplugin/python_plugin.cc b/src/emc/pythonplugin/python_plugin.cc index f59cd80d163..b9462a8841f 100644 --- a/src/emc/pythonplugin/python_plugin.cc +++ b/src/emc/pythonplugin/python_plugin.cc @@ -339,7 +339,6 @@ int PythonPlugin::configure(const char *iniFilename, const char *section) { IniFile inifile; - std::optional inistring; if (section == NULL) { logPP(1, "no section"); @@ -360,16 +359,16 @@ int PythonPlugin::configure(const char *iniFilename, char real_path[PATH_MAX]; char expandinistring[PATH_MAX]; - if ((inistring = inifile.Find("TOPLEVEL", section))) { - if (inifile.TildeExpansion(*inistring,expandinistring,sizeof(expandinistring))) { + if (auto inistring = inifile.Find("TOPLEVEL", section)) { + if (inifile.TildeExpansion(inistring->c_str(),expandinistring,sizeof(expandinistring))) { logPP(-1, "TildeExpansion failed '%s'", toplevel); status = PLUGIN_BAD_PATH; return status; } toplevel = strstore(expandinistring); - if ((inistring = inifile.Find("RELOAD_ON_CHANGE", section))) - reload_on_change = (atoi(*inistring) > 0); + if (auto reload_str = inifile.Find("RELOAD_ON_CHANGE", section)) + reload_on_change = (atoi(reload_str->c_str()) > 0); if (realpath(toplevel, real_path) == NULL) { logPP(-1, "can\'t resolve path to '%s'", toplevel); @@ -394,16 +393,16 @@ int PythonPlugin::configure(const char *iniFilename, abs_path = strstore(real_path); } - if ((inistring = inifile.Find("LOG_LEVEL", section))) - log_level = atoi(*inistring); + if (auto inistring = inifile.Find("LOG_LEVEL", section)) + log_level = atoi(inistring->c_str()); else log_level = 0; char pycmd[PATH_MAX + 64]; int n = 1; int lineno; - while ((inistring = inifile.Find("PATH_PREPEND", "PYTHON", - n, &lineno))) { - if (inifile.TildeExpansion(*inistring,expandinistring,sizeof(expandinistring))) { + while (auto inistring = inifile.Find("PATH_PREPEND", "PYTHON", + n, &lineno)) { + if (inifile.TildeExpansion(inistring->c_str(),expandinistring,sizeof(expandinistring))) { logPP(-1, "TildeExpansion failed '%s'", toplevel); status = PLUGIN_EXCEPTION_DURING_PATH_PREPEND; return status; @@ -420,9 +419,9 @@ int PythonPlugin::configure(const char *iniFilename, n++; } n = 1; - while ((inistring = inifile.Find("PATH_APPEND", "PYTHON", - n, &lineno))) { - if (inifile.TildeExpansion(*inistring,expandinistring,sizeof(expandinistring))) { + while (auto inistring = inifile.Find("PATH_APPEND", "PYTHON", + n, &lineno)) { + if (inifile.TildeExpansion(inistring->c_str(),expandinistring,sizeof(expandinistring))) { logPP(-1, "TildeExpansion failed '%s'", toplevel); status = PLUGIN_EXCEPTION_DURING_PATH_APPEND; return status; diff --git a/src/emc/rs274ngc/interp_namedparams.cc b/src/emc/rs274ngc/interp_namedparams.cc index cb0127698b4..61704960ed0 100644 --- a/src/emc/rs274ngc/interp_namedparams.cc +++ b/src/emc/rs274ngc/interp_namedparams.cc @@ -964,7 +964,6 @@ double Interp::inicheck() { IniFile inifile; const char *filename; - std::optional inistring; double result = -1.0; if ((filename = getenv("INI_FILE_NAME")) == NULL) { @@ -976,8 +975,8 @@ double Interp::inicheck() return -1.0; } - if ((inistring = inifile.Find("LINEAR_UNITS", "TRAJ"))) { - if (!strcmp(*inistring, "inch")) { + if (auto inistring = inifile.Find("LINEAR_UNITS", "TRAJ")) { + if (*inistring == "inch") { result = 0.0; } else { result = 1.0; diff --git a/src/emc/rs274ngc/rs274ngc_pre.cc b/src/emc/rs274ngc/rs274ngc_pre.cc index a2fc8758530..c74799d32cc 100644 --- a/src/emc/rs274ngc/rs274ngc_pre.cc +++ b/src/emc/rs274ngc/rs274ngc_pre.cc @@ -884,7 +884,6 @@ int Interp::init() fprintf(stderr,"Unable to open inifile:%s:\n", iniFileName); } else { bool opt; - std::optional inistring; inifile.Find(&_setup.tool_change_at_g30, "TOOL_CHANGE_AT_G30", "EMCIO"); inifile.Find(&_setup.tool_change_quill_up, "TOOL_CHANGE_QUILL_UP", "EMCIO"); @@ -920,14 +919,14 @@ int Interp::init() inifile.Find(&opt, "OWORD_WARNONLY", "RS274NGC"); if (opt) _setup.feature_set |= FEATURE_OWORD_WARNONLY; - if ((inistring =inifile.Find("LOCKING_INDEXER_JOINT", "AXIS_A"))) { - _setup.a_indexer_jnum = atol(*inistring); + if (auto inistring = inifile.Find("LOCKING_INDEXER_JOINT", "AXIS_A")) { + _setup.a_indexer_jnum = atol(inistring->c_str()); } - if ((inistring =inifile.Find("LOCKING_INDEXER_JOINT", "AXIS_B"))) { - _setup.b_indexer_jnum = atol(*inistring); + if (auto inistring = inifile.Find("LOCKING_INDEXER_JOINT", "AXIS_B")) { + _setup.b_indexer_jnum = atol(inistring->c_str()); } - if ((inistring =inifile.Find("LOCKING_INDEXER_JOINT", "AXIS_C"))) { - _setup.c_indexer_jnum = atol(*inistring); + if (auto inistring = inifile.Find("LOCKING_INDEXER_JOINT", "AXIS_C")) { + _setup.c_indexer_jnum = atol(inistring->c_str()); } inifile.Find(&_setup.orient_offset, "ORIENT_OFFSET", "RS274NGC"); inifile.Find(&_setup.parameter_g73_peck_clearance, "G73_PECK_CLEARANCE", "RS274NGC"); @@ -937,18 +936,18 @@ int Interp::init() _setup.debugmask |= EMC_DEBUG_UNCONDITIONAL; - if((inistring = inifile.Find("LOG_LEVEL", "RS274NGC"))) + if(auto inistring = inifile.Find("LOG_LEVEL", "RS274NGC")) { - _setup.loggingLevel = atol(*inistring); + _setup.loggingLevel = atol(inistring->c_str()); } // default the log_file to stderr. - if((inistring = inifile.Find("LOG_FILE", "RS274NGC"))) + if(auto inistring = inifile.Find("LOG_FILE", "RS274NGC")) { - if ((log_file = fopen(*inistring, "a")) == NULL) { + if ((log_file = fopen(inistring->c_str(), "a")) == NULL) { log_file = stderr; logDebug( "(%d): Unable to open log file:%s, using stderr", - getpid(), *inistring); + getpid(), inistring->c_str()); } } else { log_file = stderr; @@ -957,30 +956,30 @@ int Interp::init() _setup.use_lazy_close = 1; _setup.wizard_root[0] = 0; - if((inistring = inifile.Find("WIZARD_ROOT", "WIZARD"))) + if(auto inistring = inifile.Find("WIZARD_ROOT", "WIZARD")) { - logDebug("[WIZARD]WIZARD_ROOT:%s", *inistring); - if (realpath(*inistring, _setup.wizard_root) == NULL) { + logDebug("[WIZARD]WIZARD_ROOT:%s", inistring->c_str()); + if (realpath(inistring->c_str(), _setup.wizard_root) == NULL) { //realpath didn't find the file - logDebug("realpath failed to find wizard_root:%s:", *inistring); + logDebug("realpath failed to find wizard_root:%s:", inistring->c_str()); } } logDebug("_setup.wizard_root:%s:", _setup.wizard_root); _setup.program_prefix[0] = 0; - if((inistring = inifile.Find("PROGRAM_PREFIX", "DISPLAY"))) + if(auto inistring = inifile.Find("PROGRAM_PREFIX", "DISPLAY")) { // found it char expandinistring[LINELEN]; - if (inifile.TildeExpansion(*inistring,expandinistring,sizeof(expandinistring))) { - logDebug("TildeExpansion failed for: %s",*inistring); + if (inifile.TildeExpansion(inistring->c_str(),expandinistring,sizeof(expandinistring))) { + logDebug("TildeExpansion failed for: %s",inistring->c_str()); } if (realpath(expandinistring, _setup.program_prefix) == NULL){ //realpath didn't find the file - logDebug("realpath failed to find program_prefix:%s:", *inistring); + logDebug("realpath failed to find program_prefix:%s:", inistring->c_str()); } logDebug("program prefix:%s: prefix:%s:", - *inistring, _setup.program_prefix); + inistring->c_str(), _setup.program_prefix); } else { @@ -988,7 +987,7 @@ int Interp::init() } logDebug("_setup.program_prefix:%s:", _setup.program_prefix); - if((inistring = inifile.Find("SUBROUTINE_PATH", "RS274NGC"))) + if(auto inistring = inifile.Find("SUBROUTINE_PATH", "RS274NGC")) { // found it int dct; @@ -999,7 +998,7 @@ int Interp::init() _setup.subroutines[dct] = NULL; } - rtapi_strxcpy(tmpdirs,*inistring); + rtapi_strxcpy(tmpdirs,inistring->c_str()); nextdir = strtok(tmpdirs,":"); // first token dct = 0; while (1) { @@ -1031,15 +1030,15 @@ int Interp::init() } // subroutine to execute on aborts - for instance to retract // toolchange HAL pins - if ((inistring = inifile.Find("ON_ABORT_COMMAND", "RS274NGC"))) { - _setup.on_abort_command = strstore(*inistring); + if (auto inistring = inifile.Find("ON_ABORT_COMMAND", "RS274NGC")) { + _setup.on_abort_command = strstore(inistring->c_str()); logDebug("_setup.on_abort_command=%s", _setup.on_abort_command); } else { _setup.on_abort_command = NULL; } // initialize the Python plugin singleton - if ((inistring = inifile.Find("TOPLEVEL", "PYTHON"))) { + if (inifile.Find("TOPLEVEL", "PYTHON")) { int status = python_plugin->configure(iniFileName,"PYTHON"); if (status != PLUGIN_OK) { Error("Python plugin configure() failed, status = %d", status); @@ -1051,10 +1050,10 @@ int Interp::init() _setup.g_remapped.clear(); _setup.m_remapped.clear(); _setup.remaps.clear(); - while ((inistring = inifile.Find("REMAP", "RS274NGC", - n, &lineno))) { + while (auto inistring = inifile.Find("REMAP", "RS274NGC", + n, &lineno)) { - CHP(parse_remap( *inistring, lineno)); + CHP(parse_remap( inistring->c_str(), lineno)); n++; } @@ -2513,7 +2512,6 @@ VARIABLE_FILE = rs274ngc.var int Interp::ini_load(const char *filename) { IniFile inifile; - std::optional inistring; // open it if (inifile.Open(filename) == false) { @@ -2525,12 +2523,13 @@ int Interp::ini_load(const char *filename) char parameter_file_name[LINELEN]={}; - if ((inistring = inifile.Find("PARAMETER_FILE", "RS274NGC"))) { - if (strlen(*inistring) >= sizeof(parameter_file_name)) { + if (auto inistring = inifile.Find("PARAMETER_FILE", "RS274NGC")) { + if (inistring->length() >= sizeof(parameter_file_name)) { logDebug("%s:[RS274NGC]PARAMETER_FILE is too long (max len %zu)", filename, sizeof(parameter_file_name)-1); } else { - strncpy(parameter_file_name, *inistring, sizeof(parameter_file_name)); + strncpy(parameter_file_name, inistring->c_str(), sizeof(parameter_file_name)-1); + parameter_file_name[sizeof(parameter_file_name)-1] = '\0'; logDebug("found PARAMETER_FILE:%s:", parameter_file_name); } } else { diff --git a/src/emc/sai/driver.cc b/src/emc/sai/driver.cc index 2c1882b950c..4381ff2bc49 100644 --- a/src/emc/sai/driver.cc +++ b/src/emc/sai/driver.cc @@ -675,7 +675,6 @@ int main (int argc, char ** argv) } _sai._external_length_units = 0.03937007874016; if (inifile!= 0) { - std::optional inistring; IniFile ini; // open it if (ini.Open(inifile) == false) { @@ -683,8 +682,8 @@ int main (int argc, char ** argv) exit(1); } - if ((inistring = ini.Find("LINEAR_UNITS", "TRAJ"))) { - if (!strcmp(*inistring, "mm")) { + if (auto inistring = ini.Find("LINEAR_UNITS", "TRAJ")) { + if (*inistring == "mm") { _sai._external_length_units = 1.0; } } diff --git a/src/emc/task/emcsvr.cc b/src/emc/task/emcsvr.cc index b2550c8fc8c..571f1b42a22 100644 --- a/src/emc/task/emcsvr.cc +++ b/src/emc/task/emcsvr.cc @@ -34,7 +34,6 @@ static int iniLoad(const char *filename) { IniFile inifile; - std::optional inistring; char version[LINELEN], machine[LINELEN]; // open it @@ -44,28 +43,28 @@ static int iniLoad(const char *filename) // EMC debugging flags emc_debug = 0; // disabled by default - if ((inistring = inifile.Find("DEBUG", "EMC"))) { + if (auto inistring = inifile.Find("DEBUG", "EMC")) { // parse to global - if (sscanf(*inistring, "%x", &emc_debug) < 1) { + if (sscanf(inistring->c_str(), "%x", &emc_debug) < 1) { perror("failed to parse [EMC] DEBUG"); } } // set output for RCS messages set_rcs_print_destination(RCS_PRINT_TO_STDOUT); // use stdout by default - if ((inistring = inifile.Find("RCS_DEBUG_DEST", "EMC"))) { + if (auto inistring = inifile.Find("RCS_DEBUG_DEST", "EMC")) { static RCS_PRINT_DESTINATION_TYPE type; - if (!strcmp(*inistring, "STDOUT")) { + if (*inistring == "STDOUT") { type = RCS_PRINT_TO_STDOUT; - } else if (!strcmp(*inistring, "STDERR")) { + } else if (*inistring == "STDERR") { type = RCS_PRINT_TO_STDERR; - } else if (!strcmp(*inistring, "FILE")) { + } else if (*inistring == "FILE") { type = RCS_PRINT_TO_FILE; - } else if (!strcmp(*inistring, "LOGGER")) { + } else if (*inistring == "LOGGER") { type = RCS_PRINT_TO_LOGGER; - } else if (!strcmp(*inistring, "MSGBOX")) { + } else if (*inistring == "MSGBOX") { type = RCS_PRINT_TO_MESSAGE_BOX; - } else if (!strcmp(*inistring, "NULL")) { + } else if (*inistring == "NULL") { type = RCS_PRINT_TO_NULL; } else { type = RCS_PRINT_TO_STDOUT; @@ -82,9 +81,9 @@ static int iniLoad(const char *filename) } // set flags if RCS_DEBUG in ini file - if ((inistring = inifile.Find("RCS_DEBUG", "EMC"))) { + if (auto inistring = inifile.Find("RCS_DEBUG", "EMC")) { long unsigned int flags; - if (sscanf(*inistring, "%lx", &flags) < 1) { + if (sscanf(inistring->c_str(), "%lx", &flags) < 1) { perror("failed to parse [EMC] RCS_DEBUG"); } // clear all flags @@ -94,18 +93,18 @@ static int iniLoad(const char *filename) } // output infinite RCS errors by default max_rcs_errors_to_print = -1; - if ((inistring = inifile.Find("RCS_MAX_ERR", "EMC"))) { - if (sscanf(*inistring, "%d", &max_rcs_errors_to_print) < 1) { + if (auto inistring = inifile.Find("RCS_MAX_ERR", "EMC")) { + if (sscanf(inistring->c_str(), "%d", &max_rcs_errors_to_print) < 1) { perror("failed to parse [EMC] RCS_MAX_ERR"); } } if (emc_debug & EMC_DEBUG_CONFIG) { - inistring = inifile.Find("VERSION", "EMC"); - strncpy(version, inistring.value_or("unknown"), LINELEN-1); + auto ver = inifile.Find("VERSION", "EMC"); + strncpy(version, ver ? ver->c_str() : "unknown", LINELEN-1); - inistring = inifile.Find("MACHINE", "EMC"); - strncpy(machine, inistring.value_or("unknown"), LINELEN-1); + auto mach = inifile.Find("MACHINE", "EMC"); + strncpy(machine, mach ? mach->c_str() : "unknown", LINELEN-1); extern char *program_invocation_short_name; rcs_print( @@ -114,9 +113,9 @@ static int iniLoad(const char *filename) ); } - if ((inistring = inifile.Find("NML_FILE", "EMC"))) { + if (auto inistring = inifile.Find("NML_FILE", "EMC")) { // copy to global - rtapi_strxcpy(emc_nmlfile, *inistring); + rtapi_strxcpy(emc_nmlfile, inistring->c_str()); } else { // not found, use default } diff --git a/src/emc/task/emctask.cc b/src/emc/task/emctask.cc index a96f264e3d8..a17f604a448 100644 --- a/src/emc/task/emctask.cc +++ b/src/emc/task/emctask.cc @@ -116,18 +116,17 @@ int emcTaskInit() char path[EMC_SYSTEM_CMD_LEN]; struct stat buf; IniFile inifile; - std::optional inistring; ZERO_EMC_POSE(emcStatus->task.toolOffset); inifile.Open(emc_inifile); // Identify user_defined_function directories - if ((inistring = inifile.Find("PROGRAM_PREFIX", "DISPLAY"))) { - if (strlen(*inistring) >= sizeof(mdir[0])) { + if (auto inistring = inifile.Find("PROGRAM_PREFIX", "DISPLAY")) { + if (inistring->length() >= sizeof(mdir[0])) { rcs_print("[DISPLAY]PROGRAM_PREFIX too long (max len %zu)\n", sizeof(mdir[0])); return -1; } - strncpy(mdir[0], *inistring, sizeof(mdir[0])); + rtapi_strlcpy(mdir[0], inistring->c_str(), sizeof(mdir[0])); } else { // default dir if no PROGRAM_PREFIX rtapi_strlcpy(mdir[0], "nc_files", sizeof(mdir[0])); @@ -136,17 +135,17 @@ int emcTaskInit() // user can specify a list of directories for user defined functions // with a colon (:) separated list - if ((inistring = inifile.Find("USER_M_PATH", "RS274NGC"))) { + if (auto inistring = inifile.Find("USER_M_PATH", "RS274NGC")) { char* nextdir; char tmpdirs[PATH_MAX]; for (dct=1; dct < MAX_M_DIRS; dct++) mdir[dct][0] = 0; - if (strlen(*inistring) >= sizeof(tmpdirs)) { + if (inistring->length() >= sizeof(tmpdirs)) { rcs_print("[RS274NGC]USER_M_PATH too long (max len %zu)\n", sizeof(tmpdirs)); return -1; } - strncpy(tmpdirs, *inistring, sizeof(tmpdirs)); + rtapi_strlcpy(tmpdirs, inistring->c_str(), sizeof(tmpdirs)); nextdir = strtok(tmpdirs,":"); // first token dct = 1; @@ -431,13 +430,12 @@ int emcTaskPlanInit() { if(!pinterp) { IniFile inifile; - std::optional inistring; inifile.Open(emc_inifile); - if((inistring = inifile.Find("INTERPRETER", "TASK"))) { - pinterp = interp_from_shlib(*inistring); + if(auto inistring = inifile.Find("INTERPRETER", "TASK")) { + pinterp = interp_from_shlib(inistring->c_str()); fprintf(stderr, "interp_from_shlib() -> %p\n", pinterp); if (!pinterp) { - fprintf(stderr, "failed to load [TASK]INTERPRETER (%s)\n", *inistring); + fprintf(stderr, "failed to load [TASK]INTERPRETER (%s)\n", inistring->c_str()); return -1; } } diff --git a/src/emc/task/emctaskmain.cc b/src/emc/task/emctaskmain.cc index 36d6b38a951..5aee7ad435e 100644 --- a/src/emc/task/emctaskmain.cc +++ b/src/emc/task/emctaskmain.cc @@ -3095,7 +3095,6 @@ static int emctask_shutdown(void) static int iniLoad(const char *filename) { IniFile inifile; - std::optional inistring; char version[LINELEN], machine[LINELEN]; double saveDouble; int saveInt; @@ -3105,9 +3104,9 @@ static int iniLoad(const char *filename) return -1; } - if ((inistring = inifile.Find("JOINTS", "KINS"))) { + if (auto inistring = inifile.Find("JOINTS", "KINS")) { // copy to global - if (1 != sscanf(*inistring, "%i", &joints)) { + if (1 != sscanf(inistring->c_str(), "%i", &joints)) { joints = 0; } } else { @@ -3117,28 +3116,28 @@ static int iniLoad(const char *filename) // EMC debugging flags emc_debug = 0; // disabled by default - if ((inistring = inifile.Find("DEBUG", "EMC"))) { + if (auto inistring = inifile.Find("DEBUG", "EMC")) { // parse to global - if (sscanf(*inistring, "%x", &emc_debug) < 1) { + if (sscanf(inistring->c_str(), "%x", &emc_debug) < 1) { perror("failed to parse [EMC] DEBUG"); } } // set output for RCS messages set_rcs_print_destination(RCS_PRINT_TO_STDOUT); // use stdout by default - if ((inistring = inifile.Find("RCS_DEBUG_DEST", "EMC"))) { + if (auto inistring = inifile.Find("RCS_DEBUG_DEST", "EMC")) { static RCS_PRINT_DESTINATION_TYPE type; - if (!strcmp(*inistring, "STDOUT")) { + if (*inistring == "STDOUT") { type = RCS_PRINT_TO_STDOUT; - } else if (!strcmp(*inistring, "STDERR")) { + } else if (*inistring == "STDERR") { type = RCS_PRINT_TO_STDERR; - } else if (!strcmp(*inistring, "FILE")) { + } else if (*inistring == "FILE") { type = RCS_PRINT_TO_FILE; - } else if (!strcmp(*inistring, "LOGGER")) { + } else if (*inistring == "LOGGER") { type = RCS_PRINT_TO_LOGGER; - } else if (!strcmp(*inistring, "MSGBOX")) { + } else if (*inistring == "MSGBOX") { type = RCS_PRINT_TO_MESSAGE_BOX; - } else if (!strcmp(*inistring, "NULL")) { + } else if (*inistring == "NULL") { type = RCS_PRINT_TO_NULL; } else { type = RCS_PRINT_TO_STDOUT; @@ -3155,9 +3154,9 @@ static int iniLoad(const char *filename) } // set flags if RCS_DEBUG in ini file - if ((inistring = inifile.Find("RCS_DEBUG", "EMC"))) { + if (auto inistring = inifile.Find("RCS_DEBUG", "EMC")) { long unsigned int flags; - if (sscanf(*inistring, "%lx", &flags) < 1) { + if (sscanf(inistring->c_str(), "%lx", &flags) < 1) { perror("failed to parse [EMC] RCS_DEBUG"); } // clear all flags @@ -3167,18 +3166,18 @@ static int iniLoad(const char *filename) } // output infinite RCS errors by default max_rcs_errors_to_print = -1; - if ((inistring = inifile.Find("RCS_MAX_ERR", "EMC"))) { - if (sscanf(*inistring, "%d", &max_rcs_errors_to_print) < 1) { + if (auto inistring = inifile.Find("RCS_MAX_ERR", "EMC")) { + if (sscanf(inistring->c_str(), "%d", &max_rcs_errors_to_print) < 1) { perror("failed to parse [EMC] RCS_MAX_ERR"); } } if (emc_debug & EMC_DEBUG_CONFIG) { - inistring = inifile.Find("VERSION", "EMC"); - rtapi_strlcpy(version, inistring.value_or("unknown"), LINELEN-1); + auto ver = inifile.Find("VERSION", "EMC"); + rtapi_strlcpy(version, ver ? ver->c_str() : "unknown", LINELEN-1); - inistring = inifile.Find("MACHINE", "EMC"); - rtapi_strlcpy(machine, inistring.value_or("unknown"), LINELEN-1); + auto mach = inifile.Find("MACHINE", "EMC"); + rtapi_strlcpy(machine, mach ? mach->c_str() : "unknown", LINELEN-1); extern char *program_invocation_short_name; rcs_print( "%s (%d) task: machine '%s' version '%s'\n", @@ -3186,16 +3185,16 @@ static int iniLoad(const char *filename) ); } - if ((inistring = inifile.Find("NML_FILE", "EMC"))) { + if (auto inistring = inifile.Find("NML_FILE", "EMC")) { // copy to global - rtapi_strxcpy(emc_nmlfile, *inistring); + rtapi_strxcpy(emc_nmlfile, inistring->c_str()); } else { // not found, use default } saveInt = emc_task_interp_max_len; //remember default or previously set value - if ((inistring = inifile.Find("INTERP_MAX_LEN", "TASK"))) { - if (1 == sscanf(*inistring, "%d", &emc_task_interp_max_len)) { + if (auto inistring = inifile.Find("INTERP_MAX_LEN", "TASK")) { + if (1 == sscanf(inistring->c_str(), "%d", &emc_task_interp_max_len)) { if (emc_task_interp_max_len <= 0) { emc_task_interp_max_len = saveInt; } @@ -3204,16 +3203,16 @@ static int iniLoad(const char *filename) } } - if ((inistring = inifile.Find("RS274NGC_STARTUP_CODE", "RS274NGC"))) { + if (auto inistring = inifile.Find("RS274NGC_STARTUP_CODE", "RS274NGC")) { // copy to global - rtapi_strxcpy(rs274ngc_startup_code, *inistring); + rtapi_strxcpy(rs274ngc_startup_code, inistring->c_str()); } saveDouble = emc_task_cycle_time; EMC_TASK_CYCLE_TIME_ORIG = emc_task_cycle_time; emcTaskNoDelay = 0; - if ((inistring = inifile.Find("CYCLE_TIME", "TASK"))) { - if (1 == sscanf(*inistring, "%lf", &emc_task_cycle_time)) { + if (auto inistring = inifile.Find("CYCLE_TIME", "TASK")) { + if (1 == sscanf(inistring->c_str(), "%lf", &emc_task_cycle_time)) { // found it // if it's <= 0.0, then flag that we don't want to // wait at all, which will set the EMC_TASK_CYCLE_TIME @@ -3226,7 +3225,7 @@ static int iniLoad(const char *filename) emc_task_cycle_time = saveDouble; rcs_print ("invalid [TASK] CYCLE_TIME in %s (%s); using default %f\n", - filename, *inistring, emc_task_cycle_time); + filename, inistring->c_str(), emc_task_cycle_time); } } else { // not found, using default @@ -3235,8 +3234,8 @@ static int iniLoad(const char *filename) } - if ((inistring = inifile.Find("NO_FORCE_HOMING", "TRAJ"))) { - if (1 == sscanf(*inistring, "%d", &no_force_homing)) { + if (auto inistring = inifile.Find("NO_FORCE_HOMING", "TRAJ")) { + if (1 == sscanf(inistring->c_str(), "%d", &no_force_homing)) { // found it // if it's <= 0.0, then set it 0 so that homing is required before MDI or Auto if (no_force_homing <= 0) { @@ -3247,7 +3246,7 @@ static int iniLoad(const char *filename) no_force_homing = 0; rcs_print ("invalid [TRAJ] NO_FORCE_HOMING in %s (%s); using default %d\n", - filename, *inistring, no_force_homing); + filename, inistring->c_str(), no_force_homing); } } else { // not found, using default @@ -3255,13 +3254,13 @@ static int iniLoad(const char *filename) } // configurable template for iocontrol reason display - if ((inistring = inifile.Find("IO_ERROR", "TASK"))) { - io_error = strdup(*inistring); + if (auto inistring = inifile.Find("IO_ERROR", "TASK")) { + io_error = strdup(inistring->c_str()); } // max number of queued MDI commands - if ((inistring = inifile.Find("MDI_QUEUED_COMMANDS", "TASK"))) { - max_mdi_queued_commands = atoi(*inistring); + if (auto inistring = inifile.Find("MDI_QUEUED_COMMANDS", "TASK")) { + max_mdi_queued_commands = atoi(inistring->c_str()); } // close it diff --git a/src/emc/task/taskclass.cc b/src/emc/task/taskclass.cc index 3c481483490..ffb98ed1c9b 100644 --- a/src/emc/task/taskclass.cc +++ b/src/emc/task/taskclass.cc @@ -138,14 +138,13 @@ Task::Task(EMC_IO_STAT & emcioStatus_in) : if (inifile.Open(ini_filename)) { inifile.Find(&random_toolchanger, "RANDOM_TOOLCHANGER", "EMCIO"); - std::optional t; - if ((t = inifile.Find("TOOL_TABLE", "EMCIO"))) - tooltable_filename = strdup(*t); + if (auto t = inifile.Find("TOOL_TABLE", "EMCIO")) + tooltable_filename = strdup(t->c_str()); - if ((t = inifile.Find("DB_PROGRAM", "EMCIO"))) { + if (auto t = inifile.Find("DB_PROGRAM", "EMCIO")) { db_mode = tooldb_t::DB_ACTIVE; tooldata_set_db(db_mode); - strncpy(db_program, *t, LINELEN - 1); + strncpy(db_program, t->c_str(), LINELEN - 1); } if (tooltable_filename != NULL && db_program[0] != '\0') { @@ -206,11 +205,11 @@ Task::~Task() {}; static int readToolChange(IniFile *toolInifile) { int retval = 0; - std::optional inistring; - if ((inistring = toolInifile->Find("TOOL_CHANGE_POSITION", "EMCIO"))) { + auto inistring = toolInifile->Find("TOOL_CHANGE_POSITION", "EMCIO"); + if (inistring) { /* found an entry */ - if (9 == sscanf(*inistring, "%lf %lf %lf %lf %lf %lf %lf %lf %lf", + if (9 == sscanf(inistring->c_str(), "%lf %lf %lf %lf %lf %lf %lf %lf %lf", &tool_change_position.tran.x, &tool_change_position.tran.y, &tool_change_position.tran.z, @@ -222,7 +221,7 @@ static int readToolChange(IniFile *toolInifile) &tool_change_position.w)) { have_tool_change_position=9; retval=0; - } else if (6 == sscanf(*inistring, "%lf %lf %lf %lf %lf %lf", + } else if (6 == sscanf(inistring->c_str(), "%lf %lf %lf %lf %lf %lf", &tool_change_position.tran.x, &tool_change_position.tran.y, &tool_change_position.tran.z, @@ -234,7 +233,7 @@ static int readToolChange(IniFile *toolInifile) tool_change_position.w = 0.0; have_tool_change_position = 6; retval = 0; - } else if (3 == sscanf(*inistring, "%lf %lf %lf", + } else if (3 == sscanf(inistring->c_str(), "%lf %lf %lf", &tool_change_position.tran.x, &tool_change_position.tran.y, &tool_change_position.tran.z)) { diff --git a/src/emc/task/taskintf.cc b/src/emc/task/taskintf.cc index 2f180645b59..5029143ae42 100644 --- a/src/emc/task/taskintf.cc +++ b/src/emc/task/taskintf.cc @@ -1733,14 +1733,14 @@ int emcPositionLoad() { ini.Open(emc_inifile); auto posfile = ini.Find("POSITION_FILE", "TRAJ"); ini.Close(); - if(!posfile || !posfile.value()[0]) return 0; - FILE *f = fopen(*posfile, "r"); + if(!posfile || posfile->empty()) return 0; + FILE *f = fopen(posfile->c_str(), "r"); if(!f) return 0; for(int i=0; ic_str()); return -1; } } @@ -1748,7 +1748,7 @@ int emcPositionLoad() { int result = 0; for(int i=0; ic_str()); result = -1; } } @@ -1758,9 +1758,9 @@ int emcPositionLoad() { int emcPositionSave() { IniFile ini; - std::optional posfile; ini.Open(emc_inifile); + std::optional posfile; try { posfile = ini.Find("POSITION_FILE", "TRAJ"); } catch (IniFile::Exception e) { @@ -1769,10 +1769,10 @@ int emcPositionSave() { } ini.Close(); - if(!posfile || !posfile.value()[0]) return 0; + if(!posfile || posfile->empty()) return 0; // like the var file, make sure the posfile is recreated according to umask - unlink(*posfile); - FILE *f = fopen(*posfile, "w"); + unlink(posfile->c_str()); + FILE *f = fopen(posfile->c_str(), "w"); if(!f) return -1; for(int i=0; ii->Find(s2, s1, num)) - return PyUnicode_FromString(out.value()); + return PyUnicode_FromString(out.value().c_str()); Py_INCREF(Py_None); return Py_None; @@ -135,7 +135,7 @@ static PyObject *Ini_findall(pyIniFile *self, PyObject *args) { PyObject *result = PyList_New(0); while(auto out = self->i->Find(s2, s1, num)) { - PyList_Append(result, PyUnicode_FromString(out.value())); + PyList_Append(result, PyUnicode_FromString(out.value().c_str())); num++; } return result; diff --git a/src/emc/usr_intf/emcsh.cc b/src/emc/usr_intf/emcsh.cc index e9fb7b18681..b61f72fefe8 100644 --- a/src/emc/usr_intf/emcsh.cc +++ b/src/emc/usr_intf/emcsh.cc @@ -389,7 +389,6 @@ static int emc_ini(ClientData /*clientdata*/, Tcl_Interp * interp, int objc, Tcl_Obj * CONST objv[]) { IniFile inifile; - std::optional inistring; const char *varstr, *secstr, *defaultstr; defaultstr = 0; @@ -409,14 +408,15 @@ static int emc_ini(ClientData /*clientdata*/, defaultstr = Tcl_GetStringFromObj(objv[3], 0); } - if (!(inistring = inifile.Find(varstr, secstr))) { + auto inistring = inifile.Find(varstr, secstr); + if (!inistring) { if (defaultstr != 0) { setresult(interp,(char *) defaultstr); } return TCL_OK; } - setresult(interp, *inistring); + setresult(interp, inistring->c_str()); // close it inifile.Close(); diff --git a/src/emc/usr_intf/halui.cc b/src/emc/usr_intf/halui.cc index e9e23735c94..d71eaf0ef5b 100644 --- a/src/emc/usr_intf/halui.cc +++ b/src/emc/usr_intf/halui.cc @@ -1381,7 +1381,6 @@ static int sendSpindleOverride(int spindle, double override) static int iniLoad(const char *filename) { IniFile inifile; - std::optional inistring; char version[LINELEN], machine[LINELEN]; double d; int i; @@ -1393,28 +1392,28 @@ static int iniLoad(const char *filename) // EMC debugging flags emc_debug = 0; // disabled by default - if ((inistring = inifile.Find("DEBUG", "EMC"))) { + if (auto inistring = inifile.Find("DEBUG", "EMC")) { // parse to global - if (sscanf(*inistring, "%x", &emc_debug) < 1) { + if (sscanf(inistring->c_str(), "%x", &emc_debug) < 1) { perror("failed to parse [EMC] DEBUG"); } } // set output for RCS messages set_rcs_print_destination(RCS_PRINT_TO_STDOUT); // use stdout by default - if ((inistring = inifile.Find("RCS_DEBUG_DEST", "EMC"))) { + if (auto inistring = inifile.Find("RCS_DEBUG_DEST", "EMC")) { static RCS_PRINT_DESTINATION_TYPE type; - if (!strcmp(*inistring, "STDOUT")) { + if (*inistring == "STDOUT") { type = RCS_PRINT_TO_STDOUT; - } else if (!strcmp(*inistring, "STDERR")) { + } else if (*inistring == "STDERR") { type = RCS_PRINT_TO_STDERR; - } else if (!strcmp(*inistring, "FILE")) { + } else if (*inistring == "FILE") { type = RCS_PRINT_TO_FILE; - } else if (!strcmp(*inistring, "LOGGER")) { + } else if (*inistring == "LOGGER") { type = RCS_PRINT_TO_LOGGER; - } else if (!strcmp(*inistring, "MSGBOX")) { + } else if (*inistring == "MSGBOX") { type = RCS_PRINT_TO_MESSAGE_BOX; - } else if (!strcmp(*inistring, "NULL")) { + } else if (*inistring == "NULL") { type = RCS_PRINT_TO_NULL; } else { type = RCS_PRINT_TO_STDOUT; @@ -1431,9 +1430,9 @@ static int iniLoad(const char *filename) } // set flags if RCS_DEBUG in ini file - if ((inistring = inifile.Find("RCS_DEBUG", "EMC"))) { + if (auto inistring = inifile.Find("RCS_DEBUG", "EMC")) { long unsigned int flags; - if (sscanf(*inistring, "%lx", &flags) < 1) { + if (sscanf(inistring->c_str(), "%lx", &flags) < 1) { perror("failed to parse [EMC] RCS_DEBUG"); } // clear all flags @@ -1443,20 +1442,20 @@ static int iniLoad(const char *filename) } // output infinite RCS errors by default max_rcs_errors_to_print = -1; - if ((inistring = inifile.Find("RCS_MAX_ERR", "EMC"))) { - if (sscanf(*inistring, "%d", &max_rcs_errors_to_print) < 1) { + if (auto inistring = inifile.Find("RCS_MAX_ERR", "EMC")) { + if (sscanf(inistring->c_str(), "%d", &max_rcs_errors_to_print) < 1) { perror("failed to parse [EMC] RCS_MAX_ERR"); } } strncpy(version, "unknown", LINELEN-1); - if ((inistring = inifile.Find("VERSION", "EMC"))) { - strncpy(version, *inistring, LINELEN-1); + if (auto inistring = inifile.Find("VERSION", "EMC")) { + strncpy(version, inistring->c_str(), LINELEN-1); } if (emc_debug & EMC_DEBUG_CONFIG) { - if ((inistring = inifile.Find("MACHINE", "EMC"))) { - strncpy(machine, *inistring, LINELEN-1); + if (auto inistring = inifile.Find("MACHINE", "EMC")) { + strncpy(machine, inistring->c_str(), LINELEN-1); } else { strncpy(machine, "unknown", LINELEN-1); } @@ -1468,15 +1467,15 @@ static int iniLoad(const char *filename) ); } - if ((inistring = inifile.Find("NML_FILE", "EMC"))) { + if (auto inistring = inifile.Find("NML_FILE", "EMC")) { // copy to global - rtapi_strxcpy(emc_nmlfile, *inistring); + rtapi_strxcpy(emc_nmlfile, inistring->c_str()); } else { // not found, use default } - if ((inistring = inifile.Find("MAX_FEED_OVERRIDE", "DISPLAY"))) { - if (1 == sscanf(*inistring, "%lf", &d) && d > 0.0) { + if (auto inistring = inifile.Find("MAX_FEED_OVERRIDE", "DISPLAY")) { + if (1 == sscanf(inistring->c_str(), "%lf", &d) && d > 0.0) { maxFeedOverride = d; } } @@ -1485,30 +1484,30 @@ static int iniLoad(const char *filename) inifile.Find(&maxMaxVelocity, "MAX_VELOCITY", "AXIS_X")) maxMaxVelocity = 1.0; - if ((inistring = inifile.Find("MIN_SPINDLE_OVERRIDE", "DISPLAY"))) { - if (1 == sscanf(*inistring, "%lf", &d) && d > 0.0) { + if (auto inistring = inifile.Find("MIN_SPINDLE_OVERRIDE", "DISPLAY")) { + if (1 == sscanf(inistring->c_str(), "%lf", &d) && d > 0.0) { minSpindleOverride = d; } } - if ((inistring = inifile.Find("MAX_SPINDLE_OVERRIDE", "DISPLAY"))) { - if (1 == sscanf(*inistring, "%lf", &d) && d > 0.0) { + if (auto inistring = inifile.Find("MAX_SPINDLE_OVERRIDE", "DISPLAY")) { + if (1 == sscanf(inistring->c_str(), "%lf", &d) && d > 0.0) { maxSpindleOverride = d; } } - inistring = inifile.Find("COORDINATES", "TRAJ"); + auto coord = inifile.Find("COORDINATES", "TRAJ"); num_axes = 0; - if (inistring) { - if(strchr(*inistring, 'x') || strchr(*inistring, 'X')) { axis_mask |= 0x0001; num_axes++; } - if(strchr(*inistring, 'y') || strchr(*inistring, 'Y')) { axis_mask |= 0x0002; num_axes++; } - if(strchr(*inistring, 'z') || strchr(*inistring, 'Z')) { axis_mask |= 0x0004; num_axes++; } - if(strchr(*inistring, 'a') || strchr(*inistring, 'A')) { axis_mask |= 0x0008; num_axes++; } - if(strchr(*inistring, 'b') || strchr(*inistring, 'B')) { axis_mask |= 0x0010; num_axes++; } - if(strchr(*inistring, 'c') || strchr(*inistring, 'C')) { axis_mask |= 0x0020; num_axes++; } - if(strchr(*inistring, 'u') || strchr(*inistring, 'U')) { axis_mask |= 0x0040; num_axes++; } - if(strchr(*inistring, 'v') || strchr(*inistring, 'V')) { axis_mask |= 0x0080; num_axes++; } - if(strchr(*inistring, 'w') || strchr(*inistring, 'W')) { axis_mask |= 0x0100; num_axes++; } + if (coord) { + if(coord->find_first_of("xX") != std::string::npos) { axis_mask |= 0x0001; num_axes++; } + if(coord->find_first_of("yY") != std::string::npos) { axis_mask |= 0x0002; num_axes++; } + if(coord->find_first_of("zZ") != std::string::npos) { axis_mask |= 0x0004; num_axes++; } + if(coord->find_first_of("aA") != std::string::npos) { axis_mask |= 0x0008; num_axes++; } + if(coord->find_first_of("bB") != std::string::npos) { axis_mask |= 0x0010; num_axes++; } + if(coord->find_first_of("cC") != std::string::npos) { axis_mask |= 0x0020; num_axes++; } + if(coord->find_first_of("uU") != std::string::npos) { axis_mask |= 0x0040; num_axes++; } + if(coord->find_first_of("vV") != std::string::npos) { axis_mask |= 0x0080; num_axes++; } + if(coord->find_first_of("wW") != std::string::npos) { axis_mask |= 0x0100; num_axes++; } } if (num_axes ==0) { rcs_print("halui: no [TRAJ]COORDINATES specified, enabling all axes\n"); @@ -1516,14 +1515,14 @@ static int iniLoad(const char *filename) axis_mask = 0xFFFF; } - if ((inistring = inifile.Find("JOINTS", "KINS"))) { - if (1 == sscanf(*inistring, "%d", &i) && i > 0) { + if (auto inistring = inifile.Find("JOINTS", "KINS")) { + if (1 == sscanf(inistring->c_str(), "%d", &i) && i > 0) { num_joints = i; } } - if ((inistring = inifile.Find("SPINDLES", "TRAJ"))) { - if (1 == sscanf(*inistring, "%d", &i) && i > 0) { + if (auto inistring = inifile.Find("SPINDLES", "TRAJ")) { + if (1 == sscanf(inistring->c_str(), "%d", &i) && i > 0) { num_spindles = i; } } @@ -1532,33 +1531,34 @@ static int iniLoad(const char *filename) have_home_all = 1; } - if ((inistring = inifile.Find("LINEAR_UNITS", "DISPLAY"))) { - if (!strcmp(*inistring, "AUTO")) { + if (auto inistring = inifile.Find("LINEAR_UNITS", "DISPLAY")) { + if (*inistring == "AUTO") { linearUnitConversion = LINEAR_UNITS_AUTO; - } else if (!strcmp(*inistring, "INCH")) { + } else if (*inistring == "INCH") { linearUnitConversion = LINEAR_UNITS_INCH; - } else if (!strcmp(*inistring, "MM")) { + } else if (*inistring == "MM") { linearUnitConversion = LINEAR_UNITS_MM; - } else if (!strcmp(*inistring, "CM")) { + } else if (*inistring == "CM") { linearUnitConversion = LINEAR_UNITS_CM; } } - if ((inistring = inifile.Find("ANGULAR_UNITS", "DISPLAY"))) { - if (!strcmp(*inistring, "AUTO")) { + if (auto inistring = inifile.Find("ANGULAR_UNITS", "DISPLAY")) { + if (*inistring == "AUTO") { angularUnitConversion = ANGULAR_UNITS_AUTO; - } else if (!strcmp(*inistring, "DEG")) { + } else if (*inistring == "DEG") { angularUnitConversion = ANGULAR_UNITS_DEG; - } else if (!strcmp(*inistring, "RAD")) { + } else if (*inistring == "RAD") { angularUnitConversion = ANGULAR_UNITS_RAD; - } else if (!strcmp(*inistring, "GRAD")) { + } else if (*inistring == "GRAD") { angularUnitConversion = ANGULAR_UNITS_GRAD; } } - std::optional mc; - while(num_mdi_commands < MDI_MAX && (mc = inifile.Find("MDI_COMMAND", "HALUI", num_mdi_commands+1))) { - mdi_commands[num_mdi_commands++] = strdup(*mc); + while(num_mdi_commands < MDI_MAX) { + auto mc = inifile.Find("MDI_COMMAND", "HALUI", num_mdi_commands+1); + if (!mc) break; + mdi_commands[num_mdi_commands++] = strdup(mc->c_str()); } // close it diff --git a/src/emc/usr_intf/shcom.cc b/src/emc/usr_intf/shcom.cc index 6fc27756fe7..adb2ff955ce 100644 --- a/src/emc/usr_intf/shcom.cc +++ b/src/emc/usr_intf/shcom.cc @@ -1250,7 +1250,6 @@ int sendProbe(double x, double y, double z) int iniLoad(const char *filename) { IniFile inifile; - std::optional inistring; char version[LINELEN], machine[LINELEN]; char displayString[LINELEN] = ""; int t; @@ -1263,28 +1262,28 @@ int iniLoad(const char *filename) // EMC debugging flags emc_debug = 0; // disabled by default - if ((inistring = inifile.Find("DEBUG", "EMC"))) { + if (auto inistring = inifile.Find("DEBUG", "EMC")) { // parse to global - if (sscanf(*inistring, "%x", &emc_debug) < 1) { + if (sscanf(inistring->c_str(), "%x", &emc_debug) < 1) { perror("failed to parse [EMC] DEBUG"); } } // set output for RCS messages set_rcs_print_destination(RCS_PRINT_TO_STDOUT); // use stdout by default - if ((inistring = inifile.Find("RCS_DEBUG_DEST", "EMC"))) { + if (auto inistring = inifile.Find("RCS_DEBUG_DEST", "EMC")) { static RCS_PRINT_DESTINATION_TYPE type; - if (!strcmp(*inistring, "STDOUT")) { + if (*inistring == "STDOUT") { type = RCS_PRINT_TO_STDOUT; - } else if (!strcmp(*inistring, "STDERR")) { + } else if (*inistring == "STDERR") { type = RCS_PRINT_TO_STDERR; - } else if (!strcmp(*inistring, "FILE")) { + } else if (*inistring == "FILE") { type = RCS_PRINT_TO_FILE; - } else if (!strcmp(*inistring, "LOGGER")) { + } else if (*inistring == "LOGGER") { type = RCS_PRINT_TO_LOGGER; - } else if (!strcmp(*inistring, "MSGBOX")) { + } else if (*inistring == "MSGBOX") { type = RCS_PRINT_TO_MESSAGE_BOX; - } else if (!strcmp(*inistring, "NULL")) { + } else if (*inistring == "NULL") { type = RCS_PRINT_TO_NULL; } else { type = RCS_PRINT_TO_STDOUT; @@ -1301,9 +1300,9 @@ int iniLoad(const char *filename) } // set flags if RCS_DEBUG in ini file - if ((inistring = inifile.Find("RCS_DEBUG", "EMC"))) { + if (auto inistring = inifile.Find("RCS_DEBUG", "EMC")) { long unsigned int flags; - if (sscanf(*inistring, "%lx", &flags) < 1) { + if (sscanf(inistring->c_str(), "%lx", &flags) < 1) { perror("failed to parse [EMC] RCS_DEBUG"); } // clear all flags @@ -1313,20 +1312,20 @@ int iniLoad(const char *filename) } // output infinite RCS errors by default max_rcs_errors_to_print = -1; - if ((inistring = inifile.Find("RCS_MAX_ERR", "EMC"))) { - if (sscanf(*inistring, "%d", &max_rcs_errors_to_print) < 1) { + if (auto inistring = inifile.Find("RCS_MAX_ERR", "EMC")) { + if (sscanf(inistring->c_str(), "%d", &max_rcs_errors_to_print) < 1) { perror("failed to parse [EMC] RCS_MAX_ERR"); } } strncpy(version, "unknown", LINELEN-1); - if ((inistring = inifile.Find("VERSION", "EMC"))) { - strncpy(version, *inistring, LINELEN-1); + if (auto inistring = inifile.Find("VERSION", "EMC")) { + strncpy(version, inistring->c_str(), LINELEN-1); } if (emc_debug & EMC_DEBUG_CONFIG) { - if ((inistring = inifile.Find("MACHINE", "EMC"))) { - strncpy(machine, *inistring, LINELEN-1); + if (auto inistring = inifile.Find("MACHINE", "EMC")) { + strncpy(machine, inistring->c_str(), LINELEN-1); } else { strncpy(machine, "unknown", LINELEN-1); } @@ -1338,10 +1337,10 @@ int iniLoad(const char *filename) ); } - if ((inistring = inifile.Find("NML_FILE", "EMC"))) { + if (auto inistring = inifile.Find("NML_FILE", "EMC")) { // copy to global - rtapi_strxcpy(emc_nmlfile, *inistring); + rtapi_strxcpy(emc_nmlfile, inistring->c_str()); } else { // not found, use default } @@ -1349,36 +1348,35 @@ int iniLoad(const char *filename) for (t = 0; t < EMCMOT_MAX_JOINTS; t++) { jogPol[t] = 1; // set to default snprintf(displayString, sizeof(displayString), "JOINT_%d", t); - if ((inistring = - inifile.Find("JOGGING_POLARITY", displayString)) && - 1 == sscanf(*inistring, "%d", &i) && i == 0) { + auto inistring = inifile.Find("JOGGING_POLARITY", displayString); + if (inistring && 1 == sscanf(inistring->c_str(), "%d", &i) && i == 0) { // it read as 0, so override default jogPol[t] = 0; } } - if ((inistring = inifile.Find("LINEAR_UNITS", "DISPLAY"))) { - if (!strcmp(*inistring, "AUTO")) { + if (auto inistring = inifile.Find("LINEAR_UNITS", "DISPLAY")) { + if (*inistring == "AUTO") { linearUnitConversion = LINEAR_UNITS_AUTO; - } else if (!strcmp(*inistring, "INCH")) { + } else if (*inistring == "INCH") { linearUnitConversion = LINEAR_UNITS_INCH; - } else if (!strcmp(*inistring, "MM")) { + } else if (*inistring == "MM") { linearUnitConversion = LINEAR_UNITS_MM; - } else if (!strcmp(*inistring, "CM")) { + } else if (*inistring == "CM") { linearUnitConversion = LINEAR_UNITS_CM; } } else { // not found, leave default alone } - if ((inistring = inifile.Find("ANGULAR_UNITS", "DISPLAY"))) { - if (!strcmp(*inistring, "AUTO")) { + if (auto inistring = inifile.Find("ANGULAR_UNITS", "DISPLAY")) { + if (*inistring == "AUTO") { angularUnitConversion = ANGULAR_UNITS_AUTO; - } else if (!strcmp(*inistring, "DEG")) { + } else if (*inistring == "DEG") { angularUnitConversion = ANGULAR_UNITS_DEG; - } else if (!strcmp(*inistring, "RAD")) { + } else if (*inistring == "RAD") { angularUnitConversion = ANGULAR_UNITS_RAD; - } else if (!strcmp(*inistring, "GRAD")) { + } else if (*inistring == "GRAD") { angularUnitConversion = ANGULAR_UNITS_GRAD; } } else { diff --git a/src/hal/user_comps/mb2hal/mb2hal_init.c b/src/hal/user_comps/mb2hal/mb2hal_init.c index 76a6abd89ae..ccd52748c6f 100644 --- a/src/hal/user_comps/mb2hal/mb2hal_init.c +++ b/src/hal/user_comps/mb2hal/mb2hal_init.c @@ -79,6 +79,7 @@ retCode parse_common_section() char *fnct_name = "parse_common_section"; char *section = "MB2HAL_INIT", *tag; const char *tmpstr; + char tmpbuf[INI_MAX_LINELEN]; if (gbl.ini_file_ptr == NULL) { ERR(gbl.init_dbg, "gbl.ini_file_ptr NULL pointer"); @@ -90,7 +91,7 @@ retCode parse_common_section() DBG(gbl.init_dbg, "[%s] [%s] [%d]", section, tag, gbl.init_dbg); tag = "VERSION"; //optional - tmpstr = iniFind(gbl.ini_file_ptr, tag, section); + tmpstr = iniFindString(gbl.ini_file_ptr, tag, section, tmpbuf, sizeof(tmpbuf)); if (tmpstr != NULL) { int major, minor; sscanf(tmpstr, "%d.%d", &major, &minor); @@ -99,7 +100,7 @@ retCode parse_common_section() DBG(gbl.init_dbg, "[%s] [%s] [%d]", section, tag, gbl.version); tag = "HAL_MODULE_NAME"; //optional - tmpstr = iniFind(gbl.ini_file_ptr, tag, section); + tmpstr = iniFindString(gbl.ini_file_ptr, tag, section, tmpbuf, sizeof(tmpbuf)); if (tmpstr != NULL) { gbl.hal_mod_name = strdup(tmpstr); } @@ -176,6 +177,7 @@ retCode parse_transaction_section(const int mb_tx_num) char section[40]; char *tag; const char *tmpstr; + char tmpbuf[INI_MAX_LINELEN]; mb_tx_t *this_mb_tx; if (gbl.ini_file_ptr == NULL) { @@ -197,7 +199,7 @@ retCode parse_transaction_section(const int mb_tx_num) snprintf(section, sizeof(section)-1, "TRANSACTION_%02d", mb_tx_num); tag = "LINK_TYPE"; //required 1st time, then optional - tmpstr = iniFind(gbl.ini_file_ptr, tag, section); + tmpstr = iniFindString(gbl.ini_file_ptr, tag, section, tmpbuf, sizeof(tmpbuf)); if (tmpstr != NULL) { if (strcasecmp(tmpstr, "tcp") == retOK) { this_mb_tx->cfg_link_type = linkTCP; @@ -274,7 +276,7 @@ retCode parse_transaction_section(const int mb_tx_num) tag = "PIN_NAMES"; - tmpstr = iniFind(gbl.ini_file_ptr, tag, section); + tmpstr = iniFindString(gbl.ini_file_ptr, tag, section, tmpbuf, sizeof(tmpbuf)); if (tmpstr != NULL) { if(parse_pin_names(tmpstr, this_mb_tx) != retOK) { @@ -344,7 +346,7 @@ retCode parse_transaction_section(const int mb_tx_num) DBG(gbl.init_dbg, "[%s] [%s] [%d]", section, tag, this_mb_tx->cfg_debug); tag = "MB_TX_CODE"; //required - tmpstr = iniFind(gbl.ini_file_ptr, tag, section); + tmpstr = iniFindString(gbl.ini_file_ptr, tag, section, tmpbuf, sizeof(tmpbuf)); if (tmpstr != NULL) { int i; for (i=0 ; imb_tx_fnct_name, this_mb_tx->mb_tx_fnct); tag = "HAL_TX_NAME"; //optional - tmpstr = iniFind(gbl.ini_file_ptr, tag, section); + tmpstr = iniFindString(gbl.ini_file_ptr, tag, section, tmpbuf, sizeof(tmpbuf)); if (tmpstr != NULL) { strncpy(this_mb_tx->hal_tx_name, tmpstr, HAL_NAME_LEN); } @@ -376,32 +378,6 @@ retCode parse_transaction_section(const int mb_tx_num) } DBG(gbl.init_dbg, "[%s] [%s] [%s]", section, tag, this_mb_tx->hal_tx_name); - /* - str = iniFind(gbl.ini_file_ptr, "PINNAME", mb_tx_name); - if (str != NULL) { - pin_name = malloc(strlen(str) + 1); - rtapi_strxcpy(pin_name, str); // convert a const string into one - // we can modify - } - else { - pin_name = malloc(1); // empty string - *pin_name = 0; - } - if (mb_tx->name[0] != 0) { - strncpy(mb_tx_name, mb_tx->name, HAL_NAME_LEN); - } - else { - snprintf(mb_tx_name, sizeof(mb_tx_name), "%02d", mb_tx_num); - } - memcpy(&gbl.mb_tx[mb_tx_num], mb_tx, sizeof(mb_tx_t)); - rc = create_pins(mb_tx_name, &gbl.mb_tx[mb_tx_num], pin_name); - free(pin_name); - if (rc != retOK) { - ERR(gbl.init_dbg, "Failed to create pins"); - return retERR; - } - */ - return retOK; } @@ -410,6 +386,7 @@ retCode parse_tcp_subsection(const char *section, const int mb_tx_num) char *fnct_name="parse_tcp_subsection"; char *tag; const char *tmpstr; + char tmpbuf[INI_MAX_LINELEN]; mb_tx_t *this_mb_tx; if (gbl.ini_file_ptr == NULL || section == NULL) { @@ -424,7 +401,7 @@ retCode parse_tcp_subsection(const char *section, const int mb_tx_num) this_mb_tx = &gbl.mb_tx[mb_tx_num]; tag = "TCP_IP"; //required 1st time, then optional - tmpstr = iniFind(gbl.ini_file_ptr, tag, section); + tmpstr = iniFindString(gbl.ini_file_ptr, tag, section, tmpbuf, sizeof(tmpbuf)); if (tmpstr != NULL) { strncpy(this_mb_tx->cfg_tcp_ip, tmpstr, sizeof(this_mb_tx->cfg_tcp_ip)-1); } @@ -466,6 +443,7 @@ retCode parse_serial_subsection(const char *section, const int mb_tx_num) char *fnct_name="parse_serial_subsection"; char *tag; const char *tmpstr; + char tmpbuf[INI_MAX_LINELEN]; mb_tx_t *this_mb_tx; if (gbl.ini_file_ptr == NULL || section == NULL) { @@ -480,7 +458,7 @@ retCode parse_serial_subsection(const char *section, const int mb_tx_num) this_mb_tx = &gbl.mb_tx[mb_tx_num]; tag = "SERIAL_PORT"; //required 1st time - tmpstr = iniFind(gbl.ini_file_ptr, tag, section); + tmpstr = iniFindString(gbl.ini_file_ptr, tag, section, tmpbuf, sizeof(tmpbuf)); if (tmpstr != NULL) { strncpy(this_mb_tx->cfg_serial_device, tmpstr, sizeof(this_mb_tx->cfg_serial_device)-1); } @@ -548,7 +526,7 @@ retCode parse_serial_subsection(const char *section, const int mb_tx_num) DBG(gbl.init_dbg, "[%s] [%s] [%d]", section, tag, this_mb_tx->cfg_serial_data_bit); tag = "SERIAL_PARITY"; //required 1st time - tmpstr = iniFind(gbl.ini_file_ptr, tag, section); + tmpstr = iniFindString(gbl.ini_file_ptr, tag, section, tmpbuf, sizeof(tmpbuf)); if (tmpstr != NULL) { strncpy(this_mb_tx->cfg_serial_parity, tmpstr, sizeof(this_mb_tx->cfg_serial_parity)-1); } diff --git a/src/hal/user_comps/vfdb_vfd/vfdb_vfd.c b/src/hal/user_comps/vfdb_vfd/vfdb_vfd.c index df38f7ba9a3..218f5aeceb7 100644 --- a/src/hal/user_comps/vfdb_vfd/vfdb_vfd.c +++ b/src/hal/user_comps/vfdb_vfd/vfdb_vfd.c @@ -278,11 +278,12 @@ enum kwdresult {NAME_NOT_FOUND, KEYWORD_INVALID, KEYWORD_FOUND}; int findkwd(param_pointer p, const char *name, int *result, const char *keyword, int value, ...) { const char *word; + char wordbuf[INI_MAX_LINELEN]; va_list ap; const char *kwds[MAX_KWD], **s; int nargs = 0; - if ((word = iniFind(p->fp, name, p->section)) == NULL) + if ((word = iniFindString(p->fp, name, p->section, wordbuf, sizeof(wordbuf))) == NULL) return NAME_NOT_FOUND; kwds[nargs++] = keyword; @@ -311,6 +312,7 @@ int findkwd(param_pointer p, const char *name, int *result, const char *keyword, int read_ini(param_pointer p) { const char *s; + char sbuf[INI_MAX_LINELEN]; int value; if ((p->fp = fopen(p->inifile,"r")) != NULL) { @@ -328,7 +330,7 @@ int read_ini(param_pointer p) iniFindInt(p->fp, "MOTOR_HZ", p->section, &p->motor_hz); iniFindInt(p->fp, "MOTOR_RPM", p->section, &p->motor_rpm); - if ((s = iniFind(p->fp, "DEVICE", p->section))) { + if ((s = iniFindString(p->fp, "DEVICE", p->section, sbuf, sizeof(sbuf)))) { p->device = strdup(s); } value = p->parity; diff --git a/src/hal/user_comps/vfs11_vfd/vfs11_vfd.c b/src/hal/user_comps/vfs11_vfd/vfs11_vfd.c index 6304fe38306..8b172337610 100644 --- a/src/hal/user_comps/vfs11_vfd/vfs11_vfd.c +++ b/src/hal/user_comps/vfs11_vfd/vfs11_vfd.c @@ -398,11 +398,12 @@ enum kwdresult {NAME_NOT_FOUND, KEYWORD_INVALID, KEYWORD_FOUND}; int findkwd(param_pointer p, const char *name, int *result, const char *keyword, int value, ...) { const char *word; + char wordbuf[INI_MAX_LINELEN]; va_list ap; const char *kwds[MAX_KWD], **s; int nargs = 0; - if ((word = iniFind(p->fp, name, p->section)) == NULL) + if ((word = iniFindString(p->fp, name, p->section, wordbuf, sizeof(wordbuf))) == NULL) return NAME_NOT_FOUND; kwds[nargs++] = keyword; @@ -431,6 +432,7 @@ int findkwd(param_pointer p, const char *name, int *result, const char *keyword, int read_ini(param_pointer p) { const char *s; + char sbuf[INI_MAX_LINELEN]; double f; int value; @@ -447,10 +449,10 @@ int read_ini(param_pointer p) iniFindInt(p->fp, "PORT", p->section, &p->tcp_portno); iniFindInt(p->fp, "RECONNECT_DELAY", p->section, &p->reconnect_delay); - if ((s = iniFind(p->fp, "TCPDEST", p->section))) { + if ((s = iniFindString(p->fp, "TCPDEST", p->section, sbuf, sizeof(sbuf)))) { p->tcp_destip = strdup(s); } - if ((s = iniFind(p->fp, "DEVICE", p->section))) { + if ((s = iniFindString(p->fp, "DEVICE", p->section, sbuf, sizeof(sbuf)))) { p->device = strdup(s); } if (iniFindDouble(p->fp, "RESPONSE_TIMEOUT", p->section, &f)) { @@ -478,10 +480,10 @@ int read_ini(param_pointer p) (void *)NULL) == KEYWORD_INVALID) return -1; #else - if (iniFind(p->fp, "RTS_MODE", p->section) != NULL) { + if (iniFindString(p->fp, "RTS_MODE", p->section, sbuf, sizeof(sbuf)) != NULL) { fprintf(stderr,"%s: warning - the RTS_MODE feature is not available with the installed libmodbus version (%s).\n" "to enable it, uninstall libmodbus-dev and rebuild with " - "libmodbus built http://github.com/stephane/libmodbus:master .\n", + "libmodbus built http://github.com/stephane/libmodbus:master .\n", LIBMODBUS_VERSION_STRING, p->progname); } #endif diff --git a/src/hal/user_comps/xhc-hb04.cc b/src/hal/user_comps/xhc-hb04.cc index ae32dd54dd7..e3bdeda2146 100644 --- a/src/hal/user_comps/xhc-hb04.cc +++ b/src/hal/user_comps/xhc-hb04.cc @@ -668,7 +668,6 @@ static int hal_setup() int read_ini_file(char *filename) { IniFile iniFile; - std::optional bt; int nb_buttons = 0; if (!iniFile.Open(filename)) { fprintf(stderr, "%s: Could not open configuration file: %s\n", @@ -676,9 +675,10 @@ int read_ini_file(char *filename) return -1; } - while ((bt = iniFile.Find("BUTTON", section, nb_buttons+1)) && nb_buttons < NB_MAX_BUTTONS) { - if (sscanf(*bt, "%x:%255s", &xhc.buttons[nb_buttons].code, xhc.buttons[nb_buttons].pin_name) !=2 ) { - fprintf(stderr, "%s: syntax error\n", *bt); + while (auto bt = iniFile.Find("BUTTON", section, nb_buttons+1)) { + if (nb_buttons >= NB_MAX_BUTTONS) break; + if (sscanf(bt->c_str(), "%x:%255s", &xhc.buttons[nb_buttons].code, xhc.buttons[nb_buttons].pin_name) !=2 ) { + fprintf(stderr, "%s: syntax error\n", bt->c_str()); return -1; } nb_buttons++; diff --git a/src/hal/utils/halcmd.c b/src/hal/utils/halcmd.c index e45f08f0793..1486c22ea00 100644 --- a/src/hal/utils/halcmd.c +++ b/src/hal/utils/halcmd.c @@ -46,7 +46,7 @@ #include "emc/linuxcnc.h" #ifndef NO_INI -#include "inifile.h" /* iniFind() from libnml */ +#include "inifile.h" /* iniFindString() from libnml */ FILE *halcmd_inifile = NULL; #endif @@ -707,7 +707,7 @@ static int replace_vars(char *source_str, char *dest_str, int max_chars, char ** { int retval = 0; int next_delim, remaining, buf_space; - char *replacement, sec[128], var[128]; + char *replacement, sec[128], var[128], ini_buf[INI_MAX_LINELEN]; static char info[256]; char *sp=source_str, *dp=dest_str, *secP, *varP; const char @@ -776,11 +776,12 @@ static int replace_vars(char *source_str, char *dest_str, int max_chars, char ** var[next_delim]='\0'; if ( strlen(sec) > 0 ) { /* get value from INI file */ - /* cast to char ptr, we are discarding the 'const' */ - replacement = (char *) iniFind(halcmd_inifile, var, sec); + replacement = (char *) iniFindString(halcmd_inifile, var, sec, + ini_buf, sizeof(ini_buf)); } else { /* no section specified */ - replacement = (char *) iniFind(halcmd_inifile, var, NULL); + replacement = (char *) iniFindString(halcmd_inifile, var, NULL, + ini_buf, sizeof(ini_buf)); } if (replacement==NULL) { *detail = info; diff --git a/src/hal/utils/halrmt.c b/src/hal/utils/halrmt.c index c7486a41662..85c21853770 100644 --- a/src/hal/utils/halrmt.c +++ b/src/hal/utils/halrmt.c @@ -303,12 +303,6 @@ #include // rtapi_strlcpy #include "hal.h" // HAL public API decls #include "../hal_priv.h" // private HAL decls -/* non-EMC related uses of halrmt may want to avoid libnml dependency -#ifndef NO_INI -#include "inifile.h" // iniFind() from libnml -#endif -#include -*/ /*********************************************************************** * LOCAL FUNCTION DECLARATIONS * diff --git a/src/libnml/inifile/inifile.cc b/src/libnml/inifile/inifile.cc index 4ba8876ff43..35e1234c0df 100644 --- a/src/libnml/inifile/inifile.cc +++ b/src/libnml/inifile/inifile.cc @@ -93,7 +93,7 @@ IniFile::Find(int *result, StrIntPair *pPair, } int tmp; - if(sscanf(*pStr, "%i", &tmp) == 1){ + if(sscanf(pStr->c_str(), "%i", &tmp) == 1){ *result = tmp; if (lineno) *lineno = lineNo; @@ -101,7 +101,7 @@ IniFile::Find(int *result, StrIntPair *pPair, } while(pPair->pStr != NULL){ - if(strcasecmp(*pStr, pPair->pStr) == 0){ + if(strcasecmp(pStr->c_str(), pPair->pStr) == 0){ *result = pPair->value; if (lineno) *lineno = lineNo; @@ -129,7 +129,7 @@ IniFile::Find(double *result, StrDoublePair *pPair, } double tmp; - if(sscanf(*pStr, "%lf", &tmp) == 1){ + if(sscanf(pStr->c_str(), "%lf", &tmp) == 1){ if (lineno) *lineno = lineNo; *result = tmp; @@ -139,7 +139,7 @@ IniFile::Find(double *result, StrDoublePair *pPair, } while(pPair->pStr != NULL){ - if(strcasecmp(*pStr, pPair->pStr) == 0){ + if(strcasecmp(pStr->c_str(), pPair->pStr) == 0){ *result = pPair->value; if (lineno) *lineno = lineNo; @@ -163,12 +163,10 @@ IniFile::Find(double *result, StrDoublePair *pPair, @return pointer to the variable after the '=' delimiter, or @c NULL if not found */ -std::optional +std::optional IniFile::Find(const char *_tag, const char *_section, int _num, int *lineno) { - // WTF, return a pointer to the middle of a local buffer? - // FIX: this is totally non-reentrant. - static char line[LINELEN + 2] = ""; /* 1 for newline, 1 for NULL */ + char line[LINELEN + 2] = ""; /* 1 for newline, 1 for NULL */ char eline [(LINELEN + 2) * (MAX_EXTEND_LINES + 1)]; char* elineptr; @@ -345,7 +343,7 @@ IniFile::Find(const char *_tag, const char *_section, int _num, int *lineno) } if (lineno) *lineno = lineNo; - return valueString; + return std::string(valueString); } /* else continue */ } @@ -372,7 +370,7 @@ IniFile::FindString(char *dest, size_t n, const char *_tag, const char *_section auto res = Find(_tag, _section, _num, lineno); if(!res) return std::nullopt; - int r = snprintf(dest, n, "%s", *res); + int r = snprintf(dest, n, "%s", res->c_str()); if(r < 0 || (size_t)r >= n) { ThrowException(ERR_CONVERSION); return std::nullopt; @@ -386,7 +384,7 @@ IniFile::FindPath(char *dest, size_t n, const char *_tag, const char *_section, auto res = Find(_tag, _section, _num, lineno); if(!res) return std::nullopt; - if(TildeExpansion(*res, dest, n)) { + if(TildeExpansion(res->c_str(), dest, n)) { return std::nullopt; } return dest; @@ -594,11 +592,22 @@ IniFile::Exception::Print(FILE *fp) extern "C" const char * -iniFind(FILE *fp, const char *tag, const char *section) +iniFindString(FILE *fp, const char *tag, const char *section, + char *buf, size_t bufsize) { - IniFile f(false, fp); + IniFile f(false, fp); + auto result = f.FindString(buf, bufsize, tag, section); + if (!result) return nullptr; + return *result; +} - return(f.Find(tag, section).value_or(nullptr)); +extern "C" const char * +iniFind(FILE *fp, const char *tag, const char *section) +{ + // Deprecated: This function uses static storage and is not reentrant. + // Use iniFindString() instead for thread-safe operation. + static char result_storage[LINELEN]; + return iniFindString(fp, tag, section, result_storage, sizeof(result_storage)); } extern "C" int diff --git a/src/libnml/inifile/inifile.h b/src/libnml/inifile/inifile.h index 09a8c338d50..d0f31535c7b 100644 --- a/src/libnml/inifile/inifile.h +++ b/src/libnml/inifile/inifile.h @@ -25,7 +25,13 @@ extern "C" { #endif -extern const char *iniFind(FILE *fp, const char *tag, const char *section); +// Recommended buffer size for iniFindString() +#define INI_MAX_LINELEN 256 + +extern const char *iniFind(FILE *fp, const char *tag, const char *section) + __attribute__((deprecated("use iniFindString() instead"))); +extern const char *iniFindString(FILE *fp, const char *tag, const char *section, + char *buf, size_t bufsize); extern int iniFindInt(FILE *fp, const char *tag, const char *section, int *result); extern int iniFindDouble(FILE *fp, const char *tag, const char *section, double *result); extern int TildeExpansion(const char *file, char *path, size_t size); diff --git a/src/libnml/inifile/inifile.hh b/src/libnml/inifile/inifile.hh index b422cc1cc83..2c64e830be3 100644 --- a/src/libnml/inifile/inifile.hh +++ b/src/libnml/inifile/inifile.hh @@ -58,7 +58,7 @@ public: bool Close(); bool IsOpen(){ return(fp != nullptr); } - std::optional Find(const char *tag, const char *section = nullptr, + std::optional Find(const char *tag, const char *section = nullptr, int num = 1, int *lineno = nullptr); template diff --git a/src/libnml/inifile/inivar.cc b/src/libnml/inifile/inivar.cc index 7b316cfcb87..2a4fded0db5 100644 --- a/src/libnml/inifile/inivar.cc +++ b/src/libnml/inifile/inivar.cc @@ -117,10 +117,10 @@ int main(int argc, char *argv[]) if (iniString) { if (tildeExpand) { char expanded[PATH_MAX]; - iniFile.TildeExpansion(*iniString, expanded, sizeof(expanded)); + iniFile.TildeExpansion(iniString->c_str(), expanded, sizeof(expanded)); printf("%s\n", expanded); } else { - printf("%s\n", *iniString); + printf("%s\n", iniString->c_str()); } retval = 0; } else {