1 | /**
|
---|
2 | * @file cf_file.c
|
---|
3 | * @author myusgun <myusgun@gmail.com>
|
---|
4 | * @version 0.1
|
---|
5 | */
|
---|
6 | #include "cf_file.h"
|
---|
7 | #include "cf_error.h"
|
---|
8 |
|
---|
9 | #include <stdio.h>
|
---|
10 | #include <errno.h>
|
---|
11 | #include <sys/stat.h>
|
---|
12 |
|
---|
13 | #if defined(_WIN32) || defined(_WIN64)
|
---|
14 | # include <io.h>
|
---|
15 | # include <direct.h>
|
---|
16 |
|
---|
17 | # define DELIMITER '\\'
|
---|
18 | # define mkdir(a,b) _mkdir (a)
|
---|
19 | # define access(a,b) _access (a,b)
|
---|
20 | # define snprintf _snprintf
|
---|
21 |
|
---|
22 | # define F_OK 0
|
---|
23 | # define W_OK 2
|
---|
24 | # define R_OK 4
|
---|
25 |
|
---|
26 | /*------------------------------*/
|
---|
27 | # define S_IWUSR _S_IWRITE
|
---|
28 | # define S_IRUSR _S_IREAD
|
---|
29 | # define S_IXUSR _S_IEXEC
|
---|
30 | /*------------------------------*/
|
---|
31 | # define S_IRGRP 0x00000000
|
---|
32 | # define S_IWGRP 0x00000000
|
---|
33 | # define S_IXGRP 0x00000000
|
---|
34 | /*------------------------------*/
|
---|
35 | # define S_IROTH 0x00000000
|
---|
36 | # define S_IWOTH 0x00000000
|
---|
37 | # define S_IXOTH 0x00000000
|
---|
38 | /*------------------------------*/
|
---|
39 | # define S_IRWXU 0x00000000
|
---|
40 | #else // #if defined(_WIN32) || defined(_WIN64)
|
---|
41 | # include <unistd.h>
|
---|
42 |
|
---|
43 | # define DELIMITER '/'
|
---|
44 | # define O_BINARY 0x00000000
|
---|
45 | #endif // #if defined(_WIN32) || defined(_WIN64)
|
---|
46 |
|
---|
47 | #define DIRECTORY_MODE S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH
|
---|
48 | #define FILE_MODE S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH
|
---|
49 |
|
---|
50 | #define ASSERT_ARGS(x) \
|
---|
51 | if ((x)) \
|
---|
52 | return CF_ERROR_FILE_INVALID_ARGS
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * 파일 열기
|
---|
56 | *
|
---|
57 | * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
|
---|
58 | *
|
---|
59 | * @param path 파일 경로
|
---|
60 | * @param flag 파일 열기 플래그
|
---|
61 | *
|
---|
62 | * @see CF_FILE_FLAG
|
---|
63 | */
|
---|
64 | int
|
---|
65 | CF_File_Open (const char * path,
|
---|
66 | const CF_FILE_FLAG flag)
|
---|
67 | {
|
---|
68 | int result = 0;
|
---|
69 |
|
---|
70 | ASSERT_ARGS (path == NULL);
|
---|
71 |
|
---|
72 | result = open (path, (int)(flag|O_BINARY));
|
---|
73 |
|
---|
74 | if (result < 0)
|
---|
75 | return CF_ERROR_FILE_OPEN;
|
---|
76 |
|
---|
77 | return result;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * 파일 생성
|
---|
82 | *
|
---|
83 | * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
|
---|
84 | *
|
---|
85 | * @param path 파일 경로
|
---|
86 | */
|
---|
87 | int
|
---|
88 | CF_File_Create (const char * path)
|
---|
89 | {
|
---|
90 | int result = 0;
|
---|
91 |
|
---|
92 | ASSERT_ARGS (path == NULL);
|
---|
93 |
|
---|
94 | result = open (path, CF_FILE_CR|CF_FILE_WO|CF_FILE_TR, FILE_MODE);
|
---|
95 |
|
---|
96 | if (result < 0)
|
---|
97 | return CF_ERROR_FILE_CREATE;
|
---|
98 |
|
---|
99 | return result;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * 파일 닫기
|
---|
104 | *
|
---|
105 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
106 | *
|
---|
107 | * @param fd 파일 디스크립터
|
---|
108 | */
|
---|
109 | int
|
---|
110 | CF_File_Close (const int fd)
|
---|
111 | {
|
---|
112 | int result = 0;
|
---|
113 |
|
---|
114 | ASSERT_ARGS (fd < 0);
|
---|
115 |
|
---|
116 | result = close (fd);
|
---|
117 |
|
---|
118 | if (result < 0)
|
---|
119 | return CF_ERROR_FILE_CLOSE;
|
---|
120 |
|
---|
121 | return CF_OK;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * 파일 읽기
|
---|
126 | *
|
---|
127 | * @return 성공 시, 읽은 바이트 수; 실패 시, 오류 코드
|
---|
128 | *
|
---|
129 | * @param fd 파일 디스크립터
|
---|
130 | * @param buf 데이터를 저장할 메모리
|
---|
131 | * @param len 데이터를 저장할 메모리의 크기
|
---|
132 | */
|
---|
133 | int
|
---|
134 | CF_File_Read (const int fd,
|
---|
135 | void * buf,
|
---|
136 | const size_t len)
|
---|
137 | {
|
---|
138 | int result = 0;
|
---|
139 |
|
---|
140 | ASSERT_ARGS (fd < 0);
|
---|
141 | ASSERT_ARGS (buf == NULL);
|
---|
142 |
|
---|
143 | result = (int) read (fd, buf, len);
|
---|
144 |
|
---|
145 | if (result < 0)
|
---|
146 | return CF_ERROR_FILE_READ;
|
---|
147 |
|
---|
148 | return result;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * 파일 쓰기
|
---|
153 | *
|
---|
154 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
155 | *
|
---|
156 | * @param fd 파일 디스크립터
|
---|
157 | * @param buf 데이터가 저장된 메모리
|
---|
158 | * @param len 쓸 데이터의 길이
|
---|
159 | */
|
---|
160 | int
|
---|
161 | CF_File_Write (const int fd,
|
---|
162 | const void * buf,
|
---|
163 | const size_t len)
|
---|
164 | {
|
---|
165 | int result = 0;
|
---|
166 |
|
---|
167 | ASSERT_ARGS (fd < 0);
|
---|
168 | ASSERT_ARGS (buf == NULL);
|
---|
169 |
|
---|
170 | result = (int) write (fd, buf, len);
|
---|
171 |
|
---|
172 | if (result != len)
|
---|
173 | return CF_ERROR_FILE_WRITE;
|
---|
174 |
|
---|
175 | return CF_OK;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * 파일 크기 얻기
|
---|
180 | *
|
---|
181 | * @return 성공 시, 파일 크기; 실패 시, 오류 코드
|
---|
182 | *
|
---|
183 | * @param fd 파일 디스크립터
|
---|
184 | */
|
---|
185 | int
|
---|
186 | CF_File_GetSize (const int fd)
|
---|
187 | {
|
---|
188 | int result = 0;
|
---|
189 |
|
---|
190 | ASSERT_ARGS (fd < 0);
|
---|
191 |
|
---|
192 | result = (int) lseek (fd, 0, SEEK_END);
|
---|
193 |
|
---|
194 | if (result < 0 || lseek (fd, 0, SEEK_SET) < 0)
|
---|
195 | return CF_ERROR_FILE_GET_SIZE;
|
---|
196 |
|
---|
197 | return result;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * 디렉터리 생성
|
---|
202 | *
|
---|
203 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
204 | *
|
---|
205 | * @param path 생성할 디렉터리 경로
|
---|
206 | */
|
---|
207 | int
|
---|
208 | CF_File_MakeDirectory (const char * path)
|
---|
209 | {
|
---|
210 | int result = 0;
|
---|
211 | char fullPath[1024] = {0x00,};
|
---|
212 | char stepPath[256] = {0x00,};
|
---|
213 |
|
---|
214 | char * f = fullPath;
|
---|
215 | char * d = stepPath;
|
---|
216 |
|
---|
217 | ASSERT_ARGS (path == NULL);
|
---|
218 |
|
---|
219 | snprintf (fullPath, sizeof (fullPath) - 1, "%s%c", path, DELIMITER);
|
---|
220 |
|
---|
221 | for (*d++ = *f++ ; *f ; *d++ = *f++)
|
---|
222 | {
|
---|
223 | if (*f != DELIMITER)
|
---|
224 | continue;
|
---|
225 |
|
---|
226 | if (access (stepPath, F_OK) == 0)
|
---|
227 | continue;
|
---|
228 |
|
---|
229 | result = mkdir (stepPath, DIRECTORY_MODE);
|
---|
230 | if (result && errno != EEXIST)
|
---|
231 | return CF_ERROR_FILE_MAKE_DIRECTORY;
|
---|
232 | }
|
---|
233 |
|
---|
234 | return CF_OK;
|
---|
235 | }
|
---|