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

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

#1 fix preprocessor definition for windows

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