C++常用工具库(C语言文件读写,日志库,格式化字符串, 获取可执行文件所在绝对路径等)...

前言

  • 自己常用的工具库, C++ 和C语言实现
  • 使用cmake维护的项目
  • 持续更新.....
  • 提供使用范例, 详见example文件夹
  • windows使用的VS通过了的编译。 Linux(Ubuntu)使用的是 clang++ 10.0
  • 欢迎留言交流

下载地址

giteegithub

文件读写接口

/// ----------------------------------------------------------------------------------------
	/// 文件读写接口
	/// ----------------------------------------------------------------------------------------
	class ifile
	{
	public:

		///  --------------------------------------------------------------------------------
		///  @brief:	初始化
		///  @str_file - 文件所在绝对路径, 例如: C:/demo/logs/1.txt
		///  @open_mode - 以哪种方式打开文件
		///  @return - int	
		/// 			0 - 成功
		///				1 - 失败, 参数【str_file】字符串为空
		///				2 - 失败,参数【open_mode】不是给定的枚举范围
		///				3 - 失败,文件打开失败。
		///  --------------------------------------------------------------------------------
		virtual int init_(const std::string& str_file, const oct_toolkits::en_file_open_mode open_mode) = 0;


		///  --------------------------------------------------------------------------------
		///  	返回当前初始化的文件
		///  @return - std::string	
		/// 			
		///  --------------------------------------------------------------------------------
		virtual std::string file_() = 0;


		///  --------------------------------------------------------------------------------
		///  	文件是否打开
		///  @return - bool	
		/// 			true - 打开
		///				false - 没有打开
		///  --------------------------------------------------------------------------------
		virtual bool is_opened_() = 0;


		///  --------------------------------------------------------------------------------
		///  	返回文件长度,单位: 字节
		///  @return - int	
		/// 			返回值X, 
		///				-2 - 失败,请先执行初始化
		///				X >=0 , 文件长度,单位:字节
		///  --------------------------------------------------------------------------------
		virtual int length_() = 0;


		///  --------------------------------------------------------------------------------
		///  	写文件, 将数据写入
		///  @pbuf - 待写入内容
		///  @pbuf_len - 待写入数据长度
		///  @return - int	
		/// 			0 - 成功
		///				1 - 失败,请先初始化
		///				2 - 失败,参数【pbuf】为nullptr或者NULL
		///				3 - 失败,写文件失败
		///  --------------------------------------------------------------------------------
		virtual int write_(const char* pbuf, unsigned int pbuf_len) = 0;
		///  --------------------------------------------------------------------------------
		///  	写文件
		///  @str_write - 待文件内容
		///  @return - int	
		/// 			0 - 成功
		///				1 - 失败,请先初始化
		///				2 - 失败,参数【str_write】的长度为0
		///				3 - 失败,写文件失败
		///  --------------------------------------------------------------------------------
		virtual int write_(const std::string& str_write) = 0;

		///  --------------------------------------------------------------------------------
		///  	将文件内容儒道参数【str_read】中
		///  @str_read - 返回文件内容
		///  @return - int	
		/// 			0 - 成功
		///				1 - 失败,请先初始化
		///				2 - 失败,读取文件缓冲创建失败
		///				3 - 失败,读取文件失败
		///  --------------------------------------------------------------------------------
		virtual int read_(std::string& str_read) = 0;

		///  --------------------------------------------------------------------------------
		///   读文件内容, 参数【pout_buf】需要外部申请,函数内不会维护,读取成功后,文件内容存放在pout_buf中。
		///  @pout_buf - 返回读取文件内容
		///  @pout_buf_len - 【pout_buf】缓冲区的长度
		///  @return - int	
		/// 			0 - 成功
		///				1 - 失败,请先初始化
		///				2 - 失败,参数【pout_buf】为空 或者 参数【pout_buf_len】的值是0
		///  --------------------------------------------------------------------------------
		virtual int read_(char* pout_buf, unsigned int pout_buf_len) = 0;

		///  --------------------------------------------------------------------------------
		///  	判断参数【str_check_file】的文件是否存在
		///  @str_check_file - 
		///  @return - bool	
		/// 			
		///  --------------------------------------------------------------------------------
		virtual bool is_existed_(const std::string& str_check_file) = 0;

		///  --------------------------------------------------------------------------------
		///  	避免析构时出现异常,请销毁前先调用该函数完成内部资源释放
		///  @return - int	
		/// 			0 - 成功
		///  --------------------------------------------------------------------------------
		virtual int uninit_() = 0;
	};

日志库接口

