예제 #1
0
파일: config.cpp 프로젝트: bacek/xscript
XmlConfig::XmlConfigData::XmlConfigData(const char *file) :
    doc_(NULL), filename_(file) {

    namespace fs = boost::filesystem;
    std::string file_str;

#ifdef BOOST_FILESYSTEM_VERSION
    #if BOOST_FILESYSTEM_VERSION == 3
        fs::path path(file);
        file_str = path.native();
    #else
        fs::path path(file, fs::no_check);
        file_str = path.native_file_string();
    #endif
#else
    fs::path path(file, fs::no_check);
    file_str = path.native_file_string();
#endif

    if (!fs::exists(path)) {
        std::stringstream stream;
        stream << "can not read " << file_str;        
        throw std::runtime_error(stream.str());
    }
    
    doc_ = XmlDocHelper(xmlParseFile(file_str.c_str()));

    XmlUtils::throwUnless(NULL != doc_.get());
    if (NULL == xmlDocGetRootElement(doc_.get())) {
        throw std::logic_error("got empty config");
    }
    XmlUtils::throwUnless(xmlXIncludeProcess(doc_.get()) >= 0);
    findVariables(doc_);
}
예제 #2
0
XmlConfig::XmlConfig(const char *file)
: doc_(nullptr), regex_("\\$\\{([A-Za-z][A-Za-z0-9\\-]*)\\}") {
	try {
		std::ifstream f(file);
		if (!f) {
			throw std::runtime_error(std::string("can not open ").append(file));
		}

		setFilename(file);

		doc_ = XmlDocHelper(xmlParseFile(file));
		XmlUtils::throwUnless(nullptr != doc_.get());
		if (nullptr == xmlDocGetRootElement(doc_.get())) {
			throw std::logic_error("got empty config");
		}
		XmlUtils::throwUnless(xmlXIncludeProcess(doc_.get()) >= 0);
		findVariables(doc_);
	} catch (const std::ios::failure &e) {
		throw std::runtime_error(std::string("can not read ").append(file));
	}
}