GnuCash c935c2f+
Loading...
Searching...
No Matches
Public Member Functions
GncFwTokenizer Class Reference
Inheritance diagram for GncFwTokenizer:
GncTokenizer

Public Member Functions

 GncFwTokenizer (const GncFwTokenizer &)=default
 
GncFwTokenizeroperator= (const GncFwTokenizer &)=default
 
 GncFwTokenizer (GncFwTokenizer &&)=default
 
GncFwTokenizeroperator= (GncFwTokenizer &&)=default
 
void columns (const std::vector< uint32_t > &cols=std::vector< uint32_t >())
 
std::vector< uint32_t > get_columns ()
 
uint32_t get_column (uint32_t num)
 
bool col_can_delete (uint32_t col_num)
 
void col_delete (uint32_t col_num)
 
bool col_can_narrow (uint32_t col_num)
 
void col_narrow (uint32_t col_num)
 
bool col_can_widen (uint32_t col_num)
 
void col_widen (uint32_t col_num)
 
bool col_can_split (uint32_t col_num, uint32_t position)
 
void col_split (uint32_t col_num, uint32_t position)
 
void load_file (const std::string &path) override
 
int tokenize () override
 
- Public Member Functions inherited from GncTokenizer
 GncTokenizer (const GncTokenizer &)=default
 
GncTokenizeroperator= (const GncTokenizer &)=default
 
 GncTokenizer (GncTokenizer &&)=default
 
GncTokenizeroperator= (GncTokenizer &&)=default
 
const std::string & current_file ()
 
void encoding (const std::string &encoding)
 
const std::string & encoding ()
 
const std::vector< StrVec > & get_tokens ()
 

Additional Inherited Members

- Protected Attributes inherited from GncTokenizer
std::string m_utf8_contents
 
std::vector< StrVec > m_tokenized_contents
 

Detailed Description

Definition at line 49 of file gnc-tokenizer-fw.hpp.

Member Function Documentation

◆ col_can_delete()

bool GncFwTokenizer::col_can_delete ( uint32_t  col_num)

Definition at line 53 of file gnc-tokenizer-fw.cpp.

54{
55 auto last_col = m_col_vec.size() - 1;
56 if (col_num >= last_col)
57 return false;
58 else
59 return true;
60}

◆ col_can_narrow()

bool GncFwTokenizer::col_can_narrow ( uint32_t  col_num)

Definition at line 71 of file gnc-tokenizer-fw.cpp.

72{
73 // Can't narrow the last column, it always sticks to the end of the parseable data
74 auto last_col = m_col_vec.size() - 1;
75 if (col_num >= last_col)
76 return false;
77 else
78 return true;
79}

◆ col_can_split()

bool GncFwTokenizer::col_can_split ( uint32_t  col_num,
uint32_t  position 
)

Definition at line 117 of file gnc-tokenizer-fw.cpp.

118{
119 auto last_col = m_col_vec.size() - 1;
120 if (col_num > last_col)
121 return false;
122
123 uint32_t col_end = m_col_vec[col_num];
124 if (position < 1 || position >= col_end)
125 return false;
126 else
127 return true;
128}

◆ col_can_widen()

bool GncFwTokenizer::col_can_widen ( uint32_t  col_num)

Definition at line 94 of file gnc-tokenizer-fw.cpp.

95{
96 // Can't widen the last column, it always sticks to the end of the parseable data
97 auto last_col = m_col_vec.size() - 1;
98 if (col_num >= last_col)
99 return false;
100 else
101 return true;
102}

◆ col_delete()

void GncFwTokenizer::col_delete ( uint32_t  col_num)

Definition at line 62 of file gnc-tokenizer-fw.cpp.

63{
64 if (!col_can_delete (col_num))
65 return;
66
67 m_col_vec[col_num + 1] += m_col_vec[col_num];
68 m_col_vec.erase (m_col_vec.begin() + col_num);
69}

◆ col_narrow()

void GncFwTokenizer::col_narrow ( uint32_t  col_num)

Definition at line 81 of file gnc-tokenizer-fw.cpp.

82{
83 if (!col_can_narrow (col_num))
84 return;
85
86 m_col_vec[col_num]--;
87 m_col_vec[col_num + 1]++;
88
89 // Drop column if it has become 0-width now
90 if (m_col_vec[col_num] == 0)
91 m_col_vec.erase (m_col_vec.begin() + col_num);
92}

