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