VAE - Virtual Audio Engine 1
Small Data Driven Audio Engine
tklb::FileInfo Struct Reference

#include <TFile.hpp>

Collaboration diagram for tklb::FileInfo:

Public Member Functions

 FileInfo (const char *path)
 
 FileInfo ()=default
 
void scan ()
 Recursively scans the directory and its subfolder. More...
 
bool isWave ()
 check if the file has a .wav extension More...
 
bool isJSON ()
 check if the file has a .json extension More...
 
bool remove ()
 Will delete the file, does not work with folders. More...
 
std::string hashFile ()
 Simple hash operation. More...
 
void print ()
 Print the directory tree. More...
 

Static Public Member Functions

static bool write (const char *path, const char *data, const size_t length)
 Write to a file. More...
 
static std::string joinPath (const std::initializer_list< std::string > &paths)
 Concats multiple paths TODO tklb trim path dilimters. More...
 
static std::string platformPath (std::string path)
 Replaces all pathdelimters to the platform ones. More...
 
static bool isRelative (const std::string &path)
 
static void toUnixPath (std::string &path)
 

Public Attributes

std::string name
 
std::string relative
 
std::string absolute
 
bool isFolder = false
 
HeapBuffer< FileInfochildren
 

Private Member Functions

void recursivePrint (FileInfo &info, int depth)
 
void recursiveScan (FileInfo &root, bool recursive)
 
void appendDelimiter (FileInfo &info)
 

Detailed Description

Definition at line 29 of file TFile.hpp.

Constructor & Destructor Documentation

◆ FileInfo() [1/2]

tklb::FileInfo::FileInfo ( const char *  path)
inline

Definition at line 36 of file TFile.hpp.

36 {
37 if (isRelative(path)) {
38 relative = path;
39 absolute = path;
40 // TKLB_ASSERT(false)
41 // TODO tklb figure out the absolute path
42 } else {
43 absolute = path;
44 relative = "./";
45 }
48 recursiveScan(*this, false);
49 }
void recursiveScan(FileInfo &root, bool recursive)
Definition: TFile.hpp:195
static void toUnixPath(std::string &path)
Definition: TFile.hpp:176
static bool isRelative(const std::string &path)
Definition: TFile.hpp:172
std::string absolute
Definition: TFile.hpp:32
std::string relative
Definition: TFile.hpp:31
Here is the call graph for this function:

◆ FileInfo() [2/2]

tklb::FileInfo::FileInfo ( )
default

Member Function Documentation

◆ appendDelimiter()

void tklb::FileInfo::appendDelimiter ( FileInfo info)
inlineprivate

Definition at line 235 of file TFile.hpp.

235 {
236 if (info.relative.length() - info.relative.find_last_of('/') != 1) {
237 info.relative = info.relative + "/";
238 }
239 if (info.absolute.length() - info.absolute.find_last_of('/') != 1) {
240 info.absolute = info.absolute + "/";
241 }
242 }
Here is the caller graph for this function:

◆ hashFile()

std::string tklb::FileInfo::hashFile ( )
inline

Simple hash operation.

Definition at line 93 of file TFile.hpp.

93 {
94 const char* path = absolute.c_str();
95
96 if (isFolder) {
97 return ""; // Can't has folder
98 }
99 std::ifstream fp(path);
100 std::stringstream ss;
101 // Unable to hash file, return an empty hash.
102 if (!fp.is_open()) {
103 return "";
104 }
105
106 // Hashing
107 uint32_t magic = 5381;
108 char c;
109 while (fp.get(c)) {
110 magic = ((magic << 5) + magic) + c; // magic * 33 + c
111 }
112
113 ss << std::hex << std::setw(8) << std::setfill('0') << magic;
114 return ss.str();
115 }
bool isFolder
Definition: TFile.hpp:33

◆ isJSON()

bool tklb::FileInfo::isJSON ( )
inline

check if the file has a .json extension

Definition at line 72 of file TFile.hpp.

72 {
73 return name.length() - name.find_last_of(".JSON") == 5
74 || name.length() - name.find_last_of(".json") == 5;
75 }
std::string name
Definition: TFile.hpp:30

◆ isRelative()

static bool tklb::FileInfo::isRelative ( const std::string &  path)
inlinestatic

Definition at line 172 of file TFile.hpp.

172 {
173 return path[0] == '.';
174 }
Here is the caller graph for this function:

◆ isWave()

bool tklb::FileInfo::isWave ( )
inline

check if the file has a .wav extension

Definition at line 64 of file TFile.hpp.

64 {
65 return name.length() - name.find_last_of(".WAV") == 4
66 || name.length() - name.find_last_of(".wav") == 4;
67 }

◆ joinPath()

static std::string tklb::FileInfo::joinPath ( const std::initializer_list< std::string > &  paths)
inlinestatic

Concats multiple paths TODO tklb trim path dilimters.

Definition at line 145 of file TFile.hpp.

145 {
146 std::string joined;
147 for (auto &i : paths) {
148 joined += i;
149 }
150 return joined;
151 }

◆ platformPath()

static std::string tklb::FileInfo::platformPath ( std::string  path)
inlinestatic

Replaces all pathdelimters to the platform ones.

Definition at line 156 of file TFile.hpp.

