[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>
|
---|
| 22 | #endif // #ifdef _WIN32
|
---|
| 23 |
|
---|
| 24 | int
|
---|
| 25 | CF_File_Open (const char * path,
|
---|
| 26 | const int flag)
|
---|
| 27 | {
|
---|
| 28 | int result = open (path, flag, 0);
|
---|
| 29 |
|
---|
| 30 | if (result < 0)
|
---|
| 31 | return CF_ERROR_FILE_OPEN;
|
---|
| 32 |
|
---|
| 33 | return result;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | int
|
---|
| 37 | CF_File_Create (const char * path)
|
---|
| 38 | {
|
---|
[14] | 39 | int result = open (path, O_CREAT | O_WRONLY | O_TRUNC, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
|
---|
[4] | 40 |
|
---|
| 41 | if (result < 0)
|
---|
| 42 | return CF_ERROR_FILE_CREATE;
|
---|
| 43 |
|
---|
| 44 | return result;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | int
|
---|
| 48 | CF_File_Close (const int fd)
|
---|
| 49 | {
|
---|
| 50 | int result = 0;
|
---|
| 51 |
|
---|
| 52 | if (fd < 0)
|
---|
| 53 | return CF_ERROR_FILE_INVALID_ARGS;
|
---|
| 54 |
|
---|
| 55 | result = close (fd);
|
---|
| 56 |
|
---|
| 57 | if (result < 0)
|
---|
| 58 | return CF_ERROR_FILE_CLOSE;
|
---|
| 59 |
|
---|
| 60 | return CF_OK;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | int
|
---|
| 64 | CF_File_Read (const int fd,
|
---|
| 65 | void * buf,
|
---|
| 66 | const size_t len)
|
---|
| 67 | {
|
---|
| 68 | int result = (int) read (fd, buf, len);
|
---|
| 69 |
|
---|
| 70 | if (result < 0)
|
---|
| 71 | return CF_ERROR_FILE_READ;
|
---|
| 72 |
|
---|
| 73 | return CF_OK;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | int
|
---|
| 77 | CF_File_Write (const int fd,
|
---|
| 78 | const void * buf,
|
---|
| 79 | const size_t len)
|
---|
| 80 | {
|
---|
| 81 | int result = (int) write (fd, buf, len);
|
---|
| 82 |
|
---|
| 83 | if (result != len)
|
---|
| 84 | return CF_ERROR_FILE_WRITE;
|
---|
| 85 |
|
---|
| 86 | return CF_OK;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | int
|
---|
| 90 | CF_File_GetSize (const int fd)
|
---|
| 91 | {
|
---|
| 92 | int result = (int) lseek (fd, 0, SEEK_END);
|
---|
| 93 |
|
---|
| 94 | if (result < 0 || lseek (fd, 0, SEEK_SET) < 0)
|
---|
| 95 | return CF_ERROR_FILE_GET_SIZE;
|
---|
| 96 |
|
---|
| 97 | return result;
|
---|
| 98 | }
|
---|