3737 #include < unistd.h>
3838#endif
3939
40- namespace DotNameUtils {
40+ namespace dotnamecpp {
41+ namespace utils {
4142
4243 // Core filesystem operations
4344 namespace fs {
44- namespace io {
45- inline std::string readFile (const std::filesystem::path& filePath) {
46- std::ifstream file (filePath, std::ios::in);
47- if (!file.is_open ()) {
48- throw std::ios_base::failure (" Failed to open file: " + filePath.string ());
49- }
50- std::stringstream buffer;
51- buffer << file.rdbuf ();
52- return buffer.str ();
45+ inline std::string readFile (const std::filesystem::path& filePath) {
46+ std::ifstream file (filePath, std::ios::in);
47+ if (!file.is_open ()) {
48+ throw std::ios_base::failure (" Failed to open file: " + filePath.string ());
5349 }
50+ std::stringstream buffer;
51+ buffer << file.rdbuf ();
52+ return buffer.str ();
53+ }
5454
55- inline void writeFile (const std::filesystem::path& filePath, const std::string& content) {
56- std::ofstream file (filePath, std::ios::out | std::ios::trunc);
57- if (!file.is_open ()) {
58- throw std::ios_base::failure (" Failed to open file: " + filePath.string ());
59- }
60- file << content;
55+ inline void writeFile (const std::filesystem::path& filePath, const std::string& content) {
56+ std::ofstream file (filePath, std::ios::out | std::ios::trunc);
57+ if (!file.is_open ()) {
58+ throw std::ios_base::failure (" Failed to open file: " + filePath.string ());
6159 }
62- } // namespace io
60+ file << content;
61+ }
6362
64- namespace path {
65- inline std::filesystem::path getParentPath (const std::filesystem::path& filePath) {
66- return filePath.parent_path ();
67- }
63+ inline std::filesystem::path getParentPath (const std::filesystem::path& filePath) {
64+ return filePath.parent_path ();
65+ }
6866
69- inline std::string getFileName (const std::filesystem::path& filePath) {
70- return filePath.filename ().string ();
71- }
67+ inline std::string getFileName (const std::filesystem::path& filePath) {
68+ return filePath.filename ().string ();
69+ }
7270
73- inline bool fileExists (const std::filesystem::path& filePath) {
74- return std::filesystem::exists (filePath);
75- }
71+ inline bool fileExists (const std::filesystem::path& filePath) {
72+ return std::filesystem::exists (filePath);
73+ }
7674
77- inline std::filesystem::path getStandalonePath () {
78- std::filesystem::path path;
75+ inline std::filesystem::path getExecutablePath () {
76+ std::filesystem::path path;
7977
8078#ifdef _WIN32
81- // C-Like is intended here for cross-platform compatibility
82- char buffer[MAX_PATH];
83- GetModuleFileNameA (NULL , buffer, MAX_PATH);
84- path = buffer;
79+ // C-Like is intended here for cross-platform compatibility
80+ char buffer[MAX_PATH];
81+ GetModuleFileNameA (NULL , buffer, MAX_PATH);
82+ path = buffer;
8583#elif defined(__APPLE__)
86- // C-Like is intended here for cross-platform compatibility
87- char buffer[PATH_MAX];
88- uint32_t bufferSize = PATH_MAX;
89- if (_NSGetExecutablePath (buffer, &bufferSize) == 0 ) {
90- path = buffer;
91- }
84+ // C-Like is intended here for cross-platform compatibility
85+ char buffer[PATH_MAX];
86+ uint32_t bufferSize = PATH_MAX;
87+ if (_NSGetExecutablePath (buffer, &bufferSize) == 0 ) {
88+ path = buffer;
89+ }
9290#elif defined(__EMSCRIPTEN__)
93- // Emscripten doesn't have readlink, use current directory
94- path = std::filesystem::current_path ().string ();
91+ // Emscripten doesn't have readlink, use current directory
92+ path = std::filesystem::current_path ().string ();
9593#elif defined(__linux__)
96- // C-Like is intended here for cross-platform compatibility
97- char buffer[4096 ];
98- ssize_t len = readlink (" /proc/self/exe" , buffer, sizeof (buffer) - 1 );
99- if (len != -1 ) {
100- buffer[len] = ' \0 ' ;
101- path = buffer;
102- }
103- #endif
104- return path;
94+ // C-Like is intended here for cross-platform compatibility
95+ char buffer[4096 ];
96+ ssize_t len = readlink (" /proc/self/exe" , buffer, sizeof (buffer) - 1 );
97+ if (len != -1 ) {
98+ buffer[len] = ' \0 ' ;
99+ path = buffer;
105100 }
106- } // namespace path
101+ #endif
102+ return path;
103+ }
107104
108- namespace mgmt {
109- inline void createDirectory (const std::filesystem::path& dirPath) {
110- if (!std::filesystem::exists (dirPath)) {
111- std::filesystem::create_directories (dirPath);
112- }
105+ inline void createDirectory (const std::filesystem::path& dirPath) {
106+ if (!std::filesystem::exists (dirPath)) {
107+ std::filesystem::create_directories (dirPath);
113108 }
109+ }
114110
115- inline void remove (const std::filesystem::path& path) {
116- if (std::filesystem::exists (path)) {
117- std::filesystem::remove_all (path);
118- }
111+ inline void remove (const std::filesystem::path& path) {
112+ if (std::filesystem::exists (path)) {
113+ std::filesystem::remove_all (path);
119114 }
115+ }
120116
121- inline std::vector<std::filesystem::path> listFiles (const std::filesystem::path& dirPath) {
122- std::vector<std::filesystem::path> files;
123- for (const auto & entry : std::filesystem::directory_iterator (dirPath)) {
124- files.push_back (entry.path ());
125- }
126- return files;
117+ inline std::vector<std::filesystem::path> listFiles (const std::filesystem::path& dirPath) {
118+ std::vector<std::filesystem::path> files;
119+ for (const auto & entry : std::filesystem::directory_iterator (dirPath)) {
120+ files.push_back (entry.path ());
127121 }
128- } // namespace mgmt
122+ return files;
123+ }
129124 } // namespace fs
130125
131126 // String utilities
132- namespace str {
127+ namespace string {
133128 inline std::string addDots (const std::string& str) {
134129 std::string result;
135130 for (size_t i = 0 ; i < str.length (); ++i) {
@@ -150,36 +145,36 @@ namespace DotNameUtils {
150145 }
151146 return result;
152147 }
153- } // namespace str
148+ } // namespace string
154149
155150 // JSON utilities
156151 namespace json {
157- // Load JSON from file
158- inline nlohmann::json loadFromFile (const std::filesystem::path& filePath) {
159- if (!fs::path ::fileExists (filePath)) {
160- throw std::ios_base::failure (" JSON file does not exist: " + filePath.string ());
161- }
152+ // Load JSON from file
153+ inline nlohmann::json loadFromFile (const std::filesystem::path& filePath) {
154+ if (!utils::fs ::fileExists (filePath)) {
155+ throw std::ios_base::failure (" JSON file does not exist: " + filePath.string ());
156+ }
162157
163- try {
164- std::string content = fs::io::readFile (filePath);
165- return nlohmann::json::parse (content);
166- } catch (const nlohmann::json::parse_error& e) {
167- throw std::runtime_error (
168- " JSON parse error in file " + filePath.string () + " : " + e.what ());
158+ try {
159+ std::string content = utils::fs::readFile (filePath);
160+ return nlohmann::json::parse (content);
161+ } catch (const nlohmann::json::parse_error& e) {
162+ throw std::runtime_error (
163+ " JSON parse error in file " + filePath.string () + " : " + e.what ());
164+ }
169165 }
170- }
171166
172- // Save JSON to file
173- inline void saveToFile (const std::filesystem::path& filePath, const nlohmann::json& jsonData,
174- int indent = 2 ) {
175- try {
176- std::string jsonString = jsonData.dump (indent);
177- fs::io::writeFile (filePath, jsonString);
178- } catch (const std::exception& e) {
179- throw std::runtime_error (
180- " Failed to save JSON to file " + filePath.string () + " : " + e.what ());
167+ // Save JSON to file
168+ inline void saveToFile (const std::filesystem::path& filePath, const nlohmann::json& jsonData,
169+ int indent = 2 ) {
170+ try {
171+ std::string jsonString = jsonData.dump (indent);
172+ utils::fs::writeFile (filePath, jsonString);
173+ } catch (const std::exception& e) {
174+ throw std::runtime_error (
175+ " Failed to save JSON to file " + filePath.string () + " : " + e.what ());
176+ }
181177 }
182- }
183178
184179 // Get value with default fallback
185180 template <typename T>
@@ -362,6 +357,8 @@ namespace DotNameUtils {
362357 }
363358
364359 } // namespace json
365- } // namespace DotNameUtils
360+
361+ } // namespace utils
362+ } // namespace dotnamecpp
366363
367364// MIT License Copyright (c) 2024-2025 Tomáš Mark
0 commit comments