diff --git a/aqvm/base/file/file.c b/aqvm/base/file/file.c index c0ace40..8213508 100644 --- a/aqvm/base/file/file.c +++ b/aqvm/base/file/file.c @@ -39,7 +39,7 @@ int AqvmBaseFile_UnlockFile(struct AqvmBaseFile_File* file) { } void AqvmBaseFile_clearerr(struct AqvmBaseFile_File* stream) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) != 0) { // TODO return; } @@ -48,7 +48,7 @@ void AqvmBaseFile_clearerr(struct AqvmBaseFile_File* stream) { } int AqvmBaseFile_fclose(struct AqvmBaseFile_File* stream) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) != 0) { // TODO return -1; } @@ -70,7 +70,7 @@ int AqvmBaseFile_fclose(struct AqvmBaseFile_File* stream) { } int AqvmBaseFile_feof(struct AqvmBaseFile_File* stream) { - if (stream == NULL || AqvmBaseFile_ferror(stream)) { + if (stream == NULL || AqvmBaseFile_ferror(stream) != 0) { // TODO return -1; } @@ -90,7 +90,7 @@ int AqvmBaseFile_ferror(struct AqvmBaseFile_File* stream) { } int AqvmBaseFile_fflush(struct AqvmBaseFile_File* stream) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) != 0) { // TODO return -1; } @@ -116,7 +116,7 @@ int AqvmBaseFile_fflush(struct AqvmBaseFile_File* stream) { } int AqvmBaseFile_fgetpos(struct AqvmBaseFile_File* stream, fpos_t* pos) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) || + if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) != 0 || pos == NULL) { // TODO return -1; @@ -156,7 +156,7 @@ struct AqvmBaseFile_File* AqvmBaseFile_fopen(const char* filename, } stream->file = fopen(filename, mode); - if (stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream->file == NULL || AqvmBaseFile_ferror(stream) != 0) { free(stream); // TODO return NULL; @@ -174,7 +174,7 @@ struct AqvmBaseFile_File* AqvmBaseFile_fopen(const char* filename, size_t AqvmBaseFile_fread(void* ptr, size_t size, size_t nmemb, struct AqvmBaseFile_File* stream) { if (ptr == NULL || stream == NULL || stream->file == NULL || - AqvmBaseFile_ferror(stream)) { + AqvmBaseFile_ferror(stream) != 0) { // TODO return 0; } @@ -197,7 +197,7 @@ size_t AqvmBaseFile_fread(void* ptr, size_t size, size_t nmemb, struct AqvmBaseFile_File* AqvmBaseFile_freopen( const char* filename, const char* mode, struct AqvmBaseFile_File* stream) { if (filename == NULL || mode == NULL || stream == NULL || - stream->file == NULL || AqvmBaseFile_ferror(stream)) { + stream->file == NULL || AqvmBaseFile_ferror(stream) != 0) { // TODO return NULL; } @@ -229,7 +229,7 @@ struct AqvmBaseFile_File* AqvmBaseFile_freopen( int AqvmBaseFile_fseek(struct AqvmBaseFile_File* stream, long int offset, int whence) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) != 0) { // TODO return -1; } @@ -255,7 +255,7 @@ int AqvmBaseFile_fseek(struct AqvmBaseFile_File* stream, long int offset, } int AqvmBaseFile_fsetpos(struct AqvmBaseFile_File* stream, const fpos_t* pos) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) || + if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) != 0 || pos == NULL) { // TODO return -1; @@ -282,7 +282,7 @@ int AqvmBaseFile_fsetpos(struct AqvmBaseFile_File* stream, const fpos_t* pos) { } long int AqvmBaseFile_ftell(struct AqvmBaseFile_File* stream) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) != 0) { // TODO return -1L; } @@ -310,7 +310,7 @@ long int AqvmBaseFile_ftell(struct AqvmBaseFile_File* stream) { size_t AqvmBaseFile_fwrite(const void* ptr, size_t size, size_t nmemb, struct AqvmBaseFile_File* stream) { if (ptr == NULL || stream == NULL || stream->file == NULL || - AqvmBaseFile_ferror(stream)) { + AqvmBaseFile_ferror(stream) != 0) { // TODO return 0; } @@ -359,7 +359,7 @@ int AqvmBaseFile_rename(const char* old_filename, const char* new_filename) { } void AqvmBaseFile_rewind(struct AqvmBaseFile_File* stream) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) != 0) { // TODO return; } @@ -377,7 +377,7 @@ void AqvmBaseFile_rewind(struct AqvmBaseFile_File* stream) { } void AqvmBaseFile_setbuf(struct AqvmBaseFile_File* stream, char* buffer) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) != 0) { // TODO return; } @@ -396,7 +396,7 @@ void AqvmBaseFile_setbuf(struct AqvmBaseFile_File* stream, char* buffer) { int AqvmBaseFile_setvbuf(struct AqvmBaseFile_File* stream, char* buffer, int mode, size_t size) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) != 0) { // TODO return -1; } @@ -430,7 +430,7 @@ struct AqvmBaseFile_File* AqvmBaseFile_tmpfile(void) { } stream->file = tmpfile(); - if (stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream->file == NULL || AqvmBaseFile_ferror(stream) != 0) { free(stream); // TODO return NULL; diff --git a/aqvm/base/file/file_id/file_id.c b/aqvm/base/file/file_id/file_id.c index 2d112ac..6833a94 100644 --- a/aqvm/base/file/file_id/file_id.c +++ b/aqvm/base/file/file_id/file_id.c @@ -27,7 +27,7 @@ int AqvmBaseFileFileId_GetFileId(const char* filename, return -2; } #elif _WIN32 - if (AqvmBaseFileFileIdUnix_GetFileId(filename, file_id) != 0) { + if (AqvmBaseFileFileIdWindows_GetFileId(filename, file_id) != 0) { // TODO return -3; } @@ -48,7 +48,7 @@ uint32_t AqvmBaseFileFileId_GetFileIdHash(AqvmBaseFileFileId_FileId* file_id) { #ifdef __unix__ return AqvmBaseFileFileIdUnix_GetFileIdHash(file_id); #elif _WIN32 - return AqvmBaseFileFileIdUnix_GetFileIdHash(file_id); + return AqvmBaseFileFileIdWindows_GetFileIdHash(file_id); #else // TODO return 0; diff --git a/aqvm/base/file/file_id/windows/file_id.c b/aqvm/base/file/file_id/windows/file_id.c index 74c0982..96dbfc6 100644 --- a/aqvm/base/file/file_id/windows/file_id.c +++ b/aqvm/base/file/file_id/windows/file_id.c @@ -7,6 +7,8 @@ #include +#include "aqvm/base/hash/hash.h" + int AqvmBaseFileFileIdWindows_GetFileId( const char* filename, AqvmBaseFileFileIdWindows_FileId* file_id) { if (filename == NULL || file_id == NULL) { @@ -23,9 +25,9 @@ int AqvmBaseFileFileIdWindows_GetFileId( BY_HANDLE_FILE_INFORMATION file_info; if (GetFileInformationByHandle(handle_file, &file_info)) { - file_id.dwVolumeSerialNumber = file_info.dwVolumeSerialNumber; - file_id.nFileIndexHigh = file_info.nFileIndexHigh; - file_id.nFileIndexLow = file_info.nFileIndexLow; + file_id->dwVolumeSerialNumber = file_info.dwVolumeSerialNumber; + file_id->nFileIndexHigh = file_info.nFileIndexHigh; + file_id->nFileIndexLow = file_info.nFileIndexLow; } else { // TODO return -3; diff --git a/aqvm/base/file/windows/file.h b/aqvm/base/file/windows/file.h index 279fd78..9be4da5 100644 --- a/aqvm/base/file/windows/file.h +++ b/aqvm/base/file/windows/file.h @@ -13,5 +13,7 @@ HANDLE AqvmBaseFileWindows_FileToHandle(struct AqvmBaseFile_File* file); +// fopen freopen setbuf tmpfile tmpnam vsprintf + #endif #endif \ No newline at end of file diff --git a/aqvm/base/io/io.c b/aqvm/base/io/io.c index 37eafc1..8506041 100644 --- a/aqvm/base/io/io.c +++ b/aqvm/base/io/io.c @@ -43,13 +43,62 @@ int AqvmBaseIo_CloseIo() { return 0; } +int AqvmBaseIo_LockStream(struct AqvmBaseFile_File* stream) { + if (stream == NULL || stream->file == NULL || + AqvmBaseFile_ferror(stream) != 0) { + // TODO + return -1; + } + if (stream->file == stdout || stream->file == stderr || + stream->file == stdin) { + if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { + // TODO + return -2; + } + } else { + if (AqvmBaseProcessFileLock_LockFile(stream) != 0) { + // TODO + return -3; + } + if (AqvmBaseFile_LockFile(stream) != 0) { + // TODO + return -4; + } + } +} + +int AqvmBaseIo_UnlockStream(struct AqvmBaseFile_File* stream) { + if (stream == NULL || stream->file == NULL || + AqvmBaseFile_ferror(stream) != 0) { + // TODO + return -1; + } + if (stream->file == stdout || stream->file == stderr || + stream->file == stdin) { + if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { + // TODO + return -2; + } + } else { + if (AqvmBaseProcessFileLock_LockFile(stream) != 0) { + // TODO + return -3; + } + if (AqvmBaseFile_LockFile(stream) != 0) { + // TODO + return -4; + } + } +} + int AqvmBaseIo_OutputToStream(struct AqvmBaseFile_File* stream, ...) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream == NULL || stream->file == NULL || + AqvmBaseFile_ferror(stream) != 0) { // TODO return -1; } - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_LockStream(stream) != 0) { // TODO return -2; } @@ -67,7 +116,7 @@ int AqvmBaseIo_OutputToStream(struct AqvmBaseFile_File* stream, ...) { } va_end(arg); - if (AqvmBaseThreadingMutex_CloseMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_UnlockStream(stream) != 0) { // TODO return -4; } @@ -76,7 +125,8 @@ int AqvmBaseIo_OutputToStream(struct AqvmBaseFile_File* stream, ...) { } int AqvmBaseIo_fgetc(struct AqvmBaseFile_File* stream) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream == NULL || stream->file == NULL || + AqvmBaseFile_ferror(stream) != 0) { // TODO return -2; } @@ -85,38 +135,16 @@ int AqvmBaseIo_fgetc(struct AqvmBaseFile_File* stream) { return -3; } - if (stream->file == stdin) { - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { - // TODO - return -4; - } - } else { - if (AqvmBaseProcessFileLock_LockFile(stream) != 0) { - // TODO - return -5; - } - if (AqvmBaseFile_LockFile(stream) != 0) { - // TODO - return -6; - } + if (AqvmBaseIo_LockStream(stream) != 0) { + // TODO + return -4; } int result = fgetc(stream->file); - if (stream->file == stdin) { - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex) != 0) { - // TODO - return -7; - } - } else { - if (AqvmBaseProcessFileLock_UnlockFile(stream) != 0) { - // TODO - return -8; - } - if (AqvmBaseFile_UnlockFile(stream) != 0) { - // TODO - return -9; - } + if (AqvmBaseIo_UnlockStream(stream) != 0) { + // TODO + return -5; } if (result == EOF) { @@ -124,15 +152,15 @@ int AqvmBaseIo_fgetc(struct AqvmBaseFile_File* stream) { return EOF; } // TODO - return -10; + return -6; } return result; } char* AqvmBaseIo_fgets(char* str, int n, struct AqvmBaseFile_File* stream) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) || - str == NULL || n <= 0) { + if (stream == NULL || stream->file == NULL || + AqvmBaseFile_ferror(stream) != 0 || str == NULL || n <= 0) { // TODO return NULL; } @@ -141,38 +169,16 @@ char* AqvmBaseIo_fgets(char* str, int n, struct AqvmBaseFile_File* stream) { return NULL; } - if (stream->file == stdin) { - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { - // TODO - return NULL; - } - } else { - if (AqvmBaseProcessFileLock_LockFile(stream) != 0) { - // TODO - return NULL; - } - if (AqvmBaseFile_LockFile(stream) != 0) { - // TODO - return NULL; - } + if (AqvmBaseIo_LockStream(stream) != 0) { + // TODO + return NULL; } char* result = fgets(str, n, stream->file); - if (stream->file == stdin) { - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex) != 0) { - // TODO - return NULL; - } - } else { - if (AqvmBaseProcessFileLock_UnlockFile(stream) != 0) { - // TODO - return NULL; - } - if (AqvmBaseFile_UnlockFile(stream) != 0) { - // TODO - return NULL; - } + if (AqvmBaseIo_UnlockStream(stream) != 0) { + // TODO + return NULL; } if (result == NULL) { @@ -188,8 +194,8 @@ char* AqvmBaseIo_fgets(char* str, int n, struct AqvmBaseFile_File* stream) { int AqvmBaseIo_fprintf(struct AqvmBaseFile_File* stream, const char* format, ...) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) || - format == NULL) { + if (stream == NULL || stream->file == NULL || + AqvmBaseFile_ferror(stream) != 0 || format == NULL) { // TODO return -1; } @@ -198,20 +204,9 @@ int AqvmBaseIo_fprintf(struct AqvmBaseFile_File* stream, const char* format, return -2; } - if (stream->file == stdout || stream->file == stderr) { - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { - // TODO - return -3; - } - } else { - if (AqvmBaseProcessFileLock_LockFile(stream) != 0) { - // TODO - return -4; - } - if (AqvmBaseFile_LockFile(stream) != 0) { - // TODO - return -5; - } + if (AqvmBaseIo_LockStream(stream) != 0) { + // TODO + return -3; } va_list arg; @@ -219,32 +214,22 @@ int AqvmBaseIo_fprintf(struct AqvmBaseFile_File* stream, const char* format, int result = vfprintf(stream->file, format, arg); va_end(arg); - if (stream->file == stdout || stream->file == stderr) { - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex)) { - // TODO - return -6; - } - } else { - if (AqvmBaseProcessFileLock_UnlockFile(stream)) { - // TODO - return -7; - } - if (AqvmBaseFile_UnlockFile(stream)) { - // TODO - return -8; - } + if (AqvmBaseIo_UnlockStream(stream) != 0) { + // TODO + return -4; } if (result < 0) { // TODO - return -9; + return -5; } return 0; } int AqvmBaseIo_fputc(int character, struct AqvmBaseFile_File* stream) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream == NULL || stream->file == NULL || + AqvmBaseFile_ferror(stream) != 0) { // TODO return -2; } @@ -253,43 +238,21 @@ int AqvmBaseIo_fputc(int character, struct AqvmBaseFile_File* stream) { return -3; } - if (stream->file == stdout || stream->file == stderr) { - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { - // TODO - return -4; - } - } else { - if (AqvmBaseProcessFileLock_LockFile(stream) != 0) { - // TODO - return -5; - } - if (AqvmBaseFile_LockFile(stream) != 0) { - // TODO - return -6; - } + if (AqvmBaseIo_LockStream(stream) != 0) { + // TODO + return -4; } int result = fputc(character, stream->file); - if (stream->file == stdout || stream->file == stderr) { - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex)) { - // TODO - return -7; - } - } else { - if (AqvmBaseProcessFileLock_UnlockFile(stream)) { - // TODO - return -8; - } - if (AqvmBaseFile_UnlockFile(stream)) { - // TODO - return -9; - } + if (AqvmBaseIo_UnlockStream(stream) != 0) { + // TODO + return -5; } if (result == EOF) { // TODO - return -10; + return -6; } return 0; @@ -297,7 +260,7 @@ int AqvmBaseIo_fputc(int character, struct AqvmBaseFile_File* stream) { int AqvmBaseIo_fputs(const char* str, struct AqvmBaseFile_File* stream) { if (str == NULL || stream == NULL || stream->file == NULL || - AqvmBaseFile_ferror(stream)) { + AqvmBaseFile_ferror(stream) != 0) { // TODO return -1; } @@ -306,43 +269,21 @@ int AqvmBaseIo_fputs(const char* str, struct AqvmBaseFile_File* stream) { return -2; } - if (stream->file == stdout || stream->file == stderr) { - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { - // TODO - return -3; - } - } else { - if (AqvmBaseProcessFileLock_LockFile(stream) != 0) { - // TODO - return -4; - } - if (AqvmBaseFile_LockFile(stream) != 0) { - // TODO - return -5; - } + if (AqvmBaseIo_LockStream(stream) != 0) { + // TODO + return -3; } int result = fputs(str, stream->file); - if (stream->file == stdout || stream->file == stderr) { - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex)) { - // TODO - return -6; - } - } else { - if (AqvmBaseProcessFileLock_UnlockFile(stream)) { - // TODO - return -7; - } - if (AqvmBaseFile_UnlockFile(stream)) { - // TODO - return -8; - } + if (AqvmBaseIo_UnlockStream(stream) != 0) { + // TODO + return -4; } if (result == EOF) { // TODO - return -9; + return -5; } return 0; @@ -350,8 +291,8 @@ int AqvmBaseIo_fputs(const char* str, struct AqvmBaseFile_File* stream) { int AqvmBaseIo_fscanf(struct AqvmBaseFile_File* stream, const char* format, ...) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) || - format == NULL) { + if (stream == NULL || stream->file == NULL || + AqvmBaseFile_ferror(stream) != 0 || format == NULL) { // TODO return -2; } @@ -360,20 +301,9 @@ int AqvmBaseIo_fscanf(struct AqvmBaseFile_File* stream, const char* format, return -3; } - if (stream->file == stdin) { - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { - // TODO - return -4; - } - } else { - if (AqvmBaseProcessFileLock_LockFile(stream) != 0) { - // TODO - return -5; - } - if (AqvmBaseFile_LockFile(stream) != 0) { - // TODO - return -6; - } + if (AqvmBaseIo_LockStream(stream) != 0) { + // TODO + return -4; } va_list arg; @@ -381,32 +311,22 @@ int AqvmBaseIo_fscanf(struct AqvmBaseFile_File* stream, const char* format, int result = vfscanf(stream->file, format, arg); va_end(arg); - if (stream->file == stdin) { - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex) != 0) { - // TODO - return -7; - } - } else { - if (AqvmBaseProcessFileLock_UnlockFile(stream) != 0) { - // TODO - return -8; - } - if (AqvmBaseFile_UnlockFile(stream) != 0) { - // TODO - return -9; - } + if (AqvmBaseIo_UnlockStream(stream) != 0) { + // TODO + return -5; } if (result == EOF) { // TODO - return -10; + return -6; } return result; } int AqvmBaseIo_getc(struct AqvmBaseFile_File* stream) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream == NULL || stream->file == NULL || + AqvmBaseFile_ferror(stream) != 0) { return -2; } if (stream->file == stdout || stream->file == stderr) { @@ -414,38 +334,16 @@ int AqvmBaseIo_getc(struct AqvmBaseFile_File* stream) { return -3; } - if (stream->file == stdin) { - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { - // TODO - return -4; - } - } else { - if (AqvmBaseProcessFileLock_LockFile(stream) != 0) { - // TODO - return -5; - } - if (AqvmBaseFile_LockFile(stream) != 0) { - // TODO - return -6; - } + if (AqvmBaseIo_LockStream(stream) != 0) { + // TODO + return -4; } int result = getc(stream->file); - if (stream->file == stdin) { - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex) != 0) { - // TODO - return -7; - } - } else { - if (AqvmBaseProcessFileLock_UnlockFile(stream) != 0) { - // TODO - return -8; - } - if (AqvmBaseFile_UnlockFile(stream) != 0) { - // TODO - return -9; - } + if (AqvmBaseIo_UnlockStream(stream) != 0) { + // TODO + return -5; } if (result == EOF) { @@ -453,7 +351,7 @@ int AqvmBaseIo_getc(struct AqvmBaseFile_File* stream) { return EOF; } else { // TODO - return -10; + return -6; } } @@ -461,18 +359,19 @@ int AqvmBaseIo_getc(struct AqvmBaseFile_File* stream) { } int AqvmBaseIo_getchar(void) { - if (AqvmBaseFile_ferror(AqvmBaseIo_stdin)) { + if (AqvmBaseFile_ferror(AqvmBaseIo_stdin) != 0) { // TODO return -2; } - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { + + if (AqvmBaseIo_LockStream(AqvmBaseIo_stdin) != 0) { // TODO return -3; } int result = getchar(); - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_UnlockStream(AqvmBaseIo_stdin) != 0) { // TODO return -4; } @@ -490,17 +389,18 @@ int AqvmBaseIo_getchar(void) { } int AqvmBaseIo_perror(const char* str) { - if (str == NULL) { + if (str == NULL||AqvmBaseFile_ferror(AqvmBaseIo_stdout) != 0) { return -1; } - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { + + if (AqvmBaseIo_LockStream(AqvmBaseIo_stderr) != 0) { // TODO return -2; } perror(str); - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_UnlockStream(AqvmBaseIo_stderr) != 0) { // TODO return -3; } @@ -509,10 +409,11 @@ int AqvmBaseIo_perror(const char* str) { } int AqvmBaseIo_printf(const char* format, ...) { - if (format == NULL) { + if (format == NULL||AqvmBaseFile_ferror(AqvmBaseIo_stdout) != 0) { return -1; } - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { + + if (AqvmBaseIo_LockStream(AqvmBaseIo_stdout) != 0) { // TODO return -2; } @@ -522,7 +423,7 @@ int AqvmBaseIo_printf(const char* format, ...) { int result = vprintf(format, arg); va_end(arg); - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex)) { + if (AqvmBaseIo_UnlockStream(AqvmBaseIo_stdout) != 0) { // TODO return -3; } @@ -536,7 +437,8 @@ int AqvmBaseIo_printf(const char* format, ...) { } int AqvmBaseIo_putc(int character, struct AqvmBaseFile_File* stream) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream == NULL || stream->file == NULL || + AqvmBaseFile_ferror(stream) != 0) { // TODO return -1; } @@ -545,61 +447,40 @@ int AqvmBaseIo_putc(int character, struct AqvmBaseFile_File* stream) { return -2; } - if (stream->file == stdout || stream->file == stderr) { - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { - // TODO - return -3; - } - } else { - if (AqvmBaseProcessFileLock_LockFile(stream) != 0) { - // TODO - return -4; - } - if (AqvmBaseFile_LockFile(stream) != 0) { - // TODO - return -5; - } + if (AqvmBaseIo_LockStream(stream) != 0) { + // TODO + return -3; } int result = putc(character, stream->file); - if (stream->file == stdout || stream->file == stderr) { - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex) != 0) { - // TODO - return -6; - } - } else { - if (AqvmBaseProcessFileLock_UnlockFile(stream) != 0) { - // TODO - return -7; - } - if (AqvmBaseFile_UnlockFile(stream) != 0) { - // TODO - return -8; - } + if (AqvmBaseIo_UnlockStream(stream) != 0) { + // TODO + return -4; } if (result == EOF) { // TODO - return -9; + return -5; } return result; } int AqvmBaseIo_putchar(int character) { - if (AqvmBaseFile_ferror(AqvmBaseIo_stdout)) { + if (AqvmBaseFile_ferror(AqvmBaseIo_stdout) != 0) { // TODO return -2; } - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { + + if (AqvmBaseIo_LockStream(AqvmBaseIo_stdout) != 0) { // TODO return -3; } int result = putchar(character); - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_UnlockStream(AqvmBaseIo_stdout) != 0) { // TODO return -4; } @@ -613,19 +494,19 @@ int AqvmBaseIo_putchar(int character) { } int AqvmBaseIo_puts(const char* str) { - if (str == NULL) { + if (str == NULL||AqvmBaseFile_ferror(AqvmBaseIo_stdout) != 0) { // TODO return -1; } - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_LockStream(AqvmBaseIo_stdout) != 0) { // TODO return -2; } int result = puts(str); - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_UnlockStream(AqvmBaseIo_stdout) != 0) { // TODO return -3; } @@ -639,12 +520,12 @@ int AqvmBaseIo_puts(const char* str) { } int AqvmBaseIo_scanf(const char* format, ...) { - if (format == NULL) { + if (format == NULL || AqvmBaseFile_ferror(AqvmBaseIo_stdin) != 0) { // TODO return -1; } - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_LockStream(AqvmBaseIo_stdout) != 0) { // TODO return -2; } @@ -654,7 +535,7 @@ int AqvmBaseIo_scanf(const char* format, ...) { int result = vfscanf(stdin, format, arg); va_end(arg); - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_UnlockStream(AqvmBaseIo_stdout) != 0) { // TODO return -3; } @@ -668,12 +549,13 @@ int AqvmBaseIo_scanf(const char* format, ...) { } int AqvmBaseIo_snprintf(char* str, size_t size, const char* format, ...) { - if (str == NULL || format == NULL) { + if (str == NULL || format == NULL || + AqvmBaseFile_ferror(AqvmBaseIo_stdout) != 0) { // TODO return -1; } - if (AqvmBaseThreadingMutex_InitializeMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_LockStream(AqvmBaseIo_stdout) != 0) { // TODO return -2; } @@ -683,7 +565,7 @@ int AqvmBaseIo_snprintf(char* str, size_t size, const char* format, ...) { int result = vsnprintf(str, size, format, arg); va_end(arg); - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_UnlockStream(AqvmBaseIo_stdout) != 0) { // TODO return -3; } @@ -697,12 +579,13 @@ int AqvmBaseIo_snprintf(char* str, size_t size, const char* format, ...) { } int AqvmBaseIo_sprintf(char* str, const char* format, ...) { - if (str == NULL || format == NULL) { + if (str == NULL || format == NULL || + AqvmBaseFile_ferror(AqvmBaseIo_stdout) != 0) { // TODO return -1; } - if (AqvmBaseThreadingMutex_InitializeMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_LockStream(AqvmBaseIo_stdout) != 0) { // TODO return -2; } @@ -712,7 +595,7 @@ int AqvmBaseIo_sprintf(char* str, const char* format, ...) { int result = vsprintf(str, format, arg); va_end(arg); - if (AqvmBaseThreadingMutex_CloseMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_UnlockStream(AqvmBaseIo_stdout) != 0) { // TODO return -3; } @@ -726,12 +609,13 @@ int AqvmBaseIo_sprintf(char* str, const char* format, ...) { } int AqvmBaseIo_sscanf(const char* str, const char* format, ...) { - if (str == NULL || format == NULL) { + if (str == NULL || format == NULL || + AqvmBaseFile_ferror(AqvmBaseIo_stdout) != 0) { // TODO return -1; } - if (AqvmBaseThreadingMutex_InitializeMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_LockStream(AqvmBaseIo_stdout) != 0) { // TODO return -2; } @@ -741,7 +625,7 @@ int AqvmBaseIo_sscanf(const char* str, const char* format, ...) { int result = vsscanf(str, format, arg); va_end(arg); - if (AqvmBaseThreadingMutex_CloseMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_UnlockStream(AqvmBaseIo_stdout) != 0) { // TODO return -3; } @@ -755,19 +639,20 @@ int AqvmBaseIo_sscanf(const char* str, const char* format, ...) { } int AqvmBaseIo_ungetc(int character, struct AqvmBaseFile_File* stream) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream)) { + if (stream == NULL || stream->file == NULL || + AqvmBaseFile_ferror(stream) != 0) { // TODO return -1; } - if (AqvmBaseThreadingMutex_InitializeMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_LockStream(AqvmBaseIo_stdout) != 0) { // TODO return -2; } int result = ungetc(character, stream->file); - if (AqvmBaseThreadingMutex_CloseMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_UnlockStream(AqvmBaseIo_stdout) != 0) { // TODO return -3; } @@ -782,8 +667,8 @@ int AqvmBaseIo_ungetc(int character, struct AqvmBaseFile_File* stream) { int AqvmBaseIo_vfprintf(struct AqvmBaseFile_File* stream, const char* format, va_list arg) { - if (stream == NULL || stream->file == NULL || AqvmBaseFile_ferror(stream) || - format == NULL) { + if (stream == NULL || stream->file == NULL || + AqvmBaseFile_ferror(stream) != 0 || format == NULL) { // TODO return -1; } @@ -792,53 +677,32 @@ int AqvmBaseIo_vfprintf(struct AqvmBaseFile_File* stream, const char* format, return -2; } - if (stream->file == stdout || stream->file == stderr) { - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { - // TODO - return -3; - } - } else { - if (AqvmBaseProcessFileLock_LockFile(stream) != 0) { - // TODO - return -4; - } - if (AqvmBaseFile_LockFile(stream) != 0) { - // TODO - return -5; - } + if (AqvmBaseIo_LockStream(stream) != 0) { + // TODO + return -3; } int result = vfprintf(stream->file, format, arg); - if (result < 0) { + + if (AqvmBaseIo_UnlockStream(AqvmBaseIo_stdout) != 0) { // TODO - return -6; + return -4; } - va_end(arg); - if (stream->file == stdout || stream->file == stderr) { - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex)) { - // TODO - return -7; - } - } else { - if (AqvmBaseProcessFileLock_UnlockFile(stream)) { - // TODO - return -8; - } - if (AqvmBaseFile_UnlockFile(stream)) { - // TODO - return -9; - } + if (result < 0) { + // TODO + return -5; } return 0; } int AqvmBaseIo_vprintf(const char* format, va_list arg) { - if (format == NULL) { + if (format == NULL || AqvmBaseFile_ferror(AqvmBaseIo_stdout) != 0) { return -1; } - if (AqvmBaseThreadingMutex_LockMutex(&AqvmBaseIo_printMutex) != 0) { + + if (AqvmBaseIo_LockStream(AqvmBaseIo_stdout) != 0) { // TODO return -2; } @@ -850,7 +714,7 @@ int AqvmBaseIo_vprintf(const char* format, va_list arg) { } va_end(arg); - if (AqvmBaseThreadingMutex_UnlockMutex(&AqvmBaseIo_printMutex)) { + if (AqvmBaseIo_UnlockStream(AqvmBaseIo_stdout) != 0) { // TODO return -4; } @@ -859,26 +723,27 @@ int AqvmBaseIo_vprintf(const char* format, va_list arg) { } int AqvmBaseIo_vsprintf(char* str, const char* format, va_list arg) { - if (str == NULL || format == NULL) { + if (str == NULL || format == NULL || + AqvmBaseFile_ferror(AqvmBaseIo_stdout) != 0) { // TODO return -1; } - if (AqvmBaseThreadingMutex_InitializeMutex(&AqvmBaseIo_printMutex) != 0) { + if (AqvmBaseIo_LockStream(AqvmBaseIo_stdout) != 0) { // TODO return -2; } int result = vsprintf(str, format, arg); - if (result < 0) { + + if (AqvmBaseIo_UnlockStream(AqvmBaseIo_stdout) != 0) { // TODO - return -3; + return -4; } - va_end(arg); - if (AqvmBaseThreadingMutex_CloseMutex(&AqvmBaseIo_printMutex) != 0) { + if (result < 0) { // TODO - return -4; + return -5; } return 0; diff --git a/aqvm/base/io/io.h b/aqvm/base/io/io.h index cc4c42c..5ae5a00 100644 --- a/aqvm/base/io/io.h +++ b/aqvm/base/io/io.h @@ -19,6 +19,10 @@ int AqvmBaseIo_InitializeIo(); int AqvmBaseIo_CloseIo(); +int AqvmBaseIo_LockStream(struct AqvmBaseFile_File* stream); + +int AqvmBaseIo_UnlockStream(struct AqvmBaseFile_File* stream); + int AqvmBaseIo_OutputToStream(struct AqvmBaseFile_File* stream, ...); int AqvmBaseIo_fgetc(struct AqvmBaseFile_File* stream); diff --git a/aqvm/base/process/file_lock/windows/file_lock.c b/aqvm/base/process/file_lock/windows/file_lock.c index 6c42cbc..8ac8554 100644 --- a/aqvm/base/process/file_lock/windows/file_lock.c +++ b/aqvm/base/process/file_lock/windows/file_lock.c @@ -16,7 +16,7 @@ int AqvmBaseProcessFileLockWindows_LockFile(struct AqvmBaseFile_File* file) { return -1; } - HANDLE handle_file = AqvmBaseFileWindows_FileToHandle(file->file); + HANDLE handle_file = AqvmBaseFileWindows_FileToHandle(file); if (handle_file == INVALID_HANDLE_VALUE) { // TODO return -2; @@ -35,7 +35,7 @@ int AqvmBaseProcessFileLockWindows_UnlockFile(struct AqvmBaseFile_File* file) { return -1; } - HANDLE handle_file = AqvmBaseFileWindows_FileToHandle(file->file); + HANDLE handle_file = AqvmBaseFileWindows_FileToHandle(file); if (handle_file == INVALID_HANDLE_VALUE) { // TODO return -2;