Spinning Topp Logo BlackTopp Studios
inc
resourceutilities.h
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _resourceutilities_h
41 #define _resourceutilities_h
42 
43 #include "datatypes.h"
44 
45 namespace Mezzanine
46 {
47  namespace Resource
48  {
49  ///////////////////////////////////////////////////////////////////////////////
50  // MainArg Utilities
51 
52  /// @brief Store the Main arguments for later use.
53  /// @param ArgCount How many arguments will be passed in ArgVars.
54  /// @param ArgVars A pointer to an array, with ArgCount elements, of char* which point to null terminated c strings.
55  void MEZZ_LIB CacheMainArgs(int ArgCount, char** ArgVars);
56 
57  ///////////////////////////////////////////////////////////////////////////////
58  // File Management
59 
60  /*/// @brief Deletes a file existing on the filesystem.
61  /// @param PathAndFile A string containing both the path to the directory where the file is located, and the name of the file to be deleted.
62  /// @return Returns true if the operation was successful, false if it failed.
63  Boole MEZZ_LIB RemoveFile(const String& PathAndFile);// */
64 
65  ///////////////////////////////////////////////////////////////////////////////
66  // Basic Directory Management
67 
68  /// @brief Creates a single new directory.
69  /// @remarks This function will only create the directory specified at the end of the path.
70  /// @param DirectoryPath The path for the newly created directory.
71  /// @return Returns true if the directory was created, false in the case of a non-critical error.
72  Boole MEZZ_LIB CreateDirectory(const String& DirectoryPath);
73  /// @brief Creates all directories that do not exist in the provided path.
74  /// @param DirectoryPath The path for the newly created directory or directories.
75  /// @return Returns true if all directories were created, false in the case of a non-critical error.
76  Boole MEZZ_LIB CreateDirectoryPath(const String& DirectoryPath);
77  /// @brief Checks to see if the given path exists and if it is a folder.
78  /// @exception On Error this might throw a Mezzanine::IOException with detail about why it failed.
79  /// @param DirectoryPath A String containing the path to test.
80  /// @return True if the item indicated by DirectoryPath exists and it is a directory, false if it does not exist or exists but is a file.
81  Boole MEZZ_LIB DoesDirectoryExist(const String& DirectoryPath);
82  /// @brief Remove an empty directory.
83  /// @exception On Error this might throw a Mezzanine::IOException with details about why it failed.
84  /// @param DirectoryPath The Path to the directory to remove.
85  void MEZZ_LIB RemoveDirectory(const String& DirectoryPath);
86 
87  /// @brief Get a Listing of the files and subdirectories in a directory.
88  /// @details This follows normal command line conventions, "." is the current directory,
89  /// ".." is the parent directory. To access the file system root you will need to use a
90  /// leading "c:/", "c:\\", or "/" as appropriate for the operating system the software will run on.
91  /// @param Dir The directory to check.
92  /// @return This will return a pointer to a set of Strings the caller is responsible for deleting or a null pointer on an error.
93  StringVector MEZZ_LIB GetDirContents(const String& Dir = ".");
94 
95  ///////////////////////////////////////////////////////////////////////////////
96  // Path Utilities
97 
98  /// @brief Get the directory portion of a string.
99  /// @param FileName A complete path and filename.
100  /// @return If passed "/a/b/c.txt" or "c:\windirs\crash.exe" this will return "/a/b/" or "c:\windirs\".
101  String MEZZ_LIB DirName(const String& FileName);
102  /// @brief Get the filename portion of a string.
103  /// @param FileName A complete path and filename.
104  /// @return If passed "/a/b/c.txt" or "c:\windirs\crash.exe" this will return "c.txt" or "crash.exe".
105  String MEZZ_LIB BaseName(const String& FileName);
106 
107  /// @brief Get the character used to separate directories
108  /// @return Backslash '\' on windows and Forward slash '/' on other operating systems.
110  /// @brief Get the character used to separate entries in the system PATH
111  /// @return Semicolon ';' on windows and Forward slash ':' on other operating systems.
113 
114  /// @brief Convenience method to verify the necessary system separator is present when concatenating.
115  /// @param FilePath The directory path to the file.
116  /// @param FileName The name of the file.
117  /// @return Returns a full string that is the concatenated path and filename.
118  String MEZZ_LIB CombinePathAndFileName(const String& FilePath, const String& FileName);
119 
120  ///////////////////////////////////////////////////////////////////////////////
121  // System Path Utilities
122 
123  /// @brief Get the $PATH or %PATH% split and order for easy checking of how the OS does it.
124  /// @param PATH Defaults to the PATH environment variable. But any value like a system path will be split the return of GetPathSeparator().
125  /// @return A collection of directories that this system will for executables in the order they will be checked.
126  StringVector MEZZ_LIB GetSystemPATH(const String& PATH = String(getenv("PATH")));
127 
128  /// @brief Search the system path the same way most systems do to find an executable.
129  /// @warning This function is case sensitive and not all operating systems are.
130  /// @todo Add support for extension handling on windows. "cmd" should find "cmd.exe" in system32, but currently "cmd.exe" needs to be searched.
131  /// @param ExecutableName The executable to look for.
132  /// @return If the executable is not found "" is returned otherwise the first directory in the PATH containing it is returned.
133  String MEZZ_LIB Which(const String& ExecutableName);
134 
135  ///////////////////////////////////////////////////////////////////////////////
136  // Execution Directory Utilities
137 
138  /// @brief Get the Path to the current executable, in a fast way if possible.
139  /// @return A String containing the path to the current executable.
141  /// @brief Get the Path to the current executable, fast from Args if Possible or from a system call otherwise.
142  /// @param ArgCount How many arguments will be passed in ArgVars.
143  /// @param ArgVars A pointer to an array, with ArgCount elements, of char* which point to null terminated c strings.
144  /// @warning If you pass bogus arguments to this bad things can and will happen. Infinite loops, segfaults etc... Just pass what main gives you
145  /// @warning Not all system provide all the needed information to determine the executable directory
146  /// @return A String containing the path to the current executable.
147  String MEZZ_LIB GetExecutableDir(int ArgCount, char** ArgVars);
148 
149  /// @brief Used a system call to get the curent Directory the executable is in. This make an external system call and is likely slower than GetExecutableDirFromArg
150  /// @return This will return the current path this executable is stored in.
152 
153  /// @brief Uses the main parameters stored using "CacheMainArgs" to attempt determine executable directory.
154  /// @return Either a valid Path, '.' if the working dir is likely correct or "" if nothing could be determined.
156  /// @brief Attempt to get the executable directory from the a set of variables like those passed into Main.
157  /// @note This is the fastest way to get the Executable location, but might not work on all platforms.
158  /// @warning If you pass bogus arguments to this bad things can and will happen. Infinite loops, segfaults etc... Just pass what main gives you.
159  /// @warning Not all systems provide all the needed information to determine the executable directory.
160  /// @param ArgCount How many arguments will be passed in ArgVars.
161  /// @param ArgVars A pointer to an array, with ArgCount elements, of char* which point to null terminated c strings.
162  /// @return If a whole path is present in ArgVars[0] this returns the directory part of that path, if this uses the executable file this returns '.', otherwise this with return "" indicating it is not usable.
163  String MEZZ_LIB GetExecutableDirFromArg(int ArgCount, char** ArgVars);
164 
165  ///////////////////////////////////////////////////////////////////////////////
166  // Working Directory Utilities
167 
168  /// @brief Change directory, to the directory indicated.
169  /// @param ChangeTo The new directory to work from.
170  void MEZZ_LIB ChangeWorkingDirectory(const String& ChangeTo);
171  /// @brief Get the working directory as a Mezzanine::String
172  /// @return The Directory the game was called from (not nescessarilly the location of the executable), as a Mezzanine::String
174 
175  ///////////////////////////////////////////////////////////////////////////////
176  // Application Data Directory Utilities
177 
178  /// @brief Resolves a string describing one of the platform data paths to the actual path it is.
179  /// @remarks Currently there are only 4 preset path variables, and depending on platform they two or more may go to the same location.
180  /// The valid variables are as follows: LocalAppData, ShareableAppData, CurrentUserData, and CommonUserData. These are not case sensative.
181  /// @param PathVar String containing the name of the path variable.
182  /// @return Returns the actual path of the variable provided.
184 
185  /// @brief Gets the path to the directory intended for game and engine config data that is not meant to be shared.
186  /// @return Returns a string containing the path to the Local Application Data Directory.
188  /// @brief Gets the path to the directory intended for game and engine config data that is allowed to be shared.
189  /// @return Returns a string containing the path to the Shareable Application Data Directory.
191  /// @brief Gets the path to the directory intended for game saves and user profile data for the current user.
192  /// @return Returns a string containing the path to the Current User Data Directory.
194  /// @brief Gets the path to the directory intended for game saves and user profile data for all users.
195  /// @return Returns a string containing the path to the Common User Data Directory.
197  }//Resource
198 }//Mezzanine
199 
200 #endif
StringVector GetSystemPATH(const String &PATH)
Get the $PATH or PATH% split and order for easy checking of how the OS does it.
std::vector< String > StringVector
This is a simple datatype for a vector container of strings.
Definition: datatypes.h:212
bool Boole
Generally acts a single bit, true or false.
Definition: datatypes.h:173
String ResolveDataPathFromString(const String &PathVar)
Resolves a string describing one of the platform data paths to the actual path it is...
String GetExecutableDir()
Get the Path to the current executable, in a fast way if possible.
Char8 GetPathSeparator()
Get the character used to separate entries in the system PATH.
All the definitions for datatypes as well as some basic conversion functions are defined here...
Boole CreateDirectoryPath(const String &DirectoryPath)
Creates all directories that do not exist in the provided path.
String CombinePathAndFileName(const String &FilePath, const String &FileName)
Convenience method to verify the necessary system separator is present when concatenating.
String GetExecutableDirFromArg()
Uses the main parameters stored using "CacheMainArgs" to attempt determine executable directory...
String GetLocalAppDataDir()
Gets the path to the directory intended for game and engine config data that is not meant to be share...
String GetShareableAppDataDir()
Gets the path to the directory intended for game and engine config data that is allowed to be shared...
StringVector GetDirContents(const String &Dir)
Get a Listing of the files and subdirectories in a directory.
void ChangeWorkingDirectory(const String &ChangeTo)
Change directory, to the directory indicated.
char Char8
A datatype to represent one character.
Definition: datatypes.h:169
String DirName(const String &FileName)
Get the directory portion of a string.
String Which(const String &ExecutableName)
Search the system path the same way most systems do to find an executable.
String GetWorkingDirectory()
Get the working directory as a Mezzanine::String.
String GetCommonUserDataDir()
Gets the path to the directory intended for game saves and user profile data for all users...
String GetExecutableDirFromSystem()
Used a system call to get the curent Directory the executable is in. This make an external system cal...
Boole CreateDirectory(const String &DirectoryPath)
Creates a single new directory.
Char8 GetDirectorySeparator()
Get the character used to separate directories.
String BaseName(const String &FileName)
Get the filename portion of a string.
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
void RemoveDirectory(const String &DirectoryPath)
Remove an empty directory. Error this might throw a Mezzanine::IOException with details about why it ...
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
void CacheMainArgs(int ArgCount, char **ArgVars)
Store the Main arguments for later use.
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159
Boole DoesDirectoryExist(const String &DirectoryPath)
Checks to see if the given path exists and if it is a folder. Error this might throw a Mezzanine::IOE...
String GetCurrentUserDataDir()
Gets the path to the directory intended for game saves and user profile data for the current user...