src/Env/Env.cpp

説明を見る。
00001 // 
00002 // Copyright (c) 2005-2008 Kenichi Watanabe.
00003 // Copyright (c) 2005-2008 Yasuhiro Watari.
00004 // Copyright (c) 2005-2008 Hironori Ichibayashi.
00005 // Copyright (c) 2008-2009 Kazuo Horio.
00006 // Copyright (c) 2009-2013 Naruki Kurata.
00007 // Copyright (c) 2005-2013 Ryota Shioya.
00008 // Copyright (c) 2005-2013 Masahiro Goshima.
00009 // 
00010 // This software is provided 'as-is', without any express or implied
00011 // warranty. In no event will the authors be held liable for any damages
00012 // arising from the use of this software.
00013 // 
00014 // Permission is granted to anyone to use this software for any purpose,
00015 // including commercial applications, and to alter it and redistribute it
00016 // freely, subject to the following restrictions:
00017 // 
00018 // 1. The origin of this software must not be misrepresented; you must not
00019 // claim that you wrote the original software. If you use this software
00020 // in a product, an acknowledgment in the product documentation would be
00021 // appreciated but is not required.
00022 // 
00023 // 2. Altered source versions must be plainly marked as such, and must not be
00024 // misrepresented as being the original software.
00025 // 
00026 // 3. This notice may not be removed or altered from any source
00027 // distribution.
00028 // 
00029 // 
00030 
00031 
00032 #include <pch.h>
00033 #include "Env/Env.h"
00034 #include <boost/filesystem.hpp>
00035 
00036 namespace Onikiri
00037 {
00038     Environment g_env;
00039 }
00040 
00041 using namespace Onikiri;
00042 using namespace std;
00043 
00044 Environment::Path::Path()
00045 {
00046     m_useXMLFile = false;
00047     m_useSimulatorExecFile = false;
00048 }
00049 
00050 static String RemoveFileName(const String& path)
00051 {
00052     String ret;
00053     int indexBs = (int)path.rfind( "\\" );
00054     int indexSl = (int)path.rfind( "/" );
00055     int indexPath = indexSl > indexBs ? indexSl : indexBs;
00056     if(indexPath == -1)
00057         ret = "./";
00058     else
00059         ret.assign(path.begin(), path.begin() + indexPath + 1);
00060     return ret;
00061 }
00062 
00063 String Environment::Path::Get()
00064 {
00065     if(m_useSimulatorExecFile){
00066         return m_execFilePath + m_path;
00067     }
00068     else if(m_useXMLFile){
00069         const ParamXMLPath path = GetChildRootPath() + "/@UseXMLFilePath";
00070         String xmlFile;
00071         if(!g_paramDB.GetSourceXMLFile(path, xmlFile)){
00072             THROW_RUNTIME_ERROR("UseXMLFilePath is enabled and parameter is not found in XML file.");
00073         }
00074         return RemoveFileName( xmlFile ) + m_path;
00075     }
00076 
00077     return m_path;
00078 }
00079 
00080 void Environment::Path::Initialize( Environment& env )
00081 {
00082     m_startupPath  = env.GetStartupPath();
00083     m_execFilePath = RemoveFileName( env.GetCmdLineArgs()[0] );
00084 }
00085 
00086 // ----
00087 Environment::Environment()
00088 {
00089     m_cmdLineArgs.clear();
00090     m_execBeginTime = 0;
00091     m_execPeriod = 0;
00092     m_error = false;
00093     m_dumpSuccess = false;
00094     m_outputPrintToSTDOUT = true;
00095     m_suppressInternalMessage = false;
00096     m_suppressWarning = false;
00097     m_paramDBInitialized = false;
00098 
00099     m_versionString.format(
00100         "%x.%02x", 
00101         ONIKIRI_VERSION >> ONIKIRI_VERSION_DECIMAL_POINT,
00102         ONIKIRI_VERSION & ((1<<ONIKIRI_VERSION_DECIMAL_POINT)-1) );
00103 }
00104 
00105 Environment::~Environment()
00106 {
00107 }
00108 
00109 
00110 void Environment::Initialize(
00111     int argc, 
00112     char* argv[],
00113     const std::vector<String>& defaultParams )
00114 {
00115     if( argc <= 1 ){
00116         THROW_RUNTIME_ERROR( "No input parameter is passed." );
00117     }
00118 
00119     m_startupPath = 
00120         boost::filesystem::initial_path().string() + "/";
00121 
00122     if(!g_paramDB.Initialize( m_startupPath )){
00123         THROW_RUNTIME_ERROR("Initializing ParamDB failed.");
00124     }
00125     m_paramDBInitialized = true;
00126 
00127     for(size_t i = 0; i < defaultParams.size(); i++){
00128         g_paramDB.AddUserDefaultParam( defaultParams[i] );
00129     }
00130 
00131     for( int i = 0; i < argc; i++){
00132         m_cmdLineArgs.push_back(argv[i]);
00133     }
00134     g_paramDB.LoadParameters( m_cmdLineArgs );
00135 
00136     LoadParam();
00137     m_hostWorkPath.Initialize(*this);
00138 
00139     m_outputPrintToSTDOUT = m_outputPrintFileName == "";
00140     if(!m_outputPrintToSTDOUT){
00141         String printFileName = GetHostWorkPath() + m_outputPrintFileName;
00142         m_outputPrintStream.open( printFileName );
00143         if(!m_outputPrintStream){
00144             m_outputPrintToSTDOUT = true;   // It is required to output below message.
00145             THROW_RUNTIME_ERROR( "Could not open '%s'.", m_outputPrintFileName.c_str() );
00146         }
00147     }
00148 
00149     SuppressWaning( m_suppressWarning );
00150     PrintInternal( "Onikiri Version %s\n", m_versionString.c_str() );
00151     BeginExec();
00152 }
00153 
00154 void Environment::Finalize()
00155 {
00156     EndExec();
00157     ReleaseParam();
00158 
00159     if(m_paramDBInitialized){
00160         DumpResult();
00161         m_dumpSuccess = true;
00162     }
00163 
00164     g_paramDB.Finalize();
00165 }
00166 
00167 void Environment::DumpResult()
00168 {
00169     if( m_error ){
00170         // Set an error message to the following path.
00171         g_paramDB.Set( "/Session/Result/Error/Message/text()", m_errorMsg );
00172     }
00173 
00174     // Dump XML data to a string.
00175     String resultStr = 
00176         g_paramDB.DumpResultXML( m_outputXMLLevel, m_outputXMLFilter );
00177 
00178     bool outputToFile = false;
00179     if(m_outputXMLFileName != ""){
00180         std::ofstream ofs;
00181         String fileName = GetHostWorkPath() + m_outputXMLFileName;
00182         ofs.open( fileName );
00183         if(ofs){
00184             ofs << resultStr.c_str();
00185             ofs.close();
00186             outputToFile = true;
00187         }
00188         else{
00189             Print("Could not open output xml file '%s'.\n", fileName.c_str());
00190         }
00191     }
00192 
00193     if(!outputToFile){
00194         // Not used "Print"
00195         printf( "\n\n%s", resultStr.c_str() );
00196     }
00197 }
00198 
00199 void Environment::Print(const string& str)
00200 {
00201     return Print( str.c_str() );
00202 }
00203 
00204 void Environment::Print(const char* fmt, ...)
00205 {
00206     String str;
00207     va_list arg;
00208     va_start(arg, fmt);
00209     str.format_arg(fmt, arg);
00210     va_end(arg);
00211 
00212     // 
00213     if( m_outputPrintToSTDOUT )
00214         printf( "%s", str.c_str() );
00215     else
00216         m_outputPrintStream << str;
00217 }
00218 
00219 void Environment::PrintInternal(const char* fmt, ...)
00220 {
00221     String str;
00222     va_list arg;
00223     va_start(arg, fmt);
00224     str.format_arg(fmt, arg);
00225     va_end(arg);
00226 
00227     if(!m_suppressInternalMessage)
00228         Print(str.c_str());
00229 }
00230 
00231 
00232 const vector<String>& Environment::GetCfgXmlFiles()
00233 {
00234     return m_cfgXmlFiles;
00235 }
00236 
00237 const vector<String>& Environment::GetCmdLineArgs()
00238 {
00239     return m_cmdLineArgs;
00240 }
00241 
00242 String Environment::GetHostWorkPath()
00243 {
00244     return m_hostWorkPath.Get();
00245 }
00246 
00247 String Environment::GetStartupPath()
00248 {
00249     return m_startupPath;
00250 }
00251 
00252 void Environment::BeginExec()
00253 {
00254     m_execBeginTime = clock();
00255 }
00256 
00257 void Environment::EndExec()
00258 {
00259     m_execPeriod =
00260         ((u64)clock() - m_execBeginTime) * 1000 / CLOCKS_PER_SEC;
00261 }
00262 
00263 void Environment::SetError( bool error, const std::string& msg )
00264 {
00265     m_error = error;
00266     m_errorMsg = msg;
00267 }
00268 
00269 bool Environment::IsDumpSuccess()
00270 {
00271     return m_dumpSuccess;
00272 }
00273 
00274 void Environment::PrintFatalErrorXML()
00275 {
00276     Print(
00277         "<?xml version='1.0' encoding='UTF-8'?>\n"
00278         "<Session>\n"
00279         "  <Result Error='1'>\n"
00280         "</Session>\n"
00281     );
00282 }
00283 
00284 bool Environment::IsSuppressedInternalMessage()
00285 {
00286     return m_suppressInternalMessage;
00287 };

Onikiri2に対してTue Jun 18 14:34:20 2013に生成されました。  doxygen 1.4.7