SIEdit/lib/util.h

117 lines
1.7 KiB
C
Raw Normal View History

2022-07-17 21:51:16 -04:00
#ifndef UTIL_H
#define UTIL_H
#include <fstream>
2022-07-18 03:27:00 -04:00
#include <iostream>
2022-07-17 21:51:16 -04:00
#include "types.h"
namespace si {
2022-07-18 03:27:00 -04:00
inline uint32_t ReadU32(std::istream &is)
2022-07-17 21:51:16 -04:00
{
uint32_t u;
is.read((char *) &u, sizeof(u));
return u;
}
2022-07-18 03:27:00 -04:00
inline void WriteU32(std::ostream &os, uint32_t u)
2022-07-17 21:51:16 -04:00
{
os.write((const char *) &u, sizeof(u));
}
2022-07-18 03:27:00 -04:00
inline uint16_t ReadU16(std::istream &is)
2022-07-17 21:51:16 -04:00
{
uint16_t u;
is.read((char *) &u, sizeof(u));
return u;
}
2022-07-18 03:27:00 -04:00
inline void WriteU16(std::ostream &os, uint16_t u)
2022-07-17 21:51:16 -04:00
{
os.write((const char *) &u, sizeof(u));
}
2022-07-18 03:27:00 -04:00
inline uint8_t ReadU8(std::istream &is)
2022-07-17 21:51:16 -04:00
{
uint8_t u;
is.read((char *) &u, sizeof(u));
return u;
}
2022-07-18 03:27:00 -04:00
inline void WriteU8(std::ostream &os, uint8_t u)
2022-07-17 21:51:16 -04:00
{
os.write((const char *) &u, sizeof(u));
}
2022-07-18 03:27:00 -04:00
inline Vector3 ReadVector3(std::istream &is)
2022-07-17 21:51:16 -04:00
{
Vector3 u;
is.read((char *) &u, sizeof(u));
return u;
}
2022-07-18 03:27:00 -04:00
inline void WriteVector3(std::ostream &os, Vector3 v)
2022-07-17 21:51:16 -04:00
{
os.write((const char *) &v, sizeof(v));
}
2022-07-18 03:27:00 -04:00
inline std::string ReadString(std::istream &is)
2022-07-17 21:51:16 -04:00
{
std::string d;
while (true) {
char c;
is.read(&c, 1);
if (c == 0) {
break;
}
d.push_back(c);
}
return d;
}
2022-07-18 03:27:00 -04:00
inline void WriteString(std::ostream &os, const std::string &d)
2022-07-17 21:51:16 -04:00
{
os.write(d.c_str(), d.size());
// Ensure null terminator
const char nullterm = 0;
os.write(&nullterm, 1);
}
2022-07-18 03:27:00 -04:00
inline bytearray ReadBytes(std::istream &is, size_t size)
2022-07-17 21:51:16 -04:00
{
bytearray d;
d.resize(size);
is.read(d.data(), size);
return d;
}
2022-07-18 03:27:00 -04:00
inline void WriteBytes(std::ostream &os, const bytearray &ba)
2022-07-17 21:51:16 -04:00
{
os.write(ba.data(), ba.size());
}
2022-07-18 03:27:00 -04:00
inline std::ostream &LogDebug()
{
return std::cout << "[DEBUG] ";
}
inline std::ostream &LogWarning()
{
return std::cerr << "[WARNING] ";
}
inline std::ostream &LogError()
{
return std::cerr << "[ERROR] ";
}
2022-07-17 21:51:16 -04:00
}
#endif // UTIL_H