Repository navigation
Expand file tree
/
Copy pathio_utils.h
More file actions
167 lines (148 loc) · 5.26 KB
/
Copy pathio_utils.h
File metadata and controls
167 lines (148 loc) · 5.26 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// io_utils.h — small I/O helpers shared by daemon code and test harness.
// Header-only: all functions are static inline to avoid extra link targets.
#pragma once
#include <cstdint>
#include <cstdio>
#include <fstream>
#include <limits>
#include <sstream>
#include <string>
#include <vector>
#if defined(_WIN32)
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
#else
# include <cerrno>
# include <unistd.h>
#endif
namespace dflash::common {
// ── Binary file I/O ────────────────────────────────────────────────
static inline std::vector<int32_t> read_int32_file(const std::string & path) {
std::ifstream f(path, std::ios::binary | std::ios::ate);
if (!f) return {};
const std::streampos pos = f.tellg();
if (pos == std::streampos(-1)) return {};
const std::streamoff bytes_off = pos - std::streampos(0);
if (bytes_off < 0 || bytes_off % (std::streamoff)sizeof(int32_t) != 0) return {};
const size_t bytes = (size_t)bytes_off;
if (bytes > (size_t)std::numeric_limits<std::streamsize>::max()) return {};
const size_t n = bytes / sizeof(int32_t);
f.seekg(0, std::ios::beg);
if (!f) return {};
std::vector<int32_t> out(n);
if (bytes > 0) {
f.read((char *)out.data(), (std::streamsize)bytes);
if (!f || f.gcount() != (std::streamsize)bytes) return {};
}
return out;
}
static inline bool write_int32_file(const std::string & path,
const std::vector<int32_t> & v) {
std::ofstream f(path, std::ios::binary);
if (!f) return false;
f.write((const char *)v.data(), v.size() * sizeof(int32_t));
return (bool)f;
}
static inline bool write_binary_file(const std::string & path,
const void * data, size_t bytes) {
std::ofstream f(path, std::ios::binary);
if (!f) return false;
if (bytes > 0) f.write((const char *)data, (std::streamsize)bytes);
return (bool)f;
}
static inline bool read_binary_file_exact(const std::string & path,
void * data, size_t bytes) {
std::ifstream f(path, std::ios::binary);
if (!f) return false;
if (bytes > 0) f.read((char *)data, (std::streamsize)bytes);
return (bool)f;
}
// ── String helpers ──────────────────────────────────────────────────
static inline std::string read_line_tail(std::istringstream & iss) {
std::string tail;
std::getline(iss, tail);
const size_t first = tail.find_first_not_of(" \t");
if (first == std::string::npos) return {};
if (first > 0) tail.erase(0, first);
return tail;
}
// ── Streaming / fd I/O ──────────────────────────────────────────────
static inline void stream_emit_fd(int stream_fd, int32_t tok) {
if (stream_fd < 0) return;
#if defined(_WIN32)
DWORD written = 0;
const int32_t v = tok;
WriteFile((HANDLE)(intptr_t)stream_fd, &v, sizeof(v), &written, nullptr);
#else
const int32_t v = tok;
(void)::write(stream_fd, &v, sizeof(v));
#endif
}
#if !defined(_WIN32)
static inline bool read_exact_fd(int fd, void * data, size_t bytes) {
char * p = (char *)data;
size_t done = 0;
while (done < bytes) {
ssize_t n = ::read(fd, p + done, bytes - done);
if (n == 0) return false;
if (n < 0) {
if (errno == EINTR) continue;
return false;
}
done += (size_t)n;
}
return true;
}
static inline bool write_exact_fd(int fd, const void * data, size_t bytes) {
const char * p = (const char *)data;
size_t done = 0;
while (done < bytes) {
ssize_t n = ::write(fd, p + done, bytes - done);
if (n < 0) {
if (errno == EINTR) continue;
return false;
}
done += (size_t)n;
}
return true;
}
#else
#include <io.h>
static inline bool read_exact_fd(int fd, void * data, size_t bytes) {
char * p = (char *)data;
size_t done = 0;
while (done < bytes) {
int n = _read(fd, p + done, (unsigned int)(bytes - done > UINT_MAX ? UINT_MAX : bytes - done));
if (n == 0) return false;
if (n < 0) {
if (errno == EINTR) continue;
return false;
}
done += (size_t)n;
}
return true;
}
static inline bool write_exact_fd(int fd, const void * data, size_t bytes) {
const char * p = (const char *)data;
size_t done = 0;
while (done < bytes) {
int n = _write(fd, p + done, (unsigned int)(bytes - done > UINT_MAX ? UINT_MAX : bytes - done));
if (n < 0) {
if (errno == EINTR) continue;
return false;
}
done += (size_t)n;
}
return true;
}
#endif
// ── Numeric helpers ─────────────────────────────────────────────────
static inline int argmax_f32(const float * x, int n) {
int best = 0;
float bv = x[0];
for (int i = 1; i < n; i++) if (x[i] > bv) { bv = x[i]; best = i; }
return best;
}
} // namespace dflash::common