156 {
157 for (size_t i = 1; i < path.size(); i++) {
158 if ((path[i - 1] == '/' || path[i - 1] == '\\') && (path[i] == '/' || path[i] == '\\')) {
159 // need to get rid of double slashes
160 TKLB_ASSERT(false);
161 }
162 }
163
164 for (size_t i = 0; i < path.size(); i++) {
165 if (path[i] == '/' || path[i] == '\\') {
166 path[i] = PATH_DELIMITER[0];
167 }
168 }
169 return path;
170 }
#define TKLB_ASSERT(condition)
Wrap assertions.
Definition: TAssert.h:18
const std::string PATH_DELIMITER
Definition: TFile.hpp:22

◆ print()

void tklb::FileInfo::print ( )
inline

Print the directory tree.

Definition at line 120 of file TFile.hpp.

120 {
121 recursivePrint(*this, 0);
122 }
void recursivePrint(FileInfo &info, int depth)
Definition: TFile.hpp:185
Here is the call graph for this function:

◆ recursivePrint()

void tklb::FileInfo::recursivePrint ( FileInfo info,
int  depth 
)
inlineprivate

Definition at line 185 of file TFile.hpp.

185 {
186 char space[256];
187 std::fill_n(space, 256, 0);
188 std::fill_n(space, depth, ' ');
189 TKLB_PRINT("%s%s\n", space, info.name.c_str())
190 for (int i = 0; i < info.children.size(); i++) {
191 recursivePrint(info.children[i], depth + 1);
192 }
193 }
#define TKLB_PRINT(...)
Definition: TPrint.h:4
HeapBuffer< FileInfo > children
Definition: TFile.hpp:34
Here is the call graph for this function:
Here is the caller graph for this function:

◆ recursiveScan()

void tklb::FileInfo::recursiveScan ( FileInfo root,
bool  recursive 
)
inlineprivate

Definition at line 195 of file TFile.hpp.

195 {
196 root.children.clear();
197 struct dirent** files = nullptr;
198 const int count = scandir(root.absolute.c_str(), &files, nullptr, alphasort);
199 if (count >= 0) {
200 root.isFolder = true;
201 appendDelimiter(root);
202 for (int i = 0; i < count; i++) {
203 const struct dirent* ent = files[i];
204 if (ent == nullptr) {
205 continue;
206 }
207 if (ent->d_name[0] != '.') {
208 FileInfo info;
209 info.isFolder = ent->d_type == DT_DIR;
210 info.name = ent->d_name;
211 info.relative = root.relative + info.name;
212 info.absolute = root.absolute + info.name;
213 if (recursive && info.isFolder) {
214 recursiveScan(info, true);
215 }
216 root.children.push(info);
217 }
218 #ifdef _WIN32
219 free(files[i]);
220 #else // _WIN32
221 TKLB_STD_POOL.deallocate(files[i]);
222 #endif // _WIN32
223 }
224 } else {
225 root.isFolder = false;
226 }
227 #ifdef _WIN32
228 free(files);
229 #else // _WIN32
230 TKLB_STD_POOL.deallocate(files);
231 #endif // _WIN32
232
233 }
#define TKLB_STD_POOL
Definition: TMemory.hpp:13
#define free(ptr)
void appendDelimiter(FileInfo &info)
Definition: TFile.hpp:235
FileInfo()=default
Here is the call graph for this function:
Here is the caller graph for this function:

◆ remove()

bool tklb::FileInfo::remove ( )
inline

Will delete the file, does not work with folders.

Definition at line 80 of file TFile.hpp.

80 {
81 const char* path = absolute.c_str();
82 if (!isFolder) {
83 return std::remove(path) == 0;
84 }
85 // TODO tklb Folder deleteion
86 TKLB_ASSERT(false);
87 return false;
88 }

◆ scan()

void tklb::FileInfo::scan ( )
inline

Recursively scans the directory and its subfolder.

Definition at line 57 of file TFile.hpp.

57 {
58 recursiveScan(*this, true);
59 }
Here is the call graph for this function:

◆ toUnixPath()

static void tklb::FileInfo::toUnixPath ( std::string &  path)
inlinestatic

Definition at line 176 of file TFile.hpp.

176 {
177 for (size_t i = 1; i < path.size(); i++) {
178 if (path[i] == '\\') {
179 path[i] = '/';
180 }
181 }
182 }
Here is the caller graph for this function:

◆ write()

static bool tklb::FileInfo::write ( const char *  path,
const char *  data,
const size_t  length 
)
inlinestatic

Write to a file.

Parameters
pathThe path to the file
dataThe data buffer
lengthThe length of the data buffer

Definition at line 130 of file TFile.hpp.

130 {
131 try {
132 auto file = std::fstream(path, std::ios::out | std::ios::binary);
133 file.write(data, length);
134 file.close();
135 return true;
136 } catch (...) {
137 return false;
138 }
139 }

Member Data Documentation

◆ absolute

std::string tklb::FileInfo::absolute

Definition at line 32 of file TFile.hpp.

◆ children

HeapBuffer<FileInfo> tklb::FileInfo::children

Definition at line 34 of file TFile.hpp.

◆ isFolder

bool tklb::FileInfo::isFolder = false

Definition at line 33 of file TFile.hpp.

◆ name

std::string tklb::FileInfo::name

Definition at line 30 of file TFile.hpp.

◆ relative

std::string tklb::FileInfo::relative

Definition at line 31 of file TFile.hpp.


The documentation for this struct was generated from the following file: