Changeset 151 in libcf for trunk/src/cf_file.c


Ignore:
Timestamp:
10/31/13 10:17:24 (11 years ago)
Author:
cheese
Message:

#1 fix interface and add util module

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/cf_file.c

    r128 r151  
    1111
    1212#include <stdio.h>
     13#include <string.h>
    1314#include <fcntl.h>
    1415#include <errno.h>
     
    5152#define FILE_MODE       S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH
    5253
     54#define ASSERT_CTX(__ctx)   \
     55    if (__ctx == NULL)      \
     56        return CF_ERROR_FILE_INVALID_CTX
     57
    5358#define ASSERT_ARGS(x)  \
    5459    if ((x))            \
    5560        return CF_ERROR_FILE_INVALID_ARGS
     61
     62typedef struct __cf_file_ctx__
     63{
     64    int             fd;
     65    char            path[1024];
     66    CF_FILE_FLAG    flag;
     67} CF_FILE_CONTEXT;
    5668
    5769static int
     
    7385 * 파일 열기
    7486 *
    75  * \return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
    76  *
     87 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     88 *
     89 * \param ctx   파일 컨텍스트
    7790 * \param path  파일 경로
    7891 * \param flag  파일 열기 플래그
     
    8194 */
    8295int
    83 CF_File_Open (const char            * path,
     96CF_File_Open (cf_ctx                * ctx,
     97              const char            * path,
    8498              const CF_FILE_FLAG    flag)
    8599{
     100    int fd = 0;
    86101    int result = 0;
    87102    int osflag = 0;
    88103
     104    CF_FILE_CONTEXT * context = NULL;
     105
     106    ASSERT_CTX (ctx);
    89107    ASSERT_ARGS (path == NULL);
    90108
    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    }
    96136
    97137    return result;
     
    99139
    100140/**
    101  * 파일 생성
    102  *
    103  * \return 성공 시, 파일 디스크립터; 실패 시, 오류 코드
    104  *
    105  * \param path 파일 경로
    106  */
    107 int
    108 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 /**
    123141 * 파일 닫기
    124142 *
    125143 * \return 성공 시, CF_OK; 실패 시, 오류 코드
    126144 *
    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 */
     147int
     148CF_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);
    135161
    136162    result = close (fd);
     
    146172 * \return 성공 시, 읽은 바이트 수; 실패 시, 오류 코드
    147173 *
    148  * \param fd    파일 디스크립터
     174 * \param ctx   파일 컨텍스트
    149175 * \param buf   데이터를 저장할 메모리
    150176 * \param len   데이터를 저장할 메모리의 크기
    151177 */
    152178int
    153 CF_File_Read (const int     fd,
     179CF_File_Read (const cf_ctx  ctx,
    154180              void          * buf,
    155181              const size_t  len)
    156182{
    157183    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);
    160189    ASSERT_ARGS (buf == NULL);
    161190
    162     result = (int) read (fd, buf, len);
     191    result = (int) read (context->fd, buf, len);
    163192    if (result < 0)
    164193        return CF_ERROR_FILE_READ;
     
    172201 * \return 성공 시, CF_OK; 실패 시, 오류 코드
    173202 *
    174  * \param fd    파일 디스크립터
     203 * \param ctx   파일 컨텍스트
    175204 * \param buf   데이터가 저장된 메모리
    176205 * \param len   쓸 데이터의 길이
    177206 */
    178207int
    179 CF_File_Write (const int    fd,
     208CF_File_Write (const cf_ctx ctx,
    180209               const void   * buf,
    181210               const size_t len)
    182211{
    183212    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);
    186218    ASSERT_ARGS (buf == NULL);
    187219
    188     result = (int) write (fd, buf, len);
     220    result = (int) write (context->fd, buf, len);
    189221    if (result != len)
    190222      return CF_ERROR_FILE_WRITE;
     
    206238    int fd = 0;
    207239
     240    CF_FILE_CONTEXT * context = NULL;
     241
    208242    ASSERT_ARGS (path == NULL);
    209243
    210     fd = CF_File_Open (path, CF_FILE_READ);
     244    fd = CF_File_Open ((cf_ctx *)&context, path, CF_FILE_READ);
    211245    if (fd < 0)
    212246        return CF_ERROR_FILE_OPEN;
     
    216250        return CF_ERROR_FILE_GET_SIZE;
    217251
    218     CF_File_Close (fd);
     252    CF_File_Close ((cf_ctx)context);
    219253
    220254    return length;
     
    245279{
    246280    int     result = 0;
    247     char    fullPath[1024] = {0x00,};
     281    size_t  length = 0;
     282    char    * fullpath = NULL;
    248283    char    stepPath[256] = {0x00,};
    249284
    250     char    * f = fullPath;
    251     char    * d = stepPath;
     285    char    * f = NULL;
     286    char    * d = NULL;
    252287
    253288    ASSERT_ARGS (path == NULL);
    254289
    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);
    256299
    257300    for (*d++ = *f++ ; *f ; *d++ = *f++)
     
    270313    return CF_OK;
    271314}
     315
     316/**
     317 * 컨텍스트가 열고 있는 파일의 경로를 가져옴
     318 *
     319 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     320 *
     321 * \param ctx   파일 컨텍스트
     322 * \param path  파일 경로를 저장할 충분한 공간의 메모리
     323 */
     324int
     325CF_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.