OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
PathsAndFiles.hpp
Go to the documentation of this file.
1//
2// Created by jstark on 2019-12-03.
3//
13#ifndef FILES_READ_AND_WRITE_PATHSANDFILES_HPP
14#define FILES_READ_AND_WRITE_PATHSANDFILES_HPP
15#include <unistd.h>
16#include <iostream>
17#include <fstream>
18#include <boost/filesystem.hpp>
19#include "VCluster/VCluster.hpp"
24static std::string get_cwd()
25{
26 char *cwd = nullptr;
27 size_t size;
28 cwd = getcwd(cwd, size);
29
30 // convert current directory to string
31 std::string s_cwd;
32 s_cwd = cwd;
33
34 // std::cout << "The current working directory is: " << dir << std::endl;
35 return s_cwd;
36}
42static bool check_if_file_exists(std::string path)
43{
44 try
45 {
46 // Create a filesystem::path object from given path string
47 boost::filesystem::path pathObj(path);
48 if (boost::filesystem::exists(pathObj) && boost::filesystem::is_regular_file(pathObj))
49 {
50 return true;
51// BOOST_LOG_TRIVIAL(info) << path << " -> File exists.";
52 }
53
54 }
55 catch (boost::filesystem::filesystem_error & e)
56 {
57 std::cerr << e.what() << std::endl;
58// BOOST_LOG_TRIVIAL(error) << "Error when checking existence of file ( " << path << " ): " << e.what();
59 }
60 return false;
61}
66static void create_file_if_not_exist(std::string path)
67{
68 auto & v_cl = create_vcluster();
69 if (v_cl.rank() == 0)
70 {
71 if ( ! check_if_file_exists(path))
72 {
73 std::ofstream outfile (path);
74// BOOST_LOG_TRIVIAL(info) << "Created file with name: " << path;
75 }
76 }
77}
78
79
85static bool check_if_directory_exists(std::string path)
86{
87 try
88 {
89 // Create a filesystem::path object from given path string
90 boost::filesystem::path pathObj(path);
91 if (boost::filesystem::exists(pathObj) && boost::filesystem::is_directory(pathObj)) return true;
92 }
93 catch (boost::filesystem::filesystem_error & e)
94 {
95 std::cerr << e.what() << std::endl;
96 }
97 return false;
98}
103static void create_directory_if_not_exist(std::string path,bool silent=0)
104{
105 auto & v_cl = create_vcluster();
106 if (v_cl.rank() == 0)
107 {
108 if ( ! check_if_directory_exists(path))
109 {
110 boost::filesystem::create_directory(path);
111// BOOST_LOG_TRIVIAL(info) << "Created directory with name: " << path;
112 }
113 else {
114 if(!silent){
115 std::cout << "Folder for current settings ( '" << path << "' ) already exists. New files will be saved to this folder." << std::endl;
116 }
117 }
118// BOOST_LOG_TRIVIAL(info) << "Folder for current settings ( '" << path << "' ) already exists. New files will be saved to this folder.";
119 }
120}
121
122
123
124
125
126#endif //FILES_READ_AND_WRITE_PATHSANDFILES_HPP
127
static bool check_if_directory_exists(std::string path)
Checks if a directory already exists.
static bool check_if_file_exists(std::string path)
Checks if a file already exists.
static void create_file_if_not_exist(std::string path)
Creates a file if not already existent.
static void create_directory_if_not_exist(std::string path, bool silent=0)
Creates a directory if not already existent.
static std::string get_cwd()
Gets the current working directory and returns path as string.