C++11 Convert unscoped enum into scoped one

As per recommendation 10 in Meyer's Effective Modern C++

This also means the string array with column type names
can no longer be shared between c and c++ code, so
set up a separate one in c++
This commit is contained in:
Geert Janssens 2016-01-31 13:53:45 +01:00 committed by Geert Janssens
parent 8f9d2ee826
commit 17b2b4668e
2 changed files with 72 additions and 80 deletions

View File

@ -124,21 +124,21 @@ const char* date_regex[] = {
// N_("Comma: 123.456,78") // N_("Comma: 123.456,78")
// }; // };
// //
///* This array contains all of the different strings for different column types. */ /* This array contains all of the different strings for different column types. */
//const gchar* gnc_csv_column_type_strs[GNC_CSV_NUM_COL_TYPES] = { const gchar* gnc_csv_col_type_strs[GncTransPropType::NUM_COL_TYPES] = {
// N_("None"), N_("None"),
// N_("Date"), N_("Date"),
// N_("Num"), N_("Num"),
// N_("Description"), N_("Description"),
// N_("Notes"), N_("Notes"),
// N_("Account"), N_("Account"),
// N_("Deposit"), N_("Deposit"),
// N_("Withdrawal"), N_("Withdrawal"),
// N_("Balance"), N_("Balance"),
// N_("Memo"), N_("Memo"),
// N_("Other Account"), N_("Other Account"),
// N_("Other Memo") N_("Other Memo")
//}; };
/** Parses a string into a date, given a format. This function /** Parses a string into a date, given a format. This function
* requires only knowing the order in which the year, month and day * requires only knowing the order in which the year, month and day
@ -326,7 +326,7 @@ int GncCsvParseData::load_file (const char* filename,
* set according to how the user wants before calling this * set according to how the user wants before calling this
* function. (Note: this function must be called with guessColTypes as * function. (Note: this function must be called with guessColTypes as
* TRUE before it is ever called with it as FALSE.) (Note: if * TRUE before it is ever called with it as FALSE.) (Note: if
* guessColTypes is TRUE, all the column types will be GNC_CSV_NONE * guessColTypes is TRUE, all the column types will be GncTransPropType::NONE
* right now.) * right now.)
* @param parse_data Data that is being parsed * @param parse_data Data that is being parsed
* @param guessColTypes TRUE to guess what the types of columns are based on the cell contents * @param guessColTypes TRUE to guess what the types of columns are based on the cell contents
@ -365,7 +365,7 @@ int GncCsvParseData::parse (gboolean guessColTypes, GError** error)
/* Free column_types if it's already been created. */ /* Free column_types if it's already been created. */
column_types.clear(); column_types.clear();
} }
column_types.resize(orig_max_row, GNC_CSV_NONE); column_types.resize(orig_max_row, GncTransPropType::NONE);
if (guessColTypes) if (guessColTypes)
{ {
@ -388,8 +388,8 @@ typedef struct
/** A struct encapsulating a property of a transaction. */ /** A struct encapsulating a property of a transaction. */
typedef struct typedef struct
{ {
int type; /**< A value from the GncCsvColumnType enum except GncTransPropType type; /**< A value from the GncTransPropType enum except
* GNC_CSV_NONE and GNC_CSV_NUM_COL_TYPES */ * GncTransPropType::NONE and GncTransPropType::NUM_COL_TYPES */
void* value; /**< Pointer to the data that will be used to configure a transaction */ void* value; /**< Pointer to the data that will be used to configure a transaction */
TransPropertyList* list; /**< The list the property belongs to */ TransPropertyList* list; /**< The list the property belongs to */
} TransProperty; } TransProperty;
@ -397,7 +397,7 @@ typedef struct
/** Constructor for TransProperty. /** Constructor for TransProperty.
* @param type The type of the new property (see TransProperty.type for possible values) * @param type The type of the new property (see TransProperty.type for possible values)
*/ */
static TransProperty* trans_property_new (int type, TransPropertyList* list) static TransProperty* trans_property_new (GncTransPropType type, TransPropertyList* list)
{ {
TransProperty* prop = g_new (TransProperty, 1); TransProperty* prop = g_new (TransProperty, 1);
prop->type = type; prop->type = type;
@ -416,10 +416,10 @@ static void trans_property_free (TransProperty* prop)
/* The types for "Date" and "Balance" (time64 and gnc_numeric, /* The types for "Date" and "Balance" (time64 and gnc_numeric,
* respectively) are typically not pointed to, we have to free * respectively) are typically not pointed to, we have to free
* them, unlike types like char* ("Description"). */ * them, unlike types like char* ("Description"). */
case GNC_CSV_DATE: case GncTransPropType::DATE:
case GNC_CSV_BALANCE: case GncTransPropType::BALANCE:
case GNC_CSV_DEPOSIT: case GncTransPropType::DEPOSIT:
case GNC_CSV_WITHDRAWAL: case GncTransPropType::WITHDRAWAL:
if (prop->value != NULL) if (prop->value != NULL)
g_free(prop->value); g_free(prop->value);
break; break;
@ -444,26 +444,26 @@ static gboolean trans_property_set (TransProperty* prop, const char* str)
regex_t regex; regex_t regex;
switch (prop->type) switch (prop->type)
{ {
case GNC_CSV_DATE: case GncTransPropType::DATE:
prop->value = g_new(time64, 1); prop->value = g_new(time64, 1);
*((time64*)(prop->value)) = parse_date(str, prop->list->date_format); *((time64*)(prop->value)) = parse_date(str, prop->list->date_format);
return *((time64*)(prop->value)) != -1; return *((time64*)(prop->value)) != -1;
case GNC_CSV_DESCRIPTION: case GncTransPropType::DESCRIPTION:
case GNC_CSV_NOTES: case GncTransPropType::NOTES:
case GNC_CSV_MEMO: case GncTransPropType::MEMO:
case GNC_CSV_OMEMO: case GncTransPropType::OMEMO:
case GNC_CSV_NUM: case GncTransPropType::NUM:
prop->value = g_strdup (str); prop->value = g_strdup (str);
return TRUE; return TRUE;
case GNC_CSV_OACCOUNT: case GncTransPropType::OACCOUNT:
prop->value = gnc_csv_account_map_search (str); prop->value = gnc_csv_account_map_search (str);
return TRUE; return TRUE;
case GNC_CSV_BALANCE: case GncTransPropType::BALANCE:
case GNC_CSV_DEPOSIT: case GncTransPropType::DEPOSIT:
case GNC_CSV_WITHDRAWAL: case GncTransPropType::WITHDRAWAL:
str_dupe = g_strdup (str); /* First, we make a copy so we can't mess up real data. */ str_dupe = g_strdup (str); /* First, we make a copy so we can't mess up real data. */
/* If a cell is empty or just spaces make its value = "0" */ /* If a cell is empty or just spaces make its value = "0" */
reti = regcomp(&regex, "[0-9]", 0); reti = regcomp(&regex, "[0-9]", 0);
@ -637,13 +637,13 @@ static gboolean trans_property_list_verify_essentials (TransPropertyList* list,
{ {
switch (((TransProperty*)(list->properties->data))->type) switch (((TransProperty*)(list->properties->data))->type)
{ {
case GNC_CSV_DATE: case GncTransPropType::DATE:
possible_errors[NO_DATE] = NULL; possible_errors[NO_DATE] = NULL;
break; break;
case GNC_CSV_BALANCE: case GncTransPropType::BALANCE:
case GNC_CSV_DEPOSIT: case GncTransPropType::DEPOSIT:
case GNC_CSV_WITHDRAWAL: case GncTransPropType::WITHDRAWAL:
possible_errors[NO_AMOUNT] = NULL; possible_errors[NO_AMOUNT] = NULL;
break; break;
default: default:
@ -756,31 +756,31 @@ static GncCsvTransLine* trans_property_list_to_trans (TransPropertyList* list, g
TransProperty* prop = (TransProperty*)(list->properties->data); TransProperty* prop = (TransProperty*)(list->properties->data);
switch (prop->type) switch (prop->type)
{ {
case GNC_CSV_DATE: case GncTransPropType::DATE:
xaccTransSetDatePostedSecsNormalized (trans_line->trans, *((time64*)(prop->value))); xaccTransSetDatePostedSecsNormalized (trans_line->trans, *((time64*)(prop->value)));
break; break;
case GNC_CSV_DESCRIPTION: case GncTransPropType::DESCRIPTION:
xaccTransSetDescription (trans_line->trans, (char*)(prop->value)); xaccTransSetDescription (trans_line->trans, (char*)(prop->value));
break; break;
case GNC_CSV_NOTES: case GncTransPropType::NOTES:
xaccTransSetNotes (trans_line->trans, (char*)(prop->value)); xaccTransSetNotes (trans_line->trans, (char*)(prop->value));
break; break;
case GNC_CSV_OACCOUNT: case GncTransPropType::OACCOUNT:
oaccount = ((Account*)(prop->value)); oaccount = ((Account*)(prop->value));
break; break;
case GNC_CSV_MEMO: case GncTransPropType::MEMO:
memo = g_strdup ((char*)(prop->value)); memo = g_strdup ((char*)(prop->value));
break; break;
case GNC_CSV_OMEMO: case GncTransPropType::OMEMO:
omemo = g_strdup ((char*)(prop->value)); omemo = g_strdup ((char*)(prop->value));
break; break;
case GNC_CSV_NUM: case GncTransPropType::NUM:
/* the 'num' is saved and passed to 'trans_add_split' below where /* the 'num' is saved and passed to 'trans_add_split' below where
* 'gnc_set_num_action' is used to set tran-num and/or split-action * 'gnc_set_num_action' is used to set tran-num and/or split-action
* per book option */ * per book option */
@ -791,7 +791,7 @@ static GncCsvTransLine* trans_property_list_to_trans (TransPropertyList* list, g
trans_line->num = g_strdup ((char*)(prop->value)); trans_line->num = g_strdup ((char*)(prop->value));
break; break;
case GNC_CSV_DEPOSIT: /* Add deposits to the existing amount. */ case GncTransPropType::DEPOSIT: /* Add deposits to the existing amount. */
if (prop->value != NULL) if (prop->value != NULL)
{ {
amount = gnc_numeric_add (*((gnc_numeric*)(prop->value)), amount = gnc_numeric_add (*((gnc_numeric*)(prop->value)),
@ -804,7 +804,7 @@ static GncCsvTransLine* trans_property_list_to_trans (TransPropertyList* list, g
} }
break; break;
case GNC_CSV_WITHDRAWAL: /* Withdrawals are just negative deposits. */ case GncTransPropType::WITHDRAWAL: /* Withdrawals are just negative deposits. */
if (prop->value != NULL) if (prop->value != NULL)
{ {
amount = gnc_numeric_add (gnc_numeric_neg(*((gnc_numeric*)(prop->value))), amount = gnc_numeric_add (gnc_numeric_neg(*((gnc_numeric*)(prop->value))),
@ -817,7 +817,7 @@ static GncCsvTransLine* trans_property_list_to_trans (TransPropertyList* list, g
} }
break; break;
case GNC_CSV_BALANCE: /* The balance gets stored in a separate field in trans_line. */ case GncTransPropType::BALANCE: /* The balance gets stored in a separate field in trans_line. */
/* We will use the "Deposit" and "Withdrawal" columns in preference to "Balance". */ /* We will use the "Deposit" and "Withdrawal" columns in preference to "Balance". */
if (!amount_set && prop->value != NULL) if (!amount_set && prop->value != NULL)
{ {
@ -935,7 +935,7 @@ int GncCsvParseData::parse_to_trans (Account* account,
for (uint j = 0; j < line.size(); j++) for (uint j = 0; j < line.size(); j++)
{ {
/* Look for "Account" columns. */ /* Look for "Account" columns. */
if (column_types[j] == GNC_CSV_ACCOUNT) if (column_types[j] == GncTransPropType::ACCOUNT)
home_account = gnc_csv_account_map_search (line[j].c_str()); home_account = gnc_csv_account_map_search (line[j].c_str());
} }
} }
@ -952,7 +952,7 @@ int GncCsvParseData::parse_to_trans (Account* account,
for (uint j = 0; j < line.size(); j++) for (uint j = 0; j < line.size(); j++)
{ {
/* We do nothing in "None" or "Account" columns. */ /* We do nothing in "None" or "Account" columns. */
if ((column_types[j] != GNC_CSV_NONE) && (column_types[j] != GNC_CSV_ACCOUNT)) if ((column_types[j] != GncTransPropType::NONE) && (column_types[j] != GncTransPropType::ACCOUNT))
{ {
/* Affect the transaction appropriately. */ /* Affect the transaction appropriately. */
TransProperty* property = trans_property_new (column_types[j], list); TransProperty* property = trans_property_new (column_types[j], list);
@ -965,7 +965,7 @@ int GncCsvParseData::parse_to_trans (Account* account,
{ {
loop_err = true; loop_err = true;
gchar *error_message = g_strdup_printf (_("%s column could not be understood."), gchar *error_message = g_strdup_printf (_("%s column could not be understood."),
_(gnc_csv_column_type_strs[property->type])); _(gnc_csv_col_type_strs[property->type]));
*line_errs_it = error_message; *line_errs_it = error_message;
g_free (error_message); g_free (error_message);
@ -1026,7 +1026,7 @@ int GncCsvParseData::parse_to_trans (Account* account,
} }
} }
if (std::find(column_types.begin(),column_types.end(), GNC_CSV_BALANCE) != if (std::find(column_types.begin(),column_types.end(), GncTransPropType::BALANCE) !=
column_types.end()) // This is only used if we have one home account column_types.end()) // This is only used if we have one home account
{ {
Split *split, *osplit; Split *split, *osplit;
@ -1091,15 +1091,7 @@ int GncCsvParseData::parse_to_trans (Account* account,
bool bool
GncCsvParseData::check_for_column_type (int type) GncCsvParseData::check_for_column_type (GncTransPropType type)
{ {
gboolean ret = FALSE; return (std::find (column_types.begin(), column_types.end(), type) != column_types.end());
int j, ncols = column_types.size(); /* ncols is the number of columns in the data. */
for (j = 0; j < ncols; j++)
{
if (column_types[j] == type)
ret = TRUE;
}
return ret;
} }

View File

@ -44,22 +44,22 @@ extern "C" {
/** Enumeration for column types. These are the different types of /** Enumeration for column types. These are the different types of
* columns that can exist in a CSV/Fixed-Width file. There should be * columns that can exist in a CSV/Fixed-Width file. There should be
* no two columns with the same type except for the GNC_CSV_NONE * no two columns with the same type except for the GncTransPropType::NONE
* type. */ * type. */
enum GncCsvColumnType { enum class GncTransPropType {
GNC_CSV_NONE, NONE,
GNC_CSV_DATE, DATE,
GNC_CSV_NUM, NUM,
GNC_CSV_DESCRIPTION, DESCRIPTION,
GNC_CSV_NOTES, NOTES,
GNC_CSV_ACCOUNT, ACCOUNT,
GNC_CSV_DEPOSIT, DEPOSIT,
GNC_CSV_WITHDRAWAL, WITHDRAWAL,
GNC_CSV_BALANCE, BALANCE,
GNC_CSV_MEMO, MEMO,
GNC_CSV_OACCOUNT, OACCOUNT,
GNC_CSV_OMEMO, OMEMO,
GNC_CSV_NUM_COL_TYPES NUM_COL_TYPES
}; };
/** Error domain for the csv importer. */ /** Error domain for the csv importer. */
@ -103,7 +103,7 @@ extern const int num_date_formats;
extern const gchar* date_format_user[]; extern const gchar* date_format_user[];
/* This array contains all of the different strings for different column types. */ /* This array contains all of the different strings for different column types. */
extern const gchar* gnc_csv_column_type_strs[]; extern const gchar* gnc_csv_col_type_strs[];
using str_vec_t = std::vector<std::string> ; using str_vec_t = std::vector<std::string> ;
@ -123,12 +123,12 @@ public:
int parse (gboolean guessColTypes, GError** error); int parse (gboolean guessColTypes, GError** error);
int parse_to_trans (Account* account, gboolean redo_errors); int parse_to_trans (Account* account, gboolean redo_errors);
bool check_for_column_type (int type); bool check_for_column_type (GncTransPropType type);
std::unique_ptr<GncTokenizer> tokenizer; /**< Will handle file loading/encoding conversion/splitting into fields */ std::unique_ptr<GncTokenizer> tokenizer; /**< Will handle file loading/encoding conversion/splitting into fields */
std::vector<str_vec_t> orig_lines; /**< file_str parsed into a two-dimensional array of strings */ std::vector<str_vec_t> orig_lines; /**< file_str parsed into a two-dimensional array of strings */
std::vector<str_vec>::size_type orig_max_row; /**< Holds the maximum value in orig_row_lengths */ std::vector<str_vec>::size_type orig_max_row; /**< Holds the maximum value in orig_row_lengths */
std::vector<GncCsvColumnType> column_types; /**< Vector of values from the GncCsvColumnType enumeration */ std::vector<GncTransPropType> column_types; /**< Vector of values from the GncCsvColumnType enumeration */
GList* transactions; /**< List of GncCsvTransLine*s created using orig_lines and column_types */ GList* transactions; /**< List of GncCsvTransLine*s created using orig_lines and column_types */
int date_format; /**< The format of the text in the date columns from date_format_internal. */ int date_format; /**< The format of the text in the date columns from date_format_internal. */
guint start_row; /**< The start row to generate transactions from. */ guint start_row; /**< The start row to generate transactions from. */