AFLOW
 
Loading...
Searching...
No Matches
aurostd_xfile.h
Go to the documentation of this file.
1// ***************************************************************************
2// * *
3// * Aflow STEFANO CURTAROLO - Duke University 2003-2024 *
4// * *
5// ***************************************************************************
6
7#ifndef _AUROSTD_XFILE_H_
8#define _AUROSTD_XFILE_H_
9
10#include <cstddef>
11#include <deque>
12#include <filesystem>
13#include <fstream>
14#include <regex>
15#include <sstream>
16#include <string>
17#include <vector>
18
19#include <archive.h>
20
21namespace aurostd {
22 namespace fs = std::filesystem;
23
24 // compresion helper //HE20241216
26 const std::vector<std::string> compression_suffix = {"", ".xz", ".zip", ".bz2", ".gz", ".zst"};
27 compression_type GetCompressionType(const fs::path& path);
28
29 // make directories
30 bool DirectoryMake(const std::string& directory_raw);
31 bool SSH_DirectoryMake(const std::string& user, std::string machine, std::string Directory);
32
33 // list directory content
34 bool SubDirectoryLS(const std::string& directory_raw, std::vector<std::string>& vsubd); // CO20200731
35 bool DirectoryLS(const std::string& directory_raw, std::vector<std::string>& vfiles);
36 bool DirectoryLS(const std::string& directory_raw, std::deque<std::string>& vfiles);
37
38 // filename helper
39 std::string dirname(const std::string& file); // CO20210315
40 std::string basename(const std::string& file); // CO20210315
41 std::string GetFileExtension(const std::string& FileName);
42 std::string CleanFileName(const std::string& fileIN);
43
44 // aflow directory status
45 bool DirectoryLocked(const std::string& directory, const std::string& LOCK);
46 bool DirectoryLocked(const std::string& directory);
47 bool DirectorySkipped(const std::string& directory);
48
49 // general file/directory status
50 bool DirectoryWritable(const std::string& directory_raw);
51 bool IsDirectory(const std::string& path);
52 bool IsCompressed(const std::string& FileNameIn, std::string& FileNameOut);
53 bool IsCompressed(const std::string& FileNameIn);
54 bool FileExist(const std::string& FileName);
55 bool FileExist(const std::string& FileName, std::string& FileNameOut);
56 bool CompressFileExist(const std::string& FileName);
57 bool CompressFileExist(const std::string& FileNameRaw, std::string& FileNameOut);
58 bool CompressFileWithinList(const std::vector<std::string>& list, const std::string& input);
59 bool CompressFileWithinList(const std::vector<std::string>& list, const std::string& input, std::string& output);
60 bool FileEmpty(const std::string& FileNameRaw);
61 bool FileNotEmpty(const std::string& FileName);
62 long int GetTimestampModified(const std::string&); // ME20180712
63 long int SecondsSinceFileModified(const std::string&); // CO20210315
64 unsigned long long int FileSize(const std::string& FileName); // ME20191001
65 void InFileExistCheck(const std::string& routine, const std::string& FileNameRaw, const std::ifstream& file_to_check);
66
67 // temporary files
68 std::string TmpStrCreate(const std::string& identifier_raw = "", const std::string& tmpdir_raw = "", bool hidden = false, bool directory = false); // CO20210624
69 std::string TmpFileCreate(const std::string& identifier = "", const std::string& tmpdir = "", bool hidden = false); // CO20210315 - empty tmpdir means use XHOST.tmpfs
70 std::string TmpDirectoryCreate(const std::string& identifier = "", const std::string& tmpdir = "", bool hidden = false); // CO20210315 - empty tmpdir means use XHOST.tmpfs
71
72 // copy link move
73 bool CopyFile(const std::string& file_from, const std::string& file_to);
74 bool LinkFile(const std::string& file_from, const std::string& file_to, bool soft = true);
75 bool file2directory(const std::string& file, const std::string& destination);
76 bool file2directory(const std::vector<std::string>& files, const std::string& destination);
77 bool file2file(const std::string& file, const std::string& destination); // CO20171025
78
79 // delete
80 bool RemoveFile(const std::string& file);
81 bool RemoveFile(const std::string& directory, const std::regex& regex_pattern);
82 bool RemoveFiles(const std::string& directory);
83 bool RemoveFile(const std::vector<std::string>& files);
84 bool RemoveDirectory(const std::string& directory);
85
86 // read file to std::string
87 std::string file2string(const std::string& FileNameIN);
88 size_t file2string(const std::string& FileNameIN, std::string& StringIN);
89 std::string compressfile2string(const std::string& FileNameIN);
90 size_t compressfile2string(const std::string& FileNameIN, std::string& content);
91
92 // read file to vector of std::string
93 size_t file2vectorstring(const std::string& FileNameIN, std::vector<std::string>& vline, bool consecutive = false, bool trim_edges = false);
94 size_t file2vectorstring(const std::string& FileNameIN, std::deque<std::string>& vline);
95 size_t compressfile2vectorstring(const std::string& FileNameIN, std::vector<std::string>& vline, bool consecutive = false, bool trim_edges = false);
96 size_t compressfile2vectorstring(const std::string& FileNameIN, std::deque<std::string>& vline);
97
98 // read file into a stringstream
99 void file2stringstream(const std::string& FileNameIN, std::stringstream& StringstreamIN);
100 void compressfile2stringstream(const std::string& file_raw, std::stringstream& content);
101
102 // write files
103 bool string2file(const std::string& StringOUTPUT, const std::string& FileNameRawOUTPUT, compression_type ct = None, const std::string& mode = "");
104 bool string2compressfile(const std::string& content, const std::string& file_raw, compression_type ct = XZ);
105 bool stringstream2file(const std::stringstream& StringstreamOUTPUT, const std::string& FileNameOUTPUT, compression_type ct = None, const std::string& mode = "");
106 bool stringstream2compressfile(const std::stringstream& content, const std::string& file, compression_type ct = XZ);
107 template <template <class...> class vtype> bool vectorstring2file(const vtype<std::string>& vline, const std::string& FileNameOUT, compression_type ct = None);
108
109 // compression helpers
110
111 void CompressInit(archive* a, compression_type ct, bool single);
112 void DecompressFile(const std::string& file_raw, const std::string& outfile_raw = "", bool keep = false);
113 void DecompressFile(const std::string& file_raw, bool keep);
114 void DecompressFiles(const std::string& archive_file, const std::string& directory = "", bool keep = false);
115 void DecompressFiles(const std::string& archive_file, bool keep);
116 void CompressFile(const std::string& file_raw, compression_type ct = XZ, bool keep = false); // with default
117 fs::path CompressFiles(const std::vector<std::string>& files, const fs::path& relative_to_path, const std::string& archive_name, compression_type ct = XZ, bool keep = false);
118 bool MatchCompressed(const std::string& CompressedFileName, const std::string& FileNameOUT);
119 bool compressfile2tempfile(const std::string& FileNameIN, std::string& FileNameOUT);
120 bool compressfile2tempfile(const std::string& FileNameRawIN, std::string& FileNameOUT, bool& tempfile_created);
121 void compressfile2compressfile(const std::string& file, compression_type ct, bool keep = false);
122 void compressfiles2compressfiles(const std::string& directory, compression_type ct, bool keep = false);
123
124 namespace JSON { // forward declaration //HE20241216
125 struct object;
126 }
127 // Helper functions to retrieve embedded files //HE20230413
128 namespace EmbData {
130 std::string get_test_file(std::string file);
131 std::string get_content(const std::string& filename, const std::string& collection);
132 void save_to_file(const std::string& filename, const std::string& collection, const std::string& target_path);
133 void save_to_folder(const std::string& path, const std::string& collection, const std::string& target_path);
134 } // namespace EmbData
135} // namespace aurostd
136#endif //_AUROSTD_XFILE_H_
void save_to_file(const std::string &filename, const std::string &collection, const std::string &target_path)
write a embedded file into the filesystem
std::string get_content(const std::string &filename, const std::string &collection)
get the content of an embedded file
string get_test_file(string file)
return data for unittests
void save_to_folder(const std::string &path, const std::string &collection, const std::string &target_path)
write a embedded file into the filesystem
aurostd::JSON::object get_unit_test()
return data for unittests
unified namespace to read and write JSON
string basename(const string &file)
returns the basename of file
bool string2file(const string &StringOUTPUT, const std::string &FileNameRawOUTPUT, const compression_type ct, const string &mode)
write string into a file
void CompressFile(const string &file_raw, const compression_type ct, const bool keep)
compress file
bool vectorstring2file(const vtype< string > &vline, const string &FileNameOUT, const compression_type ct)
write a list of strings to file
bool DirectoryLS(const string &directory_raw, vector< string > &vfiles)
returns the content of a directory
bool RemoveDirectory(const string &directory)
remove a file
unsigned long long int FileSize(const string &FileName)
returns in bytes the size of a file
bool RemoveFiles(const string &directory)
removes all regular files inside a directory, not recursive
bool file2directory(const string &file, const string &destination)
move file to directory
string TmpFileCreate(const string &identifier, const string &tmpdir, const bool hidden)
create a string pointing to the location of a unique temp file
long int SecondsSinceFileModified(const string &FileNameRaw)
gets duration since file modification
long int GetTimestampModified(const string &FileNameRaw)
gets modification time
bool stringstream2compressfile(const stringstream &content, const string &file, const compression_type ct)
stringstream to compressed file
const std::vector< std::string > compression_suffix
void compressfiles2compressfiles(const string &directory, const compression_type ct, const bool keep)
convert compressed files in a directory to another compression
string TmpStrCreate(const string &identifier_raw, const string &tmpdir_raw, const bool hidden, const bool directory)
create a string pointing to the location of a unique temp file or directory
void compressfile2compressfile(const string &file, const compression_type ct, const bool keep)
convert a compressed file to another compression
bool CompressFileExist(const string &FileName)
check if a file or its compressed variant exits
string CleanFileName(const string &fileIN)
cleans file names from obvious things
size_t file2string(const string &FileNameIN, string &StringIN)
read the content of a file into a string
void InFileExistCheck(const string &routine, const string &FileNameRaw, const ifstream &file_to_check)
checks if a filestream is empty
utype mode(const xvector< utype > &a)
bool string2compressfile(const string &content, const string &file_raw, const compression_type ct)
string to compressed file
bool DirectoryMake(const string &directory_raw)
create a directory
size_t compressfile2vectorstring(const string &FileNameIN, vector< string > &vline, bool consecutive, bool trim_edges)
write the content of a compressed file into a vector of strings
bool FileExist(const string &FileName)
check if file exists
void compressfile2stringstream(const string &file_raw, stringstream &content)
write the content of a compressed file into a stringstream
bool FileEmpty(const string &FileNameRaw)
check if file is empty
string TmpDirectoryCreate(const string &identifier, const string &tmpdir, const bool hidden)
create a string pointing to the location of a unique temp directory
bool FileNotEmpty(const string &FileName)
check if file is not empty
string dirname(const string &file)
returns the dirname of file
size_t compressfile2string(const string &FileNameIN, string &content)
compressed file to string
bool SSH_DirectoryMake(const string &user, const string &machine, const string &directory_raw)
create a directory on a remote computer
bool CopyFile(const string &file_from, const string &file_to)
copy a file
string GetFileExtension(const string &FileName)
returns the file extension
bool CompressFileWithinList(const vector< string > &list, const string &input)
Determines whether a compressed file is within the list.
bool MatchCompressed(const string &CompressedFileName, const string &FileNameOUT)
compress a file based on the compression of another filed
bool IsCompressed(const string &FileNameIn)
check if a file is compressed
void DecompressFiles(const string &archive_file, const string &directory, const bool keep)
decompress files
bool IsDirectory(const string &path)
check if path is a directory
bool SubDirectoryLS(const string &directory_raw, vector< string > &vsubd)
returns all the subdirectories of a directory, including itself
bool LinkFile(const string &file_from, const string &file_to, const bool soft)
link a file
bool DirectoryWritable(const string &directory_raw)
create a temporary file to check if a directory is writable
bool file2file(const string &file, const string &destination)
move file to another file
void DecompressFile(const string &file_raw, const string &outfile_raw, const bool keep)
decompress file
bool stringstream2file(const stringstream &StringstreamOUTPUT, const string &FileNameOUTPUT, const compression_type ct, const string &mode)
write stringstream into a file
fs::path CompressFiles(const vector< string > &files_raw, const fs::path &relative_to_path, const string &archive_name, const compression_type ct, const bool keep)
compress files
bool DirectorySkipped(const string &directory)
check the existence of a SKIP file
bool DirectoryLocked(const string &directory, const string &LOCK)
check the existence of a LOCK file
bool RemoveFile(const string &file)
remove a file
size_t file2vectorstring(const string &FileNameIN, vector< string > &vline, bool consecutive, bool trim_edges)
write the content of a file into a vector of strings
compression_type GetCompressionType(const fs::path &path)
bool compressfile2tempfile(const string &FileNameIN, string &FileNameOUT)
decompress a file to a temp file
void file2stringstream(const string &FileNameIN, stringstream &StringstreamIN)
write the content of a file into a stringstream 20241216 | Hagen Eckert - add transparent compression...
void CompressInit(archive *a, const compression_type ct, const bool single)
initialize compression
storge container for a JSON object