mirror of
https://github.com/open-goal/jak-project
synced 2026-09-09 20:21:28 -04:00
Move duplicated utilities to the common util folder and remove NEXT_DIR (#29)
* move things to the common library and remove next_dir * fix for windows * one last windows fix * last fix for real this time * debug listener test * fix listener threading bug
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
#ifndef JAK_V2_BINARYREADER_H
|
||||
#define JAK_V2_BINARYREADER_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
|
||||
class BinaryReader {
|
||||
public:
|
||||
BinaryReader(uint8_t* _buffer, uint32_t _size) : buffer(_buffer), size(_size) {}
|
||||
|
||||
explicit BinaryReader(std::vector<uint8_t>& _buffer)
|
||||
: buffer((uint8_t*)_buffer.data()), size(_buffer.size()) {}
|
||||
|
||||
template <typename T>
|
||||
T read() {
|
||||
assert(seek + sizeof(T) <= size);
|
||||
T& obj = *(T*)(buffer + seek);
|
||||
seek += sizeof(T);
|
||||
return obj;
|
||||
}
|
||||
|
||||
void ffwd(int amount) {
|
||||
seek += amount;
|
||||
assert(seek <= size);
|
||||
}
|
||||
|
||||
uint32_t bytes_left() const { return size - seek; }
|
||||
|
||||
uint8_t* here() { return buffer + seek; }
|
||||
|
||||
uint32_t get_seek() { return seek; }
|
||||
|
||||
private:
|
||||
uint8_t* buffer;
|
||||
uint32_t size;
|
||||
uint32_t seek = 0;
|
||||
};
|
||||
|
||||
#endif // JAK_V2_BINARYREADER_H
|
||||
@@ -3,35 +3,10 @@
|
||||
#include <sstream>
|
||||
#include <cassert>
|
||||
|
||||
std::string read_text_file(const std::string& path) {
|
||||
std::ifstream file(path);
|
||||
std::stringstream ss;
|
||||
ss << file.rdbuf();
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string combine_path(const std::string& parent, const std::string& child) {
|
||||
return parent + "/" + child;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> read_binary_file(const std::string& filename) {
|
||||
auto fp = fopen(filename.c_str(), "rb");
|
||||
if (!fp)
|
||||
throw std::runtime_error("File " + filename + " cannot be opened");
|
||||
fseek(fp, 0, SEEK_END);
|
||||
auto len = ftell(fp);
|
||||
rewind(fp);
|
||||
|
||||
std::vector<uint8_t> data;
|
||||
data.resize(len);
|
||||
|
||||
if (fread(data.data(), len, 1, fp) != 1) {
|
||||
throw std::runtime_error("File " + filename + " cannot be read");
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
std::string base_name(const std::string& filename) {
|
||||
size_t pos = 0;
|
||||
assert(!filename.empty());
|
||||
@@ -70,13 +45,3 @@ uint32_t crc32(const uint8_t* data, size_t size) {
|
||||
uint32_t crc32(const std::vector<uint8_t>& data) {
|
||||
return crc32(data.data(), data.size());
|
||||
}
|
||||
|
||||
void write_text_file(const std::string& file_name, const std::string& text) {
|
||||
FILE* fp = fopen(file_name.c_str(), "w");
|
||||
if (!fp) {
|
||||
printf("Failed to fopen %s\n", file_name.c_str());
|
||||
throw std::runtime_error("Failed to open file");
|
||||
}
|
||||
fprintf(fp, "%s\n", text.c_str());
|
||||
fclose(fp);
|
||||
}
|
||||
@@ -4,12 +4,8 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::string read_text_file(const std::string& path);
|
||||
std::string combine_path(const std::string& parent, const std::string& child);
|
||||
std::vector<uint8_t> read_binary_file(const std::string& filename);
|
||||
std::string base_name(const std::string& filename);
|
||||
void write_text_file(const std::string& file_name, const std::string& text);
|
||||
|
||||
void init_crc();
|
||||
uint32_t crc32(const uint8_t* data, size_t size);
|
||||
uint32_t crc32(const std::vector<uint8_t>& data);
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
#include "Timer.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <Windows.h>
|
||||
#define MS_PER_SEC 1000ULL // MS = milliseconds
|
||||
#define US_PER_MS 1000ULL // US = microseconds
|
||||
#define HNS_PER_US 10ULL // HNS = hundred-nanoseconds (e.g., 1 hns = 100 ns)
|
||||
#define NS_PER_US 1000ULL
|
||||
|
||||
#define HNS_PER_SEC (MS_PER_SEC * US_PER_MS * HNS_PER_US)
|
||||
#define NS_PER_HNS (100ULL) // NS = nanoseconds
|
||||
#define NS_PER_SEC (MS_PER_SEC * US_PER_MS * NS_PER_US)
|
||||
|
||||
int Timer::clock_gettime_monotonic(struct timespec* tv) {
|
||||
static LARGE_INTEGER ticksPerSec;
|
||||
LARGE_INTEGER ticks;
|
||||
double seconds;
|
||||
|
||||
if (!ticksPerSec.QuadPart) {
|
||||
QueryPerformanceFrequency(&ticksPerSec);
|
||||
if (!ticksPerSec.QuadPart) {
|
||||
errno = ENOTSUP;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
QueryPerformanceCounter(&ticks);
|
||||
|
||||
seconds = (double)ticks.QuadPart / (double)ticksPerSec.QuadPart;
|
||||
tv->tv_sec = (time_t)seconds;
|
||||
tv->tv_nsec = (long)((ULONGLONG)(seconds * NS_PER_SEC) % NS_PER_SEC);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
void Timer::start() {
|
||||
#ifdef __linux__
|
||||
clock_gettime(CLOCK_MONOTONIC, &_startTime);
|
||||
#elif _WIN32
|
||||
clock_gettime_monotonic(&_startTime);
|
||||
#endif
|
||||
}
|
||||
|
||||
int64_t Timer::getNs() {
|
||||
struct timespec now = {};
|
||||
#ifdef __linux__
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
#elif _WIN32
|
||||
clock_gettime_monotonic(&now);
|
||||
#endif
|
||||
return (int64_t)(now.tv_nsec - _startTime.tv_nsec) +
|
||||
1000000000 * (now.tv_sec - _startTime.tv_sec);
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
#ifndef JAK_V2_TIMER_H
|
||||
#define JAK_V2_TIMER_H
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <ctime>
|
||||
|
||||
/*!
|
||||
* Timer for measuring time elapsed with clock_monotonic
|
||||
*/
|
||||
class Timer {
|
||||
public:
|
||||
/*!
|
||||
* Construct and start timer
|
||||
*/
|
||||
explicit Timer() { start(); }
|
||||
|
||||
#ifdef _WIN32
|
||||
int clock_gettime_monotonic(struct timespec* tv);
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* Start the timer
|
||||
*/
|
||||
void start();
|
||||
|
||||
/*!
|
||||
* Get milliseconds elapsed
|
||||
*/
|
||||
double getMs() { return (double)getNs() / 1.e6; }
|
||||
|
||||
double getUs() { return (double)getNs() / 1.e3; }
|
||||
|
||||
/*!
|
||||
* Get nanoseconds elapsed
|
||||
*/
|
||||
int64_t getNs();
|
||||
|
||||
/*!
|
||||
* Get seconds elapsed
|
||||
*/
|
||||
double getSeconds() { return (double)getNs() / 1.e9; }
|
||||
|
||||
struct timespec _startTime = {};
|
||||
};
|
||||
|
||||
#endif // JAK_V2_TIMER_H
|
||||
Reference in New Issue
Block a user