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

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

#1 change interface of log from context to id-number

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