Example #1
0
void DataManager::loadQuantities(const QDir& data_dir) {
	if( !data_dir.exists() ) {
		qWarning( "Error: incorrect data path.\nNothing will be loaded." );
		return;
	}

	QFile order_file(data_dir.path() + "/order");
	order_file.open(QIODevice::ReadOnly | QIODevice::Text);
	if( !order_file.exists() ) {
		qWarning("Error: order file is missing.\nNothing will be loaded.");
		return;
	}

	QDir quantities_dir( data_dir.path() + "/quantities",  QString("*.xml"), QDir::Name, QDir::Files );
	QDir icons_dir     ( data_dir.path() + "/icons",       QString("*.png"), QDir::Name, QDir::Files );

	//Read order file
	QString line;
	while( !order_file.atEnd() ) {
		line = order_file.readLine();
		line.remove('\n');

		//Try to open icon
		QPixmap icon;
		QString icon_path = icons_dir.path() + '/' + line + ".png";
		if( !icon.load(icon_path) ) {
			qWarning( "Icon for \"%s\" was not found.", qPrintable(line) );
			icon.load(":/icon/no_icon");
		}

		//Try to open XML file.
		QFile file( quantities_dir.path() + '/' + line + ".xml" );
		if(  !file.open(QIODevice::ReadOnly | QIODevice::Text)  ) {
			qWarning( "Error:Cannot open quantity %s.", qPrintable(file.fileName()) );
			continue;
		}

		//Try to load XML file.
		QDomDocument document;
		QString err_s;
		int err_l = 0, err_c = 0;
		if(  !document.setContent(&file, false, &err_s, &err_l, &err_c)  ) {
			qWarning(  "In quantity \"%s:\" At line %i, column %i. %s", qPrintable(file.fileName()), err_l, err_c, qPrintable(err_s)  );
			continue;
		}

		quantities.push_back( loadQuantity( document, icon ) );
	}
	order_file.close();
}
Example #2
0
// std::vector<std::pair<std::string, order_t> > sorted_tpl_path;
void load_sorted_templates(const std::string& get_command,
                           std::vector<std::pair<std::string, order_t> >& sorted_tpl_path)
{
    // TODO
    // read_config!
    // 1. 将.tpl所有文件,检索出来,并导出为外部序列;
    // 2. 应用 .tpl.config 的优先级描述;对序列重新排序;
    // 3. 逐个使用模板规则,创建文件;
    std::map<std::string, order_t> tpl_files;
    {
        sss::path::file_descriptor fd;
        sss::path::glob_path_recursive gp(get_command, fd);
        uint32_t low_order = 0;
        while (gp.fetch()) {
            std::string rel_path;
            if (fd.is_normal_file()) {
                if (fd.get_name() == tpl_conf_name ||
                    fd.get_name() == tpl_help_name ||
                    fd.get_name() == tpl_order_name)
                {
                    continue;
                }
                rel_path = sss::path::relative_to(fd.get_path(), get_command);
            }
            else if (fd.is_normal_dir()) {
                rel_path = sss::path::relative_to(fd.get_path(), get_command) + sss::path::sp_char;
            }
            else {
                continue;
            }
            // 默认优先级
            tpl_files[rel_path] = order_t(50, low_order++);
        }
    }

    std::string order_file_path = sss::path::append_copy(get_command, tpl_order_name);
    if (sss::path::filereadable(order_file_path)) {
        std::cout << VALUE_MSG(order_file_path) << std::endl;
        std::ifstream order_file(order_file_path);
        std::string line;
        sss::regex::CRegex reg_order(R"(^0*([0-9]+)\s+(.+)$)");
        // NOTE FIXME 如果文件名,就是数字,如何处理?
        sss::regex::CRegex reg_name(R"(^\s*(.+)\s*$)");
        uint32_t low_order = 0;
        while (std::getline(order_file, line)) {
            uint32_t high_order = 50;
            std::string rel_path;
            if (reg_order.match(line)) {
                high_order = sss::string_cast<int>(reg_order.submatch(1));
                rel_path = reg_order.submatch(2);
                path_mixed2win_if(rel_path);
                rel_path = sss::path::simplify(rel_path);
            }
            else if (reg_name.match(line)) {
                rel_path = reg_order.submatch(1);
                path_mixed2win_if(rel_path);
                sss::path::simplify(rel_path);
            }
            else {
                continue;
            }
            order_t order = order_t(high_order, low_order++);
            // std::cout << order << " = " << rel_path << std::endl;
            if (tpl_files.find(rel_path) == tpl_files.end()) {
                std::cout
                    << sss::Terminal::error
                    << "order setting file\n"
                    << "\t" << rel_path << " is not exists!"
                    << sss::Terminal::end
                    << std::endl;
                continue;
            }
            tpl_files[rel_path] = order;
        }