GnuCash c935c2f+
Loading...
Searching...
No Matches
gnc-import-price.hpp
Go to the documentation of this file.
1/********************************************************************\
2 * gnc-import-price.hpp - import prices from csv files *
3 * *
4 * This program is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU General Public License as *
6 * published by the Free Software Foundation; either version 2 of *
7 * the License, or (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License*
15 * along with this program; if not, contact: *
16 * *
17 * Free Software Foundation Voice: +1-617-542-5942 *
18 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19 * Boston, MA 02110-1301, USA gnu@gnu.org *
20\********************************************************************/
21
30#ifndef GNC_PRICE_IMPORT_HPP
31#define GNC_PRICE_IMPORT_HPP
32
33#include "config.h"
34#include "gnc-commodity.h"
35
36#include <vector>
37#include <set>
38#include <map>
39#include <memory>
40#include <optional>
41#include <cstdint>
42
43#include "gnc-tokenizer.hpp"
44#include "gnc-imp-props-price.hpp"
46
47/* A set of currency formats that the user sees. */
48extern const int num_currency_formats_price;
49extern const gchar* currency_format_user_price[];
50
57 PL_INPUT,
58 PL_ERROR,
59 PL_PREPRICE,
60 PL_SKIP
61};
62
67using parse_line_t = std::tuple<StrVec,
68 std::string,
69 std::shared_ptr<GncImportPrice>,
70 bool>;
71struct ErrorListPrice;
72
83{
84public:
85 // Constructor - Destructor
86 GncPriceImport(GncImpFileFormat format = GncImpFileFormat::UNKNOWN);
88
89 void file_format(GncImpFileFormat format);
90 GncImpFileFormat file_format();
91
92 void over_write (bool over);
93 bool over_write ();
94
95 void from_commodity (gnc_commodity *from_commodity);
96 gnc_commodity *from_commodity ();
97
98 void to_currency (gnc_commodity *to_currency);
99 gnc_commodity *to_currency ();
100
101 void currency_format (int currency_format);
102 int currency_format ();
103
104 void date_format (int date_format);
105 int date_format ();
106
107 void encoding (const std::string& encoding);
108 std::string encoding ();
109
110 void update_skipped_lines (std::optional<uint32_t> start, std::optional<uint32_t> end,
111 std::optional<bool> alt, std::optional<bool> errors);
112 uint32_t skip_start_lines ();
113 uint32_t skip_end_lines ();
114 bool skip_alt_lines ();
115 bool skip_err_lines ();
116
117 void separators (std::string separators);
118 std::string separators ();
119
120 void enable_escape (bool enable);
121 bool enable_escape ();
122
123 void settings (const CsvPriceImpSettings& settings);
124 bool save_settings ();
125
126 void settings_name (std::string name);
127 std::string settings_name ();
128
129
130 void load_file (const std::string& filename);
131
132 void tokenize (bool guessColTypes);
133
134 std::string verify();
135
139 void create_prices ();
140 bool check_for_column_type (GncPricePropType type);
141 void set_column_type_price (uint32_t position, GncPricePropType type, bool force = false);
142 std::vector<GncPricePropType> column_types_price ();
143
144 std::unique_ptr<GncTokenizer> m_tokenizer;
145 std::vector<parse_line_t> m_parsed_lines;
148 int m_prices_added;
149 int m_prices_duplicated;
150 int m_prices_replaced;
151
152private:
157 void create_price (std::vector<parse_line_t>::iterator& parsed_line);
158
159 void verify_column_selections (ErrorListPrice& error_msg);
160
161 /* Internal helper function to force reparsing of columns subject to format changes */
162 void reset_formatted_column (std::vector<GncPricePropType>& col_types);
163
164 /* Two internal helper functions that should only be called from within
165 * set_column_type_price for consistency (otherwise error messages may not be (re)set)
166 */
167 void update_price_props (uint32_t row, uint32_t col, GncPricePropType prop_type);
168
169 CsvPriceImpSettings m_settings;
170 bool m_skip_errors;
171 bool m_over_write;
172};
173
174
175#endif
The actual PriceImport class It's intended to use in the following sequence of actions:
void tokenize(bool guessColTypes)
Splits a file into cells.
void to_currency(gnc_commodity *to_currency)
Sets a to currency.
void load_file(const std::string &filename)
Loads a file into a GncPriceImport.
void encoding(const std::string &encoding)
Converts raw file data using a new encoding.
void from_commodity(gnc_commodity *from_commodity)
Sets a from commodity.
std::unique_ptr< GncTokenizer > m_tokenizer
Will handle file loading/encoding conversion/splitting into fields.
void create_prices()
This function will attempt to convert all tokenized lines into prices using the column types the user...
~GncPriceImport()
Destructor for GncPriceImport.
void file_format(GncImpFileFormat format)
Sets the file format for the file to import, which may cause the file to be reloaded as well if the p...
std::vector< parse_line_t > m_parsed_lines
source file parsed into a two-dimensional array of strings.
Commodity handling public routines.
CSV Import Settings.
parse_line_cols
An enum describing the columns found in a parse_line_t.
std::tuple< StrVec, std::string, std::shared_ptr< GncImportPrice >, bool > parse_line_t
Tuple to hold.
Class convert a file into vector of string vectors.
GncImpFileFormat
Enumeration for file formats supported by this importer.