[4] | 1 | /**
|
---|
| 2 | * cf_file.c
|
---|
| 3 | */
|
---|
| 4 | #include "cf_file.h"
|
---|
| 5 |
|
---|
| 6 | #ifdef _WIN32
|
---|
[14] | 7 | # include <stdio.h>
|
---|
[4] | 8 | # include <io.h>
|
---|
[14] | 9 | # include <sys/stat.h>
|
---|
| 10 |
|
---|
| 11 | # define S_IWUSR _S_IWRITE
|
---|
| 12 | # define S_IRUSR _S_IREAD
|
---|
| 13 | # define S_IXUSR _S_IEXEC
|
---|
| 14 | # define S_IRGRP 0x00000000
|
---|
| 15 | # define S_IWGRP 0x00000000
|
---|
| 16 | # define S_IXGRP 0x00000000
|
---|
| 17 | # define S_IROTH 0x00000000
|
---|
| 18 | # define S_IWOTH 0x00000000
|
---|
| 19 | # define S_IXOTH 0x00000000
|
---|
[4] | 20 | #else // #ifdef _WIN32
|
---|
| 21 | # include <unistd.h>
|
---|
[26] | 22 | # define O_BINARY 0x00000000
|
---|
[4] | 23 | #endif // #ifdef _WIN32
|
---|
| 24 |
|
---|
[15] | 25 | #define FILE_MODE S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH
|
---|
| 26 |
|
---|
[4] | 27 | int
|
---|
[16] | 28 | CF_File_Open (const char * path,
|
---|
| 29 | const CF_FILE_FLAG flag)
|
---|
[4] | 30 | {
|
---|
[26] | 31 | int result = open (path, flag|O_BINARY);
|
---|
[4] | 32 |
|
---|
| 33 | if (result < 0)
|
---|
| 34 | return CF_ERROR_FILE_OPEN;
|
---|
| 35 |
|
---|
| 36 | return result;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | int
|
---|
| 40 | CF_File_Create (const char * path)
|
---|
| 41 | {
|
---|
[16] | 42 | int result = open (path, CF_FILE_CR|CF_FILE_WO|CF_FILE_TR, FILE_MODE);
|
---|
[4] | 43 |
|
---|
| 44 | if (result < 0)
|
---|
| 45 | return CF_ERROR_FILE_CREATE;
|
---|
| 46 |
|
---|
| 47 | return result;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | int
|
---|
| 51 | CF_File_Close (const int fd)
|
---|
| 52 | {
|
---|
| 53 | int result = 0;
|
---|
| 54 |
|
---|
| 55 | if (fd < 0)
|
---|
| 56 | return CF_ERROR_FILE_INVALID_ARGS;
|
---|
| 57 |
|
---|
| 58 | result = close (fd);
|
---|
| 59 |
|
---|
| 60 | if (result < 0)
|
---|
| 61 | return CF_ERROR_FILE_CLOSE;
|
---|
| 62 |
|
---|
| 63 | return CF_OK;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | int
|
---|
| 67 | CF_File_Read (const int fd,
|
---|
| 68 | void * buf,
|
---|
| 69 | const size_t len)
|
---|
| 70 | {
|
---|
| 71 | int result = (int) read (fd, buf, len);
|
---|
| 72 |
|
---|
| 73 | if (result < 0)
|
---|
| 74 | return CF_ERROR_FILE_READ;
|
---|
| 75 |
|
---|
[26] | 76 | return result;
|
---|
[4] | 77 | }
|
---|
| 78 |
|
---|
| 79 | int
|
---|
| 80 | CF_File_Write (const int fd,
|
---|
| 81 | const void * buf,
|
---|
| 82 | const size_t len)
|
---|
| 83 | {
|
---|
| 84 | int result = (int) write (fd, buf, len);
|
---|
| 85 |
|
---|
| 86 | if (result != len)
|
---|
| 87 | return CF_ERROR_FILE_WRITE;
|
---|
| 88 |
|
---|
| 89 | return CF_OK;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | int
|
---|
| 93 | CF_File_GetSize (const int fd)
|
---|
| 94 | {
|
---|
| 95 | int result = (int) lseek (fd, 0, SEEK_END);
|
---|
| 96 |
|
---|
| 97 | if (result < 0 || lseek (fd, 0, SEEK_SET) < 0)
|
---|
| 98 | return CF_ERROR_FILE_GET_SIZE;
|
---|
| 99 |
|
---|
| 100 | return result;
|
---|
| 101 | }
|
---|