◆ col_split()

void GncFwTokenizer::col_split ( uint32_t  col_num,
uint32_t  position 
)

Definition at line 130 of file gnc-tokenizer-fw.cpp.

131{
132 if (col_can_split (col_num, position))
133 {
134 m_col_vec.insert (m_col_vec.begin() + col_num, position);
135 m_col_vec[col_num + 1] -= position;
136 }
137}

◆ col_widen()

void GncFwTokenizer::col_widen ( uint32_t  col_num)

Definition at line 104 of file gnc-tokenizer-fw.cpp.

105{
106 if (!col_can_widen (col_num))
107 return;
108
109 m_col_vec[col_num]++;
110 m_col_vec[col_num + 1]--;
111
112 // Drop next column if it has become 0-width now
113 if (m_col_vec[col_num + 1] == 0)
114 m_col_vec.erase (m_col_vec.begin() + col_num + 1);
115}

◆ columns()

void GncFwTokenizer::columns ( const std::vector< uint32_t > &  cols = std::vector<uint32_t>())

Definition at line 41 of file gnc-tokenizer-fw.cpp.

42{
43 m_col_vec = cols;
44}

◆ get_columns()

std::vector< uint32_t > GncFwTokenizer::get_columns ( )

Definition at line 47 of file gnc-tokenizer-fw.cpp.

48{
49 return m_col_vec;
50}

◆ load_file()

void GncFwTokenizer::load_file ( const std::string &  path)
overridevirtual

Reimplemented from GncTokenizer.

Definition at line 140 of file gnc-tokenizer-fw.cpp.

141{
142 GncTokenizer::load_file(path);
143
144 std::string line;
145 m_longest_line = 0;
146 std::istringstream in_stream(m_utf8_contents);
147 while (std::getline (in_stream, line))
148 {
149 if (line.size() > m_longest_line)
150 m_longest_line = line.size();
151
152 line.clear();
153 }
154
155 if (m_col_vec.empty())
156 /* Set a sane default for the offsets
157 * That is, assume one column with all the data */
158 m_col_vec.push_back(m_longest_line);
159 else
160 {
161 /* Adjust existing last column(s) so the total column width
162 * equals the width of the longest line
163 * This may mean
164 * - widening the last column to widen to the longest line or
165 * - deleting columns/narrowing the last one to reduce to the longest line
166 */
167 uint32_t total_width = 0;
168 for (auto col_width : m_col_vec)
169 total_width += col_width;
170
171 if (m_longest_line > total_width)
172 m_col_vec.back() += m_longest_line - total_width;
173 else if (m_longest_line < total_width)
174 {
175 while (total_width - m_col_vec.back() > m_longest_line)
176 col_delete (m_col_vec[m_col_vec.size() - 2]);
177 m_col_vec.back() -= total_width - m_longest_line;
178 }
179 }
180}

◆ tokenize()

int GncFwTokenizer::tokenize ( )
overridevirtual

Implements GncTokenizer.

Definition at line 188 of file gnc-tokenizer-fw.cpp.

189{
190 using boost::locale::conv::utf_to_utf;
191 using Tokenizer = boost::tokenizer< boost::offset_separator,
192 std::wstring::const_iterator, std::wstring > ;
193
194 boost::offset_separator sep(m_col_vec.begin(), m_col_vec.end(), false);
195
196 std::wstring wchar_contents = utf_to_utf<wchar_t>(m_utf8_contents.c_str(),
197 m_utf8_contents.c_str() + m_utf8_contents.size());
198
199 StrVec vec;
200 std::wstring line;
201
202 m_tokenized_contents.clear();
203 std::wistringstream in_stream(wchar_contents);
204
205 while (std::getline (in_stream, line))
206 {
207 Tokenizer tok(line, sep);
208 vec.clear();
209 for (auto token : tok)
210 {
211 auto stripped = boost::trim_copy(token); // strips newlines as well as whitespace
212 auto narrow = utf_to_utf<char>(stripped.c_str(), stripped.c_str()
213 + stripped.size());
214 vec.push_back (narrow);
215 }
216 m_tokenized_contents.push_back(vec);
217 line.clear(); // clear here, next check could fail
218 }
219
220 return 0;
221}

The documentation for this class was generated from the following files: