Bootstrap

结构体编解码

记录一下代码:

message.h

#include <string>

class Message
{
public:
	virtual int serializeToByte(char *buff) = 0;
	virtual int parseFromByte(char *buff) = 0;
	virtual ~Message() {}
protected:
	char* intToByte(char* target, int num);
	char* stringToByte(char* target, const std::string& str);
	char* byteToInt(char* buff, int& num);
	char* byteToString(char* buff, std::string& str);
}; 

message.cpp

#include "message.h"

char* Message::intToByte(char* target, int num)
{
	int* temp = (int*) target;
	*temp++ = num;
	return (char*)temp;
}
char* Message::stringToByte(char* target, const std::string& str)
{
	int len = str.size();
	target = intToByte(target, len);
	for (auto ch : str)
	{
		*target++ = ch;
	}
	return target;
}
char* Message::byteToInt(char* buff, int& num)
{
	int* temp = (int*)buff;
	num = (*temp)++;
	return (char*)temp;
}
char* Message::byteToString(char* buff, std::string& str)
{
	int len = 0;
	buff = byteToInt(buff, len);

	for (int i = 0; i < len; ++i)
	{
		str += *buff++;
	}

	return buff;
}

teacher.h

#include "message.h"
#include <string>

class Teacher:public Message
{
public:
	Teacher() {}
	//Teacher(int age, const string& name, const string& subject, int grade):
	//	age(age), name(name), subject(subject) ,grade(grade) {}
	Teacher(int age, const char* name, const char* subject, int grade) :
		age(age), name(name), subject(subject), grade(grade) {}

	int serializeToByte(char *buff);
	int parseFromByte(char *buff);
	int getByteSize()const;
	/*void setAge(int age);
	void setName(const string& name);
	void setSubject(const string& subject);
	void setGrade(const string& grade);
*/
	int getAge()const;
	std::string getName()const;
	std::string getSubject()const;
	int getGrade()const;
private:
	int age = 0;
	std::string name;
	std::string subject;
	int grade = 0;
};

teacher.cpp

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "teacher.h"

using namespace std;

int Teacher::serializeToByte(char* target)
{
	if (target == nullptr)
		return -1;

	target = intToByte(target, age);
	target = stringToByte(target, name);
	target = stringToByte(target, subject);
	target = intToByte(target, grade);

	return 0;
}
int Teacher::parseFromByte(char *buff)
{
	if (buff == nullptr)
		return -1;

	buff= byteToInt(buff, age);

	cout << age << endl;
	buff = byteToString(buff, name);
	buff = byteToString(buff, subject);
	buff = byteToInt(buff, grade);

	return 0;
}
int Teacher::getByteSize()const
{
	return sizeof(int) + sizeof(int)+name.size() + sizeof(int) + subject.size() +sizeof(int);
}
int Teacher::getAge()const
{
	return age;
}
string Teacher::getName()const
{
	return name;
}
string Teacher::getSubject()const
{
	return subject;
}
int Teacher::getGrade()const
{
	return grade;
}

opera.h


class fileOpreation
{
public:
	int writeFile(char *buf, int len, const char* fileName);
	int readFile(char **buf, int& len, const char* fileName);
};

opera.cpp

#include <stdio.h>
#include <fstream>
#include "fileOperation.h"
using namespace std;

int fileOpreation::writeFile(char *buf, int len, const char* fileName)
{
	FILE *fp = NULL;
	fp = fopen(fileName, "wb+");

	if (fp == NULL)
	{
		printf("fopen file error \n");
		return -1;
	}

	fwrite(buf, 1, len, fp);//将buf写入fp,写入内容的单字节数1,一共写入len个字节1.
	fclose(fp);
	return 0;
}
int fileOpreation::readFile(char **buff, int& len, const char* fileName)
{
	FILE *fp;
	fp = fopen(fileName, "r");
	fseek(fp, 0, SEEK_END);
	len = ftell(fp);

	fseek(fp, 0, SEEK_SET);
	*buff = (char *)malloc(len * sizeof(char));
	fread(*buff, len, sizeof(char), fp);
	fclose(fp);
	return 0;
}

main函数

#include "teacher.h"
#include "fileOperation.h"
#include <string>
#include <iostream>

using namespace std;

int main()
{
	
	Teacher test(24, "张三", "语文", 6);
	int len = test.getByteSize();
	char* buff = new char[len];
	test.serializeToByte(buff);

	fileOpreation fp;
	fp.writeFile(buff, len, "test.bat");
	cout << len << endl;

	char* input= nullptr;
	int inputLen = 0;
	fp.readFile(&input, inputLen, "test.bat");
	cout << inputLen << endl;

	Teacher test2;
	test2.parseFromByte(input);
	cout << test2.getAge() << endl;
	cout << test2.getName() << endl;
	cout << test2.getSubject() << endl;
	cout << test2.getGrade() << endl;
}
;