Skip to content

Commit

Permalink
File_Append -> File_OpenOrCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Dec 1, 2019
1 parent f475eec commit bb30a3a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 21 deletions.
8 changes: 3 additions & 5 deletions src/Chat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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;
}

Expand Down
7 changes: 2 additions & 5 deletions src/Logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 5 additions & 9 deletions src/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions src/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
10 changes: 10 additions & 0 deletions src/Stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down

0 comments on commit bb30a3a

Please sign in to comment.