fix(file): corrected typing for linux implementation of read and write file
This commit is contained in:
parent
d44d460cad
commit
6a5a80b3f5
1 changed files with 20 additions and 5 deletions
25
src/file.c
25
src/file.c
|
|
@ -47,15 +47,22 @@ CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned l
|
||||||
}
|
}
|
||||||
|
|
||||||
*size = file_stat.st_size;
|
*size = file_stat.st_size;
|
||||||
*content = (char*)malloc(file_stat.st_size * sizeof(char));
|
*content = (char*)malloc((file_stat.st_size + 1) * sizeof(char));
|
||||||
if (content == NULL) {
|
if (content == NULL) {
|
||||||
close(file);
|
close(file);
|
||||||
return COSMS_FILE_FAILED_TO_ALLOCATE;
|
return COSMS_FILE_FAILED_TO_ALLOCATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int remaining_bytes = file_stat.st_size;
|
unsigned long long remaining_bytes = file_stat.st_size;
|
||||||
while (remaining_bytes != 0) {
|
while (remaining_bytes != 0) {
|
||||||
int read_bytes = read(file, (*content) + (file_stat.st_size - remaining_bytes), remaining_bytes);
|
unsigned int bytes_to_read;
|
||||||
|
if (remaining_bytes > UINT_MAX) {
|
||||||
|
bytes_to_read = UINT_MAX;
|
||||||
|
} else {
|
||||||
|
bytes_to_read = (unsigned int)remaining_bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
int read_bytes = read(file, (*content) + (file_stat.st_size - remaining_bytes), bytes_to_read);
|
||||||
if (read_bytes == -1) {
|
if (read_bytes == -1) {
|
||||||
free(*content);
|
free(*content);
|
||||||
close(file);
|
close(file);
|
||||||
|
|
@ -65,6 +72,7 @@ CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned l
|
||||||
remaining_bytes -= read_bytes;
|
remaining_bytes -= read_bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
(*content)[file_stat.st_size] = '\0';
|
||||||
close(file);
|
close(file);
|
||||||
#elif defined(_WIN32)
|
#elif defined(_WIN32)
|
||||||
HANDLE file = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
HANDLE file = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||||
|
|
@ -154,9 +162,16 @@ CosmsFileError cosms_core_file_write(const char *path, const char *content, unsi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int remaining_bytes = size;
|
unsigned long long remaining_bytes = size;
|
||||||
while (remaining_bytes != 0) {
|
while (remaining_bytes != 0) {
|
||||||
int written_bytes = write(file, content + (size - remaining_bytes), remaining_bytes);
|
unsigned int bytes_to_write;
|
||||||
|
if (remaining_bytes > UINT_MAX) {
|
||||||
|
bytes_to_write = UINT_MAX;
|
||||||
|
} else {
|
||||||
|
bytes_to_write = (unsigned int)remaining_bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
int written_bytes = write(file, content + (size - remaining_bytes), bytes_to_write);
|
||||||
if (written_bytes == -1) {
|
if (written_bytes == -1) {
|
||||||
close(file);
|
close(file);
|
||||||
return COSMS_FILE_FAILED_TO_WRITE;
|
return COSMS_FILE_FAILED_TO_WRITE;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue