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