dmpapplication.cpp 5.4 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 "dmplogger.h"

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::pkg_data_path_ = "";

DmpApplication::DmpApplication()
{
    std::cout << "constructor called!" << std::endl;
    LOGGER_DEBUG("constructor called!");
}

DmpApplication::~DmpApplication()
{
    std::cout << "destructor called!" << std::endl;
    LOGGER_DEBUG("destructor called!");
}

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

void DmpApplication::Init()
{
    LOGGER_INFO("DmpApplication init...");
    ResolvePkgPath();
    if (running_from_build_dir_)
    {
        // we run from source directory - not installed to destination (specified prefix)
        prefix_path_ = "";
        set_plugin_path(build_source_path_ + '/' + DMAP_PLUGIN_SUBDIR);
        set_pkg_data_path(build_output_path_ + "/data"); // in buildDir/data - used for: doc, resources, svg
        library_path_ = build_output_path_ + '/' + DMAP_LIB_SUBDIR + '/';
        libexec_path_ = build_output_path_ + '/' + DMAP_LIBEXEC_SUBDIR + '/';
    }
    else
    {
        char *prefix_path = getenv("DMAP_PREFIX_PATH");
        if (!prefix_path)
        {
            set_prefix_path("/usr/local");
        }else{
            set_prefix_path(prefix_path);
        }
    }
    initialized_ = true;
    LOGGER_INFO("DmpApplication init done");
}
bool DmpApplication::IsRunningFromBuildDir()
{
    return running_from_build_dir_;
}
std::string DmpApplication::ApplicationDirPath()
{
    boost::filesystem::path libDir = boost::filesystem::current_path();
    return libDir.c_str();
}

std::string DmpApplication::ResolvePkgPath()
{
    std::string app_path = ApplicationDirPath();
    if (!app_path.empty() || getenv("DMAP_PREFIX_PATH"))
    {
        std::string prefix_Path = getenv("DMAP_PREFIX_PATH") ? getenv("DMAP_PREFIX_PATH") : app_path;
        // check if DMAP is run from build directory (not the install directory)
        boost::filesystem::path dmapbuildpath;
        boost::filesystem::path dir(prefix_Path);
        if (SearchFile(dir, "dmapbuildpath.txt", dmapbuildpath))
        {
            std::cout << dmapbuildpath.c_str() << std::endl;
            running_from_build_dir_ = true;
            std::ifstream fin(dmapbuildpath.c_str());
            std::getline(fin, build_source_path_);
            boost::trim(build_source_path_);
            std::getline(fin, build_output_path_);
            boost::trim(build_output_path_);
        }

        if (IsRunningFromBuildDir)
        {
            return build_output_path_ + "/Data";
        }
        else
        {
            return prefix_Path + '/' + DMAP_DATA_SUBDIR;
        }
    }
    return "";
}
std::string DmpApplication::pkg_data_path()
{
    return pkg_data_path_;
}
void DmpApplication::set_pkg_data_path(const std::string &pkg_data_path)
{
    pkg_data_path_ = pkg_data_path;
}

std::string DmpApplication::library_path()
{
  return library_path_;
}

void DmpApplication::set_prefix_path(const std::string &prefix_path)
{
    LOGGER_DEBUG("set_prefix_path: " + prefix_path);
    prefix_path_ = prefix_path;
    if(IsRunningFromBuildDir)
    {
      set_plugin_path(prefix_path_ + '/' + DMAP_PLUGIN_SUBDIR);
      set_pkg_data_path(prefix_path_ + '/' + DMAP_DATA_SUBDIR);
    }
    library_path_ = prefix_path_ + '/' + DMAP_LIB_SUBDIR + '/';
    libexec_path_ = prefix_path_ + '/' + DMAP_LIBEXEC_SUBDIR + '/';
}

bool DmpApplication::SearchFile(const boost::filesystem::path &dir, const std::string file_name, 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() == file_name)
        {
            path = iter->path();
            return true;
        }

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

void DmpApplication::set_plugin_path(const std::string &plugin_path)
{
    plugin_path_ = plugin_path;
}

std::string DmpApplication::prefix_path()
{
    return prefix_path_;
}

std::string DmpApplication::libexec_path()
{
    return libexec_path_;
}

std::string DmpApplication::plugin_path()
{
    return plugin_path_;
}

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

std::string DmpApplication::build_output_path()
{
    return build_output_path_;
}

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