/// ----------------------------------------------------------------------------------------
	/// 日志文件接口类
	/// ----------------------------------------------------------------------------------------
	class ilog
	{
	public:
		///  --------------------------------------------------------------------------------
		///  	初始化, 
		///  @info - 日志信息
		///  @return - int	
		/// 			0 - 成功
		///				1 - 失败,日志文件路径不正确
		///				2 - 失败,如果磁盘空间剩余不够,则禁止创建日志文件对象
		///				3 - 失败,无法创建日志文件路径, 无法写入日志
		///				5 - 失败,创建日志文件失败,则无法写入日志
		///  --------------------------------------------------------------------------------
		virtual int init_(const oct_toolkits::st_log_info& info) = 0;




		///  --------------------------------------------------------------------------------
		///  写日志,日志内容为文本,且每一行前面都带有时间; 例如: [2021-03-28 15:00:00:001] 日志文件内容
		///  @str_log - 待写入日志文件内容
		///  @return - int	
		/// 			0 - 成功
		///				1 - 无法写入日志,可能是日志文件创建失败,或者磁盘剩余空间不足5G
		///				2 - 失败,无法创建写日志文件对象,请先初始化
		///  --------------------------------------------------------------------------------
		virtual int log_text_(const std::string& str_log) = 0;

		///  --------------------------------------------------------------------------------
		///  	写日志
		///  @pdata - 待写入内容
		///  @pdata_len - 待写入长度
		///  @return - int	
		/// 			0 - 成功、
		///				1 - 失败,参数【pdata】为空或者参数【pdata_len】为0
		///				2 - 失败,无法写入日志,可能是日志文件创建失败,或者磁盘剩余空间不足5G
		///				3 - 失败,无法写入,日志文件读写对象创建失败,请先初始化
		///  --------------------------------------------------------------------------------
		virtual int log_text_(const char* pdata, unsigned int pdata_len) = 0;

		///  --------------------------------------------------------------------------------
		///  	将参数【str_log】每个字节的16进制写入文件,全部大写
		///  @str_log - 待写入内容
		///  @return - int	
		///				0 - 成功
		/// 			1 - 无法写入日志,可能是日志文件创建失败,或者磁盘剩余空间不足5G
		///				2 - 失败,无法写入,日志文件读写对象创建失败,请先初始化
		///  --------------------------------------------------------------------------------
		virtual int log_hex_(const std::string& str_log) = 0;

		///  --------------------------------------------------------------------------------
		///  	将参数【pdata】每个字节的16进制写入文件,全部大写
		///  @pdata - 待写入内容
		///  @pdata_len - 写入内容长度
		///  @return - int	
		/// 			0 - 成功
		///				1 - 失败,参数【pdata】为空或者参数【pdata_len】为0
		///				2 - 失败,无法写入日志,可能是日志文件创建失败,或者磁盘剩余空间不足5G
		///				3 - 失败,无法写入,日志文件读写对象创建失败,请先初始化
		///  --------------------------------------------------------------------------------
		virtual int log_hex_(const char* pdata, unsigned int pdata_len) = 0;


		///  --------------------------------------------------------------------------------
		///  	释放内部资源
		///  @return - void	
		/// 			
		///  --------------------------------------------------------------------------------
		virtual void uninit_() = 0;
	};

其他常用接口

/// ----------------------------------------------------------------------------------------
	/// 常用工具
	/// ----------------------------------------------------------------------------------------
	class lib_toolkits_api toolkits
	{
	public:
		explicit toolkits();
		virtual ~toolkits();

		///  --------------------------------------------------------------------------------
		///  	返回年月日时分秒毫秒字符串
		///  @return - std::string	
		/// 			
		///  --------------------------------------------------------------------------------
		st_now_date get_date_now_();

		///  --------------------------------------------------------------------------------
		///  	格式化字符串
		///  @pformat - 格式化字符串
		///  @ - 参数列表
		///  @return - std::string	
		/// 			返回格式化结果
		///  --------------------------------------------------------------------------------
		std::string str_format_(const char*pformat, ...);


		///  --------------------------------------------------------------------------------
		///  	获取可执行程序所在绝对路径
		///  @return - std::string	
		/// 			
		///  --------------------------------------------------------------------------------
		std::string get_executable_dir_();


		/// -----------------------------------------------------------------
		/// windows特有的转码函数, 如果是非Windows平台调用,则返回空的字符串
		/// -----------------------------------------------------------------

		///  --------------------------------------------------------------------------------
		///  @brief: 	utf8字符串转ascii
		///  @param:	const std::string & str_utf8 - utf8字符串编码 
		///  @return:	std::string	
		/// 			ascii字符串编码
		///  --------------------------------------------------------------------------------
		std::string utf8_to_ascii_(const std::string& str_utf8);
		

		///  --------------------------------------------------------------------------------
		///  @brief: 	ascii字符编码转utf8字符串
		///  @param:	const std::string & str_acsii - acsii编码字符串
		///  @return:	std::string	
		/// 			utf8字符串
		///  --------------------------------------------------------------------------------
		std::string acsii_to_utf8_(const std::string& str_acsii);

	private:
		/// 屏蔽拷贝构造函数
		toolkits(const toolkits& instance) {}
		toolkits& operator =(const toolkits& instance) { return *this; }
	};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值