1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <iostream>
#include <fstream>
#include "MyDataTypes.h"
#include <string>
#include "IOFunctions.h"
using namespace std;
u8 readu8(ifstream& filein) {
char byte = 0;
u8 val = 0;
filein.read((char*) byte, 1);
val = byte;
return val;
}
u16 readu16(ifstream& filein) {
char bytes[2];
u16 val = 0;
filein.read((char*) bytes, 2);
val = bytes[0] | (bytes[1] << 8);
return val;
}
u32 readu32(ifstream& filein) {
char bytes[4];
u32 val = 0;
filein.read((char*) bytes, 4);
val = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) |
(bytes[3] << 24);
return val;
}
u64 readu64(ifstream& filein) {
char bytes[8];
u64 val = 0;
filein.read((char*) bytes, 8);
val = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24)
| (static_cast<u64>(bytes[4]) << 32) | (static_cast<u64>(bytes[5]) << 40) |
(static_cast<u64>(bytes[6]) << 48) | (static_cast<u64>(bytes[7]) << 56);
return val;
}
void writeu8(ofstream& fileout, u8 val) {
char bytes[1];
bytes[0] = val;
fileout.write((char*) bytes, 1);
}
void writeu16(ofstream& fileout, u16 val) {
char bytes[2];
bytes[0] = (val & 0xFF);
bytes[1] = (val & 0xFF00) >> 8;
fileout.write((char*)bytes, 2);
}
void writeu32(ofstream& fileout, u32 val) {
char bytes[4];
bytes[0] = (val & 0xFF);
bytes[1] = (val & 0xFF00) >> 8;
bytes[2] = (val & 0xFF0000) >> 16;
bytes[3] = (val & 0xFF000000) >> 24;
fileout.write((char*) bytes, 4);
}
void writeu64(ofstream& fileout, u64 val) {
char bytes[8];
bytes[0] = (val & 0xFF);
bytes[1] = (val & 0xFF00) >> 8;
bytes[2] = static_cast<u8>(val & 0xFF0000) >> 16;
bytes[3] = static_cast<u8>((val & 0xFF000000) >> 24);
bytes[4] = static_cast<u8>(static_cast<u64>(val & 0xFF00000000) >> 32);
bytes[5] = static_cast<u8>(static_cast<u64>(val & 0xFF0000000000) >> 40);
bytes[6] = static_cast<u8>(static_cast<u64>(val & 0xFF000000000000) >> 48);
bytes[7] = static_cast<u8>(static_cast<u64>(val & 0xFF00000000000000) >> 56);
fileout.write((char*)bytes, 8);
}
string readstring(ifstream& filein) {
u32 len = readu32(filein);
char* buffer = new char[len];
filein.read(buffer, len);
string str(buffer, len);
delete[] buffer;
return str;
}
void writestring(ofstream& fileout, string str) {
u32 len = str.length();
writeu32(fileout, len);
fileout.write(str.c_str(), len);
}
|