aboutsummaryrefslogtreecommitdiff
path: root/source/10-common/Utils.hpp
blob: 668261bacb5b14af4fd3cf5763788c0eb2c2e3c9 (plain)
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
#pragma once

#include <robin_hood.h>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <glm/glm.hpp>
#include <string>
#include <string_view>

namespace Utils {

enum IoMode {
	Read,
	WriteTruncate,
	WriteAppend,
};

FILE* OpenCstdioFile(const std::filesystem::path& path, IoMode mode, bool binary = false);
FILE* OpenCstdioFile(const char* path, IoMode mode, bool binary = false);

/// Retrieve a whole line (marked by `\n` or EOF) into the buffer. If the line ends with EOF, two things happen:
/// 1. a `\n` character is appended to the line content, emulating as-if the line ended with `\n`.
/// 2. `false` is returned
/// Otherwise, `true` is returned.
///
/// Empty lines are not skipped at all, including the very last empty line if it exists.
bool ReadCstdioLine(FILE* file, std::string& buffer);
/// Same as the other overload, except working with a fixed-size buffer.
/// NOTE: this also gives the length of the line compared to `std::fgets`.
///       `std::fgets` requires us to run `std::strlen` on the output again to find the length
bool ReadCstdioLine(FILE* file, char* buffer, size_t bufferSize, size_t* outLineLength = nullptr);

std::string ReadFileAsString(const std::filesystem::path& path);

constexpr float Abs(float v) noexcept {
	return v < 0.0f ? -v : v;
}

bool InRangeInclusive(int n, int lower, int upper);
bool LineContains(glm::ivec2 p1, glm::ivec2 p2, glm::ivec2 candidate);

bool IsColinear(glm::ivec2 p1, glm::ivec2 p2);

template <typename T>
void HashCombine(std::size_t& seed, const T& v) {
	seed ^= std::hash<T>{}(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

std::string MakeRandomNumberedName(const char* tag);

} // namespace Utils

struct StringHash {
	using is_transparent = void;

	std::size_t operator()(const std::string& key) const { return robin_hood::hash_bytes(key.c_str(), key.size()); }
	std::size_t operator()(std::string_view key) const { return robin_hood::hash_bytes(key.data(), key.size()); }
	std::size_t operator()(const char* key) const { return robin_hood::hash_bytes(key, std::strlen(key)); }
};

struct StringEqual {
	using is_transparent = int;

	bool operator()(std::string_view lhs, const std::string& rhs) const {
		const std::string_view view = rhs;
		return lhs == view;
	}

	bool operator()(const char* lhs, const std::string& rhs) const {
		return std::strcmp(lhs, rhs.c_str()) == 0;
	}

	bool operator()(const std::string& lhs, const std::string& rhs) const {
		return lhs == rhs;
	}
};