KIDS  ver-0.0.1
KIDS : Kernel Integrated Dynamics Simulator
Loading...
Searching...
No Matches
io_utils.h
Go to the documentation of this file.
1#ifndef IO_UTILS_H
2#define IO_UTILS_H
3#include <fstream>
4#include <iomanip>
5#include <map>
6#include <sstream>
7
8#include "commonflags.h"
9
10#define COLOR_R std::string("\033[31m") // red color
11#define COLOR_G std::string("\033[32m") // green color
12#define COLOR_B std::string("\033[34m") // blue color
13#define COLOR_Y std::string("\033[33m") // yellow color
14#define COLOR_E std::string("\033[0m") // end close
15
16constexpr int FMT_WIDTH_SIZE(int X) { return X + 9; }
17#define FMT(X) " " << std::setiosflags(std::ios::scientific) << std::setprecision(X) << std::setw(FMT_WIDTH_SIZE(X))
18
19namespace utils {
20
21inline int removeFile(std::string& filename) { return remove(filename.c_str()); }
22
23inline void clearFile(std::string& filename) { std::ofstream clear(filename, std::ios::trunc); }
24
25inline void closeOFS(std::ofstream& ofs) {
26 if (ofs.is_open()) ofs.close();
27}
28
29inline bool isFileExists(const std::string& name) { return std::ifstream{name.c_str()}.good(); }
30
31#ifdef _WIN32
32#define DELIM_SEP '\\'
33#else
34#define DELIM_SEP '/'
35#endif
36
37inline std::string ParseFilePath(const std::string& s) {
38 std::string::size_type ipos = s.find_last_of(DELIM_SEP);
39 return s.substr(0, ipos);
40}
41
42inline std::string ParseFileName(const std::string& s) {
43 std::string::size_type ipos = s.find_last_of(DELIM_SEP) + 1;
44 std::string filename = s.substr(ipos, s.length() - ipos);
45 return filename.substr(0, filename.rfind("."));
46}
47
48inline std::string ParseFileExtension(const std::string& s) {
49 std::string::size_type ipos = s.find_last_of(DELIM_SEP) + 1;
50 std::string filename = s.substr(ipos, s.length() - ipos);
51 std::string::size_type kpos = filename.rfind(".") + 1;
52 return filename.substr(kpos, filename.length() - kpos);
53}
54
55inline int copyfile_from_to(const std::string& from, const std::string& to) {
56 if (from == to) return 0;
57 std::ifstream in(from, std::ios_base::in | std::ios_base::binary);
58 std::ofstream out(to, std::ios_base::out | std::ios_base::binary);
59
60 const static int BUF_SIZE = 4096;
61 char buf[BUF_SIZE];
62
63 do {
64 in.read(&buf[0], BUF_SIZE);
65 out.write(&buf[0], in.gcount());
66 } while (in.gcount() > 0);
67 in.close();
68 out.close();
69 return 0;
70}
71
72}; // namespace utils
73
74namespace global {
75
76extern int* p_argc;
77extern char*** p_argv;
78
79namespace OFS {
81const std::map<std::string, _enum> _dict = {
82 {"ENER", ENER}, {"SAMP", SAMP}, {"TRAJ", TRAJ}, {"ESAMP", ESAMP}, {"ETRAJ", ETRAJ}, {"CORR", CORR},
83};
84extern std::map<_enum, bool> _isopen;
85}; // namespace OFS
86
87// extern std::map<OFS_ENUM, bool> OFS_IS_OPEN_MAP;
88
89int parse_ostream(const std::string& str);
90
91inline bool ofs_is_open(const global::OFS::_enum& test) {
92 return (global::OFS::_isopen.find(test) != global::OFS::_isopen.end());
93}
94
95inline int del_ostream() { return 0; }
96
97}; // namespace global
98
99
100#endif // IO_UTILS_H
#define DELIM_SEP
Definition io_utils.h:34
constexpr int FMT_WIDTH_SIZE(int X)
Definition io_utils.h:16
const std::map< std::string, _enum > _dict
Definition io_utils.h:81
std::map< _enum, bool > _isopen
int del_ostream()
Definition io_utils.h:95
char *** p_argv
int parse_ostream(const std::string &str)
bool ofs_is_open(const global::OFS::_enum &test)
Definition io_utils.h:91
int * p_argc
Definition concat.h:18
void closeOFS(std::ofstream &ofs)
Definition io_utils.h:25
void clearFile(std::string &filename)
Definition io_utils.h:23
std::string ParseFilePath(const std::string &s)
Definition io_utils.h:37
std::string ParseFileName(const std::string &s)
Definition io_utils.h:42
bool isFileExists(const std::string &name)
Definition io_utils.h:29
std::string ParseFileExtension(const std::string &s)
Definition io_utils.h:48
int removeFile(std::string &filename)
Definition io_utils.h:21
int copyfile_from_to(const std::string &from, const std::string &to)
Definition io_utils.h:55