aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/console_listener.cpp10
-rw-r--r--src/common/file_util.cpp46
-rw-r--r--src/common/file_util.h17
3 files changed, 58 insertions, 15 deletions
diff --git a/src/common/console_listener.cpp b/src/common/console_listener.cpp
index 40122224..53f20d75 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
void ConsoleListener::Log(LogTypes::LOG_LEVELS Level, const char *Text)
{
#if defined(_WIN32)
- /*
- const int MAX_BYTES = 1024*10;
- char Str[MAX_BYTES];
- va_list ArgPtr;
- int Cnt;
- va_start(ArgPtr, Text);
- Cnt = vsnprintf(Str, MAX_BYTES, Text, ArgPtr);
- va_end(ArgPtr);
- */
- DWORD cCharsWritten;
WORD Color;
switch (Level)
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index d1f19e3f..78a64259 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
// return dir;
//}
-bool WriteStringToFile(bool text_file, const std::string &str, const char *filename)
+size_t WriteStringToFile(bool text_file, const std::string &str, const char *filename)
{
return FileUtil::IOFile(filename, text_file ? "w" : "wb").WriteBytes(str.data(), str.size());
}
-bool ReadFileToString(bool text_file, const char *filename, std::string &str)
+size_t ReadFileToString(bool text_file, const char *filename, std::string &str)
{
FileUtil::IOFile file(filename, text_file ? "r" : "rb");
auto const f = file.GetHandle();
@@ -780,6 +780,48 @@ bool ReadFileToString(bool text_file, const char *filename, std::string &str)
return file.ReadArray(&str[0], str.size());
}
+/**
+ * Splits the filename into 8.3 format
+ * Loosely implemented following https://en.wikipedia.org/wiki/8.3_filename
+ * @param filename The normal filename to use
+ * @param short_name A 9-char array in which the short name will be written
+ * @param extension A 4-char array in which the extension will be written
+ */
+void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name,
+ std::array<char, 4>& extension) {
+ const std::string forbidden_characters = ".\"/\\[]:;=, ";
+
+ // On a FAT32 partition, 8.3 names are stored as a 11 bytes array, filled with spaces.
+ short_name = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0'};
+ extension = {' ', ' ', ' ', '\0'};
+
+ std::string::size_type point = filename.rfind('.');
+ if (point == filename.size() - 1)
+ point = filename.rfind('.', point);
+
+ // Get short name.
+ int j = 0;
+ for (char letter : filename.substr(0, point)) {
+ if (forbidden_characters.find(letter, 0) != std::string::npos)
+ continue;
+ if (j == 8) {
+ // TODO(Link Mauve): also do that for filenames containing a space.
+ // TODO(Link Mauve): handle multiple files having the same short name.
+ short_name[6] = '~';
+ short_name[7] = '1';
+ break;
+ }
+ short_name[j++] = toupper(letter);
+ }
+
+ // Get extension.
+ if (point != std::string::npos) {
+ j = 0;
+ for (char letter : filename.substr(point + 1, 3))
+ extension[j++] = toupper(letter);
+ }
+}
+
IOFile::IOFile()
: m_file(NULL), m_good(true)
{}
diff --git a/src/common/file_util.h b/src/common/file_util.h
index c7e11ec0..173ce662 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -4,11 +4,12 @@
#pragma once
+#include <array>
#include <fstream>
#include <cstdio>
+#include <cstring>
#include <string>
#include <vector>
-#include <string.h>
#include "common/common.h"
#include "common/string_util.h"
@@ -128,8 +129,18 @@ std::string GetBundleDirectory();
std::string &GetExeDirectory();
#endif
-bool WriteStringToFile(bool text_file, const std::string &str, const char *filename);
-bool ReadFileToString(bool text_file, const char *filename, std::string &str);
+size_t WriteStringToFile(bool text_file, const std::string &str, const char *filename);
+size_t ReadFileToString(bool text_file, const char *filename, std::string &str);
+
+/**
+ * Splits the filename into 8.3 format
+ * Loosely implemented following https://en.wikipedia.org/wiki/8.3_filename
+ * @param filename The normal filename to use
+ * @param short_name A 9-char array in which the short name will be written
+ * @param extension A 4-char array in which the extension will be written
+ */
+void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name,
+ std::array<char, 4>& extension);
// simple wrapper for cstdlib file functions to
// hopefully will make error checking easier