dmpapplication.cpp 6.1 KB
/**************************************************************************
* file:              dmpapplication.cpp

* Author:            wanzhongping
* Date:              2021-02-24 11:23:15
* Email:             zhongpingw@chinadci.com
* copyright:         广州城市信息研究所有限公司
***************************************************************************/
#include "dmpapplication.h"
#include <iostream>
#include <fstream>
#include "dmpconfig.h"
#include <boost/property_tree/ini_parser.hpp>
#include <boost/property_tree/ptree.hpp>

// bool DmpApplication::running_from_build_dir_ = false;
// bool DmpApplication::initialized_ = false;
// std::string DmpApplication::plugin_path_ = "";
// std::string DmpApplication::prefix_path_ = "";
// std::string DmpApplication::libexec_path_ = "";
// std::string DmpApplication::library_path_ = "";
// std::string DmpApplication::build_source_path_ = "";
// std::string DmpApplication::build_output_path_ = "";
// std::string DmpApplication::pkgDataPath_ = "";

DmpApplication::DmpApplication()
{
    runningFromBuildDir_ = false;
    initialized_ = false;
}

DmpApplication::~DmpApplication()
{
}

DmpApplication* DmpApplication::Instance()
{
    static DmpApplication instance;
    return &instance;
}

void DmpApplication::initialize()
{
    resolvePkgPath();
    if (runningFromBuildDir_)
    {
        prefixPath_ = "";
        setPluginPath(buildSourcePath_ + '/' + DMAP_PLUGIN_SUBDIR);
        setPkgDataPath(buildOutputPath_ + "/data"); // in buildDir/data - used for: doc, resources, svg
        libraryPath_ = buildOutputPath_ + '/' + DMAP_LIB_SUBDIR + '/';
        libexecPath_ = buildOutputPath_ + '/' + DMAP_LIBEXEC_SUBDIR + '/';
    }
    else
    {
        char *prefixPath = getenv("DMAP_PREFIX_PATH");
        if (!prefixPath)
        {
            setPrefixPath("/usr/local");
        }else{
            setPrefixPath(prefixPath);
        }
    }
    // createIniFile(libexecPath_);
    initialized_ = true;
}
bool DmpApplication::isRunningFromBuildDir()
{
    return runningFromBuildDir_;
}
std::string DmpApplication::applicationDirPath()
{
    boost::filesystem::path libDir = boost::filesystem::current_path();
    return libDir.c_str();
}

std::string DmpApplication::resolvePkgPath()
{
    std::string appPath = applicationDirPath();
    if (!appPath.empty() || getenv("DMAP_PREFIX_PATH"))
    {
        std::string prefixPath = getenv("DMAP_PREFIX_PATH") ? getenv("DMAP_PREFIX_PATH") : appPath;
        // check if DMAP is run from build directory (not the install directory)
        boost::filesystem::path dmapbuildpath;
        boost::filesystem::path dir(prefixPath);
        if (searchFile(dir, "dmapbuildpath.txt", dmapbuildpath))
        {
            std::cout << dmapbuildpath.c_str() << std::endl;
            runningFromBuildDir_ = true;
            std::ifstream fin(dmapbuildpath.c_str());
            std::getline(fin, buildSourcePath_);
            boost::trim(buildSourcePath_);
            std::getline(fin, buildOutputPath_);
            boost::trim(buildOutputPath_);
        }

        if (isRunningFromBuildDir())
        {
            return buildOutputPath_ + "/Data";
        }
        else
        {
            return prefixPath + '/' + DMAP_DATA_SUBDIR;
        }
    }
    return "";
}
std::string DmpApplication::pkgDataPath()
{
    if(pkgDataPath_.empty()) {
        return resolvePkgPath();
    } else {
        return pkgDataPath_;
    }

}
void DmpApplication::setPkgDataPath(const std::string &pkgDataPath)
{
    pkgDataPath_ = pkgDataPath;
}

std::string DmpApplication::libraryPath()
{
  return libraryPath_;
}

void DmpApplication::setPrefixPath(const std::string &prefixPath)
{
    prefixPath_ = prefixPath;
    if(isRunningFromBuildDir())
    {
      setPluginPath(prefixPath_ + '/' + DMAP_PLUGIN_SUBDIR);
      setPkgDataPath(prefixPath_ + '/' + DMAP_DATA_SUBDIR);
    }
    libraryPath_ = prefixPath_ + '/' + DMAP_LIB_SUBDIR + '/';
    libexecPath_ = prefixPath_ + '/' + DMAP_LIBEXEC_SUBDIR + '/';
}

bool DmpApplication::searchFile(const boost::filesystem::path &dir, const std::string fileName, boost::filesystem::path &path)
{
    if (!boost::filesystem::exists(dir))
    {
        return false;
    }

    boost::filesystem::directory_iterator end_iter;
    for (boost::filesystem::directory_iterator iter(dir); iter != end_iter; ++iter)
    {
        if (boost::filesystem::is_regular_file(iter->status()) && iter->path().filename() == fileName)
        {
            path = iter->path();
            return true;
        }

        if (boost::filesystem::is_directory(iter->status()))
        {
            if (searchFile(iter->path(), fileName, path))
            {
                return true;
            }
        }
    }
    return false;
}

void DmpApplication::setPluginPath(const std::string &pluginPath)
{
    pluginPath_ = pluginPath;
}

std::string DmpApplication::prefixPath()
{
    return prefixPath_;
}

std::string DmpApplication::libexecPath()
{
    return libexecPath_;
}

std::string DmpApplication::pluginPath()
{
    return pluginPath_;
}

// bool DmpApplication::CreateDatabase()
// {
//     return false;
// }

std::string DmpApplication::buildOutputPath()
{
    return buildOutputPath_;
}

std::string DmpApplication::dmapSettingsDirPath()
{
    return "";
}

void DmpApplication::createIniFile(const std::string &filePath)
{
    std::string fileName = filePath + iniFileName_;
    if(!boost::filesystem::exists(fileName)) {
        boost::property_tree::ptree pt; 
        pt.put<std::string>("Core.Filter","%Severity% >= debug");  
	    pt.put<std::string>("Sinks.TextFileSettings.Destination","TextFile");
	    // pt.put<std::string>("Sinks.TextFileSettings.FileName","/var/log/dmap/%Y%m%d.log");
        // pt.put<bool>("Sinks.TextFileSettings.AutoFlush",true);
        boost::property_tree::ini_parser::write_ini(fileName,pt);
    }

    // boost::property_tree::ptree root_node, tag_system;
    // boost::property_tree::ini_parser::read_ini("config.ini", root_node);

}