📄 byteset.cpp
字号:
/* * $File: byteset.cpp * $Author: Jiakai -- gy_jk@126.com * $Date: Sun Nov 23 11:51:53 2008 *//*Copyright (C) (2008) (Jiakai) <gy_jk@126.com>This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include "byteset.h"#include "trstr.h"#include <cstring>#include <vector>using namespace std;struct Byteset_i::Detail{ const Byte *data; Bytes_len data_len, pos; bool good; void read(Byte *dest, Bytes_len count);};void Byteset_i::Detail::read(Byte *dest, Bytes_len count){ if (!good) return; if (pos + count > data_len) { good = false; return; } memcpy(dest, &data[pos], count); pos += count;}Byteset_i::Byteset_i(const char *str) : p(new Detail){ p->data = decrypt(str, p->data_len); p->good = p->data != NULL; p->pos = 0;}Byteset_i::~Byteset_i(){ delete []p->data; delete p;}bool Byteset_i::good() {return p->good;}Byteset_i& Byteset_i::read(double &x){ p->read((Byte*)&x, sizeof(double)); return *this;}Byteset_i& Byteset_i::read(unsigned long &x){ p->read((Byte*)&x, sizeof(unsigned long)); return *this;}Byteset_i& Byteset_i::read(bool &x){ p->read((Byte*)&x, sizeof(bool)); return *this;}Byteset_i& Byteset_i::read(string &str){ if (!p->good) return *this; str.erase(); while (true) { char tmp = p->data[p->pos ++]; if (tmp == 0) break; str.append(1, tmp); if (p->pos == p->data_len) { p->good = false; break; } } return *this;}//============================================================================struct Byteset_o::Detail{ static const Bytes_len UNIT_LEN; vector<Byte*> data; Bytes_len data_pos; string str; bool changed; void write(const Byte *source, Bytes_len source_len);};const Bytes_len Byteset_o::Detail::UNIT_LEN = 4096;void Byteset_o::Detail::write(const Byte *source, Bytes_len source_len){ changed = true; if (data_pos + source_len > UNIT_LEN) { Bytes_len wlen = UNIT_LEN - data_pos; memcpy(*data.rbegin() + data_pos, source, wlen); data.push_back(new Byte[UNIT_LEN]); data_pos = 0; write(source + wlen, source_len - wlen); return; } memcpy(*data.rbegin() + data_pos, source, source_len); data_pos += source_len;}Byteset_o::Byteset_o() : p(new Detail){ p->data.push_back(new Byte[p->UNIT_LEN]); p->data_pos = 0; p->changed = true;}Byteset_o::~Byteset_o(){ for (vector<Byte*>::iterator i = p->data.begin(); i != p->data.end(); i ++) delete []*i; delete p;}const char *Byteset_o::str(){ if (!p->changed) return p->str.c_str(); Bytes_len tlen = (p->data.size() - 1) * p->UNIT_LEN + p->data_pos; Byte* tmp = new Byte[tlen]; for (vector<Byte*>::size_type i = 0; i < p->data.size() - 1; i ++) memcpy(tmp + p->UNIT_LEN * i, p->data[i], p->UNIT_LEN); memcpy(tmp + p->UNIT_LEN * (p->data.size() - 1), *p->data.rbegin(), p->data_pos); if (!encrypt(tmp, tlen, p->str)) { delete []tmp; return NULL; } delete []tmp; p->changed = false; return p->str.c_str();}Byteset_o& Byteset_o::write(double x){ p->write((Byte*)&x, sizeof(double)); return *this;}Byteset_o& Byteset_o::write(unsigned long x){ p->write((Byte*)&x, sizeof(unsigned long)); return *this;}Byteset_o& Byteset_o::write(bool x){ p->write((Byte*)&x, sizeof(bool)); return *this;}Byteset_o& Byteset_o::write(const string &str){ p->write((const Byte *)str.c_str(), str.length() + 1); return *this;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -