Skip to content
Permalink
Browse files

File_Append -> File_OpenOrCreate

  • Loading branch information
UnknownShadow200 committed Dec 1, 2019
1 parent f475eec commit bb30a3a37280e24163374617ccdbcf702861b792
Showing with 24 additions and 21 deletions.
  1. +3 −5 src/Chat.c
  2. +2 −5 src/Logger.c
  3. +5 −9 src/Platform.c
  4. +2 −2 src/Platform.h
  5. +10 −0 src/Stream.c
  6. +2 −0 src/Stream.h
@@ -105,10 +105,9 @@ static void Chat_DisableLogging(void) {
Chat_AddRaw("&cDisabling chat logging");
}

static void Chat_OpenLog(struct DateTime* now) {
FileHandle file;
int i;
static void Chat_OpenLog(struct DateTime* now) {
cc_result res;
int i;
if (!Utils_EnsureDirectory("logs")) { Chat_DisableLogging(); return; }

/* Ensure multiple instances do not end up overwriting each other's log entries. */
@@ -122,15 +121,14 @@ static void Chat_OpenLog(struct DateTime* now) {
String_Format1(&logPath, "%s.log", &logName);
}

res = File_Append(&file, &logPath);
res = Stream_AppendFile(&logStream, &logPath);
if (res && res != ReturnCode_FileShareViolation) {
Chat_DisableLogging();
Logger_Warn2(res, "appending to", &logPath);
return;
}

if (res == ReturnCode_FileShareViolation) continue;
Stream_FromFile(&logStream, file);
return;
}

@@ -805,12 +805,9 @@ static cc_bool logOpen;
void Logger_Log(const String* msg) {
#ifndef CC_BUILD_WEB
static const String path = String_FromConst("client.log");
cc_result res;

if (!logOpen) {
logOpen = true;
res = File_Append(&logFile, &path);
if (!res) Stream_FromFile(&logStream, logFile);
Stream_AppendFile(&logStream, &path);
}

if (!logStream.Meta.File) return;
@@ -855,7 +852,7 @@ static void Logger_AbortCommon(cc_result result, const char* raw_msg, void* ctx)
if (ctx) Logger_DumpRegisters(ctx);
Logger_DumpBacktrace(&msg, ctx);
Logger_DumpMisc(ctx);
if (logStream.Meta.File) File_Close(logFile);
if (logStream.Meta.File) logStream.Close(&logStream);

msg.buffer[msg.length] = '\0';
Window_ShowDialog("We're sorry", msg.buffer);
@@ -422,7 +422,7 @@ cc_result File_SetModifiedTime(const String* path, TimeMS time) {
FileHandle file;
FILETIME ft;
cc_uint64 raw;
cc_result res = File_Append(&file, path);
cc_result res = File_OpenOrCreate(&file, path);
if (res) return res;

raw = 10000 * (time - FILETIME_EPOCH);
@@ -450,10 +450,8 @@ cc_result File_Open(FileHandle* file, const String* path) {
cc_result File_Create(FileHandle* file, const String* path) {
return File_Do(file, path, GENERIC_WRITE | GENERIC_READ, CREATE_ALWAYS);
}
cc_result File_Append(FileHandle* file, const String* path) {
cc_result res = File_Do(file, path, GENERIC_WRITE | GENERIC_READ, OPEN_ALWAYS);
if (res) return res;
return File_Seek(*file, 0, FILE_SEEKFROM_END);
cc_result File_OpenOrCreate(FileHandle* file, const String* path) {
return File_Do(file, path, GENERIC_WRITE | GENERIC_READ, OPEN_ALWAYS);
}

cc_result File_Read(FileHandle file, cc_uint8* data, cc_uint32 count, cc_uint32* bytesRead) {
@@ -584,10 +582,8 @@ cc_result File_Open(FileHandle* file, const String* path) {
cc_result File_Create(FileHandle* file, const String* path) {
return File_Do(file, path, O_RDWR | O_CREAT | O_TRUNC);
}
cc_result File_Append(FileHandle* file, const String* path) {
cc_result res = File_Do(file, path, O_RDWR | O_CREAT);
if (res) return res;
return File_Seek(*file, 0, FILE_SEEKFROM_END);
cc_result File_OpenOrCreate(FileHandle* file, const String* path) {
return File_Do(file, path, O_RDWR | O_CREAT);
}

cc_result File_Read(FileHandle file, cc_uint8* data, cc_uint32 count, cc_uint32* bytesRead) {
@@ -147,8 +147,8 @@ CC_API cc_result File_MarkExecutable(const String* path);
cc_result File_Create(FileHandle* file, const String* path);
/* Attempts to open an existing file for reading. */
cc_result File_Open(FileHandle* file, const String* path);
/* Attempts to open (or create) a file, for appending data to the end of the file. */
cc_result File_Append(FileHandle* file, const String* path);
/* Attempts to open an existing or create a new file for reading and writing. */
cc_result File_OpenOrCreate(FileHandle* file, const String* path);
/* Attempts to read data from the file. */
cc_result File_Read(FileHandle file, cc_uint8* data, cc_uint32 count, cc_uint32* bytesRead);
/* Attempts to write data to the file. */
@@ -125,6 +125,16 @@ cc_result Stream_CreateFile(struct Stream* s, const String* path) {
return res;
}

cc_result Stream_AppendFile(struct Stream* s, const String* path) {
FileHandle file;
cc_result res;

if ((res = File_OpenOrCreate(&file, path))) return res;
if ((res = File_Seek(file, 0, FILE_SEEKFROM_END))) return res;
Stream_FromFile(s, file);
return res;
}

cc_result Stream_WriteAllTo(const String* path, const cc_uint8* data, cc_uint32 length) {
struct Stream stream;
cc_result res, closeRes;
@@ -52,6 +52,8 @@ cc_result Stream_DefaultReadU8(struct Stream* s, cc_uint8* data);
CC_API cc_result Stream_OpenFile(struct Stream* s, const String* path);
/* Wrapper for File_Create() then Stream_FromFile() */
CC_API cc_result Stream_CreateFile(struct Stream* s, const String* path);
/* Wrapper for File_OpenOrCreate, then File_Seek(END), then Stream_FromFile() */
cc_result Stream_AppendFile(struct Stream* s, const String* path);
/* Creates or overwrites a file, setting the contents to the given data. */
cc_result Stream_WriteAllTo(const String* path, const cc_uint8* data, cc_uint32 length);
/* Wraps a file, allowing reading from/writing to/seeking in the file. */

0 comments on commit bb30a3a

Please sign in to comment.
You can’t perform that action at this time.