/**************************************************************************** * 文件名称: delimfile.h * 功能特点: 用于读写具有定界符的平文件,采用标准C++/STL编写. * 作 者: 荣耀 * 发布时间: 2003年1月11日 * 声 明: 任何个体/组织都可以将此代码用于非商业用途. * Version : beta1 * Copyright (c) royaloo.com by royal. All rights reserved. ****************************************************************************/ // #ifndef _DELIMFILE_H_ #define _DELIMFILE_H_ // #include <string> #include <vector> #include <algorithm> #include <fstream> #include <functional> // //说明: 出错信息常量清单,你可以自由转换为你所需要的语言. // const std::string err_row_smaller_than_2 = "total row count is smaller than two/n"; const std::string err_col_smaller_than_2 = "total col count is smaller than two/n"; const std::string err_col_initial_already = "cols have been initialized already/n"; const std::string err_first_row_equal_second_row = "first row no. equal second row no./n"; const std::string err_first_col_equal_second_col = "first col no. equal second col no./n"; const std::string err_col_out_of_range = "col no. out of range/n"; const std::string err_row_out_of_range = "row no. out of range/n"; const std::string err_cell_out_of_range = "cell coordinate out of range/n"; const std::string err_row_size_too_large = "row size is larger than total cols/n"; const std::string err_fail_to_initial_col_headers = "fail to initialize col headers/n"; const std::string err_fail_to_write_file = "fail to write to file/n"; const std::string err_fail_to_open_file = "fail to open file/n"; const std::string err_fail_to_clear = "fail to clear file content/n"; const std::string err_fail_to_append_row = "fail to append row/n"; const std::string err_fail_to_insert_row = "fail to insert row/n"; const std::string err_fail_to_update_row = "fail to update row/n"; const std::string err_fail_to_delete_row = "fail to delete row/n"; const std::string err_fail_to_read_row = "fail to read row/n"; const std::string err_fail_to_append_col = "fail to append col/n"; const std::string err_fail_to_insert_col = "fail to insert col/n"; const std::string err_fail_to_update_col = "fail to update col/n"; const std::string err_fail_to_delete_col = "fail to delete col/n"; const std::string err_fail_to_sort_col = "fail to sort col/n"; const std::string err_fail_to_update_cell = "fail to update cell/n"; // //用途: 每行字符串缓冲区大小,你可以根据需要,自由设定. // const int ROW_BUF_SIZE = 3000; // class Delimfile { public: // //file_name: 意欲操作的文件名,存在即打开,不存在即创建. //separator: 文件中每个cell的定界符,比如若干空格" "或一个分号";"(考虑"人类"可读性). //backup : 打开文件时是否需要做备份.是,则备份文件名为file_name + ".bak". // Delimfile(const std::string& file_name, const std::string& separator, bool backup = true); // //无任何代码. // ~Delimfile(); public: // //作用 : 初始化列标题.标题栏和普通row并无区别,行号为1的row,就是标题栏. //value: 将作为标题栏的一组列名.注意,这个方法并不限制列标题名字唯一. // bool initial_col_headers(const std::vector<std::string>& value); // //作用 : 插入一行. //value: 欲插入行的值,所有对行进行写操作的方法,都会判断value里的数目是否 // 恰好和列数相等,否则不准进行写操作.此举为了保证所有数据呈矩形. //row : 行号.在这一行之前插入. // bool insert_row(const std::vector<std::string>& value, int row); // //作用 : 更新一行. //value: 将用此值去更新目标行. //row : 行号.此行将被更新. // bool update_row(const std::vector<std::string>& value, int row); // //作用: 删除一行. //row : 行号.此行将被删除. // bool delete_row(int row); // //作用 : 读取一行. //value: 读取值存放于此. //row : 行号.此行将被读取. // bool read_row(std::vector<std::string>& value, int row); // //作用 : 在末尾附加一行. //value: 欲被附加的行的值. // bool append_row(const std::vector<std::string>& value); // //作用 : 更新符合条件的行. //value : 欲以此值进行更新. //col : 列号.将对该列进行条件判断. //condition : 条件. //only_first: 默认为false,更新符合条件的所有行,否则,只更新符合条件的第一行. // bool update_rows(const std::vector<std::string>& value, int col, std::string condition, bool only_first = false); // //作用 : 更新符合条件的行. //value : 欲以此值进行更新. //col : 列名.将对该列进行条件判断.注意,并没有限定列名不允许重复 // 倘有重复,将以具有该列名的第一列为准. //condition : 条件. //only_first: 默认为false,更新符合条件的所有行,否则,只更新符合条件的第一行. // bool update_rows(const std::vector<std::string>& value, std::string col, std::string condition, bool only_first = false); // //作用 : 删除符合条件的行. //col : 列号,将对该列进行条件判断. //condition : 条件. //only_first: 默认为false,删除符合条件的所有行,否则,只删除符合条件的第一行. // bool delete_rows(int col, const std::string& condition, bool only_first = false); // //作用 : 删除符合条件的行. //col : 列名,将对该列进行条件判断.注意,并没有限定列名不允许重复, // 倘有重复,将以具有该列名的第一列为准. //condition : 条件. //only_first: 默认为false,删除符合条件的所有行,否则,只删除符合条件的第一行. // bool delete_rows(const std::string& col, const std::string& condition, bool only_first = false); // //作用 : 读取符合条件的一行. //value : 读取结果存放于此变量之中. //col : 列号,将对该列进行条件判断. //condition: 条件. // bool read_row(std::vector<std::string>& value, int col, const std::string& condition); // //作用 : 读取符合条件的一行. //value : 读取结果存放于此变量之中. //col : 列名,将对该列进行条件判断.倘有列名重复情况,将以具有该列名的第一列为准. //condition: 条件. // bool read_row(std::vector<std::string>& value, const std::string& col, const std::string& condition); // //作用 : 读取符合条件的所有行. //values : 读取结果存放于此变量之中. //col : 列号,将对该列进行条件判断. //condition: 条件. // bool read_rows(std::vector<std::vector<std::string> >& values, int col, const std::string& condition); // //作用 : 读取符合条件的所有行. //values : 读取结果存放于此变量之中. //col : 列名.将对该列进行条件判断.倘有列名重复情况,将以具有该列名的第一列为准. //condition: 条件. // bool read_rows(std::vector<std::vector<std::string> >& values, const std::string& col, const std::string& condition); // //作用 : 追加一列. //name : 列名.即列标题.即显示在第一行的那个cell的名字. //value: 除了第一行之外的所有该列的值,默认为带有一个空格的字符串. // bool append_col(const std::string& name, const std::string& value = " "); // //作用 : 插入一列. //name : 列名,即列标题名,即显示在第一行的那个cell的名字. //col : 位置列,将在该列之前插入一列. //value: 除了第一行之外的所有该列的值,默认为带有一个空格的字符串. // bool insert_col(const std::string& name, int col, const std::string& value = " "); // //作用 : 插入一列. //name : 列名,即列标题,即显示在第一行的那个cell的名字. //col : 列名.将对该列进行条件判断.倘有列名重复情况,将以具有该列名的第一列为准. //value: 除了第一行之外所有该列的值,默认为带有一个空格的字符串. // bool insert_col(const std::string& name, const std::string& col, const std::string& value = " "); // //作用 : 更新一列. //col : 列号.将更新该列. //value: 除了第一行之外所有该列的值,默认为“updated”. // bool update_col(int col, const std::string& value = "updated"); // //作用 : 更新一列. //col : 列名.将对该列进行条件判断.倘有列名重复情况,将以具有该列名的第一列为准. //value: 除了第一行之外所有该列的值,默认为“updated”. // bool update_col(const std::string& col, const std::string& value = "updated"); // //作用: 删除一列. //col : 列号.将删除该列. // bool delete_col(int col); // //作用: 删除一列. //col : 列名.将对该列进行条件判断.倘有列名重复情况,将以具有该列名的第一列为准. // bool delete_col(const std::string& col); // //作用 : 读取一列,含列标题. //value: 读取结果存放于此. //col : 列号.将读取该列. // bool read_col(std::vector<std::string>& value, int col); // //作用 : 读取一列,含列标题. //value: 读取结果存放于此. //col : 列名.将对该列进行条件判断.倘有列名重复情况,将以具有该列名的第一列为准. // bool read_col(std::vector<std::string>& value, const std::string& col); // //作用 : 更新列标题. //value: 欲以此值进行更新. //col : 列号.将更新该列. // bool update_col_name(const std::string& value, int col); // //作用 : 更新列标题. //value: 欲以此值进行更新. //col : 列名.将对该列进行条件判断.倘有列名重复情况,将以具有该列名的第一列为准. // bool update_col_name(const std::string& value, const std::string& col); // //作用 : 更新cell(row, col)值. //value: 欲以此值进行更新. //col : 列号. //row : 行号. // bool update_cell(const std::string& value, int col, int row); // //作用 : 更新cell(row, col)值. //value: 欲以此值进行更新. //col : 列名.倘有列名重复情况,将以具有该列名的第一列为准. //row : 行号. // bool update_cell(const std::string& value, const std::string& col, int row); // //作用 : 读取cell(row, col)值. //value: 读取值存放于此. //col : 列号. //row : 行号. // bool read_cell (std::string& value, int col, int row); // //作用 : 读取cell(row, col)值. //value: 读取值存放于此. //col : 列名.倘有列名重复情况,将以具有该列名的第一列为准. //row : 行号. // bool read_cell (std::string& value, const std::string& col, int row); // //作用 : 交换两列位置. //first_col : 列号. //second_col: 行号. // bool swap_cols(int first_col, int second_col); // //作用: 交换两列位置. //col : 列名.倘有列名重复情况,将以具有该列名的第一列为准. //col : 列名.倘有列名重复情况,将以具有该列名的第一列为准. // bool swap_cols(const std::string& first_col, const std::string& second_col); // //作用 : 交换两行位置. //first_row : 行号. //second_row: 行号. // bool swap_rows(int first_row, int second_row); // //作用: 对该列进行排序(按字母顺序). //col : 列号. //asc : true为升序,false为降序.默认为升序. // bool sort_col(int col, bool asc = true); // //作用: 对该列进行排序(按字母顺序). //col : 列名.倘有列名重复情况,将以具有该列名的第一列为准. //asc : true为升序,false为降序,默认为升序. // bool sort_col(const std::string& col, bool asc = true); // //作用: 清空缓存,并清空文件内容. // bool clear(); // //作用: 开始事务. // void begin_transaction(); // //作用: 提交事务.将所有内存更改,持久化到数据文件中. // bool commit(); // //作用: 回滚到最近一次事务提交点,放弃所有内存修改. // bool rollback(); // //作用: 判断给定的列号是否在有效范围内[1, m_col_total_count]. //col : 列号. // bool is_col_in_range(int col); // //作用: 判断给定的行号是否在有效范围内[1, m_row_total_count]. //row : 行号. // bool is_row_in_range(int row); // //作用: 判断给定的cell(row, col)是否在有效范围内[1, 1] [1, m_col_total_count] // [m_row_total_count, 1] [m_row_total_count, m_col_total_count]. //col : 列号. //row : 行号. // bool is_cell_in_range(int col, int row); // //作用 : 返回所有列标题. //value: 返回值存放于此. // void get_col_names (std::vector<std::string>& value) const; // //作用: 由给定列名,返回对应列号. //col : 列名.倘有列名重复情况,将以具有该列名的第一列为准. // int get_col_no(const std::string& col) const; // //作用: 返回总行数. // int get_row_total_count() const; // //作用: 返回总列数. // int get_col_total_count() const; // //作用: 返回当前行号. // int get_current_row_no() const; // //作用: 返回备份状态. // bool get_backup_status() const; // //作用: 返回事务状态. // bool get_transaction_status() const; // //作用: 返回最近一次出错信息. // const std::string& get_last_error() const; protected: // //作用: 打开文件,将内容读取到m_rows中,行尾以"/r"定界. // bool open(); // //作用: 将vector中的字符串,进行定界处理,并返回结果字符串. // std::string delimit(const std::vector<std::string>& value); private: // //作用: 文件备份状态. // bool m_backup; // //作用: 事务状态. // bool m_transaction; // //作用: 当前行行号. // int m_current_row; // //作用: 总共行数. // int m_row_total_count; // //作用: 总共列数. // int m_col_total_count; // //作用: 被操作的文件名. // std::string m_file_name; // //作用: 定界符,比如若干空格/符号/字母等等. // std::string m_separator; // //作用: 最近一次出错信息. // std::string m_last_error; // //作用: 所有列名. // std::vector<std::string> m_col_names; // //作用: 所有行值. // std::vector<std::string> m_rows; }; #endif |