CyberEngineMkIII
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
CYBPath.cpp
Go to the documentation of this file.
1 #include "CYB.hpp"
3 
4 using namespace CYB::API::String;
5 
6 int CYB::Platform::System::Path::GetIndexOfLastSeperator(const API::String::UTF8& AString, const char ASeparator) noexcept {
7  for (auto I(AString.RawLength() - 1); I >= 0; --I)
8  if (AString.CString()[I] == ASeparator)
9  return I;
10  return -1;
11 }
12 
14  SetPath(std::move(APath));
15 }
16 
18  Path(LocateDirectory(ADirectory))
19 {}
20 
22  UTF8 CurrentPath(std::move(ABasePath));
23  for (auto& PathAddition : APaths) {
24  CurrentPath += DirectorySeparatorChar();
25  CurrentPath += PathAddition;
26  CreateDirectory(CurrentPath);
27  }
28 }
29 
31  return FPath;
32 }
33 
35  auto FirstPath(LocateDirectory(SystemPath::EXECUTABLE));
36  FirstPath += CYB::API::String::UTF8(CYB::API::String::Static(u8"../Resources"));
37  bool Throw(false);
38  try {
39  Evaluate(FirstPath);
40  }
41  catch (Exception::Internal AException) {
42  API::Assert::Equal<unsigned int>(AException.FErrorCode, Exception::Internal::PATH_EVALUATION_FAILURE);
43  Throw = true;
44  }
45  if(Throw)
47  return FirstPath;
48 }
49 
50 void CYB::Platform::System::Path::Append(const API::String::UTF8& AAppendage, const bool ACreateIfNonExistant, const bool ACreateRecursive) {
51  //first ensure we aren't breaking the rules
52  if (FPath.RawLength() + AAppendage.RawLength() + 1 > MAX_PATH_BYTES)
54 
55  //verify, file system could have changed and such
56  if (!Verify(FPath))
58 
59  auto NewPath(FPath + DirectorySeparatorChar() + AAppendage);
60  UTF8 FinalComponent;
61  if (!ACreateIfNonExistant) {
62  //try a simple cd
63  if (!Verify(NewPath)) {
64  //Okay, we may be trying to create a new file, check it's parent directory
65  const auto I(GetIndexOfLastSeperator(NewPath, *DirectorySeparatorChar()));
67  FinalComponent = UTF8(static_cast<const Dynamic&>(NewPath).SubString(I, NewPath.RawLength() - I));
68  NewPath = UTF8(static_cast<const Dynamic&>(NewPath).SubString(0, I));
69  if (!Verify(NewPath))
71  }
72  }
73  else {
74  UTF8 WorkingPath;
75  //create tokens of directories to create
77  if (ACreateRecursive) {
79  //Tokens = AAppendage.Tokenize(DirectorySeparatorChar());
80  auto DynTokens(AAppendage.Tokenize(*DirectorySeparatorChar()));
81  for (auto& Tok : DynTokens)
82  Tokens.emplace_back(std::move(Tok));
83  WorkingPath = UTF8(FPath);
84  }
85  else {
86  const auto I(GetIndexOfLastSeperator(AAppendage, *DirectorySeparatorChar()));
87  if (I == -1) {
88  Tokens.emplace_back(UTF8(AAppendage));
89  WorkingPath = UTF8(FPath);
90  }
91  else {
92  Tokens.emplace_back(UTF8(static_cast<const Dynamic&>(AAppendage).SubString(I + 1, AAppendage.RawLength() - I - 1)));
93  WorkingPath = UTF8(static_cast<const Dynamic&>(AAppendage).SubString(0, AAppendage.RawLength()));
94  if (!Verify(WorkingPath))
96  }
97  }
98  const Static Ascender(u8"..");
99  for (auto& Tok : Tokens)
100  if (Tok == Ascender)
102 
103  //and try to create them
104  CreateDirectories(std::move(WorkingPath), Tokens);
105  }
106  bool Throw(false);
107  try {
108  Evaluate(NewPath);
109  }
110  catch (Exception::Internal AException) {
111  API::Assert::Equal<unsigned int>(AException.FErrorCode, Exception::Internal::PATH_EVALUATION_FAILURE);
112  Throw = true;
113  }
114  if (Throw)
116  NewPath += std::move(FinalComponent);
117  SetPath(std::move(NewPath));
118 }
119 
120 void CYB::Platform::System::Path::Delete(bool ARecursive) {
121  bool Throw(false);
123  try {
124  if (!IsDirectory())
125  DeleteFile(FPath);
126  else if (Verify(FPath)) {
127  if (ARecursive) {
128  auto Entry(Contents());
129  for (auto& It(Entry()); It->Valid(); ++It)
130  (*It)().Delete(true); //recursion may be a bad idea here, but I'd like to see someone even TRY and overflow this
131  ARecursive = false;
132  }
133  DeleteDirectory(FPath);
134  }
135  }
136  catch (Exception::SystemData AException) {
138  Throw = true;
139  ThrowCode = static_cast<Exception::SystemData::ErrorCode>(AException.FErrorCode);
140  if (!ARecursive && ThrowCode == CYB::Exception::SystemData::PATH_LOST)
141  Throw = false;
142  }
143  if (Throw)
144  throw Exception::SystemData(ThrowCode);
145 }
146 
148  return !IsDirectory();
149 }
150 
151 int CYB::Platform::System::Path::ByteLength(void) const noexcept {
152  return FPath.RawLength();
153 }
154 
156  auto Full(FullName());
157  const auto Dot(GetIndexOfLastSeperator(Full, '.') + 1);
158  if (Dot == 0)
159  return Full;
160  return UTF8(static_cast<const Dynamic&>(Full).SubString(Dot, Full.RawLength() - Dot));
161 }
162 
165 }
166 
168  return FPathListing;
169 }
170 
172  return &FPathListing;
173 }
A variable length UTF-8 string.
Definition: UTF8String.hpp:8
static void CreateDirectories(API::String::UTF8 &&AExistingPath, const API::Container::Deque< API::String::UTF8 > &APaths)
Try and create a set of directories.
Definition: CYBPath.cpp:21
Template type for wrapping pointers and treating them as objects. Only works with CyberEngine interfa...
Definition: Object.hpp:10
const API::String::UTF8 & operator()(void) const noexceptfinaloverride
Public access to the underlying string.
Definition: CYBPath.cpp:30
Path() noexcept=default
See Default Constructors and Destructor.
API::Interop::Context & Context(void) noexcept
Get the API's Context.
Iterator for paths in a directory.
Definition: Path.hpp:40
Container::Deque< Dynamic > Tokenize(const char ASeparator) const
Split the string into a Deque of other strings based on a seperator.
int RawLength(void) const noexcept
Get the byte length of the contained CString.
API::Interop::Object< API::Path > * operator->(void) noexceptfinaloverride
Get the current path the iterator points to. Must be checked for validity after iteration. If invalid, end of iteration has been reached. Can be moved.
Definition: CYBPath.cpp:171
Uses the FindFile API to enumerate directories.
Attempted to use a path greater than the maximum allowed byte value.
Definition: Exception.hpp:77
A string pointing to unchanging data in the stack above it or the data segment. Must have UTF-8 encod...
Definition: StaticString.hpp:7
Object< AObject > ConstructObject(AArgs &&...AArguments)
Allocates the Object specified by AObject using a specified Constructor.
API::Interop::Object< API::Path::DirectoryEntry > Contents(void) const finaloverride
Get the first directory entry iterator in the path. Always valid.
Definition: CYBPath.cpp:163
static void LessThan(const AType &ALHS, const AType &ARHS) noexcept
Less than assertion function. May not be evaluated.
SystemPath
Starting points for creating paths.
Definition: Path.hpp:15
const unsigned int FErrorCode
The assigned error code.
Definition: Exception.hpp:18
A previously valid path has become invalidated, most likely due to deletion.
Definition: Exception.hpp:76
Exceptions caused by external call failures or invalid external data. Only classifies ones that can p...
Definition: Exception.hpp:65
API::String::UTF8 Extension(void) const finaloverride
Get the extension of the file without the leading period. Equivalent to FullFileName and FileName if ...
Definition: CYBPath.cpp:155
bool IsFile(void) const finaloverride
Check if the current path is a file.
Definition: CYBPath.cpp:147
A heap has no block large enough for a requested allocation and expansion failed. ...
Definition: Exception.hpp:74
Allocator & FAllocator
The Allocator.
Definition: Context.hpp:11
std::deque< AType, Interop::STLAllocator< AType >> Deque
std::deque algorithms using the CyberEngine's allocator
Definition: Containers.hpp:10
Generic error for read failures. See functions for further documentation.
Definition: Exception.hpp:72
Precompiled header for inter-engine operations.
static API::String::UTF8 GetResourceDirectory(void)
Get the string path of SystemDirectory::RESOURCE.
Definition: CYBPath.cpp:34
API::Interop::Object< API::Path > & operator*(void) noexceptfinaloverride
Get the current path the iterator points to. Must be checked for validity after iteration. If invalid, end of iteration has been reached. Can be moved.
Definition: CYBPath.cpp:167
Used for manipulating Paths. Paths will always exist either as a file or directory. Paths are '/' delimited when forming though may not be while retrieving. File names ".." will ascend a directory and '.' represents a no-op.
Definition: CYBPath.hpp:10
static int GetIndexOfLastSeperator(const API::String::UTF8 &AString, const char ASeparator) noexcept
Get the index of the last of a given seperator in a string.
Definition: CYBPath.cpp:6
A system path could not be retrieved.
Definition: Exception.hpp:79
Exceptions that are thrown internally in the engine that the should never see, these are a superset o...
Definition: Exception.hpp:104
Tried to delete a non-empty directory.
Definition: Exception.hpp:69
ErrorCode
The error code of the exception.
Definition: Exception.hpp:68
int ByteLength(void) const noexceptfinaloverride
Get the length in bytes of the Path's underlying string.
Definition: CYBPath.cpp:151
void Delete(bool ARecursive) finaloverride
Ensures the current Path doesn't exist. This will invalidate the path.
Definition: CYBPath.cpp:120
void Append(const API::String::UTF8 &AAppendage, const bool ACreateIfNonExistant, const bool ACreateRecursive) finaloverride
Append a directory/file to the path.
Definition: CYBPath.cpp:50
Used for object which aren't allocatables.
Generic error for write failures. See functions for further documentation.
Definition: Exception.hpp:73