40 std::ifstream ifs =
File::Get(path_name,
false);
43 log_err(
"csv文件加载失败:{}", path_name);
58 std::vector<std::string> line;
102 line.push_back(temp);
108 line.push_back(temp);
125 ret = std::move(all);
132void Serialize(std::string_view path_name, std::string_view out)
135 if (!
Read(path_name, mul_str))
160 std::byte* p =
nullptr;
161 std::size_t line_size = 0;
167 std::vector<TypeFlag> vec_flag;
174 log_err(
"=标签查找失败:{}", path_name);
180 std::span<std::string> vec_str = mul_str[pos[1]];
183 std::size_t max_size = 1;
185 for (std::size_t i = pos[0]; i < vec_str.size(); ++i)
187 if (vec_str[i] ==
"uint8_t")
189 vec_flag.push_back({ TypeFlag::N8, 0 });
190 line_size +=
sizeof(uint8_t);
192 else if (vec_str[i] ==
"uint16_t")
194 vec_flag.push_back({ TypeFlag::N16, 0 });
195 line_size +=
sizeof(uint16_t);
197 else if (vec_str[i] ==
"uint32_t")
199 vec_flag.push_back({ TypeFlag::N32, 0 });
200 line_size +=
sizeof(uint32_t);
202 else if (vec_str[i] ==
"int32_t")
204 vec_flag.push_back({ TypeFlag::Z32, 0 });
205 line_size +=
sizeof(int32_t);
207 else if (vec_str[i] ==
"float")
209 if (
sizeof(
float) > max_size)
210 max_size =
sizeof(
float);
212 std::size_t start = line_size ?
Math::Align(line_size,
alignof(
float)) : 0;
213 if (start != line_size) vec_flag.push_back({ TypeFlag::ALIGN, start - line_size });
215 vec_flag.push_back({ TypeFlag::FLOAT, 0 });
216 line_size +=
sizeof(float);
218 else if (vec_str[i] ==
"double")
220 if (
sizeof(
double) > max_size)
221 max_size =
sizeof(
double);
223 std::size_t start = line_size ?
Math::Align(line_size,
alignof(
double)) : 0;
224 if (start != line_size) vec_flag.push_back({ TypeFlag::ALIGN, start - line_size });
226 vec_flag.push_back({ TypeFlag::DOUBLE, 0 });
227 line_size +=
sizeof(double);
231 size_t pos = vec_str[i].find(
"str");
232 if (pos != std::string::npos)
234 if (
sizeof(
char) > max_size)
235 max_size =
sizeof(
char);
237 size_t start = line_size ?
Math::Align(line_size,
alignof(
char)) : 0;
238 if (start != line_size) vec_flag.push_back({ TypeFlag::ALIGN, start - line_size });
241 size_t str_length = std::stoi(std::string(vec_str[i].begin() + 3, vec_str[i].end()));
244 log_err(
"字符串必须指定长度,例如str8:{}", path_name);
247 vec_flag.push_back({ TypeFlag::STRING, str_length });
248 line_size +=
sizeof(char) * str_length;
252 vec_flag.push_back({ TypeFlag::NONE, 0 });
254 if (!vec_str[i].empty() && vec_str[i] !=
"=")
255 log_warn(
"未知类型标签:{},{}", path_name, vec_str[i]);
262 log_err(
"数据大小为0:{}", path_name );
267 std::size_t start = line_size ?
Math::Align(line_size, max_size) : 0;
270 p = (std::byte*)malloc(line_size);
282 ptrdiff_t w = pos[0] + count_if(vec_flag.begin(), vec_flag.end(), [](
const TypeFlag& flag)
284 return flag._type != TypeFlag::ALIGN;
288 for (
size_t h = pos[1] + 1; h < mul_str.
GetHeight(); ++h)
290 std::span<std::string> vec_str = mul_str[h];
292 if (vec_str[pos[0]] !=
"+")
301 log_err(
"类型标签未成功读取:{}", path_name);
305 if (vec_str.size() != w)
308 for (
auto& iter : vec_str)
313 log_err(
"此行元素个数不匹配:{},{}", path_name, str_all);
319 for (
size_t i = 0; i < vec_flag.size(); ++i)
321 if (vec_flag[i]._type == TypeFlag::ALIGN)
323 offset += vec_flag[i]._size;
327#define DEF_SERIALIZE_CSV_LINE(flag, type, func)\
328if (vec_flag[i]._type == TypeFlag::flag)\
331if (!vec_str[j].empty())\
333 if(Sub::IsBegin(vec_str[j], std::string("0x")))\
334 v = func(vec_str[j], nullptr, 16);\
336 v = func(vec_str[j]);\
338*(type*)(p + offset) = v;\
339offset += sizeof(type);\
344#define DEF_SERIALIZE_CSV_LINE_F(flag, type, func)\
345if (vec_flag[i]._type == TypeFlag::flag)\
347*(type*)(p + offset) = (type)(vec_str[j].empty() ? 0 : func(vec_str[j]));\
348offset += sizeof(type);\
363 if (vec_flag[i]._type == TypeFlag::STRING)
365 if (vec_str[j].empty())
366 *(
char*)(p + offset) = 0;
371 offset +=
sizeof(char) * vec_flag[i]._size;
378 if (line_size != offset)
380 log_err(
"单行写入大小不匹配:{}", path_name );
383 s.
Write(p, line_size);
393template<
typename T,
typename F>
404 log_err(
"CSV二进制文件错误:{}", path_name);
409 std::size_t size = 0;
411 if (size !=
sizeof(
T))
413 log_err(
"CSV二进制文件不匹配POD类型大小{},{}:{}", size,
sizeof(
T), path_name);
427void Deserialize(std::string_view path_name, std::vector<T>& container)
429 Deserialize<T>(path_name, [&](
T& line)
431 container.push_back(line);
bool Save(std::string_view path_name, bool compress=false)
保存到文件
void Write64(const T *data)
写入一个类型以64位,对于在64位平台的大小不一致,所以特殊处理
void Write(const T *data)
写入一个对象
unsigned GetHeight() const
void PushBack(std::vector< T > &vec)
插入行
#define DEF_SERIALIZE_CSV_LINE_F(flag, type, func)
#define DEF_SERIALIZE_CSV_LINE(flag, type, func)
#define log_warn(str,...)
void Copy(char *target, size_t size, std::string_view source, bool show_log=true)
utf-8的安全复制
bool Read(std::string_view path_name, Vector2< std::string > &ret)
读取一个CSV文件,拆解为每行(换行使用 替代,所有/变为\)
void Deserialize(std::string_view path_name, F fn)
反序列化 序列化的Csv文件到 对象
void Serialize(std::string_view path_name, std::string_view out)
序列化 逗号分隔文件
constexpr uint32_t CSV_FILE_TYPE
std::ifstream Get(std::string_view path_name, bool is_binary=true)
size_t FirstIndex(const T &container, const V &v)
find,返回下标
constexpr T Align(T x, T align)
对齐align的倍数
std::array< std::size_t, 2 > Index2
void log_err0(std::string_view str, const std::source_location location=std::source_location::current())