|
|
// io_utils.h - 通用 IO 工具:时间/目录/日志清理/GBK转码/文件保存
|
|
|
#ifndef IO_UTILS_H
|
|
|
#define IO_UTILS_H
|
|
|
|
|
|
#include <string>
|
|
|
#include <vector>
|
|
|
#include <cstdint>
|
|
|
|
|
|
// 去除字符串首尾空白(空格/tab/回车/换行)
|
|
|
std::string trim_str(const std::string& s);
|
|
|
|
|
|
// GBK(ANSI) → UTF-8。注意:仅用于 -fexec-charset=GBK 的旧构建;
|
|
|
// 当前工程用 -fexec-charset=UTF-8,字符串本身已是 UTF-8,写文件时不要再调用它。
|
|
|
std::string gbk_to_utf8(const std::string& s);
|
|
|
|
|
|
// 把标准输出(std::cout/std::cerr)镜像到当天日志文件 data/YYYYMMDD/run.log:
|
|
|
// - 程序启动时调用 start_run_log(),退出前调用 stop_run_log()
|
|
|
// - 文件按行缓冲写入(UTF-8),跨天自动切换到新日期目录
|
|
|
// - 目录随 data/日期 一起被 clean_expired_log_dirs() 删除
|
|
|
void start_run_log();
|
|
|
void stop_run_log();
|
|
|
|
|
|
// 递归创建目录(支持 "a/b/c",已存在则返回 true)
|
|
|
bool make_dirs(const std::string& path);
|
|
|
|
|
|
// 创建本次任务输出目录并返回完整路径: data/日期/时间_taskId
|
|
|
// taskId 为空时返回 data/日期/时间_ (调用方需自行创建 data 根目录)
|
|
|
std::string make_task_dir(const std::string& task_id);
|
|
|
|
|
|
// 按配置 cfg.log.retain_days 清理 data/日期 下的过期日期目录(保留最近 N 天)
|
|
|
// 在程序启动与每日 log.clean_time 时刻调用
|
|
|
void clean_expired_log_dirs();
|
|
|
|
|
|
// 保存 ASCII PCD 点云(一次性构建字符串写入,避免逐行IO)
|
|
|
void save_pcd_ascii(const std::string& filename,
|
|
|
const std::vector<double>& xs,
|
|
|
const std::vector<double>& ys,
|
|
|
const std::vector<double>& zs);
|
|
|
|
|
|
// 保存灰度 JPG(灰度转RGB后压缩为JPEG,quality=90)
|
|
|
bool save_jpg_gray(const std::string& filename, int width, int height,
|
|
|
const std::vector<uint8_t>& pixels, int quality = 90);
|
|
|
|
|
|
#endif // IO_UTILS_H
|