// // Created by tangchao on 2023/3/25. // #include "hppaConfigFile.h" Configfile::Configfile() { } void Configfile::setConfigfilePath(string configfilePath) { m_configfilePath = configfilePath; } bool Configfile::isConfigfileExist() { QFileInfo info(QString::fromStdString(m_configfilePath)); return info.exists(); } bool Configfile::parseConfigfile() { // Read the file. If there is an error, report it and exit. try { cfg.readFile(m_configfilePath); return true; } catch(const FileIOException &fioex) { std::cerr << "I/O error while reading file." << std::endl; return false; } catch(const ParseException &pex) { std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine() << " - " << pex.getError() << std::endl; return false; } } bool Configfile::getPositionRestriction(int& max, int& min) { const Setting& root = cfg.getRoot(); try { const Setting& autoFocus = root["autoFocus"]; int count = autoFocus.getLength(); const Setting& positionRestriction = autoFocus["PositionRestriction"]; string name = positionRestriction.getName(); if (!(positionRestriction.lookupValue("max", max) && positionRestriction.lookupValue("min", min) )) { return false; } } catch (const SettingNotFoundException& nfex) { // Ignore. return false; } return true; } bool Configfile::getTuningStepSize(int& coarse, int& fine) { const Setting& root = cfg.getRoot(); try { const Setting& autoFocus = root["autoFocus"]; int count = autoFocus.getLength(); const Setting& TuningStepSize = autoFocus["TuningStepSize"]; string name = TuningStepSize.getName(); if (!(TuningStepSize.lookupValue("coarse", coarse) && TuningStepSize.lookupValue("fine", fine) )) { return false; } } catch (const SettingNotFoundException& nfex) { // Ignore. return false; } return true; } bool Configfile::getFitParams(float& fa, float& fb) { const Setting& root = cfg.getRoot(); try { const Setting& autoFocus = root["autoFocus"]; int count = autoFocus.getLength(); const Setting& FitParams = autoFocus["FitParams"]; string name = FitParams.getName(); if (!(FitParams.lookupValue("fa", fa) && FitParams.lookupValue("fb", fb) )) { return false; } } catch (const SettingNotFoundException& nfex) { // Ignore. return false; } return true; } bool Configfile::getAutoFocusRange(int& max, int& min) { const Setting& root = cfg.getRoot(); try { const Setting& autoFocus = root["autoFocus"]; int count = autoFocus.getLength(); const Setting& AutoFocusRange = autoFocus["AutoFocusRange"]; string name = AutoFocusRange.getName(); if (!(AutoFocusRange.lookupValue("max", max) && AutoFocusRange.lookupValue("min", min) )) { return false; } } catch (const SettingNotFoundException& nfex) { // Ignore. return false; } return true; } bool Configfile::getXMotorParm(float& StepAnglemar, float& Lead, int& SubdivisionMultiples, float& ScaleFactor, float& MaxRange) { const Setting& root = cfg.getRoot(); try { const Setting& motionPlatform = root["motionPlatform"]; int count = motionPlatform.getLength(); const Setting& x = motionPlatform["x"]; string name = x.getName(); if (!(x.lookupValue("StepAnglemar", StepAnglemar) && x.lookupValue("Lead", Lead) && x.lookupValue("SubdivisionMultiples", SubdivisionMultiples) && x.lookupValue("ScaleFactor", ScaleFactor) && x.lookupValue("MaxRange", MaxRange) )) { return false; } } catch (const SettingNotFoundException& nfex) { // Ignore. return false; } return true; } bool Configfile::getYMotorParm(float& StepAnglemar, float& Lead, int& SubdivisionMultiples, float& ScaleFactor, float& MaxRange) { const Setting& root = cfg.getRoot(); try { const Setting& motionPlatform = root["motionPlatform"]; int count = motionPlatform.getLength(); const Setting& y = motionPlatform["y"]; string name = y.getName(); if (!(y.lookupValue("StepAnglemar", StepAnglemar) && y.lookupValue("Lead", Lead) && y.lookupValue("SubdivisionMultiples", SubdivisionMultiples) && y.lookupValue("ScaleFactor", ScaleFactor) && y.lookupValue("MaxRange", MaxRange) )) { return false; } } catch (const SettingNotFoundException& nfex) { // Ignore. return false; } return true; } bool Configfile::setMaxRange(float maxRange, QString x_y) { const Setting& root = cfg.getRoot(); try { Setting& _maxRange = root["motionPlatform"][x_y.toStdString()]["MaxRange"]; _maxRange = maxRange; writeConfig2File(); return true; } catch (const SettingNotFoundException& nfex) { cerr << "No 'MaxRange' setting in configuration file." << endl; return false; } } bool Configfile::writeConfig2File() { try { QList fileInfo = getFileInfo(QString::fromStdString(m_configfilePath)); bool ret = createDir(fileInfo[0]); cfg.writeFile(m_configfilePath.c_str()); // cerr << "New configuration successfully written to: " << m_configfilePath.c_str() << endl; } catch (const FileIOException& fioex) { cerr << "I/O error while writing configuration file: " << m_configfilePath.c_str() << endl; return false; } return true; } bool Configfile::getSN(string &SN) { try { std::string SN_tem = cfg.lookup("SN"); SN = SN_tem; return true; } catch(const SettingNotFoundException &nfex) { cerr << "No 'spectralBin' setting in configuration file." << endl; return false; } } bool Configfile::createConfigFile() { using namespace std; using namespace libconfig; Config cfg; Setting &root = cfg.getRoot(); // Add some settings to the configuration. Setting &SN = root.add("SN", Setting::TypeString) = "2004"; //autoFocus Setting & autoFocus = root.add("autoFocus", Setting::TypeGroup); Setting & PositionRestriction = autoFocus.add("PositionRestriction", Setting::TypeGroup); Setting& TuningStepSize = autoFocus.add("TuningStepSize", Setting::TypeGroup); Setting& FitParams = autoFocus.add("FitParams", Setting::TypeGroup); Setting& AutoFocusRange = autoFocus.add("AutoFocusRange", Setting::TypeGroup); Setting & PositionRestriction_max = PositionRestriction.add("max", Setting::TypeInt) = 1000; Setting & PositionRestriction_min = PositionRestriction.add("min", Setting::TypeInt) = 120; Setting& TuningStepSize_coarse = TuningStepSize.add("coarse", Setting::TypeInt) = 10; Setting& TuningStepSize_fine = TuningStepSize.add("fine", Setting::TypeInt) = 2; Setting& FitParams_fa = FitParams.add("fa", Setting::TypeFloat) = 0.0017; Setting& FitParams_fb = FitParams.add("fb", Setting::TypeFloat) = 0.3277; Setting& AutoFocusRange_max = AutoFocusRange.add("max", Setting::TypeInt) = 688; Setting& AutoFocusRange_min = AutoFocusRange.add("min", Setting::TypeInt) = 144; //motionPlatform Setting& motionPlatform = root.add("motionPlatform", Setting::TypeGroup); Setting& x = motionPlatform.add("x", Setting::TypeGroup); Setting& y = motionPlatform.add("y", Setting::TypeGroup); Setting& x_StepAnglemar = x.add("StepAnglemar", Setting::TypeFloat) = 1.8;//步距角 Setting& x_Lead = x.add("Lead", Setting::TypeFloat) = 13.5;//导程,单位:cm Setting& x_SubdivisionMultiples = x.add("SubdivisionMultiples", Setting::TypeInt) = 7;//细分倍数 Setting& x_ScaleFactor = x.add("ScaleFactor", Setting::TypeFloat) = 1.0;//缩放因子 Setting& x_MaxRange = x.add("MaxRange", Setting::TypeFloat) = 120.0; Setting& y_StepAnglemar = y.add("StepAnglemar", Setting::TypeFloat) = 1.8; Setting& y_Lead = y.add("Lead", Setting::TypeFloat) = 13.5; Setting& y_SubdivisionMultiples = y.add("SubdivisionMultiples", Setting::TypeInt) = 7; Setting& y_ScaleFactor = y.add("ScaleFactor", Setting::TypeFloat) = 0.2; Setting& y_MaxRange = y.add("MaxRange", Setting::TypeFloat) = 120.0; // Write out the new configuration. try { cfg.writeFile(m_configfilePath.c_str()); cerr << "New configuration successfully written to: " << m_configfilePath.c_str() << endl; } catch(const FileIOException &fioex) { cerr << "I/O error while writing configuration file: " << m_configfilePath.c_str() << endl; return true; } return true; } bool Configfile::updateConfigFile() { using namespace std; using namespace libconfig; static const char *output_file = "updated.cfg"; Config cfg; cfg.setOptions(Config::OptionFsync | Config::OptionSemicolonSeparators | Config::OptionColonAssignmentForGroups | Config::OptionOpenBraceOnSeparateLine); // Read the file. If there is an error, report it and exit. try { cfg.readFile("example.cfg"); } catch(const FileIOException &fioex) { std::cerr << "I/O error while reading file." << std::endl; return false; } catch(const ParseException &pex) { std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine() << " - " << pex.getError() << std::endl; return false; } // Find the 'movies' setting. Add intermediate settings if they don't yet // exist. Setting &root = cfg.getRoot(); if(! root.exists("inventory")) root.add("inventory", Setting::TypeGroup); Setting &inventory = root["inventory"]; if(! inventory.exists("movies")) inventory.add("movies", Setting::TypeList); Setting &movies = inventory["movies"]; // Create the new movie entry. Setting &movie = movies.add(Setting::TypeGroup); movie.add("title", Setting::TypeString) = "Buckaroo Banzai"; movie.add("media", Setting::TypeString) = "DVD"; movie.add("price", Setting::TypeFloat) = 12.99; movie.add("qty", Setting::TypeInt) = 20; // Write out the updated configuration. try { cfg.writeFile(output_file); cerr << "Updated configuration successfully written to: " << output_file << endl; } catch(const FileIOException &fioex) { cerr << "I/O error while writing file: " << output_file << endl; return false; } return true; }