void DebconfFrontend::cmd_x_loadtemplatefile(const QString ¶m) { QFile template_file(param); if (template_file.open(QFile::ReadOnly)) { QTextStream template_stream(&template_file); QString line = QStringLiteral(""); int linecount = 0; QHash <QString,QString> field_short_value; QHash <QString,QString> field_long_value; QString last_field_name; while ( !line.isNull() ) { ++linecount; line = template_stream.readLine(); qCDebug(DEBCONF) << linecount << line; if ( line.isEmpty() ) { if (!last_field_name.isEmpty()) { //Submit last block values. qCDebug(DEBCONF) << "submit" << last_field_name; QString item = field_short_value[QStringLiteral("template")]; QString type = field_short_value[QStringLiteral("type")]; QString short_description = field_short_value[QStringLiteral("description")]; QString long_description = field_long_value[QStringLiteral("description")]; m_data[item][DebconfFrontend::Type] = type; m_data[item][DebconfFrontend::Description] = short_description; m_data[item][DebconfFrontend::ExtendedDescription] = long_description; //Clear data. field_short_value.clear(); field_long_value.clear(); last_field_name.clear(); } } else { if (!line.startsWith(QLatin1Char(' '))) { last_field_name = line.section(QStringLiteral(": "), 0, 0).toLower(); field_short_value[last_field_name] = line.section(QStringLiteral(": "), 1); } else { if ( field_long_value[last_field_name].isEmpty() ){ field_long_value[last_field_name] = line.remove(0, 1); } else { field_long_value[last_field_name].append(QLatin1Char('\n')); if ( !(line.trimmed() == QStringLiteral(".")) ) { field_long_value[last_field_name].append(line.remove(0, 1)); } } } } } } else { say(QStringLiteral( "30 couldn't open file" )); return; } say(QStringLiteral( "0 ok" )); }
int template_load(char* filename) { int data; FILE* file; file = fopen(template_file(filename), "r"); if (file == 0) { printf("Nao consegui abrir o arquivo"); } while ((data = fgetc(file)) != EOF) { printf("%c", data); } fclose(file); return 1; }
void create_new_file_from_template( generator const& gen, std::string const& class_name, std::string const& file_extension) { std::string template_file_name = gen.m_template_file + file_extension + ".template"; std::ifstream template_file(template_file_name); if(!template_file.is_open()) { print_error_and_exit("Error opening template file \"" + template_file_name + "\""); } // Read file into string std::string file_string( (std::istreambuf_iterator<char>(template_file)), std::istreambuf_iterator<char>()); // Search for each token through the entire file, replacing as found. for(auto token_value_pair : gen.m_tokens) { size_t remove_it = file_string.find(token_value_pair.first); while(remove_it != std::string::npos) { // Find end of token (we searched for the whole token including %'s thus it is guaranteed // that it exists) size_t remove_end_it = file_string.find('%', remove_it + 1); size_t token_length = (remove_end_it + 1) - remove_it; // Replace entire token from remove_it to and including remove_end. file_string.replace(remove_it, token_length, token_value_pair.second); remove_it = file_string.find(token_value_pair.first.c_str(), remove_it); } } std::string output_file_name = class_name + file_extension; std::ofstream output_file(output_file_name); if(!output_file.is_open()) { std::cerr << "Error opening ouput file \"" << output_file_name << "\"exiting..." << std::endl; std::exit(-1); } output_file << file_string; std::cout << output_file_name << " written..." << std::endl; }
void Configuration::readTemplate(const std::string& fn, std::string& content) { std::string filename = path_templates + fn; std::ifstream template_file(filename.c_str()); content = ""; if (template_file.is_open()) { std::string line; while (template_file.good()) { std::getline(template_file, line); content = content + line + "\n"; } template_file.close(); } else { DBGERR(__FUNCTION__ << ": Cannot read template file: " << filename << "!") } }