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

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

#1 fix missed code

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