⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 util.h

📁 C++ patterns设计模式
💻 H
字号:
////////////////////////////////////////////////////////////////////////////////
// 工具模块
////////////////////////////////////////////////////////////////////////////////
// Author      : Darkay Li                                                        
// Description : 实现各种工具函数和类等
//
// Version     : 1.0
//
// Standard include files : std_inc.hpp
//
// Start Date  : 2003年5月7日
//
// Change Log  : 
//  2003年5月7日 by Darkay Li 
//-- Created
//  [5/16/2003] by Darkay Li
//-- move bcdcode class to single file
//  [5/19/2003] by Darkay Li
//-- add class FixedVector
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_UTIL
#define INCLUDED_UTIL

#if defined(HAS_PRAGMA_ONCE)
#pragma PRAGMA_ONCE_DECLARE
#endif

namespace stk
{
	//for using empty string
	static std::string s_strEmpty;
	//get the file size
	int fileSize(const char* file);
	//whether the buffer contain half chinese
	bool checkHalfChinese(const std::string& buf);
	//take part's of the base with chinese complete
	int takeUnicode(const char *begin,
		const char *end, 
		size_t howmany, 
		char *output);
	//convert something to other format
	const char* toHex(unsigned char byte, char* buf);
	const char* toHex(const char* buf, size_t length, std::string& hex);	
	void fromHex(const char *buf, size_t length, std::string &oct);
	//safe strncpy version
	inline char* strncpy( char* dest, const char* source, size_t count);
	//fill src as zero
	inline void  bzero(void *src, size_t length)
	{
		::memset(src, 0, length);
	}

	//split string which splitter is one character
	size_t splitString(const char *str, 
		char spliter, 
		int want, 
		std::vector<std::string> &vec);

	static const bool SPLT_STR_KEEP_EMPTY = true;
	//split string which splitter is multi-character
	size_t splitString(const std::string &source, 
		const char *delims, 
		std::vector<std::string> &vec,
		bool keepEmpty = false);

	//convert UCS-2 format unicode to GB2312
	std::string ConvertUCS2ToGB2312(const char *unicode, size_t unicodeLength);

	inline void toupper(std::string &str)
	{
		std::transform(str.begin(), str.end(),
			str.begin(), ::toupper);
	}
	inline void tolower(std::string &str)
	{
		std::transform(str.begin(), str.end(),
			str.begin(), ::tolower);
	}

	void trim(std::string &str);

	// two's complement of the modulo 256
	unsigned char calcCheckSum(const unsigned char *checkBegin, size_t length);

	// get the LSB and MSB
	template<typename T>
		inline unsigned char lsb(T org)
	{
		return (org & 0xFF);
	}
	template<typename T>
		inline unsigned char msb(T org)
	{
		return (org << (sizeof(org)-1) ) & 0xFF;
	}

	// value to stream convertion
	inline void int2stream(int val, char *stream)
	{
		assert(stream);
		stream[0] = (unsigned char)(val >> 24);
		stream[1] = (unsigned char)(val >> 16);
		stream[2] = (unsigned char)(val >> 8);
		stream[3] = (unsigned char)(val);
	}
    inline int stream2int(const char stream[4])
	{
		int accumulator = (unsigned char )stream[0];
		accumulator = (accumulator << 8) | (unsigned char )stream[1];
		accumulator = (accumulator << 8) | (unsigned char )stream[2];
		accumulator = (accumulator << 8) | (unsigned char )stream[3];
		return accumulator;
	}
    
    std::string indentSpace(int indent);

	//////////////////////////////////////////////////////
	//Purpose      : 辅助类:管理FILE指针,在析构的时候自动关闭文件
	//Relations    : 
	///////////////////////////////////////////////////////
	class AutoFileCloser
	{
	public:
		//////////////////////////////////////////////////////
		//Purpose      : 构造函数
		//Input Paras  : fp -- 将要被自动关闭的文件句柄
		//Output Paras : NULL
		//Return Value : NULL
		//Relations    : 
		///////////////////////////////////////////////////////
		AutoFileCloser(FILE *fp)
			: m_fp(fp)
		{
		}
		//////////////////////////////////////////////////////
		//Purpose      : 析构函数,自动关闭文件句柄
		//Input Paras  : NULL
		//Output Paras : NULL
		//Return Value : NULL
		//Relations    : 
		///////////////////////////////////////////////////////
		~AutoFileCloser()
		{
			if(m_fp)
				fclose(m_fp);
		}
	private:
		FILE *m_fp;
	};
};

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -