Exemple #1
0
void UserStringList(const std::string& str_list, std::list<std::string>& strings) {
    std::istringstream template_stream(UserString(str_list));
    std::string item;
    while (std::getline(template_stream, item)) {
        strings.push_back(item);
    }
}
Exemple #2
0
std::vector<std::string> UserStringList(const std::string& key) {
    std::lock_guard<std::recursive_mutex> stringtable_lock(stringtable_access_mutex);
    std::vector<std::string> result;
    std::istringstream template_stream(UserString(key));
    std::string item;
    while (std::getline(template_stream, item))
        result.push_back(item);
    return result;
}
Exemple #3
0
void DebconfFrontend::cmd_x_loadtemplatefile(const QString &param)
{
    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" ));
}
// ******************************************************************************************
// Copy a template from location <template_path> to location <output_path> and replace package name
// ******************************************************************************************
bool ConfigurationFilesWidget::copyTemplate( const std::string& template_path, const std::string& output_path )
{
  // Check if template strings have been loaded yet
  if( template_strings_.empty() )
  {
    loadTemplateStrings();
  }

  // Error check file
  if( ! fs::is_regular_file( template_path ) )
  {
    ROS_ERROR_STREAM( "Unable to find template file " << template_path );
    return false;
  }

  // Load file
  std::ifstream template_stream( template_path.c_str() );
  if( !template_stream.good() ) // File not found
  {
    ROS_ERROR_STREAM( "Unable to load file " << template_path );
    return false;
  }

  // Load the file to a string using an efficient memory allocation technique
  std::string template_string;
  template_stream.seekg(0, std::ios::end);
  template_string.reserve(template_stream.tellg());
  template_stream.seekg(0, std::ios::beg);
  template_string.assign( (std::istreambuf_iterator<char>(template_stream)), std::istreambuf_iterator<char>() );
  template_stream.close();

  // Replace keywords in string ------------------------------------------------------------
  for(int i = 0; i < template_strings_.size(); ++i)
  {
    boost::replace_all(template_string, template_strings_[i].first, template_strings_[i].second);
  }

  // Save string to new location -----------------------------------------------------------
  std::ofstream output_stream( output_path.c_str(), std::ios_base::trunc );
  if( !output_stream.good() )
  {
    ROS_ERROR_STREAM( "Unable to open file for writing " << output_path );
    return false;
  }

  output_stream << template_string.c_str();
  output_stream.close();

  return true; // file created successfully
}