/*************************************************************************** PFunctionHandler.cpp Author: Andreas Suter e-mail: andreas.suter@psi.ch ***************************************************************************/ /*************************************************************************** * Copyright (C) 2007-2026 by Andreas Suter * * andreas.suter@psi.c * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include #include #include #include "PFunctionHandler.h" //------------------------------------------------------------- /** * @brief Constructs a PFunctionHandler from a file. * * Reads the specified file to extract parameter values (PAR block), * map indices (MAP block), and function definitions (FUNCTIONS block). * Validates that all required blocks are present and that map indices * are within parameter bounds. * * @param fln Filename containing the function definitions * @param debug Enable debug output showing parsing details */ //------------------------------------------------------------- PFunctionHandler::PFunctionHandler(char *fln, bool debug) : fDebug(debug), fFileName(fln) { fValid = true; std::cout << std::endl << "in PFunctionHandler(char *fln)"; std::cout << std::endl << "fFileName = " << fFileName; fValid = ReadFile(); if (fValid) fValid = MapsAreValid(); } //------------------------------------------------------------- /** * @brief Constructs a PFunctionHandler from a vector of input lines. * * Parses the provided lines to extract PAR, MAP, and FUNCTIONS blocks. * This constructor is useful for testing or when input comes from * sources other than files. Validates that all required blocks are * present and that map indices are within parameter bounds. * * @param lines Vector of strings containing the input (PAR, MAP, FUNCTIONS blocks) */ //------------------------------------------------------------- PFunctionHandler::PFunctionHandler(std::vector lines) { fValid = true; fFileName = ""; std::cout << std::endl << "in PFunctionHandler(std::vector lines)"; if (lines.size() == 0) { fValid = false; return; } // analyze input bool done = false; int status; int val[10]; double dval[10]; bool inFcnBlock = false; for (unsigned int i=0; i fFuncs.size()) return -1; return fFuncs[i].GetFuncNo(); } //------------------------------------------------------------- /** * @brief Reads and parses the input file. * * Opens the file specified in fFileName and extracts: * - PAR block: parameter values (up to 10 per line) * - MAP block: map indices (up to 10 per line) * - FUNCTIONS block: function definition lines * * Lines starting with # are treated as comments and ignored. * Processing stops when END tag is encountered. * Converts all input to uppercase for case-insensitive parsing. * * @return true if file was successfully read and all required blocks found */ //------------------------------------------------------------- bool PFunctionHandler::ReadFile() { std::cout << std::endl << "in ~PFunctionHandler::ReadFile()"; if (fFileName.length() == 0) { std::cout << std::endl << "PFunctionHandler::ReadFile(): **ERROR**"; std::cout << std::endl << " no file name given :-(. Will quit"; return false; } std::ifstream f; f.open(fFileName.c_str(), std::ifstream::in); if (!f.is_open()) { std::cout << std::endl << "PFunctionHandler::ReadFile(): **ERROR**"; std::cout << std::endl << " File '" << fFileName.c_str() << "' couldn't being opened."; return false; } std::string line; char c_line[128]; bool done = false; bool success = true; int status; int val[10]; double dval[10]; bool inFcnBlock = false; while ( !f.eof() && !done) { f.getline(c_line, 128); // line of text excluding '\n' line = c_line; size_t pos = line.find('\r'); line.resize(pos); if (line.find("#") == 0) // comment hence ignore continue; boost::to_upper(line); if (line.find("PAR") == 0) { std::cout << std::endl << "this is a parameter line ..."; status = sscanf(line.c_str(), "PAR %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", &dval[0], &dval[1], &dval[2], &dval[3], &dval[4], &dval[5], &dval[6], &dval[7], &dval[8], &dval[9]); if (status < 0) { done = true; success = false; std::cout << std::endl << "invalid PAR line, sorry ..."; } else { // fill map std::cout << std::endl << "PAR line, status = " << status; for (int i=0; i maxParam) success = false; if (!success) std::cout << std::endl << "invalid MAP found ..."; return success; } //------------------------------------------------------------- /** * @brief Finds the internal vector index for a function by its number. * * Searches the fFuncs vector for a function with the specified function * number. This maps from the user-visible function number (FUN#) to the * internal 0-based vector index. * * @param funcNo Function number to search for (from FUN# declaration) * @return Index in fFuncs vector (0-based), or -1 if not found */ //------------------------------------------------------------- int PFunctionHandler::GetFuncIndex(int funcNo) { int index = -1; for (unsigned int i=0; i