📄 delimited_file.cpp
字号:
if (!transaction_) commit();
return true;
}
bool Delimited_file::update_col(const string& name, const string& value)
{
size_t col_index = get_col_no(name);
return update_col(col_index, value);
}
bool Delimited_file::delete_col(size_t col)
{
if (col_total_count_ < 1)
{
last_error_ = err_col_out_of_range;
return false;
}
if (!is_col_in_range(col))
{
last_error_ = err_col_out_of_range;
return false;
}
bool tran = transaction_;
transaction_ = true;
--col_total_count_;
for (size_t i = 1; i <= row_total_count_; ++i)
{
vector<string> tv;
read_row(tv, i);
if (tv.size() != col_total_count_)
{
transaction_ = tran;
last_error_ = err_row_size_error;
return false;
}
tv.erase(tv.begin() + col - 1);
if (i==1)
{
col_headers_.clear();
col_headers_.assign(tv.begin(), tv.end());
}
update_row(tv, i);
}
transaction_ = tran;
if (!transaction_) commit();
return true;
}
bool Delimited_file::delete_col(const string& col)
{
size_t col_index = get_col_no(col);
return delete_col(col_index);
}
bool Delimited_file::read_col(vector<string>& values, size_t col)
{
if (!is_col_in_range(col))
{
last_error_ = err_col_out_of_range;
return false;
}
vector<string> tv;
for (size_t i = 1; i <= row_total_count_; ++i)
{
read_row(tv, i);
if (tv.size() != col_total_count_)
{
last_error_ = err_row_size_error;
return false;
}
values.push_back(tv[col-1]);
}
return true;
}
bool Delimited_file::read_col(vector<string>& value, const string& col)
{
size_t col_index = get_col_no(col);
return read_col(value, col_index);
}
bool Delimited_file::update_col_name(const string& value, size_t col)
{
return update_cell(value, col, 1);
}
bool Delimited_file::update_col_name(const string& value, const string& col)
{
return update_cell(value, col, 1);
}
bool Delimited_file::update_cell(const string& value, size_t col, size_t row)
{
if (!is_cell_in_range(col, row))
{
last_error_ = err_cell_out_of_range;
return false;
}
vector<string> tv;
read_row(tv, row);
if (tv.size() != col_total_count_)
{
last_error_ = err_row_size_error;
return false;
}
tv[col-1] = value;
if (row == 1)
{
col_headers_.clear();
col_headers_.assign(tv.begin(), tv.end());
}
update_row(tv, row);
return true;
}
bool Delimited_file::update_cell(const string& value, const string& col, size_t row)
{
size_t col_index = get_col_no(col);
return update_cell(value, col_index, row);
}
bool Delimited_file::read_cell (string& value, size_t col, size_t row)
{
if (!is_cell_in_range(col, row))
{
last_error_ = err_cell_out_of_range;
return false;
}
vector<string> tv;
read_row(tv, row);
if (tv.size() != col_total_count_)
{
last_error_ = err_row_size_error;
return false;
}
value = tv[col-1];
return true;
}
bool Delimited_file::read_cell(string& value, const string& col, size_t row)
{
size_t col_index = get_col_no(col);
return read_cell(value, col_index, row);
}
bool Delimited_file::swap_cols(size_t first_col, size_t second_col)
{
if (!is_col_in_range(first_col) || !is_col_in_range(second_col))
{
last_error_ = err_col_out_of_range;
return false;
}
if (col_total_count_ < 2)
{
last_error_ = err_col_smaller_than_2;
return false;
}
if (first_col == second_col)
{
last_error_ = err_first_col_equal_second_col;
return false;
}
bool tran = transaction_;
transaction_ = true;
vector<string> tv1, tv2;
read_col(tv1, first_col);
read_col(tv2, second_col);
insert_col("second_temp_col", first_col);
for (size_t i = 1; i <= row_total_count_; ++i)
update_cell(tv2[i-1], first_col, i);
delete_col(first_col + 1);
insert_col("first_temp_col", second_col);
for (size_t i = 1; i <= row_total_count_; ++i)
update_cell(tv1[i-1], second_col, i);
delete_col(second_col + 1);
transaction_ = tran;
if (!transaction_) commit();
return true;
}
bool Delimited_file::swap_cols(const string& first_col, const string& second_col)
{
size_t first_col_index = get_col_no(first_col);
size_t second_col_index = get_col_no(second_col);
return swap_cols(first_col_index, second_col_index);
}
bool Delimited_file::swap_rows(size_t first_row, size_t second_row)
{
if (row_total_count_ < 2)
{
last_error_ = err_row_smaller_than_2;
return false;
}
if (first_row == second_row)
{
last_error_ = err_first_row_equal_second_row;
return false;
}
if (!is_row_in_range(first_row) || !is_row_in_range(second_row))
{
last_error_ = err_row_out_of_range;
return false;
}
bool tran = transaction_;
transaction_ = true;
vector<string> tv1, tv2;
read_row(tv1, first_row);
read_row(tv2, second_row);
update_row(tv1, second_row);
update_row(tv2, first_row);
transaction_ = tran;
if (!transaction_) commit();
return true;
}
bool Delimited_file::sort_col(size_t col, bool sort_header, bool asc)
{
if (!is_col_in_range(col))
{
last_error_ = err_col_out_of_range;
return false;
}
vector<string> tv_col, tv_row;
read_col(tv_col, col);
asc ? sort(tv_col.begin() + (sort_header?0:1), tv_col.end())
: sort(tv_col.begin() + (sort_header?0:1), tv_col.end(), greater<string>());
for (size_t i = 1; i <= row_total_count_; ++i)
{
for (size_t j = i; j<= row_total_count_; ++j)
{
read_row(tv_row, j);
if (tv_row.size() != col_total_count_)
{
last_error_ = err_row_size_error;
return false;
}
if (tv_row[col-1] == tv_col[i-1])
{
swap_rows(i, j);
break;
}
}
}
return true;
}
bool Delimited_file::sort_col(const string& col, bool sort_header, bool asc)
{
size_t col_index = get_col_no(col);
return sort_col(col_index, sort_header, asc);
}
bool Delimited_file::clear()
{
rows_.clear();
col_headers_.clear();
col_total_count_ = 0;
row_total_count_ = 0;
current_row_ = 0;
if (!transaction_) commit();
return true;
}
void Delimited_file::begin_transaction()
{
transaction_ = true;
}
bool Delimited_file::commit()
{
fstream file(file_name_.c_str(), ios::out);
if (!file)
{
last_error_ = err_fail_to_write_file;
return false;
}
for (size_t i = 0; i < rows_.size(); ++i)
file << rows_[i].c_str() << "\n";
transaction_ = false;
return true;
}
bool Delimited_file::rollback()
{
if (!open()) return false;
transaction_ = false;
return true;
}
void Delimited_file::next()
{
if (current_row_ < row_total_count_) ++current_row_;
}
void Delimited_file::prior()
{
if (current_row_ > 1) --current_row_;
}
bool Delimited_file::is_eof()
{
return (current_row_ == row_total_count_);
}
bool Delimited_file::is_bof()
{
return (current_row_ == 1 || current_row_ == 0);
}
bool Delimited_file::read_current_row(vector<string>& value)
{
return read_row(value, current_row_);
}
bool Delimited_file::is_col_in_range(size_t col) const
{
return (col >= 1 && col <= col_total_count_);
}
bool Delimited_file::is_row_in_range(size_t row) const
{
return (row >= 1 && row <= row_total_count_);
}
bool Delimited_file::is_cell_in_range(size_t col, size_t row) const
{
return (is_col_in_range(col) && is_row_in_range(row));
}
size_t Delimited_file::get_col_no(const string& col) const
{
for (size_t i = 0; i < col_headers_.size(); ++i)
if (!col.compare(col_headers_[i])) return (i + 1);
return 0;
}
void Delimited_file::get_col_names (vector<string>& value) const
{
value.clear();
value.assign(col_headers_.begin(), col_headers_.end());
}
size_t Delimited_file::row_total_count() const
{
return row_total_count_;
}
size_t Delimited_file::col_total_count() const
{
return col_total_count_;
}
size_t Delimited_file::current_row_no() const
{
return current_row_;
}
bool Delimited_file::backup_status() const
{
return backup_;
}
bool Delimited_file::transaction_status() const
{
return transaction_;
}
const string& Delimited_file::last_error() const
{
return last_error_;
}
bool Delimited_file::open()
{
fstream file(file_name_.c_str(), ios::in);
if (!file)
{
last_error_ = err_fail_to_open_file;
return false;
}
rows_.clear();
char buf[ROW_BUF_SIZE];
while (file.getline(buf, ROW_BUF_SIZE, '\n')) rows_.push_back(buf);
row_total_count_ = rows_.size();
if (row_total_count_ >= 1)
{
read_row(col_headers_, 1); // the first col is a title col(header)
col_total_count_ = col_headers_.size();
current_row_ = 1;
}
return true;
}
string Delimited_file::delimit(const vector<string>& value)
{
string dv, ts;
for (size_t i = 0; i < value.size(); ++i)
{
if (i != value.size()-1)
{
ts = "\"" + value[i] + "\"" + separator_;
dv += ts;
}
else // last col
{
ts = "\"" + value[i] + "\"";
dv += ts;
}
}
return dv;
}
} //namespace dragon
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -