Skip to content

Commit

Permalink
Temp save. Not complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
ax-6 committed Aug 9, 2024
1 parent ae1b11c commit b6791fb
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 43 deletions.
85 changes: 59 additions & 26 deletions aqvm/base/file/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

#include "aqvm/base/io/io.h"
#include "aqvm/base/process/file_lock/file_lock.h"
#include "aqvm/base/threading/file_lock/file_lock.h"
#include "aqvm/base/threading/mutex/mutex.h"

int AqvmBaseFile_LockFile(struct AqvmBaseFile_File* stream) {
if (AqvmBaseFile_CheckStream(stream)) {
if (AqvmBaseFile_CheckStream(stream) != 0) {
// TODO
return -1;
}
Expand All @@ -27,7 +28,7 @@ int AqvmBaseFile_LockFile(struct AqvmBaseFile_File* stream) {
}

int AqvmBaseFile_UnlockFile(struct AqvmBaseFile_File* stream) {
if (AqvmBaseFile_CheckStream(stream)) {
if (AqvmBaseFile_CheckStream(stream) != 0) {
// TODO
return -1;
}
Expand Down Expand Up @@ -108,7 +109,7 @@ int AqvmBaseFile_CheckStream(struct AqvmBaseFile_File* stream) {
}

void AqvmBaseFile_clearerr(struct AqvmBaseFile_File* stream) {
if (AqvmBaseFile_CheckStream(stream)) {
if (AqvmBaseFile_CheckStream(stream) != 0) {
// TODO
return;
}
Expand All @@ -117,29 +118,30 @@ void AqvmBaseFile_clearerr(struct AqvmBaseFile_File* stream) {
}

int AqvmBaseFile_fclose(struct AqvmBaseFile_File* stream) {
if (AqvmBaseFile_CheckStream(stream)) {
if (AqvmBaseFile_CheckStream(stream) != 0) {
// TODO
return -1;
}

int result = fclose(stream->file);
/*if (AqvmBaseThreadingMutex_CloseMutex(&stream->mutex)) {
free(stream->identifier);
if (AqvmBaseThreadingFileLock_RemoveFileLock(stream) != 0) {
// TODO
free(stream);
return -2;
}*/
}
free(stream);

if (result != 0) {
// TODO
return -3;
}

free(stream);

return 0;
}

int AqvmBaseFile_feof(struct AqvmBaseFile_File* stream) {
if (AqvmBaseFile_CheckStream(stream)) {
if (AqvmBaseFile_CheckStream(stream) != 0) {
// TODO
return -1;
}
Expand All @@ -159,7 +161,7 @@ int AqvmBaseFile_ferror(struct AqvmBaseFile_File* stream) {
}

int AqvmBaseFile_fflush(struct AqvmBaseFile_File* stream) {
if (AqvmBaseFile_CheckStream(stream)) {
if (AqvmBaseFile_CheckStream(stream) != 0) {
// TODO
return -1;
}
Expand All @@ -185,7 +187,7 @@ int AqvmBaseFile_fflush(struct AqvmBaseFile_File* stream) {
}

int AqvmBaseFile_fgetpos(struct AqvmBaseFile_File* stream, fpos_t* pos) {
if (AqvmBaseFile_CheckStream(stream) || pos == NULL) {
if (AqvmBaseFile_CheckStream(stream) != 0 || pos == NULL) {
// TODO
return -1;
}
Expand Down Expand Up @@ -222,26 +224,44 @@ struct AqvmBaseFile_File* AqvmBaseFile_fopen(const char* filename,
// TODO
return NULL;
}
stream->identifier = (AqvmBaseFileIdentifier_Identifier*)malloc(
sizeof(AqvmBaseFileIdentifier_Identifier));
if (stream->identifier == NULL) {
// TODO
free(stream);
return NULL;
}

stream->file = fopen(filename, mode);
if (stream->file == NULL || AqvmBaseFile_ferror(stream) != 0) {
free(stream->identifier);
free(stream);
// TODO
return NULL;
}

/*if (AqvmBaseThreadingMutex_InitializeMutex(&stream->mutex) != 0) {
if (AqvmBaseFileIdentifier_GetIdentifier(filename, &stream->identifier) !=
0) {
// TODO
fclose(stream->file);
free(stream->identifier);
free(stream);
return NULL;
}*/
}

if (AqvmBaseFileThreadingFileLock_AddFileLock(stream) != 0) {
fclose(stream->file);
free(stream->identifier);
free(stream);
return NULL;
}

return stream;
}

size_t AqvmBaseFile_fread(void* ptr, size_t size, size_t nmemb,
struct AqvmBaseFile_File* stream) {
if (ptr == NULL || AqvmBaseFile_CheckStream(stream)) {
if (ptr == NULL || AqvmBaseFile_CheckStream(stream) != 0) {
// TODO
return 0;
}
Expand All @@ -263,7 +283,8 @@ 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 || AqvmBaseFile_CheckStream(stream)) {
if (filename == NULL || mode == NULL ||
AqvmBaseFile_CheckStream(stream) != 0) {
// TODO
return NULL;
}
Expand Down Expand Up @@ -294,7 +315,7 @@ struct AqvmBaseFile_File* AqvmBaseFile_freopen(

int AqvmBaseFile_fseek(struct AqvmBaseFile_File* stream, long int offset,
int whence) {
if (AqvmBaseFile_CheckStream(stream)) {
if (AqvmBaseFile_CheckStream(stream) != 0) {
// TODO
return -1;
}
Expand All @@ -320,7 +341,7 @@ int AqvmBaseFile_fseek(struct AqvmBaseFile_File* stream, long int offset,
}

int AqvmBaseFile_fsetpos(struct AqvmBaseFile_File* stream, const fpos_t* pos) {
if (AqvmBaseFile_CheckStream(stream) || pos == NULL) {
if (AqvmBaseFile_CheckStream(stream) != 0 || pos == NULL) {
// TODO
return -1;
}
Expand All @@ -346,7 +367,7 @@ int AqvmBaseFile_fsetpos(struct AqvmBaseFile_File* stream, const fpos_t* pos) {
}

long int AqvmBaseFile_ftell(struct AqvmBaseFile_File* stream) {
if (AqvmBaseFile_CheckStream(stream)) {
if (AqvmBaseFile_CheckStream(stream) != 0) {
// TODO
return -1L;
}
Expand All @@ -373,7 +394,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 || AqvmBaseFile_CheckStream(stream)) {
if (ptr == NULL || AqvmBaseFile_CheckStream(stream) != 0) {
// TODO
return 0;
}
Expand Down Expand Up @@ -422,7 +443,7 @@ int AqvmBaseFile_rename(const char* old_filename, const char* new_filename) {
}

void AqvmBaseFile_rewind(struct AqvmBaseFile_File* stream) {
if (AqvmBaseFile_CheckStream(stream)) {
if (AqvmBaseFile_CheckStream(stream) != 0) {
// TODO
return;
}
Expand All @@ -440,7 +461,7 @@ void AqvmBaseFile_rewind(struct AqvmBaseFile_File* stream) {
}

void AqvmBaseFile_setbuf(struct AqvmBaseFile_File* stream, char* buffer) {
if (AqvmBaseFile_CheckStream(stream)) {
if (AqvmBaseFile_CheckStream(stream) != 0) {
// TODO
return;
}
Expand All @@ -459,7 +480,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 (AqvmBaseFile_CheckStream(stream)) {
if (AqvmBaseFile_CheckStream(stream) != 0) {
// TODO
return -1;
}
Expand All @@ -484,7 +505,7 @@ int AqvmBaseFile_setvbuf(struct AqvmBaseFile_File* stream, char* buffer,
return result;
}

struct AqvmBaseFile_File* AqvmBaseFile_tmpfile(void) {
struct AqvmBaseFile_File* AqvmBaseFile_tmpfile() {
struct AqvmBaseFile_File* stream =
(struct AqvmBaseFile_File*)malloc(sizeof(struct AqvmBaseFile_File));
if (stream == NULL) {
Expand All @@ -498,9 +519,21 @@ struct AqvmBaseFile_File* AqvmBaseFile_tmpfile(void) {
// TODO
return NULL;
}

/*AqvmBaseThreadingMutex_InitializeMutex(&stream->mutex);*/

stream->identifier = (AqvmBaseFileIdentifier_Identifier*)malloc(
sizeof(AqvmBaseFileIdentifier_Identifier));
if (stream->identifier == NULL) {
// TODO
fclose(stream->file);
free(stream);
return NULL;
}
if (AqvmBaseFileThreadingFileLock_AddFileLock(stream) != 0) {
// TODO
fclose(stream->file);
free(stream->identifier);
free(stream);
return NULL;
}
return stream;
}

Expand Down
6 changes: 3 additions & 3 deletions aqvm/base/file/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

struct AqvmBaseFile_File {
FILE* file;
AqvmBaseFileIdentifier_Identifier identifier;
struct AqvmBaseFileReadWriteLock_ReadWriteLock lock;
AqvmBaseFileIdentifier_Identifier* identifier;
struct AqvmBaseFileReadWriteLock_ReadWriteLock* lock;
};

int AqvmBaseFile_LockFile(struct AqvmBaseFile_File* stream);
Expand Down Expand Up @@ -69,7 +69,7 @@ void AqvmBaseFile_setbuf(struct AqvmBaseFile_File* stream, char* buffer);
int AqvmBaseFile_setvbuf(struct AqvmBaseFile_File* stream, char* buffer,
int mode, size_t size);

struct AqvmBaseFile_File* AqvmBaseFile_tmpfile(void);
struct AqvmBaseFile_File* AqvmBaseFile_tmpfile();

char* AqvmBaseFile_tmpnam(char* str);

Expand Down
3 changes: 2 additions & 1 deletion aqvm/base/file/read_write_lock/read_write_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ int AqvmBaseFileReadWriteLock_LockReadLock(
return -1;
}

if (AqvmBaseThreadingMutex_LockMutex(&read_write_lock->mutex) != 0) {
if (read_write_lock->read_count == 0 &&
AqvmBaseThreadingMutex_LockMutex(&read_write_lock->mutex) != 0) {
// TODO
return -2;
}
Expand Down
1 change: 1 addition & 0 deletions aqvm/base/file/read_write_lock/read_write_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

struct AqvmBaseFileReadWriteLock_ReadWriteLock {
AqvmBaseThreadingMutex_Mutex mutex;
int lock_count;
int read_count;
};

Expand Down
2 changes: 1 addition & 1 deletion aqvm/base/hash/table/table.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int AqvmBaseHashTable_InitializeHashTable(
return -2;
}
for (size_t i = 0; i < capacity; i++) {
if (AqvmBaseLinkedList_InitializeLinkedList(&hash_table->data[i])) {
if (AqvmBaseLinkedList_InitializeLinkedList(&hash_table->data[i])!=0) {
// TODO
return -3;
}
Expand Down
8 changes: 4 additions & 4 deletions aqvm/base/io/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ int AqvmBaseIo_fgetc(struct AqvmBaseFile_File* stream) {
}

if (result == EOF) {
if (AqvmBaseFile_feof(stream)) {
if (AqvmBaseFile_feof(stream) != 0) {
return EOF;
}
// TODO
Expand Down Expand Up @@ -166,7 +166,7 @@ char* AqvmBaseIo_fgets(char* str, int n, struct AqvmBaseFile_File* stream) {
}

if (result == NULL) {
if (AqvmBaseFile_feof(stream)) {
if (AqvmBaseFile_feof(stream) != 0) {
return NULL;
}
// TODO
Expand Down Expand Up @@ -326,7 +326,7 @@ int AqvmBaseIo_getc(struct AqvmBaseFile_File* stream) {
}

if (result == EOF) {
if (AqvmBaseFile_feof(stream)) {
if (AqvmBaseFile_feof(stream) != 0) {
return EOF;
} else {
// TODO
Expand Down Expand Up @@ -356,7 +356,7 @@ int AqvmBaseIo_getchar(void) {
}

if (result == EOF) {
if (AqvmBaseFile_feof(AqvmBaseIo_stdin)) {
if (AqvmBaseFile_feof(AqvmBaseIo_stdin) != 0) {
return EOF;
} else {
// TODO
Expand Down
2 changes: 1 addition & 1 deletion aqvm/base/linked_list/linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int AqvmBaseLinkedList_CloseLinkedList(
}

while (list->capacity > 0) {
if (AqvmBaseLinkedList_DeleteNode(list, 0)) {
if (AqvmBaseLinkedList_DeleteNode(list, 0) != 0) {
// TODO
return -2;
}
Expand Down
2 changes: 1 addition & 1 deletion aqvm/base/pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#ifndef AQ_AQVM_BASE_PAIR_PAIR_H_
#define AQ_AQVM_BASE_PAIR_PAIR_H_

struct AqvmBaseHashTable_Pair {
struct AqvmBase_Pair {
void* key;
void* value;
};
Expand Down
4 changes: 2 additions & 2 deletions aqvm/base/process/file_lock/file_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

int AqvmBaseProcessFileLock_LockFile(struct AqvmBaseFile_File* file) {
#ifdef __unix__
if (AqvmBaseProcessFileLockUnix_LockFile(file)) {
if (AqvmBaseProcessFileLockUnix_LockFile(file) != 0) {
// TODO
return -1;
}
Expand All @@ -33,7 +33,7 @@ int AqvmBaseProcessFileLock_LockFile(struct AqvmBaseFile_File* file) {

int AqvmBaseProcessFileLock_UnlockFile(struct AqvmBaseFile_File* file) {
#ifdef __unix__
if (AqvmBaseProcessFileLockUnix_UnlockFile(file)) {
if (AqvmBaseProcessFileLockUnix_UnlockFile(file) != 0) {
// TODO
return -1;
}
Expand Down
Loading

0 comments on commit b6791fb

Please sign in to comment.