📄 delimited_file.h
字号:
/**********************************************************************************
* *
* Copyright (c) 2003 - 2004 by Royal. All rights reserved. *
* *
* Permission to use, copy, modify, and distribute this software for any purpose *
* is hereby granted without fee, provided that this copyright and permissions *
* notice appear in all copies and derivatives, and that no charge may be made *
* for the software and its documentation except to cover cost of distribution. *
* *
* This software is provided "as is" without express or implied warranty. *
* *
**********************************************************************************/
/*
* Description:
*
* Declaration of class Delimited_file.
*
* History:
*
* Initial version created by Royal, January 2003.
* Updated it by Royal, Feb 2004.
*
* Notes:
*
* This code has been written to conform to standard C++ and STL. It has been
* compiled successfully using GNU C++ 3.2, Borland C++ 5.5, and Visual C++ 7.0.
*/
#ifndef DELIMITED_FILE_H
#define DELIMITED_FILE_H
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <functional>
using namespace std;
namespace dragon
{
namespace
{
const string err_row_smaller_than_2 = "rows count is smaller than two\n";
const string err_col_smaller_than_2 = "cols count is smaller than two\n";
const string err_col_initial_already = "cols have been initialized already\n";
const string err_first_row_equal_second_row = "first row no. equal to second row no.\n";
const string err_first_col_equal_second_col = "first col no. equal to second col no.\n";
const string err_col_out_of_range = "col no. out of range\n";
const string err_row_out_of_range = "row no. out of range\n";
const string err_cell_out_of_range = "cell coordinate out of range\n";
const string err_row_size_error = "row size is not equal to cols count\n";
const string err_fail_to_write_file = "fail to write to file\n";
const string err_fail_to_open_file = "fail to open file\n";
const string err_fail_to_read_row = "fail to read row\n";
}
// maximal size of the row buffer
static const size_t ROW_BUF_SIZE = 3 * 1024;
class Delimited_file
{
public:
Delimited_file(const string& file_name, const string& separator, bool backup = true);
Delimited_file()
{
}
~Delimited_file();
public:
bool initial_col_headers(const vector<string>& value);
bool insert_row(const vector<string>& value, size_t row);
bool update_row(const vector<string>& value, size_t row);
bool delete_row(size_t row);
bool read_row(vector<string>& value, size_t row);
bool append_row(const vector<string>& value);
bool update_rows(const vector<string>& value, size_t col, string condition, bool only_first = false);
bool update_rows(const vector<string>& value, string col, string condition, bool only_first = false);
bool delete_rows(size_t col, const string& condition, bool only_first = false);
bool delete_rows(const string& col, const string& condition, bool only_first = false);
bool read_row(vector<string>& value, size_t col, const string& condition);
bool read_row(vector<string>& value, const string& col, const string& condition);
bool read_rows(vector<vector<string> >& values, size_t col, const string& condition);
bool read_rows(vector<vector<string> >& values, const string& col, const string& condition);
bool append_col(const string& name, const string& value = "value");
bool insert_col(const string& name, size_t col, const string& value = "value");
bool insert_col(const string& name, const string& col, const string& value = "value");
bool update_col(size_t col, const string& value = "updated");
bool update_col(const string& col, const string& value = "updated");
bool delete_col(size_t col);
bool delete_col(const string& col);
bool read_col(vector<string>& value, size_t col);
bool read_col(vector<string>& value, const string& col);
bool update_col_name(const string& value, size_t col);
bool update_col_name(const string& value, const string& col);
bool update_cell(const string& value, size_t col, size_t row);
bool update_cell(const string& value, const string& col, size_t row);
bool read_cell (string& value, size_t col, size_t row);
bool read_cell (string& value, const string& col, size_t row);
bool swap_cols(size_t first_col, size_t second_col);
bool swap_cols(const string& first_col, const string& second_col);
bool swap_rows(size_t first_row, size_t second_row);
bool sort_col(size_t col, bool sort_header = false, bool asc = true);
bool sort_col(const string& col, bool sort_header = false, bool asc = true);
bool clear();
void begin_transaction();
bool commit();
bool rollback();
void next();
void prior();
bool is_eof();
bool is_bof();
bool read_current_row(vector<string>& value);
bool is_col_in_range(size_t col) const;
bool is_row_in_range(size_t row) const;
bool is_cell_in_range(size_t col, size_t row) const;
void get_col_names(vector<string>& value) const;
size_t get_col_no(const string& col) const;
size_t row_total_count() const;
size_t col_total_count() const;
size_t current_row_no() const;
bool backup_status() const;
bool transaction_status() const;
const string& last_error() const;
protected:
bool open();
string delimit(const vector<string>& value);
private:
bool backup_; // status of file backup
bool transaction_; // status of transaction
size_t current_row_; // number of current row
size_t row_total_count_; // count of rows
size_t col_total_count_; // count of cols
string file_name_; // file being processed
string separator_; // delimiter, such as ";"
string last_error_; // last error
vector<string> col_headers_; // headers of all cols
vector<string> rows_; // values of all rows
};
} // namespace dragon
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -