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

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

#1 add function to make directory and update file test code

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