| 1 | /* |
| 2 | Copyright (c) 2015 Poul Sander |
| 3 | |
| 4 | Permission is hereby granted, free of charge, to any person |
| 5 | obtaining a copy of this software and associated documentation files |
| 6 | (the "Software"), to deal in the Software without restriction, |
| 7 | including without limitation the rights to use, copy, modify, merge, |
| 8 | publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | and to permit persons to whom the Software is furnished to do so, |
| 10 | subject to the following conditions: |
| 11 | |
| 12 | The above copyright notice and this permission notice shall be |
| 13 | included in all copies or substantial portions of the Software. |
| 14 | |
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 19 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 21 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | #ifndef SAGOMISC_HPP |
| 26 | #define SAGOMISC_HPP |
| 27 | |
| 28 | #include <vector> |
| 29 | #include <string> |
| 30 | #include <memory> |
| 31 | |
| 32 | namespace sago { |
| 33 | |
| 34 | /** |
| 35 | * Returns a vector with all filenames in a given directory. |
| 36 | * PHYSFS must be setup before hand. The directory is relative to the PHYSFS base |
| 37 | * @param dir The directory to list |
| 38 | * @return A vector with the filenames in the given directory. If empty the directory was empty or did not exist |
| 39 | */ |
| 40 | std::vector<std::string> GetFileList(const char* dir); |
| 41 | |
| 42 | /** |
| 43 | * Reads an entire file into memory. |
| 44 | * PHYSFS must be setup before hand |
| 45 | * @param filename The file to read |
| 46 | * @param dest The unique pointer in which the bytes will be written |
| 47 | * @param bytes Number of bytes written |
| 48 | * @return The content of the file. If empty either the file was empty, did not exist or could not be opened |
| 49 | */ |
| 50 | void ReadBytesFromFile(const char* filename, std::unique_ptr<char[]>& dest, unsigned int& bytes); |
| 51 | |
| 52 | /** |
| 53 | * Reads an entire file into memory. |
| 54 | * PHYSFS must be setup before hand |
| 55 | * @param filename The file to read |
| 56 | * @return The content of the file. If empty either the file was empty, did not exist or could not be opened |
| 57 | */ |
| 58 | std::string GetFileContent(const char* filename); |
| 59 | |
| 60 | /** |
| 61 | * Reads an entire file into memory. |
| 62 | * PHYSFS must be setup before hand |
| 63 | * @param filename The file to read |
| 64 | * @return The content of the file. If empty either the file was empty, did not exist or could not be opened |
| 65 | */ |
| 66 | inline std::string GetFileContent(const std::string& filename) { |
| 67 | return GetFileContent(filename.c_str()); |
| 68 | }; |
| 69 | |
| 70 | bool FileExists(const char* filename); |
| 71 | |
| 72 | void WriteFileContent(const char* filename, const std::string& content); |
| 73 | |
| 74 | /** |
| 75 | * This functions converts a string on a best effort basis |
| 76 | * Unlike atol this does NOT cause undefined behavior if out of range |
| 77 | * @param c_string A string that may contain a number |
| 78 | * @return A number between LONG_MIN and LONG_MAX (both inclusive) |
| 79 | */ |
| 80 | long int StrToLong(const char* c_string); |
| 81 | |
| 82 | } //namespace sago |
| 83 | |
| 84 | #endif /* SAGOMISC_HPP */ |
| 85 |