-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_shredder.cpp
97 lines (76 loc) · 2.6 KB
/
text_shredder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "common_classes.h"
#include "text_shredder.h"
#include "singleton_random.h"
using namespace std;
TextShredder::TextShredder(const int n_width, const string str_in_file, const string str_out_file) : n_strip_width_(n_width), str_in_filename_(str_in_file), str_out_filename_(str_out_file)
{
}
// Populate vec_str_source_data_ from input file
void TextShredder::GetInput()
{
TextFileOperation::ReadText(str_in_filename_, vec_str_source_data_);
}
// Print vec_str_shredded_text_ to output file by delimiter "|"
void TextShredder::CreateOutput()
{
vector<string> vec_temp;
string str_sub;
if (vec_str_shredded_text_.size() == 0)
{
throw runtime_error("Nothing to print to output file!");
}
for (int i = 0; i < vec_str_shredded_text_.size(); ++i)
{
for (int j = 0; j< vec_str_shredded_text_[i].size(); ++j)
{
str_sub.append("|" + vec_str_shredded_text_[i][j]);
}
str_sub.append("|");
vec_temp.emplace_back(str_sub);
str_sub.clear();
}
TextFileOperation::WriteText(str_out_filename_, vec_temp);
}
// Shred text lines stored vec_str_source_data_ and save shredded result into vec_str_shredded_text_
void TextShredder::DoTextShredder()
{
vector<string> vec_temp;
string str_sub;
if (vec_str_source_data_.empty())
{
throw runtime_error("vec_str_source_data_ is empty, no data to shred!");
}
if (n_strip_width_ <= 0)
{
throw runtime_error("n_strip_width_ value is invalid!");
}
if (!vec_str_shredded_text_.empty())
vec_str_shredded_text_.clear();
if (!vec_str_trans_shredded_text_.empty())
vec_str_trans_shredded_text_.clear();
auto iter = vec_str_source_data_.begin();
while (iter != vec_str_source_data_.end())
{
int n_position = 0;
while (n_position < iter->size())
{
str_sub = iter->substr(n_position, n_strip_width_);
// Ensure the last column in the paragraph has the same width of strip
while (str_sub.length() < n_strip_width_) str_sub.append(" ");
vec_temp.emplace_back(str_sub);
n_position = n_position + n_strip_width_;
}
vec_str_shredded_text_.emplace_back(vec_temp);
vec_temp.clear();
++iter;
}
// Transpose vec_str_shredded_text_ to vec_str_trans_shredded_text_
TextStripOperation::Transpose(vec_str_shredded_text_, vec_str_trans_shredded_text_);
#ifndef UTFLAG
TextStripOperation::Disorder(vec_str_trans_shredded_text_);
#endif
// Empty vec_str_shredded_text_ here
vec_str_shredded_text_.clear();
// Transpose vec_str_trans_shredded_text_ to vec_str_shredded_text_
TextStripOperation::Transpose(vec_str_trans_shredded_text_, vec_str_shredded_text_);
}