1 | /**
|
---|
2 | * @file cf_file.h
|
---|
3 | * @author myusgun <myusgun@gmail.com>
|
---|
4 | * @version 0.1
|
---|
5 | *
|
---|
6 | * @section 샘플코드
|
---|
7 | * @code
|
---|
8 |
|
---|
9 | #include "cf_file.h"
|
---|
10 |
|
---|
11 | int main (void) {
|
---|
12 | int fd = 0;
|
---|
13 | char *name = "file.txt";
|
---|
14 | char buffer[128] = {0x00,};
|
---|
15 |
|
---|
16 | fd = CF_File_Create (name);
|
---|
17 | if (fd < 0) {
|
---|
18 | // error
|
---|
19 | }
|
---|
20 | if (CF_File_Write (fd, "file test", 9) < 0) {
|
---|
21 | // error
|
---|
22 | }
|
---|
23 | CF_File_Close (fd);
|
---|
24 |
|
---|
25 | fd = CF_File_Open (name, CF_FILE_RO);
|
---|
26 | if (fd < 0) {
|
---|
27 | // error
|
---|
28 | }
|
---|
29 | printf ("file size : %d\n", CF_File_GetSize (fd));
|
---|
30 | if (CF_File_Read (fd, buffer, sizeof (buffer)) < 0) {
|
---|
31 | // error
|
---|
32 | }
|
---|
33 | CF_File_Close (fd);
|
---|
34 |
|
---|
35 | return 0;
|
---|
36 | }
|
---|
37 |
|
---|
38 | * @endcode
|
---|
39 | */
|
---|
40 | #ifndef __CF_FILE_H__
|
---|
41 | #define __CF_FILE_H__
|
---|
42 |
|
---|
43 | #include "cf_base.h"
|
---|
44 | #include <fcntl.h>
|
---|
45 |
|
---|
46 | #define CF_ERROR_FILE_OPEN CF_ERROR_FILE - 1
|
---|
47 | #define CF_ERROR_FILE_INVALID_ARGS CF_ERROR_FILE - 2
|
---|
48 | #define CF_ERROR_FILE_READ CF_ERROR_FILE - 3
|
---|
49 | #define CF_ERROR_FILE_WRITE CF_ERROR_FILE - 4
|
---|
50 | #define CF_ERROR_FILE_CREATE CF_ERROR_FILE - 5
|
---|
51 | #define CF_ERROR_FILE_CLOSE CF_ERROR_FILE - 6
|
---|
52 | #define CF_ERROR_FILE_GET_SIZE CF_ERROR_FILE - 7
|
---|
53 |
|
---|
54 | /** 파일 열기 옵션 플래그 */
|
---|
55 | typedef enum {
|
---|
56 | CF_FILE_RO = O_RDONLY, /**< 읽기 전용 */
|
---|
57 | CF_FILE_WO = O_WRONLY, /**< 쓰기 전용 */
|
---|
58 | CF_FILE_WR = O_RDWR, /**< 읽기/쓰기 */
|
---|
59 | CF_FILE_CR = O_CREAT, /**< 파일이 존재하지 않으면 생성 */
|
---|
60 | CF_FILE_TR = O_TRUNC, /**< 파일이 존재하면 비우기 */
|
---|
61 | CF_FILE_AP = O_APPEND /**< 파일이 존재하면 이어서 쓰기 */
|
---|
62 | } E_CF_FILE_FLAG, CF_FILE_FLAG;
|
---|
63 |
|
---|
64 | #ifdef __cplusplus
|
---|
65 | extern "C" {
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * 파일 열기
|
---|
70 | *
|
---|
71 | * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
|
---|
72 | *
|
---|
73 | * @param path 파일 경로
|
---|
74 | * @param flag 파일 열기 플래그
|
---|
75 | *
|
---|
76 | * @see CF_FILE_FLAG
|
---|
77 | */
|
---|
78 | CF_EXPORT int
|
---|
79 | CF_File_Open (const char * path,
|
---|
80 | const CF_FILE_FLAG flag);
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * 파일 닫기
|
---|
84 | *
|
---|
85 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
86 | *
|
---|
87 | * @param fd 파일 디스크립터
|
---|
88 | */
|
---|
89 | CF_EXPORT int
|
---|
90 | CF_File_Close (const int fd);
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * 파일 생성
|
---|
94 | *
|
---|
95 | * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
|
---|
96 | *
|
---|
97 | * @param path 파일 경로
|
---|
98 | */
|
---|
99 | CF_EXPORT int
|
---|
100 | CF_File_Create (const char * path);
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * 파일 읽기
|
---|
104 | *
|
---|
105 | * @return 성공 시, 읽은 바이트 수; 실패 시, 오류 코드
|
---|
106 | *
|
---|
107 | * @param fd 파일 디스크립터
|
---|
108 | * @param buf 데이터를 저장할 메모리
|
---|
109 | * @param len 데이터를 저장할 메모리의 크기
|
---|
110 | */
|
---|
111 | CF_EXPORT int
|
---|
112 | CF_File_Read (const int fd,
|
---|
113 | void * buf,
|
---|
114 | const size_t len);
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * 파일 쓰기
|
---|
118 | *
|
---|
119 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
120 | *
|
---|
121 | * @param fd 파일 디스크립터
|
---|
122 | * @param buf 데이터가 저장된 메모리
|
---|
123 | * @param len 쓸 데이터의 길이
|
---|
124 | */
|
---|
125 | CF_EXPORT int
|
---|
126 | CF_File_Write (const int fd,
|
---|
127 | const void * buf,
|
---|
128 | const size_t len);
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * 파일 크기 얻기
|
---|
132 | *
|
---|
133 | * @return 성공 시, 파일 크기; 실패 시, 오류 코드
|
---|
134 | *
|
---|
135 | * @param fd 파일 디스크립터
|
---|
136 | */
|
---|
137 | CF_EXPORT int
|
---|
138 | CF_File_GetSize (const int fd);
|
---|
139 |
|
---|
140 | #ifdef __cplusplus
|
---|
141 | }
|
---|
142 | #endif
|
---|
143 |
|
---|
144 | #endif // #ifndef __CF_FILE_H__
|
---|