用C++做的一个输入输出缓冲池,可优化硬盘文件读写的速度,支持多种数据类型。
// buffer.h // Buffer classes can make input and output faster. #ifndef BUFFER_H #define BUFFER_H #include <fstream> #include <string> #include <vector> #include <sstream> class OBuffer { unsigned char* storage; int location; std::ofstream out; int bufsize; bool bin; //1-bin-output 0-text-output public: void close() { flush(); out.close(); } OBuffer(const std::string filename, bool b = true, int size = 1024); ~OBuffer() { close(); delete[] storage; } void write(const std::string& var); void write(const long var); void write(const double var); void write(const bool var); void write(const char var); void write(const int var); void write(const char* var); void write(const std::vector<std::string>& var); void write(const std::vector<long>& var); void write(const std::vector<double>& var); void write(const std::vector<bool>& var); void write(const std::vector<char>& var); void write(const std::vector<int>& var); private: void flush(); public: OBuffer& operator << (const std::string& var); OBuffer& operator << (const long var); OBuffer& operator << (const double var); OBuffer& operator << (const bool var); OBuffer& operator << (const char var); OBuffer& operator << (const int var); OBuffer& operator << (const char* var); OBuffer& operator << (const std::vector<std::string>& var); OBuffer& operator << (const std::vector<long>& var); OBuffer& operator << (const std::vector<double>& var); OBuffer& operator << (const std::vector<bool>& var); OBuffer& operator << (const std::vector<char>& var); OBuffer& operator << (const std::vector<int>& var); }; class IBuffer { unsigned char* storage; int location; std::ifstream in; int bufsize; bool bin; bool isEOF; int filesize; // in normal case, the input file isn't end // so the end is a location that can't reach // when the file is end, // the end is set a number that when // location reach it, the buffer is end; int end; bool hasTemp; char tempChar; public: IBuffer(const std::string filename, bool b = true, int size = 1024); void close() { in.close(); flush(); } ~IBuffer() { close(); delete[] storage; } bool eof() { return isEOF; } private: void flush() { location = -1; in.read((char*)storage, bufsize); if (in.eof()) end = filesize % bufsize - 1; if (end == -1) end = bufsize - 1; } char get(); public: void read(int& var); void read(long& var); void read(std::string& var); void read(double& var); void read(char& var); void read(bool& var); void read(std::vector<int>& var, int length); void read(std::vector<long>& var, int length); void read(std::vector<double>& var, int length); void read(std::vector<std::string>& var, int length); void read(std::vector<char>& var, int length); void read(std::vector<bool>& var, int length); IBuffer& operator >> (std::string& var); IBuffer& operator >> (long& var); IBuffer& operator >> (double& var); IBuffer& operator >> (bool& var); IBuffer& operator >> (char& var); IBuffer& operator >> (int& var); }; #endif
// buffer.cc // implement of the buffer.h #include "buffer.h" #include <iostream> #include <sstream> #include <fstream> #include <string> #include <cstdlib> #include <ctype.h> using namespace std; /*****************************OBuffer***********************/ OBuffer::OBuffer(const string filename, bool b, int size) : bufsize(size), location(-1), bin(b) { if (size <= 0) { cerr << "The size of buffer error!" << endl; exit(EXIT_FAILURE); } if (bin) out.open(filename.c_str(), fstream::binary); else out.open(filename.c_str()); storage = new unsigned char[bufsize]; if (storage == NULL) { cerr << "Memory error!" << endl; exit(EXIT_FAILURE); } } void OBuffer::flush() { if (location < 0) return; /*for (int i = 0; i <= location; i++) { out << storage[i]; }*/ out.write((char*)storage, location + 1); location = -1; } void OBuffer::write(const char var) { storage[++location] = (unsigned char)var; if (location >= bufsize - 1) flush(); } void OBuffer::write(const string& var) { for (int i = 0; i < var.length(); i++) write(var[i]); if (bin) write('