Ejemplo n.º 1
0
bool ConfigManager::init()
{
    char * confDir = getenv("CALVR_CONFIG_DIR");
    if(confDir)
    {
        _configDir = confDir;
    }
    else
    {
        _configDir = CalVR::instance()->getConfigDir();
    }

    std::string file;
    char * confFile = getenv("CALVR_CONFIG_FILE");
    if(confFile)
    {
        file = confFile;
    }
    else
    {
        std::cerr << "Warning: CALVR_CONFIG_FILE not set, using config.xml."
                << std::endl;
        file = "config.xml";
    }

    if(file.empty())
    {
        std::cerr << "Error: CALVR_CONFIG_FILE is empty." << std::endl;
        return false;
    }

    std::vector<std::string> fileList;
    size_t pos = 0;
    while((pos = file.find_first_of(':')) != std::string::npos)
    {
        if(pos)
        {
            fileList.push_back(file.substr(0,pos));
        }

        if(pos + 1 < file.size())
        {
            file = file.substr(pos + 1,file.size() - (pos + 1));
        }
        else
        {
            break;
        }
    }

    if(file.size())
    {
        fileList.push_back(file);
    }

    if(!fileList.size())
    {
        std::cerr << "Error: no valid config file in CALVR_CONFIG_FILE"
                << std::endl;
        return false;
    }

    for(int i = 0; i < fileList.size(); i++)
    {
        size_t pos = fileList[i].find_last_of('.');
        if(pos == std::string::npos || pos + 1 == fileList[i].size())
        {
            std::cerr
                    << "ConfigManager: Error: Unable to find extension for file: "
                    << fileList[i] << std::endl;
            return false;
        }
        std::string extension = fileList[i].substr(pos + 1,
                fileList[i].size() - (pos + 1));
        std::transform(extension.begin(),extension.end(),extension.begin(),
                ::tolower);

        ConfigFileReader * cfr = NULL;
        if(extension == "xml")
        {
            cfr = new XMLReader();
        }
        else
        {
            std::cerr
                    << "ConfigManager: Error: No reader could be identified for file: "
                    << fileList[i] << std::endl;
            return false;
        }

        if(cfr)
        {
            cfr->setDebugOutput(true);
            if(cfr->loadFile(fileList[i]))
            {
                _configFileList.push_back(cfr);
            }
            else
            {
                std::cerr << "ConfigManager: Error loading config files."
                        << std::endl;
                delete cfr;
                return false;
            }
            cfr->setDebugOutput(false);
        }
        else
        {
            std::cerr
                    << "ConfigManager: Error: ConfigFileReader pointer is NULL. file: "
                    << fileList[i] << std::endl;
            return false;
        }
    }

    _debugOutput = getBool("ConfigDebug",false);
    for(int i = 0; i < _configFileList.size(); i++)
    {
        _configFileList[i]->setDebugOutput(_debugOutput);
    }

    return true;
}