Changeset 151 in libcf for trunk/src/cf_file.c
- Timestamp:
- 10/31/13 10:17:24 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/cf_file.c
r128 r151 11 11 12 12 #include <stdio.h> 13 #include <string.h> 13 14 #include <fcntl.h> 14 15 #include <errno.h> … … 51 52 #define FILE_MODE S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH 52 53 54 #define ASSERT_CTX(__ctx) \ 55 if (__ctx == NULL) \ 56 return CF_ERROR_FILE_INVALID_CTX 57 53 58 #define ASSERT_ARGS(x) \ 54 59 if ((x)) \ 55 60 return CF_ERROR_FILE_INVALID_ARGS 61 62 typedef struct __cf_file_ctx__ 63 { 64 int fd; 65 char path[1024]; 66 CF_FILE_FLAG flag; 67 } CF_FILE_CONTEXT; 56 68 57 69 static int … … 73 85 * 파일 열기 74 86 * 75 * \return 성공 시, 파일 디스크립터; 실패 시, 오류 코드 76 * 87 * \return 성공 시, CF_OK; 실패 시, 오류 코드 88 * 89 * \param ctx 파일 컨텍스트 77 90 * \param path 파일 경로 78 91 * \param flag 파일 열기 플래그 … … 81 94 */ 82 95 int 83 CF_File_Open (const char * path, 96 CF_File_Open (cf_ctx * ctx, 97 const char * path, 84 98 const CF_FILE_FLAG flag) 85 99 { 100 int fd = 0; 86 101 int result = 0; 87 102 int osflag = 0; 88 103 104 CF_FILE_CONTEXT * context = NULL; 105 106 ASSERT_CTX (ctx); 89 107 ASSERT_ARGS (path == NULL); 90 108 91 osflag = CF_File_Local_ConvertFlag (flag) | O_BINARY; 92 93 result = open (path, osflag, FILE_MODE); 94 if (result < 0) 95 return CF_ERROR_FILE_OPEN; 109 TRY 110 { 111 context = NEWCTX (CF_FILE_CONTEXT); 112 if (context == NULL) 113 { 114 result = CF_ERROR_FILE_CREATE_CTX; 115 TRY_BREAK; 116 } 117 context->flag = flag; 118 context->fd = -1; 119 120 osflag = CF_File_Local_ConvertFlag (flag) | O_BINARY; 121 122 fd = open (path, osflag, FILE_MODE); 123 if (fd < 0) 124 { 125 result = CF_ERROR_FILE_OPEN; 126 TRY_BREAK; 127 } 128 context->fd = fd; 129 130 *ctx = context; 131 } 132 CATCH_IF (result < 0) 133 { 134 CF_File_Close ((cf_ctx) context); 135 } 96 136 97 137 return result; … … 99 139 100 140 /** 101 * 파일 생성102 *103 * \return 성공 시, 파일 디스크립터; 실패 시, 오류 코드104 *105 * \param path 파일 경로106 */107 int108 CF_File_Create (const char * path)109 {110 int result = 0;111 const int flag = CF_FILE_CREATE|CF_FILE_WRITE|CF_FILE_TRUNC;112 113 ASSERT_ARGS (path == NULL);114 115 result = CF_File_Open (path, flag);116 if (result < 0)117 return CF_ERROR_FILE_CREATE;118 119 return result;120 }121 122 /**123 141 * 파일 닫기 124 142 * 125 143 * \return 성공 시, CF_OK; 실패 시, 오류 코드 126 144 * 127 * \param fd 파일 디스크립터 128 */ 129 int 130 CF_File_Close (const int fd) 131 { 132 int result = 0; 133 134 ASSERT_ARGS (fd < 0); 145 * \param ctx 파일 컨텍스트 146 */ 147 int 148 CF_File_Close (cf_ctx ctx) 149 { 150 int result = 0; 151 int fd = 0; 152 153 CF_FILE_CONTEXT * context = (CF_FILE_CONTEXT *) ctx; 154 155 ASSERT_CTX (context); 156 ASSERT_ARGS (context->fd < 0); 157 158 fd = context->fd; 159 160 free (context); 135 161 136 162 result = close (fd); … … 146 172 * \return 성공 시, 읽은 바이트 수; 실패 시, 오류 코드 147 173 * 148 * \param fd 파일 디스크립터174 * \param ctx 파일 컨텍스트 149 175 * \param buf 데이터를 저장할 메모리 150 176 * \param len 데이터를 저장할 메모리의 크기 151 177 */ 152 178 int 153 CF_File_Read (const int fd,179 CF_File_Read (const cf_ctx ctx, 154 180 void * buf, 155 181 const size_t len) 156 182 { 157 183 int result = 0; 158 159 ASSERT_ARGS (fd < 0); 184 185 CF_FILE_CONTEXT * context = (CF_FILE_CONTEXT *) ctx; 186 187 ASSERT_CTX (context); 188 ASSERT_ARGS (context->fd < 0); 160 189 ASSERT_ARGS (buf == NULL); 161 190 162 result = (int) read ( fd, buf, len);191 result = (int) read (context->fd, buf, len); 163 192 if (result < 0) 164 193 return CF_ERROR_FILE_READ; … … 172 201 * \return 성공 시, CF_OK; 실패 시, 오류 코드 173 202 * 174 * \param fd 파일 디스크립터203 * \param ctx 파일 컨텍스트 175 204 * \param buf 데이터가 저장된 메모리 176 205 * \param len 쓸 데이터의 길이 177 206 */ 178 207 int 179 CF_File_Write (const int fd,208 CF_File_Write (const cf_ctx ctx, 180 209 const void * buf, 181 210 const size_t len) 182 211 { 183 212 int result = 0; 184 185 ASSERT_ARGS (fd < 0); 213 214 CF_FILE_CONTEXT * context = (CF_FILE_CONTEXT *) ctx; 215 216 ASSERT_CTX (context); 217 ASSERT_ARGS (context->fd < 0); 186 218 ASSERT_ARGS (buf == NULL); 187 219 188 result = (int) write ( fd, buf, len);220 result = (int) write (context->fd, buf, len); 189 221 if (result != len) 190 222 return CF_ERROR_FILE_WRITE; … … 206 238 int fd = 0; 207 239 240 CF_FILE_CONTEXT * context = NULL; 241 208 242 ASSERT_ARGS (path == NULL); 209 243 210 fd = CF_File_Open ( path, CF_FILE_READ);244 fd = CF_File_Open ((cf_ctx *)&context, path, CF_FILE_READ); 211 245 if (fd < 0) 212 246 return CF_ERROR_FILE_OPEN; … … 216 250 return CF_ERROR_FILE_GET_SIZE; 217 251 218 CF_File_Close ( fd);252 CF_File_Close ((cf_ctx)context); 219 253 220 254 return length; … … 245 279 { 246 280 int result = 0; 247 char fullPath[1024] = {0x00,}; 281 size_t length = 0; 282 char * fullpath = NULL; 248 283 char stepPath[256] = {0x00,}; 249 284 250 char * f = fullPath;251 char * d = stepPath;285 char * f = NULL; 286 char * d = NULL; 252 287 253 288 ASSERT_ARGS (path == NULL); 254 289 255 snprintf (fullPath, sizeof (fullPath) - 1, "%s%c", path, DELIMITER); 290 length = strlen (path); 291 fullpath = (char *) calloc (length + 2, 1); 292 if (!fullpath) 293 return CF_ERROR_FILE_ALLOCATE_BUFFER; 294 295 f = fullpath; 296 d = stepPath; 297 298 snprintf (fullpath, length + 1, "%s%c", path, DELIMITER); 256 299 257 300 for (*d++ = *f++ ; *f ; *d++ = *f++) … … 270 313 return CF_OK; 271 314 } 315 316 /** 317 * 컨텍스트가 열고 있는 파일의 경로를 가져옴 318 * 319 * \return 성공 시, CF_OK; 실패 시, 오류 코드 320 * 321 * \param ctx 파일 컨텍스트 322 * \param path 파일 경로를 저장할 충분한 공간의 메모리 323 */ 324 int 325 CF_File_GetPath (const cf_ctx ctx, 326 char * path) 327 { 328 CF_FILE_CONTEXT * context = (CF_FILE_CONTEXT *) ctx; 329 330 ASSERT_CTX (context); 331 ASSERT_ARGS (path == NULL); 332 333 snprintf (path, sizeof (context->path), "%s", context->path); 334 335 return CF_OK; 336 }
Note:
See TracChangeset
for help on using the changeset viewer.