Ignore:
Timestamp:
12/07/22 13:07:07 (19 months ago)
Author:
jderouillat
Message:

Add the ability to specify the location of iodef through an environment variable XIOS_IODEF_PATH. Included XML sources can integrates environment variables

File:
1 edited

Legend:

Unmodified
Added
Removed
  • XIOS3/trunk/src/node/context.cpp

    r2426 r2438  
    3535#include <chrono> 
    3636#include <random> 
     37#include <unistd.h> 
    3738 
    3839namespace xios 
     
    162163      if (attributes.end() != attributes.find("src")) 
    163164      { 
    164          StdIFStream ifs ( attributes["src"].c_str() , StdIFStream::in ); 
     165         string srcString = attributes["src"].c_str(); 
     166 
     167         // check if environment variables 
     168         string srcKey;          
     169         if (srcString.find("${") == 0) // src start wit 
     170         { 
     171           size_t envVarStart = srcString.find("${")+2; // +2 for ${ 
     172           if (srcString.find("}") != std::string::npos) 
     173           { 
     174             size_t envVarEnd = srcString.find("}")-2;  // -2 (start @ srcString begining, not after ${ 
     175             if(const char* var_path = std::getenv( srcString.substr( envVarStart, envVarEnd ).c_str() ) ) 
     176             { 
     177               srcKey=string(var_path)+"/"+srcString.substr( srcString.find("}")+1, srcString.size()-srcString.find("}") ); 
     178             } 
     179             else 
     180             { 
     181               ERROR("void CContext::parse(xml::CXMLNode & node)", 
     182                     <<endl<< "The src key " << attributes["src"].c_str()<< " contains an undefined variable." ); 
     183             } 
     184           } 
     185           else 
     186           { 
     187             ERROR("void CContext::parse(xml::CXMLNode & node)", 
     188                   <<endl<< "The src key " << attributes["src"].c_str()<< " not defined correctly." ); 
     189           } 
     190         } 
     191         else if (srcString.find("${") != 0) 
     192         { 
     193           ERROR("void CContext::parse(xml::CXMLNode & node)", 
     194                   <<endl<< "The src key " << attributes["src"].c_str()<< " should start with $" ); 
     195         } 
     196         else if (srcString.find("$") != std::string::npos) 
     197         { 
     198           ERROR("void CContext::parse(xml::CXMLNode & node)", 
     199                   <<endl<< "The src key " << attributes["src"].c_str()<< " need { and }." ); 
     200         } 
     201         else // standard behavor, without environment variable 
     202         { 
     203           srcKey = attributes["src"].c_str(); 
     204         } 
     205          
     206         StdIFStream ifs ( srcKey , StdIFStream::in ); 
    165207         if ( (ifs.rdstate() & std::ifstream::failbit ) != 0 ) 
     208         { 
     209            char currentPath[256]; 
     210            getcwd(currentPath, sizeof(currentPath)); 
    166211            ERROR("void CContext::parse(xml::CXMLNode & node)", 
    167                   <<endl<< "Can not open <"<<attributes["src"].c_str()<<"> file" ); 
     212                  <<endl<< "Can not open <"<<attributes["src"].c_str()<<"> file " 
     213                  << "from iodef path : " << currentPath ); 
     214         } 
    168215         if (!ifs.good()) 
    169216            ERROR("CContext::parse(xml::CXMLNode & node)", 
Note: See TracChangeset for help on using the changeset viewer.