Back to Site
Loading...
Searching...
No Matches
fs.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <vector>
5#include <cstdint>
6#include <cstdio>
7#include "rats_export.h"
8
9namespace librats {
10
11// File/Directory existence check
12bool file_or_directory_exists(const char* path);
13RATS_API bool directory_exists(const char* path);
14bool file_exists(const char* path);
15
16// File creation and writing
17bool create_file(const char* path, const char* content);
18bool create_file_binary(const char* path, const void* data, size_t size);
19bool append_to_file(const char* path, const char* content);
20
21// File reading
22char* read_file_text(const char* path, size_t* size_out = nullptr);
23void* read_file_binary(const char* path, size_t* size_out);
24
25// Directory operations
26bool create_directory(const char* path);
27RATS_API bool create_directories(const char* path); // Create parent directories if needed
28
29// File information
30int64_t get_file_size(const char* path);
31bool is_file(const char* path);
32bool is_directory(const char* path);
33
34// File operations
35bool delete_file(const char* path);
36bool delete_directory(const char* path);
37bool copy_file(const char* src_path, const char* dest_path);
38bool move_file(const char* src_path, const char* dest_path);
39
40// File metadata operations
41uint64_t get_file_modified_time(const char* path);
42std::string get_file_extension(const char* path);
43std::string get_filename_from_path(const char* path);
44std::string get_parent_directory(const char* path);
45
46// File chunk operations
47bool write_file_chunk(const char* path, uint64_t offset, const void* data, size_t size);
48bool read_file_chunk(const char* path, uint64_t offset, void* buffer, size_t size);
49
50// Advanced file operations
51bool create_file_with_size(const char* path, uint64_t size); // Pre-allocate file space
52bool rename_file(const char* old_path, const char* new_path);
53
54// Directory listing
56 std::string name;
57 std::string path;
59 uint64_t size;
60 uint64_t modified_time;
61};
62bool list_directory(const char* path, std::vector<DirectoryEntry>& entries);
63
64// Path utilities
65std::string combine_paths(const std::string& base, const std::string& relative);
66bool validate_path(const char* path, bool check_write_access = false);
67
68// Utility functions
69void free_file_buffer(void* buffer); // Free memory allocated by read functions
70bool get_current_directory(char* buffer, size_t buffer_size);
71bool set_current_directory(const char* path);
72
73// C++ convenience wrappers
74inline bool file_or_directory_exists(const std::string& path) { return file_or_directory_exists(path.c_str()); }
75inline bool file_exists(const std::string& path) { return file_exists(path.c_str()); }
76inline bool directory_exists(const std::string& path) { return directory_exists(path.c_str()); }
77inline bool create_file(const std::string& path, const std::string& content) {
78 return create_file(path.c_str(), content.c_str());
79}
80inline std::string read_file_text_cpp(const std::string& path) {
81 size_t size;
82 char* content = read_file_text(path.c_str(), &size);
83 if (!content) return "";
84 std::string result(content, size);
85 free_file_buffer(content);
86 return result;
87}
88
89// Additional C++ wrappers for new functions
90inline uint64_t get_file_modified_time(const std::string& path) { return get_file_modified_time(path.c_str()); }
91inline std::string get_file_extension(const std::string& path) { return get_file_extension(path.c_str()); }
92inline std::string get_filename_from_path(const std::string& path) { return get_filename_from_path(path.c_str()); }
93inline std::string get_parent_directory(const std::string& path) { return get_parent_directory(path.c_str()); }
94inline bool write_file_chunk(const std::string& path, uint64_t offset, const void* data, size_t size) {
95 return write_file_chunk(path.c_str(), offset, data, size);
96}
97inline bool read_file_chunk(const std::string& path, uint64_t offset, void* buffer, size_t size) {
98 return read_file_chunk(path.c_str(), offset, buffer, size);
99}
100inline bool create_file_with_size(const std::string& path, uint64_t size) { return create_file_with_size(path.c_str(), size); }
101inline bool rename_file(const std::string& old_path, const std::string& new_path) {
102 return rename_file(old_path.c_str(), new_path.c_str());
103}
104inline bool validate_path(const std::string& path, bool check_write_access = false) {
105 return validate_path(path.c_str(), check_write_access);
106}
107
108} // namespace librats
bool create_directory(const char *path)
std::string get_file_extension(const char *path)
bool append_to_file(const char *path, const char *content)
void free_file_buffer(void *buffer)
std::string combine_paths(const std::string &base, const std::string &relative)
bool file_exists(const char *path)
bool set_current_directory(const char *path)
bool file_or_directory_exists(const char *path)
bool create_file(const char *path, const char *content)
bool create_file_with_size(const char *path, uint64_t size)
bool delete_file(const char *path)
bool create_directories(const char *path)
bool delete_directory(const char *path)
bool rename_file(const char *old_path, const char *new_path)
bool is_directory(const char *path)
uint64_t get_file_modified_time(const char *path)
std::string read_file_text_cpp(const std::string &path)
Definition fs.h:80
bool move_file(const char *src_path, const char *dest_path)
bool is_file(const char *path)
bool read_file_chunk(const char *path, uint64_t offset, void *buffer, size_t size)
bool create_file_binary(const char *path, const void *data, size_t size)
bool copy_file(const char *src_path, const char *dest_path)
bool list_directory(const char *path, std::vector< DirectoryEntry > &entries)
bool get_current_directory(char *buffer, size_t buffer_size)
void * read_file_binary(const char *path, size_t *size_out)
int64_t get_file_size(const char *path)
std::string get_parent_directory(const char *path)
std::string get_filename_from_path(const char *path)
bool directory_exists(const char *path)
char * read_file_text(const char *path, size_t *size_out=nullptr)
bool validate_path(const char *path, bool check_write_access=false)
bool write_file_chunk(const char *path, uint64_t offset, const void *data, size_t size)
uint64_t modified_time
Definition fs.h:60
std::string name
Definition fs.h:56
std::string path
Definition fs.h:57
uint64_t size
Definition fs.h:59