Convert column_widths to a vector

This commit is contained in:
Geert Janssens 2016-12-08 18:07:12 +01:00 committed by Geert Janssens
parent 9e70166b8e
commit 1fc4b3cd9b
5 changed files with 24 additions and 57 deletions

View File

@ -284,8 +284,7 @@ csv_import_trans_load_settings (CsvImportTrans *info)
// Handle column widths, only relevant if the file format is fixed width
info->parse_data->file_format (GncImpFileFormat::FIXED_WIDTH);
GncFwTokenizer *fwtok = dynamic_cast<GncFwTokenizer*>(info->parse_data->tokenizer.get());
if (info->settings_data.column_widths, NULL)
fwtok->cols_from_string (std::string(info->settings_data.column_widths));
fwtok->columns(info->settings_data.column_widths);
info->parse_data->tokenize (false);
gnc_csv_preview_update_assist (info);
@ -575,12 +574,10 @@ csv_import_trans_save_settings_cb (GtkWidget *button, CsvImportTrans *info)
g_list_free (columns);
/* Save the column widths in fixed mode */
if (info->settings_data.csv_format)
info->settings_data.column_widths = "5,10,15";
else
if (!info->settings_data.csv_format)
{
GncFwTokenizer *fwtok = dynamic_cast<GncFwTokenizer*>(info->parse_data->tokenizer.get());
info->settings_data.column_widths = g_strdup (fwtok->cols_to_string().c_str());
info->settings_data.column_widths = fwtok->get_columns();
}
// Save the settings

View File

@ -213,8 +213,15 @@ CsvTransSettings::load (const std::string& group)
}
}
column_widths = g_key_file_get_string (keyfile, group.c_str(), CSV_COL_WIDTHS, &key_error);
column_widths.clear();
gsize list_len;
gint *col_widths_int = g_key_file_get_integer_list (keyfile, group.c_str(), CSV_COL_WIDTHS,
&list_len, &key_error);
for (uint i = 0; i < list_len; i++)
column_widths.push_back(col_widths_int[i]);
error |= handle_load_error (&key_error, group);
if (col_widths_int)
g_free (col_widths_int);
return error;
}
@ -265,7 +272,10 @@ CsvTransSettings::save (const std::string& settings_name)
}
g_key_file_set_string (keyfile, group.c_str(), CSV_COL_TYPES, ss.str().c_str());
g_key_file_set_string (keyfile, group.c_str(), CSV_COL_WIDTHS, column_widths);
if (!column_widths.emtpy())
g_key_file_set_integer_list (keyfile, group.c_str(), CSV_COL_WIDTHS,
(gint*)(column_widths.data()), column_widths.size());
// Do a test read of column types
GError *key_error = nullptr;

View File

@ -49,7 +49,7 @@ struct CsvTransSettings
CsvTransSettings() : header_rows{0}, footer_rows{0}, csv_format (true),
skip_alt_rows (false), multi_split (false),
encoding {"UTF-8"}, custom {false}, custom_entry {nullptr},
date_active {0}, currency_active {0}, column_widths{nullptr}
date_active {0}, currency_active {0}
{
for (uint i = 0; i < SEP_NUM_OF_TYPES; i++)
{
@ -99,7 +99,7 @@ std::string custom_entry; // Custom Entry
int date_active; // Date Active id
int currency_active; // Currency Active id
std::vector<GncTransPropType> column_types;// The Column types in order
const gchar *column_widths; // The Column widths
std::vector<uint> column_widths; // The Column widths
};

View File

@ -17,6 +17,12 @@ GncFwTokenizer::columns(const std::vector<uint>& cols)
m_col_vec = cols;
}
std::vector<uint>
GncFwTokenizer::get_columns()
{
return m_col_vec;
}
bool GncFwTokenizer::col_can_delete (uint col_num)
{
@ -105,50 +111,6 @@ void GncFwTokenizer::col_split (uint col_num, uint position)
}
std::string GncFwTokenizer::cols_to_string()
{
std::ostringstream colstream;
for (auto col_end : m_col_vec)
colstream<<col_end<<",";
std::string colstr = colstream.str();
if (!colstr.empty())
colstr.pop_back(); // Drop last ","
return colstr;
}
void GncFwTokenizer::cols_from_string(const std::string& col_str)
{
// Clear existing columns first
columns();
/* Set an initial column as expected by the code below
* If no data is read yet, take a rather large value
* Otherwise take the size of the widest line in the data
*/
if (m_longest_line != 0)
m_col_vec.push_back (m_longest_line);
else
m_col_vec.push_back (99999);
uint col = 0;
std::istringstream in_stream(col_str);
std::string col_end_str;
while (std::getline (in_stream, col_end_str, ','))
{
if (col_end_str.empty())
continue; // Skip empty column positions
uint col_width = std::stoi (col_end_str);
col_split (col, col_width);
col++;
}
// If no data is loaded yet, remove last, unreasonably wide column
if (m_longest_line == 0)
m_col_vec.pop_back();
}
void GncFwTokenizer::load_file(const std::string& path)
{
GncTokenizer::load_file(path);

View File

@ -58,6 +58,7 @@ public:
~GncFwTokenizer() = default; // destructor
void columns(const std::vector<uint>& cols = std::vector<uint>());
std::vector<uint> get_columns();
uint get_column (uint num);
// Column manipulators
@ -70,9 +71,6 @@ public:
bool col_can_split (uint col_num, uint position);
void col_split (uint col_num, uint position);
std::string cols_to_string();
void cols_from_string(const std::string& col_str);
void load_file (const std::string& path) override;
int tokenize() override;