source: libcf/trunk/src/cf_file.c@ 90

Last change on this file since 90 was 90, checked in by cheese, 11 years ago

#1 fix bug on codec and arrange file flag

File size: 4.7 KB
Line 
1/**
2 * @file cf_file.c
3 * @author myusgun <myusgun@gmail.com>
4 */
5#include "cf_file.h"
6#include "cf_local.h"
7#include "cf_error.h"
8
9#include <stdio.h>
10#include <fcntl.h>
11#include <errno.h>
12#include <sys/stat.h>
13
14#if defined(_WIN32) || defined(_WIN64)
15# include <io.h>
16# include <direct.h>
17
18# define DELIMITER '\\'
19# define mkdir(a,b) _mkdir (a)
20# define access(a,b) _access (a,b)
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
54static int
55CF_File_Local_ConvertFlag (const CF_FILE_FLAG flag)
56{
57 int result = 0;
58
59 if (flag & CF_FILE_READ) result |= O_RDONLY;
60 if (flag & CF_FILE_WRITE) result |= O_WRONLY;
61 if (flag & CF_FILE_RW) result |= O_RDWR;
62 if (flag & CF_FILE_CREATE) result |= O_CREAT;
63 if (flag & CF_FILE_TRUNC) result |= O_TRUNC;
64 if (flag & CF_FILE_APPEND) result |= O_APPEND;
65
66 return result;
67}
68
69/**
70 * 파일 열기
71 *
72 * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
73 *
74 * @param path 파일 경로
75 * @param flag 파일 열기 플래그
76 *
77 * @see CF_FILE_FLAG
78 */
79int
80CF_File_Open (const char * path,
81 const CF_FILE_FLAG flag)
82{
83 int result = 0;
84
85 ASSERT_ARGS (path == NULL);
86
87 result = open (path, CF_File_Local_ConvertFlag (flag) | O_BINARY);
88 if (result < 0)
89 return CF_ERROR_FILE_OPEN;
90
91 return result;
92}
93
94/**
95 * 파일 생성
96 *
97 * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
98 *
99 * @param path 파일 경로
100 */
101int
102CF_File_Create (const char * path)
103{
104 int result = 0;
105 int flag = 0;
106
107 ASSERT_ARGS (path == NULL);
108
109 flag = CF_File_Local_ConvertFlag (CF_FILE_CREATE|
110 CF_FILE_WRITE |
111 CF_FILE_TRUNC);
112
113 result = open (path, flag, FILE_MODE);
114 if (result < 0)
115 return CF_ERROR_FILE_CREATE;
116
117 return result;
118}
119
120/**
121 * 파일 닫기
122 *
123 * @return 성공 시, CF_OK; 실패 시, 오류 코드
124 *
125 * @param fd 파일 디스크립터
126 */
127int
128CF_File_Close (const int fd)
129{
130 int result = 0;
131
132 ASSERT_ARGS (fd < 0);
133
134 result = close (fd);
135 if (result < 0)
136 return CF_ERROR_FILE_CLOSE;
137
138 return CF_OK;
139}
140
141/**
142 * 파일 읽기
143 *
144 * @return 성공 시, 읽은 바이트 수; 실패 시, 오류 코드
145 *
146 * @param fd 파일 디스크립터
147 * @param buf 데이터를 저장할 메모리
148 * @param len 데이터를 저장할 메모리의 크기
149 */
150int
151CF_File_Read (const int fd,
152 void * buf,
153 const size_t len)
154{
155 int result = 0;
156
157 ASSERT_ARGS (fd < 0);
158 ASSERT_ARGS (buf == NULL);
159
160 result = (int) read (fd, buf, len);
161 if (result < 0)
162 return CF_ERROR_FILE_READ;
163
164 return result;
165}
166
167/**
168 * 파일 쓰기
169 *
170 * @return 성공 시, CF_OK; 실패 시, 오류 코드
171 *
172 * @param fd 파일 디스크립터
173 * @param buf 데이터가 저장된 메모리
174 * @param len 쓸 데이터의 길이
175 */
176int
177CF_File_Write (const int fd,
178 const void * buf,
179 const size_t len)
180{
181 int result = 0;
182
183 ASSERT_ARGS (fd < 0);
184 ASSERT_ARGS (buf == NULL);
185
186 result = (int) write (fd, buf, len);
187 if (result != len)
188 return CF_ERROR_FILE_WRITE;
189
190 return CF_OK;
191}
192
193/**
194 * 파일 크기 얻기
195 *
196 * @return 성공 시, 파일 크기; 실패 시, 오류 코드
197 *
198 * @param fd 파일 디스크립터
199 */
200int
201CF_File_GetSize (const int fd)
202{
203 int result = 0;
204
205 ASSERT_ARGS (fd < 0);
206
207 result = (int) lseek (fd, 0, SEEK_END);
208 if (result < 0 || lseek (fd, 0, SEEK_SET) < 0)
209 return CF_ERROR_FILE_GET_SIZE;
210
211 return result;
212}
213
214/**
215 * 디렉터리 생성
216 *
217 * @return 성공 시, CF_OK; 실패 시, 오류 코드
218 *
219 * @param path 생성할 디렉터리 경로
220 */
221int
222CF_File_MakeDirectory (const char * path)
223{
224 int result = 0;
225 char fullPath[1024] = {0x00,};
226 char stepPath[256] = {0x00,};
227
228 char * f = fullPath;
229 char * d = stepPath;
230
231 ASSERT_ARGS (path == NULL);
232
233 snprintf (fullPath, sizeof (fullPath) - 1, "%s%c", path, DELIMITER);
234
235 for (*d++ = *f++ ; *f ; *d++ = *f++)
236 {
237 if (*f != DELIMITER)
238 continue;
239
240 if (access (stepPath, F_OK) == 0)
241 continue;
242
243 result = mkdir (stepPath, DIRECTORY_MODE);
244 if (result && errno != EEXIST)
245 return CF_ERROR_FILE_MAKE_DIRECTORY;
246 }
247
248 return CF_OK;
249}
Note: See TracBrowser for help on using the repository browser.