+++ /dev/null
-#include <SimanIO_Configuration.hxx>
-
-#include <stdlib.h>
-
-// maximum length of the line in the parsed conf file
-const int MAX_BUFFER_SIZE=10000;
-
-using namespace std;
-
-SimanIO_Configuration::SimanIO_Configuration()
-{
-}
-
-///! Returns true is the given symbol is white space
-static bool IsWhite(const char theSym) {
- return theSym == ' ' || theSym == '\t';
-}
-
-///! Returns true if theLine starts with theCommand, returns in theVal rest part of the line in this case
-static bool IsCommand(const char* theCommand, char* theLine, char*& theVal) {
- string aLine(theLine);
- char* aComPtr = (char*)theCommand;
- theVal = theLine;
- while(*aComPtr != 0 && *theVal != 0) {
- if (IsWhite(*aComPtr)) { // skip white spaces in the command line
- aComPtr++;
- continue;
- }
- if (IsWhite(*theVal)) { // skip white spaces in the processed line
- theVal++;
- continue;
- }
- //cout<<"Compare symbols "<<tolower(*aComPtr)<<" and "<<tolower(*theVal)<<endl;
- if (tolower(*aComPtr) != tolower(*theVal)) // symbols are not match (case is not significant)
- break;
- aComPtr++;
- theVal++;
- }
- if (*aComPtr == 0) { // command has been found
- while(*theVal != 0 && IsWhite(*theVal)) { // skip white spaces in the processed line
- theVal++;
- }
- return true;
- }
- return false;
-}
-
-///! Converts string to the integer value, or returns the default vaule if failed
-static int toInt(const char* theStr, int theDefault) {
- int aResult;
- try {
- aResult = atol(theStr);
- } catch(...) {
- aResult = theDefault;
- }
- return aResult;
-}
-
-// unification of the error handling in the Parse method
-#define PARSE_ERROR(condition, message) if (condition) {cerr<<message<<endl; return false;}
-
-bool SimanIO_Configuration::Parse(const char* theConfFileName)
-{
- ifstream f(theConfFileName);
- myF = &f;
- PARSE_ERROR(!f.good(), "Can not read conf file "<<theConfFileName)
- char aBuffer[MAX_BUFFER_SIZE];
- myBuffer = aBuffer; // buffer for one line reading
- myReuseLine = false; // to read current line from the file
- char* aVal; // value in line, that is located after the identifier of the command
- while(!f.eof()) {
- ReadLine();
- if (IsCommand("Study ID :", myBuffer, aVal)) {
- // skip study ID: this info is known in Link
- } else if (IsCommand("Scenario ID :", myBuffer, aVal)) {
- // skip scenario ID: this info is known in Link
- } else if (IsCommand("User ID :", myBuffer, aVal)) {
- // skip user ID: this info is known in Link
- } else if (IsCommand("Activity", myBuffer, aVal)) {
- if (!ParseActivity(aVal)) return false;
- } else {
- PARSE_ERROR(true, "Invalid line of conf file: '"<<myBuffer<<"'");
- }
- }
- return true;
-}
-
-bool SimanIO_Configuration::ParseActivity(char* aName)
-{
- SimanIO_Activity aNewActivity;
- aNewActivity.SetName(aName);
- int activityID = -1; // to be imported later
- char* aVal; // value in line, that is located after the identifier of the command
- while(!myF->eof()) {
- ReadLine();
- if (IsCommand("Activity ID :", myBuffer, aVal)) {
- PARSE_ERROR(activityID != -1, "Duplicated activity identifier '"<<myBuffer<<"'");
- activityID = toInt(aVal, -1);
- PARSE_ERROR(activityID < 1, "Invalid activity identifier '"<<aVal<<"'");
- } else if (IsCommand("Module :", myBuffer, aVal)) { // read only if module not yet imported before
- PARSE_ERROR(aNewActivity.Module()[0] != 0, "Duplicated module name in '"<<myBuffer<<"'")
- PARSE_ERROR(aVal[0] == 0, "Invalid module name in '"<<myBuffer<<"'")
- aNewActivity.SetModule(aVal);
- } else if (IsCommand("Document :", myBuffer, aVal)) {
- if (!ParseDocument(aVal, aNewActivity)) return false;
- } else { // unknown command will be processed in higher level method
- myReuseLine = true;
- break;
- }
- }
- PARSE_ERROR(activityID == -1, "Activity '"<<aNewActivity.Name()<<"' ID is not defined");
- PARSE_ERROR(aNewActivity.Module()[0] == 0, "Module of the activity '"<<aNewActivity.Name()<<"' is not defined");
- AddActivity(aNewActivity, activityID);
- return true;
-}
-
-bool SimanIO_Configuration::ParseDocument(char* aName, SimanIO_Activity& theActivity)
-{
- SimanIO_Document aNewDocument;
- aNewDocument.SetName(aName);
- int aDocID = -1; // to be imported later
- char* aVal; // value in line, that is located after the identifier of the command
- while(!myF->eof()) {
- ReadLine();
- if (IsCommand("Document ID :", myBuffer, aVal)) {
- PARSE_ERROR(aDocID != -1, "Duplicated document identifier '"<<myBuffer<<"'");
- aDocID = toInt(aVal, -1);
- PARSE_ERROR(aDocID < 1, "Invalid document identifier '"<<aVal<<"'");
- } else if (IsCommand("Source file:", myBuffer, aVal) || IsCommand("Result file:", myBuffer, aVal)) {
- if (!ParseFile(aNewDocument)) return false;
- } else { // unknown command will be processed in higher level method
- myReuseLine = true;
- break;
- }
- }
- PARSE_ERROR(aDocID == -1, "Document '"<<aNewDocument.Name()<<"' ID is not defined");
- theActivity.AddDocument(aDocID, aNewDocument);
- return true;
-}
-
-bool SimanIO_Configuration::ParseFile(SimanIO_Document& theDocument)
-{
- SimanIO_File aNewFile;
- aNewFile.id = -1;
- int aDocID = -1; // to be imported later
- char* aVal; // value in line, that is located after the identifier of the command
-
- // current line contains one of the next two commands
- if (IsCommand("Result file :", myBuffer, aVal)) {
- aNewFile.result = true;
- aNewFile.url = aVal;
- } else if (IsCommand("Source file :", myBuffer, aVal)) {
- aNewFile.result = false;
- aNewFile.url = aVal;
- }
- PARSE_ERROR(aNewFile.url.empty(), "File '"<<myBuffer<<"' has invalid URL");
- while(!myF->eof()) {
- ReadLine();
- if (IsCommand("Automatic processing :", myBuffer, aVal)) {
- char* aVal2;
- if (IsCommand("file-download", aVal, aVal2)) aNewFile.proc = FILE_DOWNLOAD;
- else if (IsCommand("file-import", aVal, aVal2)) aNewFile.proc = FILE_IMPORT;
- else PARSE_ERROR(true, "Invalid automatic processing value '"<<aVal<<"'");
- } else if (IsCommand("State :", myBuffer, aVal)) {
- char* aVal2;
- if (IsCommand("file-actual", aVal, aVal2)) aNewFile.state = FILE_ACTUAL;
- else if (IsCommand("file-outdated", aVal, aVal2)) aNewFile.state = FILE_OUTDATED;
- else PARSE_ERROR(true, "Invalid state value '"<<aVal<<"'");
- } else if (IsCommand("File ID :", myBuffer, aVal)) {
- PARSE_ERROR(aNewFile.id != -1, "Duplicated file identifier '"<<myBuffer<<"'");
- aNewFile.id = toInt(aVal, -1);
- PARSE_ERROR(aNewFile.id < 0, "Invalid file identifier '"<<aVal<<"'");
- } else { // unknown command will be processed in higher level method
- myReuseLine = true;
- break;
- }
- }
- PARSE_ERROR(aNewFile.id < 0, "File '"<<aNewFile.url<<"' ID is not defined");
- theDocument.AddFile(aNewFile);
- return true;
-}
-
-int SimanIO_Configuration::AddActivity(const SimanIO_Activity& theActivity, const int theId)
-{
- int anId = theId;
- if (theId < 0) { // generate the maximum unique Id
- if (myActivities.empty()) anId = 1; // the first
- else {
- anId = myActivities.rbegin()->first + 1; // take the maximum Id plus one (in map integers are ordered)
- }
- }
- myActivities[anId] = theActivity;
-}
-
-SimanIO_Activity& SimanIO_Configuration::GetOrCreateActivity(const int theId)
-{
- if (myActivities.find(theId) == myActivities.end()) {
- myActivities[theId] = SimanIO_Activity();
- }
- return myActivities[theId];
-}
-
-void SimanIO_Configuration::ReadLine() {
- if (myReuseLine) { // reuse current line
- myReuseLine = false;
- return;
- }
- do {
- myF->getline(myBuffer, MAX_BUFFER_SIZE - 1);
- } while(!myF->eof() && myBuffer[0] == 0); // continue if there is empty line
-}
-
-/////////////////////////////////// iterator methods ///////////////////////////
-
-SimanIO_Configuration::ActivitiesIterator::ActivitiesIterator(SimanIO_Configuration& theConf)
-{
- myIter = theConf.myActivities.begin();
- myEnd = theConf.myActivities.end();
-}
-
-void SimanIO_Configuration::ActivitiesIterator::Next()
-{
- myIter++;
-}
-
-bool SimanIO_Configuration::ActivitiesIterator::More()
-{
- return myIter != myEnd;
-}
-
-const int SimanIO_Configuration::ActivitiesIterator::ActivityId()
-{
- return myIter->first;
-}
-
-SimanIO_Activity& SimanIO_Configuration::ActivitiesIterator::Activity()
-{
- return myIter->second;
-}