diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/console_listener.cpp | 10 | ||||
| -rw-r--r-- | src/common/file_util.cpp | 46 | ||||
| -rw-r--r-- | src/common/file_util.h | 17 |
3 files changed, 58 insertions, 15 deletions
diff --git a/src/common/console_listener.cpp b/src/common/console_listener.cpp index 40122224c..53f20d754 100644 --- a/src/common/console_listener.cpp +++ b/src/common/console_listener.cpp | |||
| @@ -241,16 +241,6 @@ void ConsoleListener::PixelSpace(int Left, int Top, int Width, int Height, bool | |||
| 241 | void ConsoleListener::Log(LogTypes::LOG_LEVELS Level, const char *Text) | 241 | void ConsoleListener::Log(LogTypes::LOG_LEVELS Level, const char *Text) |
| 242 | { | 242 | { |
| 243 | #if defined(_WIN32) | 243 | #if defined(_WIN32) |
| 244 | /* | ||
| 245 | const int MAX_BYTES = 1024*10; | ||
| 246 | char Str[MAX_BYTES]; | ||
| 247 | va_list ArgPtr; | ||
| 248 | int Cnt; | ||
| 249 | va_start(ArgPtr, Text); | ||
| 250 | Cnt = vsnprintf(Str, MAX_BYTES, Text, ArgPtr); | ||
| 251 | va_end(ArgPtr); | ||
| 252 | */ | ||
| 253 | DWORD cCharsWritten; | ||
| 254 | WORD Color; | 244 | WORD Color; |
| 255 | 245 | ||
| 256 | switch (Level) | 246 | switch (Level) |
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index d1f19e3ff..78a642599 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp | |||
| @@ -763,12 +763,12 @@ const std::string& GetUserPath(const unsigned int DirIDX, const std::string &new | |||
| 763 | // return dir; | 763 | // return dir; |
| 764 | //} | 764 | //} |
| 765 | 765 | ||
| 766 | bool WriteStringToFile(bool text_file, const std::string &str, const char *filename) | 766 | size_t WriteStringToFile(bool text_file, const std::string &str, const char *filename) |
| 767 | { | 767 | { |
| 768 | return FileUtil::IOFile(filename, text_file ? "w" : "wb").WriteBytes(str.data(), str.size()); | 768 | return FileUtil::IOFile(filename, text_file ? "w" : "wb").WriteBytes(str.data(), str.size()); |
| 769 | } | 769 | } |
| 770 | 770 | ||
| 771 | bool ReadFileToString(bool text_file, const char *filename, std::string &str) | 771 | size_t ReadFileToString(bool text_file, const char *filename, std::string &str) |
| 772 | { | 772 | { |
| 773 | FileUtil::IOFile file(filename, text_file ? "r" : "rb"); | 773 | FileUtil::IOFile file(filename, text_file ? "r" : "rb"); |
| 774 | auto const f = file.GetHandle(); | 774 | auto const f = file.GetHandle(); |
| @@ -780,6 +780,48 @@ bool ReadFileToString(bool text_file, const char *filename, std::string &str) | |||
| 780 | return file.ReadArray(&str[0], str.size()); | 780 | return file.ReadArray(&str[0], str.size()); |
| 781 | } | 781 | } |
| 782 | 782 | ||
| 783 | /** | ||
| 784 | * Splits the filename into 8.3 format | ||
| 785 | * Loosely implemented following https://en.wikipedia.org/wiki/8.3_filename | ||
| 786 | * @param filename The normal filename to use | ||
| 787 | * @param short_name A 9-char array in which the short name will be written | ||
| 788 | * @param extension A 4-char array in which the extension will be written | ||
| 789 | */ | ||
| 790 | void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name, | ||
| 791 | std::array<char, 4>& extension) { | ||
| 792 | const std::string forbidden_characters = ".\"/\\[]:;=, "; | ||
| 793 | |||
| 794 | // On a FAT32 partition, 8.3 names are stored as a 11 bytes array, filled with spaces. | ||
| 795 | short_name = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0'}; | ||
| 796 | extension = {' ', ' ', ' ', '\0'}; | ||
| 797 | |||
| 798 | std::string::size_type point = filename.rfind('.'); | ||
| 799 | if (point == filename.size() - 1) | ||
| 800 | point = filename.rfind('.', point); | ||
| 801 | |||
| 802 | // Get short name. | ||
| 803 | int j = 0; | ||
| 804 | for (char letter : filename.substr(0, point)) { | ||
| 805 | if (forbidden_characters.find(letter, 0) != std::string::npos) | ||
| 806 | continue; | ||
| 807 | if (j == 8) { | ||
| 808 | // TODO(Link Mauve): also do that for filenames containing a space. | ||
| 809 | // TODO(Link Mauve): handle multiple files having the same short name. | ||
| 810 | short_name[6] = '~'; | ||
| 811 | short_name[7] = '1'; | ||
| 812 | break; | ||
| 813 | } | ||
| 814 | short_name[j++] = toupper(letter); | ||
| 815 | } | ||
| 816 | |||
| 817 | // Get extension. | ||
| 818 | if (point != std::string::npos) { | ||
| 819 | j = 0; | ||
| 820 | for (char letter : filename.substr(point + 1, 3)) | ||
| 821 | extension[j++] = toupper(letter); | ||
| 822 | } | ||
| 823 | } | ||
| 824 | |||
| 783 | IOFile::IOFile() | 825 | IOFile::IOFile() |
| 784 | : m_file(NULL), m_good(true) | 826 | : m_file(NULL), m_good(true) |
| 785 | {} | 827 | {} |
diff --git a/src/common/file_util.h b/src/common/file_util.h index c7e11ec0c..173ce6623 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h | |||
| @@ -4,11 +4,12 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <array> | ||
| 7 | #include <fstream> | 8 | #include <fstream> |
| 8 | #include <cstdio> | 9 | #include <cstdio> |
| 10 | #include <cstring> | ||
| 9 | #include <string> | 11 | #include <string> |
| 10 | #include <vector> | 12 | #include <vector> |
| 11 | #include <string.h> | ||
| 12 | 13 | ||
| 13 | #include "common/common.h" | 14 | #include "common/common.h" |
| 14 | #include "common/string_util.h" | 15 | #include "common/string_util.h" |
| @@ -128,8 +129,18 @@ std::string GetBundleDirectory(); | |||
| 128 | std::string &GetExeDirectory(); | 129 | std::string &GetExeDirectory(); |
| 129 | #endif | 130 | #endif |
| 130 | 131 | ||
| 131 | bool WriteStringToFile(bool text_file, const std::string &str, const char *filename); | 132 | size_t WriteStringToFile(bool text_file, const std::string &str, const char *filename); |
| 132 | bool ReadFileToString(bool text_file, const char *filename, std::string &str); | 133 | size_t ReadFileToString(bool text_file, const char *filename, std::string &str); |
| 134 | |||
| 135 | /** | ||
| 136 | * Splits the filename into 8.3 format | ||
| 137 | * Loosely implemented following https://en.wikipedia.org/wiki/8.3_filename | ||
| 138 | * @param filename The normal filename to use | ||
| 139 | * @param short_name A 9-char array in which the short name will be written | ||
| 140 | * @param extension A 4-char array in which the extension will be written | ||
| 141 | */ | ||
| 142 | void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name, | ||
| 143 | std::array<char, 4>& extension); | ||
| 133 | 144 | ||
| 134 | // simple wrapper for cstdlib file functions to | 145 | // simple wrapper for cstdlib file functions to |
| 135 | // hopefully will make error checking easier | 146 | // hopefully will make error checking easier |