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