Add asserts
All checks were successful
CI / build-and-analyze (push) Successful in 1m9s

This commit is contained in:
2026-08-08 16:47:24 +03:00
parent ce2f70896e
commit 5a189f6f1d
3 changed files with 110 additions and 14 deletions

View File

@@ -1,5 +1,6 @@
#include "../CgeFile.h"
#include <windows.h>
#include <assert.h>
struct CgeFile {
HANDLE handle;
@@ -52,6 +53,9 @@ static int errorCodeFromError(void) {
static int fileInit(CgeFile *file, const char *path, int mode) {
DWORD access = 0, how = 0;
assert(file != NULL);
assert(path != NULL);
if (!path)
return CGE_FILE_EINVAL;
@@ -96,17 +100,24 @@ CgeFile *CgeFileNew(const char *path, int mode, int *result) {
CgeFile *file;
int code = 0;
assert(path != NULL);
if ((file = malloc(sizeof(*file)))) {
if ((code = fileInit(file, path, mode))) {
free(file);
file = NULL;
}
}
if (result) *result = code;
if (result)
*result = code;
return file;
}
void CgeFileFree(CgeFile *file) {
if (!file)
return;
CloseHandle(file->handle);
free(file);
}
@@ -114,15 +125,22 @@ void CgeFileFree(CgeFile *file) {
int CgeFileRead(CgeFile *file, void *buffer, size_t size, size_t *actual) {
DWORD readed;
assert(file != NULL);
assert(file->handle != INVALID_HANDLE_VALUE);
assert(buffer != NULL);
if (file->flags & CGE_FILE_ERROR)
return 0;
if (!ReadFile(file->handle, buffer, (DWORD)size, &readed, NULL))
goto error;
if (!readed) file->flags |= CGE_FILE_EOF;
else file->flags &= ~CGE_FILE_EOF;
if (actual) *actual = readed;
if (!readed)
file->flags |= CGE_FILE_EOF;
else
file->flags &= ~CGE_FILE_EOF;
if (actual)
*actual = readed;
return 1;
@@ -137,6 +155,10 @@ int CgeFileWrite(CgeFile *file, const void *buffer, size_t size,
DWORD written;
LARGE_INTEGER position;
assert(file != NULL);
assert(file->handle != INVALID_HANDLE_VALUE);
assert(buffer != NULL);
if (file->flags & CGE_FILE_ERROR)
return 0;
@@ -149,7 +171,8 @@ int CgeFileWrite(CgeFile *file, const void *buffer, size_t size,
if (!WriteFile(file->handle, buffer, (DWORD)size, &written, NULL))
goto error;
if (actual) *actual = written;
if (actual)
*actual = written;
return 1;
@@ -162,6 +185,10 @@ error:
int CgeFileTell(CgeFile *file, int64_t *offset) {
LARGE_INTEGER dummy, position;
assert(file != NULL);
assert(file->handle != INVALID_HANDLE_VALUE);
assert(offset != NULL);
if (file->flags & CGE_FILE_ERROR)
return 0;
@@ -181,6 +208,10 @@ error:
int CgeFileSeek(CgeFile *file, int64_t offset, int whence) {
LARGE_INTEGER position;
assert(file != NULL);
assert(file->handle != INVALID_HANDLE_VALUE);
assert(whence == CGE_FILE_SET || whence == CGE_FILE_CUR || whence == CGE_FILE_END);
if (file->flags & CGE_FILE_ERROR)
return 0;
@@ -198,6 +229,9 @@ error:
}
int CgeFileFlush(CgeFile *file) {
assert(file != NULL);
assert(file->handle != INVALID_HANDLE_VALUE);
if (!FlushFileBuffers(file->handle))
goto error;
@@ -212,6 +246,9 @@ error:
int CgeFileSize(CgeFile *file, int64_t *size) {
LARGE_INTEGER dummy;
assert(file != NULL);
assert(file->handle != INVALID_HANDLE_VALUE);
if (file->flags & CGE_FILE_ERROR)
return 0;
@@ -228,15 +265,22 @@ error:
}
int CgeFileFlags(CgeFile *file, int *flags) {
assert(file != NULL);
assert(flags != NULL);
*flags = file->flags;
return 1;
}
int CgeFileClear(CgeFile *file) {
assert(file != NULL);
file->flags &= ~CGE_FILE_ERROR;
return 1;
}
int CgeFileErrorCode(CgeFile *file) {
assert(file != NULL);
return file->lastError;
}