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

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

#1 modify assertion logic

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