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

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

#1 separate example code and doxygen comment and fix logging push logic by vfire

File size: 2.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
8#ifdef _WIN32
9# include <stdio.h>
10# include <io.h>
11# include <sys/stat.h>
12
13# define S_IWUSR _S_IWRITE
14# define S_IRUSR _S_IREAD
15# define S_IXUSR _S_IEXEC
16# define S_IRGRP 0x00000000
17# define S_IWGRP 0x00000000
18# define S_IXGRP 0x00000000
19# define S_IROTH 0x00000000
20# define S_IWOTH 0x00000000
21# define S_IXOTH 0x00000000
22#else // #ifdef _WIN32
23# include <unistd.h>
24# define O_BINARY 0x00000000
25#endif // #ifdef _WIN32
26
27#define FILE_MODE S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH
28
29/**
30 * 파일 열기
31 *
32 * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
33 *
34 * @param path 파일 경로
35 * @param flag 파일 열기 플래그
36 *
37 * @see CF_FILE_FLAG
38 */
39int
40CF_File_Open (const char * path,
41 const CF_FILE_FLAG flag)
42{
43 int result = open (path, (int)(flag|O_BINARY));
44
45 if (result < 0)
46 return CF_ERROR_FILE_OPEN;
47
48 return result;
49}
50
51/**
52 * 파일 생성
53 *
54 * @return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
55 *
56 * @param path 파일 경로
57 */
58int
59CF_File_Create (const char * path)
60{
61 int result = open (path, CF_FILE_CR|CF_FILE_WO|CF_FILE_TR, FILE_MODE);
62
63 if (result < 0)
64 return CF_ERROR_FILE_CREATE;
65
66 return result;
67}
68
69/**
70 * 파일 닫기
71 *
72 * @return 성공 시, CF_OK; 실패 시, 오류 코드
73 *
74 * @param fd 파일 디스크립터
75 */
76int
77CF_File_Close (const int fd)
78{
79 int result = 0;
80
81 if (fd < 0)
82 return CF_ERROR_FILE_INVALID_ARGS;
83
84 result = close (fd);
85
86 if (result < 0)
87 return CF_ERROR_FILE_CLOSE;
88
89 return CF_OK;
90}
91
92/**
93 * 파일 읽기
94 *
95 * @return 성공 시, 읽은 바이트 수; 실패 시, 오류 코드
96 *
97 * @param fd 파일 디스크립터
98 * @param buf 데이터를 저장할 메모리
99 * @param len 데이터를 저장할 메모리의 크기
100 */
101int
102CF_File_Read (const int fd,
103 void * buf,
104 const size_t len)
105{
106 int result = (int) read (fd, buf, len);
107
108 if (result < 0)
109 return CF_ERROR_FILE_READ;
110
111 return result;
112}
113
114/**
115 * 파일 쓰기
116 *
117 * @return 성공 시, CF_OK; 실패 시, 오류 코드
118 *
119 * @param fd 파일 디스크립터
120 * @param buf 데이터가 저장된 메모리
121 * @param len 쓸 데이터의 길이
122 */
123int
124CF_File_Write (const int fd,
125 const void * buf,
126 const size_t len)
127{
128 int result = (int) write (fd, buf, len);
129
130 if (result != len)
131 return CF_ERROR_FILE_WRITE;
132
133 return CF_OK;
134}
135
136/**
137 * 파일 크기 얻기
138 *
139 * @return 성공 시, 파일 크기; 실패 시, 오류 코드
140 *
141 * @param fd 파일 디스크립터
142 */
143int
144CF_File_GetSize (const int fd)
145{
146 int result = (int) lseek (fd, 0, SEEK_END);
147
148 if (result < 0 || lseek (fd, 0, SEEK_SET) < 0)
149 return CF_ERROR_FILE_GET_SIZE;
150
151 return result;
152}
Note: See TracBrowser for help on using the repository browser.