Changeset 151 in libcf


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

#1 fix interface and add util module

Location:
trunk
Files:
3 added
34 edited

Legend:

Unmodified
Added
Removed
  • trunk/docs/doxyfile

    r119 r151  
    3939# a quick idea about the purpose of the project. Keep the description short.
    4040
    41 PROJECT_BRIEF          = CFLibrary
     41PROJECT_BRIEF          = "CFLibrary (Cheese Foundation Library)"
    4242
    4343# With the PROJECT_LOGO tag one can specify an logo or icon that is
     
    5353# where doxygen was started. If left blank the current directory will be used.
    5454
    55 OUTPUT_DIRECTORY       = ../documentation
     55OUTPUT_DIRECTORY       = ../Documentation
    5656
    5757# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create
  • trunk/include/cf_base.h

    r145 r151  
    1010 * \mainpage
    1111
    12 <h2 style="line-height: 10%;">Welcome to documentation page of libCF</h2>
    13 &nbsp; - CFLibrary (cheese's foundation library)
     12<h2 style="line-height: 10%;">Welcome to documentation for libcf</h2>
     13&nbsp; :: CFLibrary (Cheese Foundation Library)
    1414
    1515&nbsp; This documentation (suggested by nvdark) includes only manual of APIs.
     
    4040} CF_BOOL;
    4141
     42/** 공용 건텍스트 */
     43typedef void *  cf_ctx;
     44
    4245/** 성공 시, 반환 값 */
    4346#define CF_OK                           0
  • trunk/include/cf_error.h

    r128 r151  
    2121#define CF_ERROR_FILE_READ                  CF_ERROR_FILE - 3
    2222#define CF_ERROR_FILE_WRITE                 CF_ERROR_FILE - 4
    23 #define CF_ERROR_FILE_CREATE                CF_ERROR_FILE - 5
     23#define CF_ERROR_FILE_CREATE_CTX            CF_ERROR_FILE - 5
    2424#define CF_ERROR_FILE_CLOSE                 CF_ERROR_FILE - 6
    2525#define CF_ERROR_FILE_GET_SIZE              CF_ERROR_FILE - 7
    2626#define CF_ERROR_FILE_MAKE_DIRECTORY        CF_ERROR_FILE - 8
     27#define CF_ERROR_FILE_ALLOCATE_BUFFER       CF_ERROR_FILE - 9
     28#define CF_ERROR_FILE_INVALID_CTX           CF_ERROR_FILE - 10
    2729/* }}} file */
    2830
     
    4749#define CF_ERROR_SOCKET_SEND                CF_ERROR_SOCKET - 16
    4850#define CF_ERROR_SOCKET_RECV                CF_ERROR_SOCKET - 17
    49 #define CF_ERROR_SOCKET_INTERNAL            CF_ERROR_SOCKET - 18
     51#define CF_ERROR_SOCKET_CHECK_DESC_SET      CF_ERROR_SOCKET - 18
     52#define CF_ERROR_SOCKET_INVALID_CTX         CF_ERROR_SOCKET - 19
     53#define CF_ERROR_SOCKET_CREATE_CTX          CF_ERROR_SOCKET - 20
    5054/* }}} socket */
    5155
     
    8589#define CF_ERROR_LOG                        CF_ERROR_BASE * 6
    8690/*------------------------------------------------------------*/
    87 #define CF_ERROR_LOG_INITIALIZE             CF_ERROR_LOG - 1
    88 #define CF_ERROR_LOG_INVALID_CTX            CF_ERROR_LOG - 2
    89 #define CF_ERROR_LOG_SET_MULTITHREAD        CF_ERROR_LOG - 3
    90 #define CF_ERROR_LOG_UNSET_MULTITHREAD      CF_ERROR_LOG - 4
    91 #define CF_ERROR_LOG_LOCK_CTX               CF_ERROR_LOG - 5
    92 #define CF_ERROR_LOG_UNLOCK_CTX             CF_ERROR_LOG - 6
    93 #define CF_ERROR_LOG_FLUSH                  CF_ERROR_LOG - 7
    94 #define CF_ERROR_LOG_INVALID_MAPID          CF_ERROR_LOG - 8
    95 #define CF_ERROR_LOG_NOT_MAPPED_ID          CF_ERROR_LOG - 9
    96 #define CF_ERROR_LOG_ALREADY_MAPPED_ID      CF_ERROR_LOG - 10
    97 #define CF_ERROR_LOG_NOT_INITIALIZE         CF_ERROR_LOG - 11
    98 #define CF_ERROR_LOG_INVALID_ARGS           CF_ERROR_LOG - 12
    99 #define CF_ERROR_LOG_CREATE_CTX             CF_ERROR_LOG - 13
    100 #define CF_ERROR_LOG_CREATE_FILE            CF_ERROR_LOG - 14
    101 #define CF_ERROR_LOG_ALLOCATE_BUFFER        CF_ERROR_LOG - 15
     91#define CF_ERROR_LOG_INVALID_CTX            CF_ERROR_LOG - 1
     92#define CF_ERROR_LOG_SET_MULTITHREAD        CF_ERROR_LOG - 2
     93#define CF_ERROR_LOG_UNSET_MULTITHREAD      CF_ERROR_LOG - 3
     94#define CF_ERROR_LOG_FLUSH                  CF_ERROR_LOG - 4
     95#define CF_ERROR_LOG_INVALID_ARGS           CF_ERROR_LOG - 5
     96#define CF_ERROR_LOG_CREATE_CTX             CF_ERROR_LOG - 6
     97#define CF_ERROR_LOG_CREATE_FILE            CF_ERROR_LOG - 7
     98#define CF_ERROR_LOG_ALLOCATE_BUFFER        CF_ERROR_LOG - 8
    10299/* }}} log */
    103100
     
    128125/* }}} bitwise */
    129126
     127/* util {{{ */
     128#define CF_ERROR_UTIL                       CF_ERROR_BASE * 10
     129/*------------------------------------------------------------*/
     130#define CF_ERROR_UTIL_INVALID_ARGS          CF_ERROR_UTIL - 1
     131/* }}} util */
     132
     133/* context-manager {{{ */
     134#define CF_ERROR_CTXMGR                     CF_ERROR_BASE * 11
     135/*------------------------------------------------------------*/
     136#define CF_ERROR_CTXMGR_INITIALIZE          CF_ERROR_CTXMGR - 1
     137#define CF_ERROR_CTXMGR_FINALIZE            CF_ERROR_CTXMGR - 2
     138#define CF_ERROR_CTXMGR_REGISTER            CF_ERROR_CTXMGR - 3
     139#define CF_ERROR_CTXMGR_UNREGISTER          CF_ERROR_CTXMGR - 4
     140#define CF_ERROR_CTXMGR_FIND_FIRST_CTX      CF_ERROR_CTXMGR - 5
     141#define CF_ERROR_CTXMGR_FIND_CTX            CF_ERROR_CTXMGR - 6
     142#define CF_ERROR_CTXMGR_CHECK_DUPLICATION   CF_ERROR_CTXMGR - 7
     143#define CF_ERROR_CTXMGR_ALREADY_REGISTERED  CF_ERROR_CTXMGR - 8
     144#define CF_ERROR_CTXMGR_NOT_REGISTERED      CF_ERROR_CTXMGR - 9
     145#define CF_ERROR_CTXMGR_CREATE_POOL         CF_ERROR_CTXMGR - 10
     146#define CF_ERROR_CTXMGR_GET                 CF_ERROR_CTXMGR - 11
     147/* }}} context-manager */
     148
    130149#endif // #ifndef __CF_ERROR_H__
  • trunk/include/cf_file.h

    r128 r151  
    3131
    3232CF_EXPORT int
    33 CF_File_Open            (const char         * path,
     33CF_File_Open            (cf_ctx             * ctx,
     34                         const char         * path,
    3435                         const CF_FILE_FLAG flag);
    3536
    3637CF_EXPORT int
    37 CF_File_Create          (const char * path);
     38CF_File_Close           (cf_ctx ctx);
    3839
    3940CF_EXPORT int
    40 CF_File_Close           (const int fd);
    41 
    42 CF_EXPORT int
    43 CF_File_Read            (const int      fd,
     41CF_File_Read            (const cf_ctx   ctx,
    4442                         void           * buf,
    4543                         const size_t   len);
    4644
    4745CF_EXPORT int
    48 CF_File_Write           (const int      fd,
     46CF_File_Write           (const cf_ctx   ctx,
    4947                         const void     * buf,
    5048                         const size_t   len);
     
    5957CF_File_MakeDirectory   (const char * path);
    6058
     59CF_EXPORT int
     60CF_File_GetPath         (const cf_ctx   ctx,
     61                         char           * path);
     62
    6163#ifdef __cplusplus
    6264}
  • trunk/include/cf_list.h

    r128 r151  
    1313#include "cf_base.h"
    1414
    15 /** 리스트 컨텍스트 (Opaque) */
    16 typedef void *  CF_List_Ctx;
    17 
    1815/** 리스트 탐색자 (Opaque) */
    19 typedef void *  CF_Traverser;
    20 
    21 /** 추가 위치 */
    22 typedef enum
    23 {
    24     CF_DIRECTION_BEFORE,    /**< traverser의 앞 */
    25     CF_DIRECTION_AFTER      /**< traverser의 뒤 */
    26 } CF_DIRECTION;
     16typedef cf_ctx  cf_traverser;
    2717
    2818#ifdef __cplusplus
     
    3121
    3222CF_EXPORT int
    33 CF_List_CreateCtx       (CF_List_Ctx * ctx);
     23CF_List_Create          (cf_ctx * ctx);
    3424
    3525CF_EXPORT int
    36 CF_List_DestroyCtx      (CF_List_Ctx ctx);
     26CF_List_Destroy         (cf_ctx ctx);
    3727
    3828CF_EXPORT int
    39 CF_List_Front           (CF_List_Ctx    ctx,
    40                          CF_Traverser   * traverser);
     29CF_List_Front           (cf_ctx         ctx,
     30                         cf_traverser   * traverser);
    4131
    4232CF_EXPORT int
    43 CF_List_Rear            (CF_List_Ctx    ctx,
    44                          CF_Traverser   * traverser);
     33CF_List_Rear            (cf_ctx         ctx,
     34                         cf_traverser   * traverser);
    4535
    4636CF_EXPORT int
    47 CF_List_Insert          (CF_List_Ctx        ctx,
    48                          const CF_Traverser traverser,
    49                          const CF_DIRECTION direction,
     37CF_List_AddFront        (cf_ctx     ctx,
     38                         const void * element);
     39
     40CF_EXPORT int
     41CF_List_AddRear         (cf_ctx     ctx,
     42                         const void * element);
     43
     44CF_EXPORT int
     45CF_List_InsertBefore    (cf_ctx             ctx,
     46                         const cf_traverser traverser,
    5047                         const void         * element);
    5148
    5249CF_EXPORT int
    53 CF_List_Remove          (CF_List_Ctx    ctx,
    54                          CF_Traverser   * traverser);
     50CF_List_InsertAfter     (cf_ctx             ctx,
     51                         const cf_traverser traverser,
     52                         const void         * element);
    5553
    5654CF_EXPORT int
    57 CF_List_RemoveAll       (CF_List_Ctx ctx);
     55CF_List_Set             (cf_traverser   traverser,
     56                         const void     * element);
    5857
    5958CF_EXPORT int
    60 CF_List_GetElement      (const CF_Traverser traverser,
     59CF_List_Get             (const cf_traverser traverser,
    6160                         void               ** element);
    6261
    6362CF_EXPORT int
    64 CF_List_Prev            (CF_Traverser * traverser);
     63CF_List_Remove          (cf_ctx         ctx,
     64                         cf_traverser   * traverser);
    6565
    6666CF_EXPORT int
    67 CF_List_Next            (CF_Traverser * traverser);
     67CF_List_RemoveAll       (cf_ctx ctx);
    6868
    6969CF_EXPORT int
    70 CF_List_GetSize         (CF_List_Ctx ctx);
     70CF_List_Prev            (cf_traverser * traverser);
     71
     72CF_EXPORT int
     73CF_List_Next            (cf_traverser * traverser);
     74
     75CF_EXPORT int
     76CF_List_GetSize         (cf_ctx ctx);
    7177
    7278#ifdef __cplusplus
  • trunk/include/cf_log.h

    r128 r151  
    2929
    3030CF_EXPORT int
    31 CF_Log_Initialize       (const int poolSize);
    32 
    33 CF_EXPORT int
    34 CF_Log_Finalize         (void);
    35 
    36 CF_EXPORT int
    37 CF_Log_Open             (const int  mapid,
     31CF_Log_Create           (cf_ctx     * ctx,
    3832                         const char * path,
    3933                         const int  memsize);
    4034
    4135CF_EXPORT int
    42 CF_Log_Close            (const int mapid);
     36CF_Log_Destroy          (cf_ctx ctx);
    4337
    4438CF_EXPORT int
    45 CF_Log_Write            (const int  mapid,
     39CF_Log_Write            (cf_ctx     ctx,
    4640                         const char * prefix,
    4741                         const char * fmt, ...);
    4842
    4943CF_EXPORT int
    50 CF_Log_Flush            (const int mapid);
     44CF_Log_Flush            (cf_ctx ctx);
    5145
    5246CF_EXPORT int
    53 CF_Log_SetMT            (const int      mapid,
    54                          const CF_BOOL  flag);
     47CF_Log_SetMultiThread   (cf_ctx ctx);
     48
     49CF_EXPORT int
     50CF_Log_UnsetMultiThread (cf_ctx ctx);
    5551
    5652#ifdef __cplusplus
  • trunk/include/cf_mutex.h

    r128 r151  
    55 *
    66 * \brief 뮤텍스 지원
    7  *
    8  * \example thread.c
    97 */
    108#ifndef __CF_MUTEX_H__
     
    1311#include "cf_base.h"
    1412
    15 /** 뮤텍스 컨텍스트 */
    16 typedef void *  CF_Mutex_Ctx;
    17 
    1813#ifdef __cplusplus
    1914extern "C" {
     
    2116
    2217CF_EXPORT int
    23 CF_Mutex_CreateCtx      (CF_Mutex_Ctx * ctx);
     18CF_Mutex_Create         (cf_ctx * ctx);
    2419
    2520CF_EXPORT int
    26 CF_Mutex_DestoryCtx     (CF_Mutex_Ctx ctx);
     21CF_Mutex_Destory        (cf_ctx ctx);
    2722
    2823CF_EXPORT int
    29 CF_Mutex_Lock           (CF_Mutex_Ctx ctx);
     24CF_Mutex_Lock           (cf_ctx ctx);
    3025
    3126CF_EXPORT int
    32 CF_Mutex_Unlock         (CF_Mutex_Ctx ctx);
     27CF_Mutex_Unlock         (cf_ctx ctx);
    3328
    3429#ifdef __cplusplus
  • trunk/include/cf_queue.h

    r128 r151  
    1313#include "cf_base.h"
    1414
    15 /** 큐 컨텍스트 (Opaque) */
    16 typedef void *  CF_Queue_Ctx;
    17 
    1815#ifdef __cplusplus
    1916extern "C" {
     
    2118
    2219CF_EXPORT int
    23 CF_Queue_CreateCtx      (CF_Queue_Ctx * ctx);
     20CF_Queue_Create         (cf_ctx * ctx);
    2421
    2522CF_EXPORT int
    26 CF_Queue_DestroyCtx     (CF_Queue_Ctx ctx);
     23CF_Queue_Destroy        (cf_ctx ctx);
    2724
    2825CF_EXPORT int
    29 CF_Queue_Put            (CF_Queue_Ctx   ctx,
    30                          const void     * element);
     26CF_Queue_Put            (cf_ctx     ctx,
     27                         const void * element);
    3128
    3229CF_EXPORT int
    33 CF_Queue_Get            (CF_Queue_Ctx   ctx,
    34                          void           ** element);
     30CF_Queue_Get            (cf_ctx ctx,
     31                         void   ** element);
    3532
    3633CF_EXPORT int
    37 CF_Queue_Front          (CF_Queue_Ctx   ctx,
    38                          void           ** element);
     34CF_Queue_Front          (cf_ctx ctx,
     35                         void   ** element);
    3936
    4037CF_EXPORT int
    41 CF_Queue_GetSize        (CF_Queue_Ctx ctx);
     38CF_Queue_GetSize        (cf_ctx ctx);
    4239
    4340#ifdef __cplusplus
  • trunk/include/cf_socket.h

    r128 r151  
    1616
    1717/** 타임아웃을 설정하지 않음 */
    18 #define CF_SOCKET_NO_TIMEOUT        -1
     18#define CF_SOCKET_NO_TIMEOUT        0
    1919
    2020#ifdef __cplusplus
    2121extern "C" {
    2222#endif
    23 
    24 CF_EXPORT CF_BOOL
    25 CF_Socket_IsInitialized     (void);
    2623
    2724CF_EXPORT int
     
    3229
    3330CF_EXPORT int
    34 CF_Socket_Close             (const int sock);
     31CF_Socket_Create            (cf_ctx * ctx);
    3532
    3633CF_EXPORT int
    37 CF_Socket_SetOption         (const int      sock,
     34CF_Socket_Close             (cf_ctx ctx);
     35
     36CF_EXPORT int
     37CF_Socket_Connect           (cf_ctx                 ctx,
     38                             const char             * ip,
     39                             const unsigned short   port);
     40
     41CF_EXPORT int
     42CF_Socket_Server            (cf_ctx                 ctx,
     43                             const unsigned short   port,
     44                             const int              backlog);
     45
     46CF_EXPORT int
     47CF_Socket_Accept            (const cf_ctx   ctx,
     48                             cf_ctx         * client);
     49
     50CF_EXPORT int
     51CF_Socket_Send              (const cf_ctx   ctx,
     52                             const void     * buf,
     53                             const size_t   len);
     54
     55CF_EXPORT int
     56CF_Socket_Recv              (const cf_ctx   ctx,
     57                             void           * buf,
     58                             const size_t   len);
     59
     60CF_EXPORT int
     61CF_Socket_SetOption         (const cf_ctx   ctx,
    3862                             const int      optname,
    3963                             const void     * optval,
     
    4165
    4266CF_EXPORT int
    43 CF_Socket_GetOption         (const int  sock,
    44                              const int  optname,
    45                              void       * optval,
    46                              size_t     * optlen);
     67CF_Socket_GetOption         (const cf_ctx   ctx,
     68                             const int      optname,
     69                             void           * optval,
     70                             size_t         * optlen);
    4771
    4872CF_EXPORT int
    49 CF_Socket_Connect           (const char             * ip,
    50                              const unsigned short   port);
     73CF_Socket_SetTimeOut        (cf_ctx ctx,
     74                             int    timeout);
    5175
    5276CF_EXPORT int
    53 CF_Socket_ConnectTimeout    (const char             * ip,
    54                              const unsigned short   port,
    55                              const int              timeout);
     77CF_Socket_GetIP             (const cf_ctx   ctx,
     78                             char           * ip);
    5679
    5780CF_EXPORT int
    58 CF_Socket_Server            (const unsigned short   port,
    59                              const int              backlog);
    60 
    61 CF_EXPORT int
    62 CF_Socket_Accept            (const int sock);
    63 
    64 CF_EXPORT int
    65 CF_Socket_Send              (const int      sock,
    66                              const void     * buf,
    67                              const size_t   len);
    68 
    69 CF_EXPORT int
    70 CF_Socket_SendTimeout       (const int      sock,
    71                              const void     * buf,
    72                              const size_t   len,
    73                              const int      timeout);
    74 
    75 CF_EXPORT int
    76 CF_Socket_Recv              (const int      sock,
    77                              void           * buf,
    78                              const size_t   len);
    79 
    80 CF_EXPORT int
    81 CF_Socket_RecvTimeout       (const int      sock,
    82                              void           * buf,
    83                              const size_t   len,
    84                              const int      timeout);
     81CF_Socket_GetPort           (const cf_ctx   ctx,
     82                             unsigned short * port);
    8583
    8684#ifdef __cplusplus
  • trunk/include/cf_stack.h

    r128 r151  
    1313#include "cf_base.h"
    1414
    15 /** 스택 컨텍스트 (Opaque) */
    16 typedef void *  CF_Stack_Ctx;
    17 
    1815#ifdef __cplusplus
    1916extern "C" {
     
    2118
    2219CF_EXPORT int
    23 CF_Stack_CreateCtx      (CF_Stack_Ctx * ctx);
     20CF_Stack_Create         (cf_ctx * ctx);
    2421
    2522CF_EXPORT int
    26 CF_Stack_DestroyCtx     (CF_Stack_Ctx ctx);
     23CF_Stack_Destroy        (cf_ctx ctx);
    2724
    2825CF_EXPORT int
    29 CF_Stack_Push           (CF_Stack_Ctx   ctx,
    30                          const void     * element);
     26CF_Stack_Push           (cf_ctx     ctx,
     27                         const void * element);
    3128
    3229CF_EXPORT int
    33 CF_Stack_Pop            (CF_Stack_Ctx   ctx,
    34                          void           ** element);
     30CF_Stack_Pop            (cf_ctx ctx,
     31                         void   ** element);
    3532
    3633CF_EXPORT int
    37 CF_Stack_Top            (CF_Stack_Ctx   ctx,
    38                          void           ** element);
     34CF_Stack_Top            (cf_ctx ctx,
     35                         void   ** element);
    3936
    4037CF_EXPORT int
    41 CF_Stack_GetSize        (CF_Stack_Ctx ctx);
     38CF_Stack_GetSize        (cf_ctx ctx);
    4239
    4340#ifdef __cplusplus
  • trunk/include/cf_thread.h

    r147 r151  
    1616typedef int (* CF_Thread_Function) (void *);
    1717
    18 /** 스레드 컨텍스트 */
    19 typedef void *  CF_Thread_Ctx;
    20 
    2118#ifdef __cplusplus
    2219extern "C" {
     
    2421
    2522CF_EXPORT int
    26 CF_Thread_CreateCtx     (CF_Thread_Ctx      * ctx,
     23CF_Thread_Create        (cf_ctx             * ctx,
    2724                         CF_Thread_Function callback,
    2825                         void               * arg);
    2926
    3027CF_EXPORT int
    31 CF_Thread_Start         (CF_Thread_Ctx ctx);
     28CF_Thread_Start         (cf_ctx ctx);
    3229
    3330CF_EXPORT int
    34 CF_Thread_DestroyCtx    (CF_Thread_Ctx ctx);
     31CF_Thread_Destroy       (cf_ctx ctx);
    3532
    3633CF_EXPORT int
    37 CF_Thread_Join          (CF_Thread_Ctx ctx);
     34CF_Thread_Join          (cf_ctx ctx);
    3835
    3936#ifdef __cplusplus
  • trunk/makeinclude/platform.mk

    r134 r151  
    99DOXYGEN := $(shell which doxygen)
    1010
    11 DOXYGEN_OUTPUT_PATH = ../documentation
     11DOXYGEN_OUTPUT_PATH = ../Documentation
    1212DOXYGEN_PATH        = ../docs
    1313DOXYGEN_FILE        = $(DOXYGEN_PATH)/doxyfile
     
    1616# set options
    1717#------------------------------------------------------------
    18 ifeq ($(DEBUG), YES)
     18ifeq ($(DEBUG), yes)
    1919    DEFS = -D_DEBUG
    2020    FLAG = -g
  • trunk/makeinclude/userdefine.mk

    r119 r151  
    99# compile with debugging flag and definition
    1010# > YES or NO
    11 DEBUG           = YES
     11DEBUG           = yes
    1212
    1313# create doxygen documents automatically
    1414# > YES or NO
    15 DOXYGEN_CREATE  = YES
     15DOXYGEN_CREATE  = yes
  • trunk/src/cf_codec.c

    r129 r151  
    1313#include <stdio.h>
    1414
     15#define BASE64_PADDING_CHAR_INDEX   64
     16
    1517#define ASSERT_ARGS(x)  \
    1618    if ((x))            \
    1719        return CF_ERROR_CODEC_INVALID_ARGS
    1820
    19 const static cf_byte g_ascii_HexDecode[] = {
    20     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*  00 -  15 */
    21     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*  16 -  31 */
    22     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*  32 -  47 */
    23      '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',0x00,0x00,0x00,0x00,0x00,0x00,    /*  48 -  63 */
    24     0x00, 'A', 'A', 'A', 'A', 'A', 'A',0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*  64 -  79 */
    25     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*  80 -  95 */
    26     0x00, 'a', 'a', 'a', 'a', 'a', 'a',0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*  96 - 111 */
    27     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /* 112 - 127 */
    28     /* end of ascii character */
    29     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /* 128 - 143 */
    30     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /* 144 - 159 */
    31     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /* 160 - 175 */
    32     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /* 176 - 191 */
    33     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /* 192 - 207 */
    34     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /* 208 - 223 */
    35     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /* 224 - 239 */
    36     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /* 240 - 255 */
    37 };
    38 
    39 const static char g_table_Base64Encode[] = {
    40 #define BASE64_PADDING_CHAR_INDEX   64
    41     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',     /* 00 - 07 */
    42     'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',     /* 08 - 15 */
    43     'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',     /* 16 - 23 */
    44     'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',     /* 24 - 31 */
    45     'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',     /* 32 - 39 */
    46     'o', 'p', 'q', 'r', 's', 't', 'u', 'v',     /* 40 - 47 */
    47     'w', 'x', 'y', 'z', '0', '1', '2', '3',     /* 48 - 55 */
    48     '4', '5', '6', '7', '8', '9', '+', '/',     /* 56 - 63 */
    49     '='                                         /* padding */
    50 };
    51 
    52 const static cf_byte g_ascii_Base64Decode[] = {
    53     0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,    /*  00 -  15 */
    54     0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,    /*  16 -  31 */
    55     0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,  62,0xff,0xff,0xff,  63,    /*  32 -  47 */
    56       52,  53,  54,  55,  56,  57,  58,  59,  60,  61,0xff,0xff,0xff,  64,0xff,0xff,    /*  48 -  63 */
    57     0xff,   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14,    /*  64 -  79 */
    58       15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,0xff,0xff,0xff,0xff,0xff,    /*  80 -  95 */
    59     0xff,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,  40,    /*  96 - 111 */
    60       41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,0xff,0xff,0xff,0xff,0xff,    /* 112 - 127 */
    61     /* end of ascii character */
    62     0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,    /* 128 - 143 */
    63     0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,    /* 144 - 159 */
    64     0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,    /* 160 - 175 */
    65     0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,    /* 176 - 191 */
    66     0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,    /* 192 - 207 */
    67     0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,    /* 208 - 223 */
    68     0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,    /* 224 - 239 */
    69     0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,    /* 240 - 255 */
    70 };
    71 
    72 
    7321/**
    7422 * hex-encode
     
    8836                     char           * hex)
    8937{
    90     size_t  iter = 0;
    91     size_t  hexlen = len * 2 + 1;
    92 
    93     const cf_byte   * ptr = bin;
    94 
    95     const static char   hexchar[] = {'0', '1', '2', '3',
    96                                      '4', '5', '6', '7',
    97                                      '8', '9', 'a', 'b',
    98                                      'c', 'd', 'e', 'f'};
     38    const cf_byte   * src = bin;
     39    char            * dst = hex;
     40
     41    const static char   hexenc[] = {
     42        '0', '1', '2', '3', '4', '5', '6', '7',
     43        '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
     44    };
    9945
    10046    ASSERT_ARGS (bin == NULL);
    10147    ASSERT_ARGS (hex == NULL);
    10248
    103     for (iter = 0 ; iter < hexlen ; iter += 2, ptr++)
    104     {
    105         hex[iter    ] = hexchar[((*(ptr)) >> 4) & 0x0f];
    106         hex[iter + 1] = hexchar[((*(ptr))     ) & 0x0f];
    107     }
    108     hex[hexlen - 1] = '\0';
     49    for ( ; *src ; src++)
     50    {
     51        *dst++ = hexenc[((*src) >> 4) & 0x0f];
     52        *dst++ = hexenc[((*src)     ) & 0x0f];
     53    }
     54    *dst = '\0';
    10955
    11056    return CF_OK;
     
    11460 * hex-decode
    11561 *
    116  * \return 성공 시, 디코딩된 바이너리 데이터의 길이; 실패 시, 오류 코드
     62 * \return 성공 시, CF_OK; 실패 시, 오류 코드
    11763 *
    11864 * \param hex   16진수 문자열
     
    12975{
    13076    size_t      length = 0; /* absolutely even-number */
    131     size_t      iter = 0;
    132     size_t      binlen = 0;
    133 
    134     const char  * ptr = hex;
     77
     78    const char  * src = hex;
     79    cf_byte     * dst = bin;
    13580    char        buf = 0;
    13681    cf_byte     val = 0;
    13782    cf_byte     asciiHex = 0;
    13883
     84    const static cf_byte hexdec[] = {
     85        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*  00 -  07 */
     86        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*  08 -  15 */
     87        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*  16 -  23 */
     88        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*  24 -  31 */
     89        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*  32 -  39 */
     90        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*  40 -  47 */
     91         '0', '0', '0', '0', '0', '0', '0', '0',    /*  48 -  55 */
     92         '0', '0',0x00,0x00,0x00,0x00,0x00,0x00,    /*  56 -  63 */
     93        0x00, 'A', 'A', 'A', 'A', 'A', 'A',0x00,    /*  64 -  71 */
     94        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*  71 -  79 */
     95        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*  80 -  87 */
     96        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /*  88 -  95 */
     97        0x00, 'a', 'a', 'a', 'a', 'a', 'a',0x00,    /*  96 - 103 */
     98        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /* 104 - 111 */
     99        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    /* 112 - 119 */
     100        0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00     /* 120 - 127 */
     101    };
     102
    139103    ASSERT_ARGS (hex == NULL);
    140104    ASSERT_ARGS (bin == NULL);
    141105    ASSERT_ARGS (len == NULL);
    142106
    143     *len = 0;
    144 
    145     for ( ; g_ascii_HexDecode[(int)*ptr] && *ptr ; ptr++, length++);
    146 
    147     if (*ptr)
     107    for ( ; hexdec[(int)*src] && *src ; src++, length++);
     108
     109    if (*src)
    148110        return CF_ERROR_CODEC_NOT_HEXSTRING;
    149111
    150     binlen = length / 2;
    151 
    152     for (iter = 0, ptr = hex ; *ptr ; iter++)
     112    for (src = hex ; *src ; )
    153113    {
    154114        val = 0;    /* init/re-init docoding-buffer */
    155115
    156116        /* decode one character */
    157 #define DECODE_HEX(x)                               \
    158         do {                                        \
    159         buf = *(x);                                 \
    160         val = (cf_byte)(val << 4);          \
    161         asciiHex = g_ascii_HexDecode[(int)buf];     \
    162                                                     \
    163         val |=  (cf_byte)                       \
     117#define DECODE_HEX(x)                   \
     118        do {                            \
     119        buf = *(x);                     \
     120        val = (cf_byte)(val << 4);      \
     121        asciiHex = hexdec[(int)buf];    \
     122        \
     123        val |=  (cf_byte)               \
    164124                (buf - asciiHex + (asciiHex == '0' ? 0 : 10));  \
    165125        } while (0)
    166126
    167127        /* decode one byte by decode two character */
    168         DECODE_HEX (ptr++);
    169         DECODE_HEX (ptr++);
    170 
    171         bin[iter] = val;
    172     }
    173 
    174     * len = binlen;
     128        DECODE_HEX (src++);
     129        DECODE_HEX (src++);
     130
     131        *dst++ = val;
     132    }
     133    *len = length / 2;
    175134
    176135    return CF_OK;
     
    196155    const cf_byte   * src = bin;
    197156    char            * dst = base64;
     157
     158    const static char base64enc[] = {
     159        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', /* 00 - 07 */
     160        'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', /* 08 - 15 */
     161        'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', /* 16 - 23 */
     162        'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', /* 24 - 31 */
     163        'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 32 - 39 */
     164        'o', 'p', 'q', 'r', 's', 't', 'u', 'v', /* 40 - 47 */
     165        'w', 'x', 'y', 'z', '0', '1', '2', '3', /* 48 - 55 */
     166        '4', '5', '6', '7', '8', '9', '+', '/', /* 56 - 63 */
     167        '='                                     /* padding */
     168    };
    198169
    199170    ASSERT_ARGS (src == NULL);
     
    207178#define SEXTUPLE_E_6(__x)   (((__x[2])     ) & 0x3f)
    208179
    209     for ( ; src - bin < len - 2 ; src += 3)
    210     {
    211         *dst++ = g_table_Base64Encode[SEXTUPLE_E_1 (src)];
    212         *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)|
    213                                       SEXTUPLE_E_3 (src)];
    214         *dst++ = g_table_Base64Encode[SEXTUPLE_E_4 (src)|
    215                                       SEXTUPLE_E_5 (src)];
    216         *dst++ = g_table_Base64Encode[SEXTUPLE_E_6 (src)];
    217     }
    218 
    219     if (src - bin < len)
    220     {
    221         *dst++ = g_table_Base64Encode[SEXTUPLE_E_1 (src)];
     180    for ( ; (size_t)(src - bin) < len - 2 ; src += 3)
     181    {
     182        *dst++ = base64enc[SEXTUPLE_E_1 (src)];
     183        *dst++ = base64enc[SEXTUPLE_E_2 (src) | SEXTUPLE_E_3 (src)];
     184        *dst++ = base64enc[SEXTUPLE_E_4 (src) | SEXTUPLE_E_5 (src)];
     185        *dst++ = base64enc[SEXTUPLE_E_6 (src)];
     186    }
     187
     188    if ((size_t)(src - bin) < len)
     189    {
     190        *dst++ = base64enc[SEXTUPLE_E_1 (src)];
    222191
    223192        if (src - bin == len - 1)
    224193        {
    225             *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)];
    226             *dst++ = g_table_Base64Encode[BASE64_PADDING_CHAR_INDEX];
     194            *dst++ = base64enc[SEXTUPLE_E_2 (src)];
     195            *dst++ = base64enc[BASE64_PADDING_CHAR_INDEX];
    227196        }
    228197        else
    229198        {
    230             *dst++ = g_table_Base64Encode[SEXTUPLE_E_2 (src)|
    231                                           SEXTUPLE_E_3 (src)];
    232             *dst++ = g_table_Base64Encode[SEXTUPLE_E_4 (src)];
     199            *dst++ = base64enc[SEXTUPLE_E_2 (src) | SEXTUPLE_E_3 (src)];
     200            *dst++ = base64enc[SEXTUPLE_E_4 (src)];
    233201        }
    234202
    235         *dst++ = g_table_Base64Encode[BASE64_PADDING_CHAR_INDEX];
     203        *dst++ = base64enc[BASE64_PADDING_CHAR_INDEX];
    236204    }
    237205    *dst = '\0';
     
    243211 * Base64-decode
    244212 *
    245  * \return 성공 시, 디코딩된 바이너리 데이터의 길이; 실패 시, 오류 코드
     213 * \return 성공 시, CF_OK; 실패 시, 오류 코드
    246214 *
    247215 * \param base64    base64 문자열
     
    259227    const char  * src = base64;
    260228    cf_byte     * dst = bin;
    261     int         remain = 0;
    262     int         binlen = 0;
     229    size_t      remain = 0;
     230    size_t      binlen = 0;
     231
     232    const static cf_byte base64dec[] = {
     233        0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,    /*  00 -  07 */
     234        0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,    /*  08 -  15 */
     235        0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,    /*  16 -  23 */
     236        0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,    /*  24 -  31 */
     237        0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,    /*  32 -  39 */
     238        0xff,0xff,0xff,  62,0xff,0xff,0xff,  63,    /*  40 -  47 */
     239          52,  53,  54,  55,  56,  57,  58,  59,    /*  48 -  55 */
     240          60,  61,0xff,0xff,0xff,  64,0xff,0xff,    /*  56 -  63 */
     241        0xff,   0,   1,   2,   3,   4,   5,   6,    /*  64 -  71 */
     242           7,   8,   9,  10,  11,  12,  13,  14,    /*  71 -  79 */
     243          15,  16,  17,  18,  19,  20,  21,  22,    /*  80 -  87 */
     244          23,  24,  25,0xff,0xff,0xff,0xff,0xff,    /*  88 -  95 */
     245        0xff,  26,  27,  28,  29,  30,  31,  32,    /*  96 - 103 */
     246          33,  34,  35,  36,  37,  38,  39,  40,    /* 104 - 111 */
     247          41,  42,  43,  44,  45,  46,  47,  48,    /* 112 - 119 */
     248          49,  50,  51,0xff,0xff,0xff,0xff,0xff     /* 120 - 127 */
     249    };
    263250
    264251    ASSERT_ARGS (src == NULL);
     
    266253    ASSERT_ARGS (len == NULL);
    267254
    268     *len = 0;
    269 
    270     while (g_ascii_Base64Decode[(int)*src] < BASE64_PADDING_CHAR_INDEX) src++;
     255    while (base64dec[(int)*src] < BASE64_PADDING_CHAR_INDEX) src++;
    271256
    272257    if (*src == 0xff)
    273258        return CF_ERROR_CODEC_NOT_BASE64;
    274259
    275     remain = (int)(src - base64);
     260    remain = (size_t)(src - base64);
    276261    binlen = ((remain + 2/* max padding length */) / 4) * 3;
    277262
    278 #define SEXTUPLE_D_1(src)   (g_ascii_Base64Decode[(int)src[0]] << 2)
    279 #define SEXTUPLE_D_2(src)   (g_ascii_Base64Decode[(int)src[1]] >> 4)
    280 #define SEXTUPLE_D_3(src)   (g_ascii_Base64Decode[(int)src[1]] << 4)
    281 #define SEXTUPLE_D_4(src)   (g_ascii_Base64Decode[(int)src[2]] >> 2)
    282 #define SEXTUPLE_D_5(src)   (g_ascii_Base64Decode[(int)src[2]] << 6)
    283 #define SEXTUPLE_D_6(src)   (g_ascii_Base64Decode[(int)src[3]]     )
     263#define SEXTUPLE_D_1(src)   (base64dec[(int)src[0]] << 2)
     264#define SEXTUPLE_D_2(src)   (base64dec[(int)src[1]] >> 4)
     265#define SEXTUPLE_D_3(src)   (base64dec[(int)src[1]] << 4)
     266#define SEXTUPLE_D_4(src)   (base64dec[(int)src[2]] >> 2)
     267#define SEXTUPLE_D_5(src)   (base64dec[(int)src[2]] << 6)
     268#define SEXTUPLE_D_6(src)   (base64dec[(int)src[3]]     )
    284269
    285270    for (src = base64 ; remain > 4 ; remain -= 4, src += 4)
     
    297282        *dst++ = (cf_byte)(SEXTUPLE_D_5 (src) | SEXTUPLE_D_6 (src));
    298283
    299     return binlen - (4 - remain);
    300 }
     284    *len = binlen - (4 - remain);
     285
     286    return CF_OK;
     287}
  • trunk/src/cf_debug.c

    r147 r151  
    2828        return CF_ERROR_DEBUG_INVALID_CTX
    2929
    30 /** 디버그 컨텍스트 (Opaque) */
    31 typedef void *  CF_Debug_Ctx;
    32 
    3330/** 콜스택 인터페이스 */
    34 typedef struct cf_debug_callStack {
     31typedef struct cf_debug_callStack
     32{
    3533    char    file[NAME_LENGTH + 1];      /* *< 파일 이름 */
    3634    char    function[NAME_LENGTH + 1];  /* *< 함수 이름 */
     
    5048} CF_DEBUG_CALLSTACK;
    5149
    52 /** 디버그 컨텍스트 (CF_Debug_Ctx의 구현) */
     50/** 디버그 컨텍스트 (cf_ctx의 구현) */
    5351typedef struct __cf_debug_ctx__
    5452{
    55     int             fd;
    56     CF_Mutex_Ctx    mutex;
     53    int     fd;
     54    cf_ctx  mutex;
    5755
    5856    CF_DEBUG_CALLSTACK  callstack;
    59 } CF_DEBUG_CTX;
    60 
    61 static CF_Debug_Ctx gDebugSingleCtx = NULL;
     57} CF_DEBUG_CONTEXT;
     58
     59static cf_ctx   gDebugSingleCtx = NULL;
    6260
    6361static int
     
    187185 */
    188186static int
    189 CF_Debug_CallStackPush (CF_Debug_Ctx    ctx,
    190                         const char      * file,
    191                         const char      * func,
    192                         const int       line)
    193 {
    194     CF_DEBUG_CTX        * context = (CF_DEBUG_CTX *) ctx;
     187CF_Debug_CallStackPush (cf_ctx      ctx,
     188                        const char  * file,
     189                        const char  * func,
     190                        const int   line)
     191{
     192    CF_DEBUG_CONTEXT    * context = (CF_DEBUG_CONTEXT *) ctx;
    195193    CF_DEBUG_CALLSTACK  * push = NULL;
    196194
    197195    ASSERT_CTX (ctx);
    198196
    199     push = (CF_DEBUG_CALLSTACK *) calloc (sizeof (CF_DEBUG_CALLSTACK), 1);
     197    push = NEWCTX (CF_DEBUG_CALLSTACK);
    200198    if (push == NULL)
    201199        return CF_ERROR_DEBUG_PUSH_CALLSTACK;
     
    223221 */
    224222static int
    225 CF_Debug_CallStackPeek (CF_Debug_Ctx        ctx,
     223CF_Debug_CallStackPeek (cf_ctx              ctx,
    226224                        CF_Debug_CallStack  * callstack)
    227225{
    228     CF_DEBUG_CTX        * context = (CF_DEBUG_CTX *) ctx;
     226    CF_DEBUG_CONTEXT    * context = (CF_DEBUG_CONTEXT *) ctx;
    229227    CF_DEBUG_CALLSTACK  * pop = NULL;
    230228
     
    254252 */
    255253static int
    256 CF_Debug_CallStackPop (CF_Debug_Ctx         ctx,
     254CF_Debug_CallStackPop (cf_ctx               ctx,
    257255                       CF_Debug_CallStack   * callstack)
    258256{
    259     CF_DEBUG_CTX        * context = (CF_DEBUG_CTX *) ctx;
     257    CF_DEBUG_CONTEXT    * context = (CF_DEBUG_CONTEXT *) ctx;
    260258    CF_DEBUG_CALLSTACK  * pop = NULL;
    261259
     
    283281 */
    284282static int
    285 CF_Debug_DestroyCtx (CF_Debug_Ctx ctx)
    286 {
    287     CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx;
     283CF_Debug_Destroy (cf_ctx ctx)
     284{
     285    CF_DEBUG_CONTEXT * context = (CF_DEBUG_CONTEXT *) ctx;
    288286
    289287    ASSERT_CTX (ctx);
     
    293291
    294292    if (context->mutex)
    295         CF_Mutex_DestoryCtx (context->mutex);
     293        CF_Mutex_Destory (context->mutex);
    296294
    297295    free (context);
     
    303301 * 디버그 컨텍스트를 생성
    304302 *
    305  * \return 성공 시, CF_Debug_Ctx 형태의 컨텍스트; 실패 시, NULL
     303 * \return 성공 시, cf_ctx 형태의 컨텍스트; 실패 시, NULL
    306304 *
    307305 * \param ctx 디버그 컨텍스트
    308306 */
    309307static int
    310 CF_Debug_CreateCtx (CF_Debug_Ctx * ctx)
    311 {
    312     int             result = 0;
    313     CF_DEBUG_CTX    * context = NULL;
     308CF_Debug_Create (cf_ctx * ctx)
     309{
     310    int result = 0;
     311
     312    CF_DEBUG_CONTEXT * context = NULL;
    314313
    315314    TRY
    316315    {
    317         context = (CF_DEBUG_CTX *) calloc (sizeof (CF_DEBUG_CTX), 1);
     316        context = NEWCTX (CF_DEBUG_CONTEXT);
    318317        if (context == NULL)
    319318        {
     
    322321        }
    323322
    324         result = CF_Mutex_CreateCtx (&context->mutex);
     323        result = CF_Mutex_Create (&context->mutex);
    325324        if (result < 0)
    326325        {
     
    328327        }
    329328
    330         *ctx = (CF_Debug_Ctx) context;
     329        *ctx = (cf_ctx) context;
    331330    }
    332331    CATCH_IF (result < 0)
    333332    {
    334         CF_Debug_DestroyCtx (context);
     333        CF_Debug_Destroy (context);
    335334    }
    336335
     
    349348
    350349    if (gDebugSingleCtx == NULL)
    351         result = CF_Debug_CreateCtx (&gDebugSingleCtx);
     350        result = CF_Debug_Create (&gDebugSingleCtx);
    352351
    353352    return result;
     
    362361CF_Debug_Finalize (void)
    363362{
    364     CF_Debug_Ctx ctx = gDebugSingleCtx;
     363    cf_ctx ctx = gDebugSingleCtx;
    365364
    366365    gDebugSingleCtx = NULL;
    367366
    368     return CF_Debug_DestroyCtx (ctx);
     367    return CF_Debug_Destroy (ctx);
    369368}
    370369
     
    383382                        const int   line)
    384383{
    385     CF_DEBUG_CTX * ctx = (CF_DEBUG_CTX *)gDebugSingleCtx;
     384    CF_DEBUG_CONTEXT * ctx = (CF_DEBUG_CONTEXT *)gDebugSingleCtx;
    386385
    387386    ASSERT_CTX (ctx);
     
    402401CF_Debug_LeaveFunction (void)
    403402{
    404     CF_DEBUG_CTX * ctx = (CF_DEBUG_CTX *)gDebugSingleCtx;
     403    CF_DEBUG_CONTEXT * ctx = (CF_DEBUG_CONTEXT *)gDebugSingleCtx;
    405404
    406405    ASSERT_CTX (ctx);
     
    425424    int iter = 0;
    426425
    427     CF_DEBUG_CTX        * ctx = gDebugSingleCtx;
     426    CF_DEBUG_CONTEXT    * ctx = gDebugSingleCtx;
    428427    CF_DEBUG_CALLSTACK  * callstack = NULL;
    429428
  • 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}
  • trunk/src/cf_list.c

    r149 r151  
    2424        return CF_ERROR_DS_INVALID_ARGS
    2525
    26 /** 리스트 노드 (CF_Traverser의 구현) */
     26/** 리스트 노드 (cf_traverser의 구현) */
    2727typedef struct __cf_node__
    2828{
     
    3232} CF_NODE;
    3333
    34 /** 리스트 컨텍스트 (CF_List_Ctx의 구현) */
     34/** 리스트 컨텍스트 (cf_ctx의 구현) */
    3535typedef struct __cf_list__
    3636{
     
    4848 */
    4949int
    50 CF_List_CreateCtx (CF_List_Ctx * ctx)
     50CF_List_Create (cf_ctx * ctx)
    5151{
    5252    CF_LIST_CONTEXT * context = NULL;
    5353
    54     ASSERT_ARGS (ctx == NULL);
    55 
    56     context = (CF_LIST_CONTEXT *) calloc (sizeof (CF_LIST_CONTEXT), 1);
     54    ASSERT_CTX (ctx);
     55
     56    context = NEWCTX (CF_LIST_CONTEXT);
    5757    if (context == NULL)
    5858        return CF_ERROR_DS_CREATE_CTX;
    5959
    60     *ctx = (CF_List_Ctx) context;
     60    *ctx = (cf_ctx) context;
    6161
    6262    return CF_OK;
     
    7171 */
    7272int
    73 CF_List_DestroyCtx (CF_List_Ctx ctx)
     73CF_List_Destroy (cf_ctx ctx)
    7474{
    7575    CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx;
     
    8585
    8686/**
    87  * 리스트 탐색자(Traverser)를 Front 위치로 설정
     87 * 리스트 탐색자(traverser)를 Front 위치로 설정
    8888 *
    8989 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     
    9393 */
    9494int
    95 CF_List_Front (CF_List_Ctx  ctx,
    96                CF_Traverser * traverser)
     95CF_List_Front (cf_ctx       ctx,
     96               cf_traverser * traverser)
    9797{
    9898    CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx;
     
    101101    ASSERT_TRAVERSER (traverser);
    102102
    103     *traverser = (CF_Traverser *) context->front;
    104 
    105     return CF_OK;
    106 }
    107 
    108 /**
    109  * 리스트 탐색자(Traverser)를 Rear 위치로 설정
     103    *traverser = (cf_traverser *) context->front;
     104
     105    return CF_OK;
     106}
     107
     108/**
     109 * 리스트 탐색자(traverser)를 Rear 위치로 설정
    110110 *
    111111 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     
    115115 */
    116116int
    117 CF_List_Rear (CF_List_Ctx   ctx,
    118               CF_Traverser  * traverser)
     117CF_List_Rear (cf_ctx        ctx,
     118              cf_traverser  * traverser)
    119119{
    120120    CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx;
     
    123123    ASSERT_TRAVERSER (traverser);
    124124
    125     *traverser = (CF_Traverser *) context->rear;
    126 
    127     return CF_OK;
    128 }
    129 
    130 /**
    131  * 리스트에 새 데이터 삽입
     125    *traverser = (cf_traverser *) context->rear;
     126
     127    return CF_OK;
     128}
     129
     130/**
     131 * 리스트 처음에 새 데이터 삽입
     132 *
     133 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     134 *
     135 * \param ctx       리스트 컨텍스트
     136 * \param element   삽입할 데이터 주소
     137 */
     138int
     139CF_List_AddFront (cf_ctx        ctx,
     140                  const void    * element)
     141{
     142    int             result = 0;
     143    cf_traverser    trav = NULL;
     144
     145    ASSERT_CTX (ctx);
     146
     147    result = CF_List_Front (ctx, &trav);
     148    if (result < 0)
     149        return result;
     150
     151    return CF_List_InsertBefore (ctx, trav, element);
     152}
     153
     154/**
     155 * 리스트 끝에 새 데이터 삽입
     156 *
     157 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     158 *
     159 * \param ctx       리스트 컨텍스트
     160 * \param element   삽입할 데이터 주소
     161 */
     162int
     163CF_List_AddRear (cf_ctx     ctx,
     164                 const void * element)
     165{
     166    int             result = 0;
     167    cf_traverser    trav = NULL;
     168
     169    ASSERT_CTX (ctx);
     170
     171    result = CF_List_Rear (ctx, &trav);
     172    if (result < 0)
     173        return result;
     174
     175    return CF_List_InsertAfter (ctx, trav, element);
     176}
     177
     178/**
     179 * 리스트 탐색자(traverser)의 앞에 새 데이터 삽입
    132180 *
    133181 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     
    135183 * \param ctx       리스트 컨텍스트
    136184 * \param traverser 리스트 탐색자
    137  * \param direction 탐색자의 전/후를 지정
    138185 * \param element   삽입할 데이터 주소
    139  *
    140  * \remarks
    141  * traverser가 NULL일 때,
    142  * direction이 CF_DIRECTION_BEFORE라면 Front 위치이고
    143  * direction이 CF_DIRECTION_AFTER라면 Rear 위치에 삽입
    144  */
    145 int
    146 CF_List_Insert (CF_List_Ctx         ctx,
    147                 const CF_Traverser  traverser,
    148                 const CF_DIRECTION  direction,
    149                 const void          * element)
     186 */
     187int
     188CF_List_InsertBefore (cf_ctx                ctx,
     189                      const cf_traverser    traverser,
     190                      const void            * element)
    150191{
    151192    CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx;
    152     CF_NODE     * node = NULL;
    153     CF_NODE     * newnode = NULL;
    154 
    155     ASSERT_CTX (ctx);
    156 
    157     newnode = (CF_NODE *) calloc (sizeof (CF_NODE), 1);
     193    CF_NODE         * node = NULL;
     194    CF_NODE         * newnode = NULL;
     195
     196    ASSERT_CTX (ctx);
     197
     198    newnode = NEWCTX (CF_NODE);
    158199    if (newnode == NULL)
    159200        return CF_ERROR_DS_CREATE_NODE;
     
    169210        }
    170211
    171         if (direction == CF_DIRECTION_BEFORE)
     212        node = traverser ? (CF_NODE *) traverser : context->front;
     213
     214        newnode->next = node;
     215        if (node)
    172216        {
    173             node = traverser ? (CF_NODE *) traverser : context->front;
    174 
    175             newnode->next = node;
    176             if (node)
    177             {
    178                 newnode->prev = node->prev;
    179                 node->prev  = newnode;
    180                 if (newnode->prev)
    181                     newnode->prev->next = newnode;
    182             }
    183 
    184             if (node == context->front)
    185                 context->front = newnode;
     217            newnode->prev = node->prev;
     218            node->prev  = newnode;
     219            if (newnode->prev)
     220                newnode->prev->next = newnode;
    186221        }
    187         else /* if (direction == CF_DIRECTION_AFTER) */
     222
     223        if (node == context->front)
     224            context->front = newnode;
     225    } NO_CATCH;
     226
     227    context->size++;
     228
     229    return CF_OK;
     230}
     231
     232/**
     233 * 리스트 탐색자(traverser)의 뒤에 새 데이터 삽입
     234 *
     235 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     236 *
     237 * \param ctx       리스트 컨텍스트
     238 * \param traverser 리스트 탐색자
     239 * \param element   삽입할 데이터 주소
     240 */
     241int
     242CF_List_InsertAfter (cf_ctx             ctx,
     243                     const cf_traverser traverser,
     244                     const void         * element)
     245{
     246    CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx;
     247    CF_NODE         * node = NULL;
     248    CF_NODE         * newnode = NULL;
     249
     250    ASSERT_CTX (ctx);
     251
     252    newnode = NEWCTX (CF_NODE);
     253    if (newnode == NULL)
     254        return CF_ERROR_DS_CREATE_NODE;
     255
     256    newnode->element = (void *) element;
     257
     258    TRY
     259    {
     260        if (CF_List_GetSize (ctx) == 0)
    188261        {
    189             node = traverser ? (CF_NODE *) traverser : context->rear;
    190 
    191             newnode->prev = node;
    192             if (node)
    193             {
    194                 newnode->next = node->next;
    195                 node->next = newnode;
    196                 if (newnode->next)
    197                     newnode->next->prev = newnode;
    198             }
    199 
    200             if (node == context->rear)
    201                 context->rear = newnode;
     262            context->front = context->rear = newnode;
     263            TRY_BREAK;
    202264        }
     265
     266        node = traverser ? (CF_NODE *) traverser : context->rear;
     267
     268        newnode->prev = node;
     269        if (node)
     270        {
     271            newnode->next = node->next;
     272            node->next = newnode;
     273            if (newnode->next)
     274                newnode->next->prev = newnode;
     275        }
     276
     277        if (node == context->rear)
     278            context->rear = newnode;
    203279    } NO_CATCH;
    204280
     
    209285
    210286/**
     287 * 리스트 탐색자(traverser)의 자리에 새 데이터 삽입
     288 *
     289 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     290 *
     291 * \param traverser 리스트 탐색자
     292 * \param element   할당할 데이터 주소
     293 */
     294int
     295CF_List_Set (const cf_traverser traverser,
     296             const void         * element)
     297{
     298    CF_NODE * node = (CF_NODE *) traverser;
     299
     300    ASSERT_TRAVERSER (traverser);
     301
     302    node->element = (void *) element;
     303
     304    return CF_OK;
     305}
     306
     307/**
     308 * 탐색자 위치의 데이터를 가져옴
     309 *
     310 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     311 *
     312 * \param traverser 리스트 탐색자
     313 * \param element   데이터 주소
     314 */
     315int
     316CF_List_Get (const cf_traverser traverser,
     317             void               ** element)
     318{
     319    CF_NODE * node = (CF_NODE *) traverser;
     320
     321    ASSERT_TRAVERSER (traverser);
     322    ASSERT_ARGS (element == NULL);
     323
     324    *element = node->element;
     325
     326    return CF_OK;
     327}
     328
     329/**
    211330 * 리스트에서 탐색자 위치의 항목을 삭제
    212331 *
     
    217336 */
    218337int
    219 CF_List_Remove (CF_List_Ctx     ctx,
    220                 CF_Traverser    * traverser)
     338CF_List_Remove (cf_ctx          ctx,
     339                cf_traverser    * traverser)
    221340{
    222341    CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx;
     
    263382 */
    264383int
    265 CF_List_RemoveAll (CF_List_Ctx ctx)
    266 {
    267     CF_Traverser traverser = NULL;
     384CF_List_RemoveAll (cf_ctx ctx)
     385{
     386    cf_traverser traverser = NULL;
    268387
    269388    ASSERT_CTX (ctx);
     
    279398
    280399/**
    281  * 탐색자 위치의 데이터를 가져옴
    282  *
    283  * \return 성공 시, CF_OK; 실패 시, 오류 코드
    284  *
    285  * \param traverser 리스트 탐색자
    286  * \param element   데이터 주소
    287  */
    288 int
    289 CF_List_GetElement (const CF_Traverser  traverser,
    290                     void                ** element)
    291 {
    292     CF_NODE * node = (CF_NODE *) traverser;
    293 
    294     ASSERT_TRAVERSER (traverser);
    295     ASSERT_ARGS (element == NULL);
    296 
    297     *element = node->element;
    298 
    299     return CF_OK;
    300 }
    301 
    302 /**
    303400 * 탐색자 위치를 이전으로 이동
    304401 *
     
    308405 */
    309406int
    310 CF_List_Prev (CF_Traverser * traverser)
     407CF_List_Prev (cf_traverser * traverser)
    311408{
    312409    CF_NODE * node = (CF_NODE *) *traverser;
     
    316413
    317414    node = node->prev;
    318     *traverser = (CF_Traverser *) node;
     415    *traverser = (cf_traverser *) node;
    319416    if (node == NULL)
    320417        return CF_ERROR_DS_NO_MORE;
     
    331428 */
    332429int
    333 CF_List_Next (CF_Traverser * traverser)
     430CF_List_Next (cf_traverser * traverser)
    334431{
    335432    CF_NODE * node = (CF_NODE *) *traverser;
     
    339436
    340437    node = node->next;
    341     *traverser = (CF_Traverser *) node;
     438    *traverser = (cf_traverser *) node;
    342439    if (node == NULL)
    343440        return CF_ERROR_DS_NO_MORE;
     
    354451 */
    355452int
    356 CF_List_GetSize (CF_List_Ctx ctx)
     453CF_List_GetSize (cf_ctx ctx)
    357454{
    358455    CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx;
  • trunk/src/cf_local.h

    r128 r151  
    66#ifndef __CF_LOCAL_H__
    77#define __CF_LOCAL_H__
     8
     9#include <stdlib.h>
    810
    911/** 파일 이름 및 경로 길이 */
     
    1820#define CATCH_ELSE              else
    1921
     22/**
     23 * 유틸 매크로
     24 *
     25 * \remarks
     26 * header는 별도로 include 해야 함
     27 */
     28#define NEWCTX(__ctx)           ((__ctx *) calloc (sizeof (__ctx), 1))
     29
    2030/** non-compatible function */
    2131#if defined(_WIN32) || defined(_WIN64)
  • trunk/src/cf_log.c

    r149 r151  
    1919#include "cf_local.h"
    2020#include "cf_error.h"
     21#include "cf_util.h"
    2122
    2223#include <stdio.h>
     
    2930# include <Windows.h>
    3031#else
    31 # include <sys/time.h>
    3232#endif
     33
     34#define LOG_BUFFER_DEFAULT_SIZE         128 * 1024
    3335
    3436#define ASSERT_CTX(__ctx)   \
     
    3638        return CF_ERROR_LOG_INVALID_CTX
    3739
    38 #define ASSERT_INIT()                   \
    39     if (gLogPool.ctxPool == NULL    ||  \
    40         gLogPool.poolSize <= 0      )   \
    41         return CF_ERROR_LOG_NOT_INITIALIZE
    42 
    43 #define ASSERT_MAPID(__mapid)           \
    44     if (gLogPool.poolSize <= __mapid)   \
    45         return CF_ERROR_LOG_INVALID_MAPID
    46 
    47 #define ASSERT_MAPPED_CTX(__mapid)          \
    48     if (gLogPool.ctxPool[__mapid] != NULL)  \
    49         return CF_ERROR_LOG_ALREADY_MAPPED_ID
    50 
    51 #define ASSERT_NOT_MAPPED_CTX(__mapid)      \
    52     if (gLogPool.ctxPool[__mapid] == NULL)  \
    53         return CF_ERROR_LOG_NOT_MAPPED_ID
    54 
    55 #define LOG_BUFFER_DEFAULT_SIZE         128 * 1024
    56 
    57 #define LOG_DATETIME_LENGTH             sizeof ("0000-00-00 00:00:00.000") - 1
    58 
    59 /** 로그 컨텍스트 (Opaque) */
    60 typedef void *  CF_Log_Ctx;
    61 
    62 typedef struct __cf_util_datetime__
    63 {
    64     int year;
    65     int month;
    66     int day;
    67     int week; /* SUN:0 ~ SAT:6 */
    68 
    69     int hour;
    70     int min;
    71     int sec;
    72     int usec;
    73 } CF_LOG_DATETIME;
    74 
    75 /** 로그 컨텍스트 (CF_Log_Ctx의 구현) */
     40#define ASSERT_ARGS(x)  \
     41    if ((x))            \
     42        return CF_ERROR_LOG_INVALID_ARGS
     43
     44/** 로그 컨텍스트 (cf_ctx의 구현) */
    7645typedef struct __cf_log_ctx__
    7746{
    7847    char            path[NAME_LENGTH + 1];
    79     int             fd;
     48    cf_ctx          file;
    8049    char            * buffer;
    8150    size_t          size;   /* entire size of buffer */
    8251    size_t          length; /* data length in current */
    83     CF_Mutex_Ctx    mutex;
     52    cf_ctx  mutex;
    8453} CF_LOG_CONTEXT;
    85 
    86 typedef struct __cf_log_array__
    87 {
    88     CF_Log_Ctx  * ctxPool;
    89     int         poolSize;
    90 } CF_LOG_POOL;
    91 
    92 static CF_LOG_POOL  gLogPool;
    93 
    94 #if defined(_WIN32) || defined(_WIN64)
    95 /* {{{ */
    96 struct timezone
    97 {
    98     int tz_minuteswest; /* minutes W of Greenwich */
    99     int tz_dsttime;     /* type of dst correction */
    100 };
    101 
    102 int gettimeofday (struct timeval *tv, struct timezone *tz)
    103 {
    104     FILETIME            ft;
    105     unsigned __int64    buf =0;
    106     //static int tzflag = 0;
    107 
    108     if (NULL != tv)
    109     {
    110         GetSystemTimeAsFileTime (&ft);
    111 
    112         buf |=  ft.dwHighDateTime;
    113         buf <<= 32;
    114         buf |=  ft.dwLowDateTime;
    115 
    116         if (buf)
    117         {
    118             buf /= 10;
    119             buf -= ((369 * 365 + 89) * (unsigned __int64) 86400) * 1000000;
    120         }
    121 
    122         tv->tv_sec = (long)(buf / 1000000UL);
    123         tv->tv_usec = (long)(buf % 1000000UL);
    124     }
    125 
    126     /*
    127     if (NULL != tz)
    128     {
    129         if (!tzflag)
    130         {
    131             _tzset();
    132             tzflag++;
    133         }
    134 
    135         // Adjust for the timezone west of Greenwich
    136         tz->tz_minuteswest = _timezone / 60;
    137         tz->tz_dsttime = _daylight;
    138     }
    139     */
    140 
    141     return 0;
    142 }
    143 /* }}} */
    144 #endif
    145 
    146 static int
    147 CF_Log_Local_GetTime (CF_LOG_DATETIME * dt)
    148 {
    149     struct timeval  timeVal;
    150     struct tm       * timeSt;
    151 
    152     gettimeofday (&timeVal, NULL);
    153     timeSt = localtime ((const time_t *)&timeVal.tv_sec);
    154 
    155     dt->year    = timeSt->tm_year + 1900;
    156     dt->month   = timeSt->tm_mon + 1;
    157     dt->day     = timeSt->tm_mday;
    158     dt->week    = timeSt->tm_wday;
    159 
    160     dt->hour    = timeSt->tm_hour;
    161     dt->min     = timeSt->tm_min;
    162     dt->sec     = timeSt->tm_sec;
    163 
    164     dt->usec    = (int) timeVal.tv_usec;
    165 
    166     return CF_OK;
    167 }
    168 
    169 static int
    170 CF_Log_Local_GetTimeString (char * buffer)
    171 {
    172     CF_LOG_DATETIME dt;
    173     CF_Log_Local_GetTime (&dt);
    174 
    175     snprintf (buffer, LOG_DATETIME_LENGTH,
    176               "%04d-%02d-%02d %02d:%02d:%02d.%03d",
    177               dt.year, dt.month, dt.day,
    178               dt.hour, dt.min, dt.sec, dt.usec);
    179 
    180     return CF_OK;
    181 }
    18254
    18355static int
    18456CF_Log_Local_Flush (CF_LOG_CONTEXT * ctx)
    18557{
    186     if (CF_File_Write (ctx->fd, ctx->buffer, ctx->length) < 0)
     58    if (CF_File_Write (ctx->file, ctx->buffer, ctx->length) < 0)
    18759        return CF_ERROR_LOG_FLUSH;
    18860
     
    20880                   const size_t     demandSize)
    20981{
    210     int result = CF_OK;
     82    int result = 0;
     83
     84    size_t writeSize;
     85    size_t remainSize;
    21186
    21287    if (ctx->size > 0) /* 버퍼단위 버퍼링.... */
    21388    {
    214         size_t writeSize;
    215         size_t remainSize;
    216 
    21789        remainSize = demandSize;
    21890        while (remainSize)
    21991        {
    220             writeSize = (ctx->size - ctx->length) < remainSize ? (ctx->size - ctx->length) : remainSize;
    221 
    222             memcpy (ctx->buffer + ctx->length, buffer + demandSize - remainSize, writeSize);
     92            writeSize = (ctx->size - ctx->length) < remainSize
     93                      ? (ctx->size - ctx->length)
     94                      : remainSize;
     95
     96            memcpy (ctx->buffer + ctx->length,
     97                    buffer + demandSize - remainSize,
     98                    writeSize);
    22399            ctx->length += writeSize;
    224100
     
    247123 * \param ctx 로그 컨텍스트
    248124 */
    249 static int
    250 CF_Log_SetMultiThread (CF_Log_Ctx ctx)
    251 {
    252     CF_LOG_CONTEXT * context = (CF_LOG_CONTEXT *) ctx;
    253 
    254     ASSERT_CTX (ctx);
    255 
    256     if (CF_Mutex_CreateCtx (&context->mutex) < 0)
     125int
     126CF_Log_SetMultiThread (cf_ctx ctx)
     127{
     128    CF_LOG_CONTEXT * context = (CF_LOG_CONTEXT *) ctx;
     129
     130    ASSERT_CTX (ctx);
     131
     132    if (CF_Mutex_Create (&context->mutex) < 0)
    257133        return CF_ERROR_LOG_SET_MULTITHREAD;
    258134
     
    267143 * \param ctx 로그 컨텍스트
    268144 */
    269 static int
    270 CF_Log_UnsetMultiThread (CF_Log_Ctx ctx)
    271 {
    272     CF_LOG_CONTEXT * context = (CF_LOG_CONTEXT *) ctx;
    273 
    274     ASSERT_CTX (ctx);
    275 
    276     if (CF_Mutex_DestoryCtx (context->mutex) < 0)
     145int
     146CF_Log_UnsetMultiThread (cf_ctx ctx)
     147{
     148    CF_LOG_CONTEXT * context = (CF_LOG_CONTEXT *) ctx;
     149
     150    ASSERT_CTX (ctx);
     151
     152    if (CF_Mutex_Destory (context->mutex) < 0)
    277153        return CF_ERROR_LOG_UNSET_MULTITHREAD;
    278154
     
    293169 */
    294170static int
    295 CF_Log_Local_WriteVA (CF_Log_Ctx    ctx,
    296                       const char    * prefix,
    297                       const char    * fmt,
    298                       va_list       valist)
     171CF_Log_Local_WriteVA (CF_LOG_CONTEXT    * context,
     172                      const char        * prefix,
     173                      const char        * fmt,
     174                      va_list           valist)
    299175{
    300176#define BUF_LEN 16 * 1024
    301     CF_LOG_CONTEXT  * context = (CF_LOG_CONTEXT *) ctx;
    302     char        buffer[BUF_LEN + 1] = {0x00,};
    303     char        datetime[LOG_DATETIME_LENGTH + 1] = {0x00,};
    304     int         length = 0;
    305     int         result = 0;
    306 
    307     ASSERT_CTX (ctx);
    308 
    309     CF_Log_Local_GetTimeString (datetime);
     177    char    buffer[BUF_LEN + 1] = {0x00,};
     178    char    datetime[CF_UTIL_DATETIME_LENGTH + 1] = {0x00,};
     179    int     length = 0;
     180    int     result = 0;
     181
     182    CF_UTIL_DATETIME dt;
     183
     184    ASSERT_CTX (context);
     185
     186    CF_Util_GetCurrentTime (&dt);
     187    CF_Util_GetTimeString (&dt, datetime);
    310188    length = snprintf (buffer, BUF_LEN, "[%s][%s] ", datetime, prefix);
    311189    vsnprintf (&buffer[length], BUF_LEN - (size_t)length, fmt, valist);
     
    329207 */
    330208int
    331 CF_Log_WriteCtx (CF_Log_Ctx ctx,
    332                  const char * prefix,
    333                  const char * fmt, ...)
    334 {
     209CF_Log_Write (cf_ctx        ctx,
     210              const char    * prefix,
     211              const char    * fmt, ...)
     212{
     213    CF_LOG_CONTEXT * context = (CF_LOG_CONTEXT *) ctx;
     214
    335215    int     result = 0;
    336216    va_list valist;
    337217
    338218    va_start (valist, fmt);
    339     result = CF_Log_Local_WriteVA (ctx, prefix, fmt, valist);
     219    result = CF_Log_Local_WriteVA (context, prefix, fmt, valist);
    340220    va_end (valist);
    341221
     
    350230 * \param ctx 로그 컨텍스트
    351231 */
    352 static int
    353 CF_Log_FlushCtx (CF_Log_Ctx ctx)
    354 {
    355     CF_LOG_CONTEXT  * context = (CF_LOG_CONTEXT *) ctx;
    356     int         result = 0;
     232int
     233CF_Log_Flush (cf_ctx ctx)
     234{
     235    int result = 0;
     236
     237    CF_LOG_CONTEXT * context = (CF_LOG_CONTEXT *) ctx;
    357238
    358239    ASSERT_CTX (ctx);
     
    372253 * \param ctx 로그 컨텍스트
    373254 */
    374 static int
    375 CF_Log_DestroyCtx (CF_Log_Ctx ctx)
     255int
     256CF_Log_Destroy (cf_ctx ctx)
    376257{
    377258    CF_LOG_CONTEXT * context = (CF_LOG_CONTEXT *) ctx;
     
    381262    if (context->size > 0)
    382263    {
    383         CF_Log_FlushCtx (ctx);
     264        CF_Log_Flush (ctx);
    384265        free (context->buffer);
    385266        context->buffer = NULL;
     
    387268    }
    388269
    389     CF_File_Close (context->fd);
    390 
    391     CF_Mutex_DestoryCtx (context->mutex);
     270    CF_File_Close (context->file);
     271
     272    CF_Mutex_Destory (context->mutex);
    392273    context->mutex = NULL;
     274
     275    free (context);
    393276
    394277    return CF_OK;
     
    406289 * \see CF_LOG_DEFAULT_BUFFER, CF_LOG_NO_BUFFER
    407290 */
    408 static int
    409 CF_Log_CreateCtx (CF_Log_Ctx    * ctx,
    410                   const char    * path,
    411                   const int     memsize)
     291int
     292CF_Log_Create (cf_ctx       * ctx,
     293               const char   * path,
     294               const int    memsize)
    412295{
    413296    int result = 0;
     
    417300             : memsize;
    418301
    419     CF_LOG_CONTEXT  * context = NULL;
    420 
    421     if (path == NULL)
    422         return CF_ERROR_LOG_INVALID_ARGS;
     302    CF_LOG_CONTEXT * context = NULL;
     303
     304    ASSERT_CTX (ctx);
     305    ASSERT_ARGS (path == NULL);
    423306
    424307    TRY
    425308    {
    426         context = (CF_LOG_CONTEXT *) calloc (sizeof (CF_LOG_CONTEXT), 1);
     309        context = NEWCTX (CF_LOG_CONTEXT);
    427310        if (context == NULL)
    428311        {
     
    431314        }
    432315
    433         context->fd = CF_File_Open (path, fileFlag);
    434         if (context->fd < 0)
     316        result = CF_File_Open (&context->file, path, fileFlag);
     317        if (result < 0)
    435318        {
    436319            result = CF_ERROR_LOG_CREATE_FILE;
    437320            TRY_BREAK;
    438321        }
    439         snprintf (context->path, sizeof (context->path) -1, "%s", path);
     322        snprintf (context->path, sizeof (context->path) - 1, "%s", path);
    440323
    441324        if (size > 0)
     
    450333        }
    451334
    452         *ctx = (CF_Log_Ctx) context;
     335        *ctx = (cf_ctx) context;
    453336    }
    454337    CATCH_IF (result < 0)
    455338    {
    456         CF_Log_DestroyCtx ((CF_Log_Ctx) context);
    457     }
    458 
    459     return result;
    460 }
    461 
    462 /**
    463  * 로그 컨텍스트에 아이디 넘버 할당<br />
    464  * 로그 기록 시, 아이디 넘버를 사용하면 해당 로그로 기록할 수 있음
    465  *
    466  * \return 성공 시, CF_OK; 실패 시, 오류 코드
    467  *
    468  * \param mapid 부여할 아이디 넘버
    469  * \param ctx   로그 컨텍스트
    470  *
    471  * \remarks 반드시 먼저 초기화 해야하며, 초기화 시에 주어진 번호보다 작은 아이디 넘버를 사용
    472  */
    473 static int
    474 CF_Log_MapCtxID (const int          mapid,
    475                  const CF_Log_Ctx   ctx)
    476 {
    477     ASSERT_INIT ();
    478     ASSERT_MAPID (mapid);
    479     ASSERT_MAPPED_CTX (mapid);
    480 
    481     gLogPool.ctxPool[mapid] = ctx;
    482 
    483     return CF_OK;
    484 }
    485 
    486 /**
    487  * 아이디 넘버에 해당하는 로그를 닫고 해당하는 컨텍스트를 해제
    488  *
    489  * \return 성공 시, CF_OK; 실패 시, 오류 코드
    490  *
    491  * \param mapid 로그의 아이디 넘버
    492  *
    493  * \remarks 아이디 넘버에 해당하는 컨텍스트가 해제되므로 주의
    494  */
    495 static int
    496 CF_Log_UnmapCtxID (const int mapid)
    497 {
    498     ASSERT_INIT ();
    499     ASSERT_MAPID (mapid);
    500     ASSERT_NOT_MAPPED_CTX (mapid);
    501 
    502     CF_Log_DestroyCtx (gLogPool.ctxPool[mapid]);
    503 
    504     free (gLogPool.ctxPool[mapid]);
    505     gLogPool.ctxPool[mapid] = NULL;
    506 
    507     return CF_OK;
    508 }
    509 
    510 /**
    511  * 아이디 넘버에 해당하는 로그 컨텍스트를 얻기
    512  *
    513  * \return 성공 시, CF_OK; 실패 시, 오류 코드
    514  *
    515  * \param mapid 로그의 아이디 넘버
    516  * \param ctx   로그 컨텍스트 받을 주소
    517  */
    518 static int
    519 CF_Log_GetMappedCtx (const int  mapid,
    520                      CF_Log_Ctx * ctx)
    521 {
    522     ASSERT_INIT ();
    523     ASSERT_MAPID (mapid);
    524     ASSERT_NOT_MAPPED_CTX (mapid);
    525 
    526     *ctx = gLogPool.ctxPool[mapid];
    527 
    528     return CF_OK;
    529 }
    530 
    531 /**
    532  * 로그를 사용하기 위해 초기화
    533  *
    534  * \return 성공 시, CF_OK; 실패 시, 오류 코드
    535  *
    536  * \param poolSize  로그 풀 크기로, 로그 아이디 넘버의 최대 값
    537  */
    538 int
    539 CF_Log_Initialize (const int poolSize)
    540 {
    541     memset (&gLogPool, 0x00, sizeof (CF_LOG_POOL));
    542 
    543     if (poolSize > 0)
    544     {
    545         gLogPool.ctxPool =
    546             (CF_Log_Ctx *) calloc ((size_t) poolSize, sizeof (CF_Log_Ctx));
    547         if (gLogPool.ctxPool == NULL)
    548             return CF_ERROR_LOG_INITIALIZE;
    549         gLogPool.poolSize = poolSize;
    550     }
    551 
    552     return CF_OK;
    553 }
    554 
    555 /**
    556  * 로그가 모두 사용된 후 자원 해제
    557  *
    558  * \return CF_OK 반환
    559  */
    560 int
    561 CF_Log_Finalize (void)
    562 {
    563     int mapid = 0;
    564 
    565     for (mapid = 0 ; mapid < gLogPool.poolSize ; mapid++)
    566     {
    567         CF_Log_UnmapCtxID (mapid);
    568     }
    569 
    570     if (gLogPool.ctxPool != NULL)
    571         free (gLogPool.ctxPool);
    572 
    573     memset (&gLogPool, 0x00, sizeof (CF_LOG_POOL));
    574 
    575     return CF_OK;
    576 }
    577 
    578 /**
    579  * 로그 열기
    580  *
    581  * \return 성공 시, CF_OK; 실패 시, 오류 코드
    582  *
    583  * \param mapid     로그의 아이디 넘버
    584  * \param path      로그 파일 경로
    585  * \param memsize   로그 버퍼 크기
    586  *
    587  * \see CF_LOG_DEFAULT_BUFFER, CF_LOG_NO_BUFFER
    588  */
    589 int
    590 CF_Log_Open (const int  mapid,
    591              const char * path,
    592              const int  memsize)
    593 {
    594     int         result = 0;
    595     CF_Log_Ctx  ctx = NULL;
    596 
    597     result = CF_Log_CreateCtx (&ctx, path, memsize);
    598     if (result < 0)
    599         return result;
    600 
    601     result = CF_Log_MapCtxID (mapid, ctx);
    602     if (result < 0)
    603         CF_Log_DestroyCtx (ctx);
    604 
    605     return result;
    606 }
    607 
    608 /**
    609  * 로그 닫기
    610  *
    611  * \return 성공 시, CF_OK; 실패 시, 오류 코드
    612  *
    613  * \param mapid 로그의 아이디 넘버
    614  */
    615 int
    616 CF_Log_Close (const int mapid)
    617 {
    618     return CF_Log_UnmapCtxID (mapid);
    619 }
    620 
    621 /**
    622  * 로그 버퍼의 데이터를 즉시 로그 파일에 쓰기
    623  *
    624  * \return 성공 시, CF_OK; 실패 시, 오류 코드
    625  *
    626  * \param mapid 로그의 아이디 넘버
    627  */
    628 int
    629 CF_Log_Flush (const int mapid)
    630 {
    631     int         result = 0;
    632     CF_Log_Ctx  ctx = NULL;
    633 
    634     result = CF_Log_GetMappedCtx (mapid, &ctx);
    635     if (result < 0)
    636         return result;
    637 
    638     result = CF_Log_FlushCtx (ctx);
    639 
    640     return result;
    641 }
    642 
    643 /**
    644  * 로그 컨텍스트에 멀티쓰레드 모드 설정
    645  *
    646  * \return 성공 시, CF_OK; 실패 시, 오류 코드
    647  *
    648  * \param mapid 로그의 아이디 넘버
    649  * \param flag  설정/해제 bool 플래그
    650  *
    651  * \see CF_BOOL
    652  */
    653 int
    654 CF_Log_SetMT (const int     mapid,
    655               const CF_BOOL flag)
    656 {
    657     int         result = 0;
    658     CF_Log_Ctx  ctx = NULL;
    659 
    660     result = CF_Log_GetMappedCtx (mapid, &ctx);
    661     if (result < 0)
    662         return result;
    663 
    664     result = (flag) ? CF_Log_SetMultiThread (ctx) :
    665                       CF_Log_UnsetMultiThread (ctx);
    666 
    667     return result;
    668 }
    669 
    670 /**
    671  * 로그 쓰기
    672  *
    673  * \return 성공 시, CF_OK; 실패 시, 오류 코드
    674  *
    675  * \param mapid     로그의 아이디 넘버
    676  * \param prefix    로그의 프리픽스 문자열
    677  * \param fmt       포맷 스트링
    678  * \param ...       가변 인자
    679  */
    680 int
    681 CF_Log_Write (const int     mapid,
    682               const char    * prefix,
    683               const char    * fmt, ...)
    684 {
    685     int         result = 0;
    686     CF_Log_Ctx  ctx = NULL;
    687     va_list     valist;
    688 
    689     result = CF_Log_GetMappedCtx (mapid, &ctx);
    690     if (result < 0)
    691         return result;
    692 
    693     va_start (valist, fmt);
    694     result = CF_Log_Local_WriteVA (ctx, prefix, fmt, valist);
    695     va_end (valist);
    696 
    697     return result;
    698 }
     339        CF_Log_Destroy ((cf_ctx) context);
     340    }
     341
     342    return result;
     343}
  • trunk/src/cf_mutex.c

    r149 r151  
    2121#endif // #if defined(_WIN32) || defined(_WIN64)
    2222
    23 #define ASSERT_MUTEX_CTX(__ctx) \
    24     if (__ctx == NULL)          \
     23#define ASSERT_CTX(__ctx)   \
     24    if (__ctx == NULL)      \
    2525        return CF_ERROR_MUTEX_INVALID_CTX
    2626
     
    3838 */
    3939int
    40 CF_Mutex_CreateCtx (CF_Mutex_Ctx * ctx)
     40CF_Mutex_Create (cf_ctx * ctx)
    4141{
    4242    int result = 0;
     
    4444    CF_MUTEX_CONTEXT * context = NULL;
    4545
    46     context = (CF_MUTEX_CONTEXT *) calloc (sizeof (CF_MUTEX_CONTEXT), 1);
     46    ASSERT_CTX (ctx);
     47
     48    context = NEWCTX (CF_MUTEX_CONTEXT);
    4749    if (context == NULL)
    4850        return CF_ERROR_MUTEX_CREATE_CTX;
     
    7072    CATCH_IF (result < 0)
    7173    {
    72         CF_Mutex_DestoryCtx ((CF_Mutex_Ctx) context);
     74        CF_Mutex_Destory ((cf_ctx) context);
    7375    }
    7476
     
    8486 */
    8587int
    86 CF_Mutex_DestoryCtx (CF_Mutex_Ctx ctx)
     88CF_Mutex_Destory (cf_ctx ctx)
    8789{
    88     CF_MUTEX_CONTEXT * context = ctx;
     90    CF_MUTEX_CONTEXT * context = (CF_MUTEX_CONTEXT *) ctx;
    8991
    90     ASSERT_MUTEX_CTX (ctx);
     92    ASSERT_CTX (ctx);
    9193
    9294#if defined(_WIN32) || defined(_WIN64)
     
    112114 */
    113115int
    114 CF_Mutex_Lock (CF_Mutex_Ctx ctx)
     116CF_Mutex_Lock (cf_ctx ctx)
    115117{
    116     CF_MUTEX_CONTEXT * context = ctx;
     118    CF_MUTEX_CONTEXT * context = (CF_MUTEX_CONTEXT *) ctx;
    117119
    118     ASSERT_MUTEX_CTX (ctx);
     120    ASSERT_CTX (ctx);
    119121
    120122#if defined(_WIN32) || defined(_WIN64)
     
    135137 */
    136138int
    137 CF_Mutex_Unlock (CF_Mutex_Ctx ctx)
     139CF_Mutex_Unlock (cf_ctx ctx)
    138140{
    139     CF_MUTEX_CONTEXT * context = ctx;
     141    CF_MUTEX_CONTEXT * context = (CF_MUTEX_CONTEXT *) ctx;
    140142
    141     ASSERT_MUTEX_CTX (ctx);
     143    ASSERT_CTX (ctx);
    142144
    143145#if defined(_WIN32) || defined(_WIN64)
  • trunk/src/cf_queue.c

    r128 r151  
    88#include "cf_queue.h"
    99#include "cf_list.h"
     10#include "cf_local.h"
    1011#include "cf_error.h"
    1112
    1213#include <stdio.h>
     14
     15static int
     16CF_Queue_Local_Get (cf_ctx  ctx,
     17                    void    ** element,
     18                    CF_BOOL removeFlag)
     19{
     20    int             result = 0;
     21    cf_ctx          list = (cf_ctx) ctx;
     22    cf_traverser    trav = NULL;
     23
     24    result = CF_List_Front (list, &trav);
     25    if (result < 0)
     26        return result;
     27
     28    result = CF_List_Get (trav, element);
     29    if (result < 0)
     30        return result;
     31
     32    result = (removeFlag == CF_TRUE)
     33           ? CF_List_Remove (ctx, &trav)
     34           : CF_OK;
     35
     36    return result;
     37}
    1338
    1439/**
     
    2045 */
    2146int
    22 CF_Queue_CreateCtx (CF_Queue_Ctx * ctx)
     47CF_Queue_Create (cf_ctx * ctx)
    2348{
    24     return CF_List_CreateCtx ((CF_List_Ctx *) ctx);
     49    return CF_List_Create ((cf_ctx *) ctx);
    2550}
    2651
     
    3358 */
    3459int
    35 CF_Queue_DestroyCtx (CF_Queue_Ctx ctx)
     60CF_Queue_Destroy (cf_ctx ctx)
    3661{
    37     return CF_List_DestroyCtx (ctx);
     62    return CF_List_Destroy (ctx);
    3863}
    3964
     
    4772 */
    4873int
    49 CF_Queue_Put (CF_Queue_Ctx  ctx,
     74CF_Queue_Put (cf_ctx        ctx,
    5075              const void    * element)
    5176{
    52     int             result = 0;
    53     CF_List_Ctx     list = (CF_List_Ctx) ctx;
    54     CF_Traverser    traverser = NULL;
     77    cf_ctx list = (cf_ctx) ctx;
    5578
    56     result = CF_List_Rear (list, &traverser);
    57     if (result < 0)
    58         return result;
    59 
    60     return CF_List_Insert (list, traverser, CF_DIRECTION_AFTER, element);
     79    return CF_List_AddRear (list, element);
    6180}
    6281
     
    7089 */
    7190int
    72 CF_Queue_Get (CF_Queue_Ctx  ctx,
    73               void          ** element)
     91CF_Queue_Get (cf_ctx    ctx,
     92              void      ** element)
    7493{
    75     int         result = 0;
    76     CF_List_Ctx list = (CF_List_Ctx) ctx;
    77     CF_Traverser    traverser = NULL;
    78 
    79     result = CF_Queue_Front (ctx, element);
    80     if (result < 0)
    81         return result;
    82 
    83     result = CF_List_Front (list, &traverser);
    84     if (result < 0)
    85         return result;
    86 
    87     return CF_List_Remove (ctx, &traverser);
     94    return CF_Queue_Local_Get (ctx, element, CF_TRUE);
    8895}
    8996
     
    97104 */
    98105int
    99 CF_Queue_Front (CF_Queue_Ctx    ctx,
    100                 void            ** element)
     106CF_Queue_Front (cf_ctx  ctx,
     107                void    ** element)
    101108{
    102     int             result = 0;
    103     CF_List_Ctx     list = (CF_List_Ctx) ctx;
    104     CF_Traverser    traverser = NULL;
    105 
    106     result = CF_List_Front (list, &traverser);
    107     if (result < 0)
    108         return result;
    109 
    110     return CF_List_GetElement (traverser, element);
     109    return CF_Queue_Local_Get (ctx, element, CF_FALSE);
    111110}
    112111
     
    119118 */
    120119int
    121 CF_Queue_GetSize (CF_Queue_Ctx ctx)
     120CF_Queue_GetSize (cf_ctx ctx)
    122121{
    123     return CF_List_GetSize ((CF_List_Ctx) ctx);
     122    return CF_List_GetSize ((cf_ctx) ctx);
    124123}
  • trunk/src/cf_socket.c

    r142 r151  
    99#include "cf_local.h"
    1010#include "cf_error.h"
     11#include "cf_util.h"
    1112
    1213#include <stdio.h>
     
    3334#endif
    3435
     36#define ASSERT_CTX(__ctx)   \
     37    if (__ctx == NULL)      \
     38        return CF_ERROR_SOCKET_INVALID_CTX
     39
     40#define ASSERT_ARGS(x)  \
     41    if ((x))            \
     42        return CF_ERROR_SOCKET_INVALID_ARGS
     43
    3544#define ASSERT_INIT()                   \
    3645    if (!CF_Socket_IsInitialized ())    \
     46        return CF_ERROR_SOCKET_NOT_INITIALIZED
     47
     48#define TRY_INIT()                  \
     49    if (CF_Socket_Initialize ())    \
    3750        return CF_ERROR_SOCKET_NOT_INITIALIZED
    3851
     
    4558# define sa_family_t        unsigned short
    4659# define close(__sock)      closesocket(__sock)
    47 # define GET_SYSTEM_ERROR() WSAGetLastError ()
    4860# define ERROR_INTR         WSAEINTR
    4961#else
    50 # define GET_SYSTEM_ERROR() errno
    5162# define ERROR_INTR         EINTR
    5263#endif
     64
     65#define MAX_IP_LENGTH   256
     66
     67typedef struct __cf_socket_ctx__
     68{
     69    int             sock;
     70    int             timeout;
     71    char            ip[MAX_IP_LENGTH];
     72    unsigned short  port;
     73} CF_SOCKET_CONTEXT;
    5374
    5475static CF_BOOL gInitialized = CF_FALSE;
     
    6485    int flags = fcntl (sock, F_GETFL, 0);
    6586
    66     if (boolean)
    67         flags |=  O_NONBLOCK;
    68     else
    69         flags &= ~O_NONBLOCK;
     87    if (boolean)    flags |=  O_NONBLOCK;
     88    else            flags &= ~O_NONBLOCK;
    7089
    7190    fcntl (sock, F_SETFL, flags);
     
    7493
    7594static int
    76 CF_Socket_Local_SetLinger (const int sock)
    77 {
    78     struct linger linger;
    79 
    80     linger.l_onoff = 1;
    81     linger.l_linger = 0;
    82 
    83     return CF_Socket_SetOption (sock,
    84                                 SO_LINGER,
    85                                 &linger,
    86                                 sizeof (linger));
    87 }
    88 
    89 static int
    90 CF_Socket_Local_SetReUseAddr (const int sock)
    91 {
    92     int reuseaddr = 1;
    93 
    94     return CF_Socket_SetOption (sock,
    95                                 SO_REUSEADDR,
    96                                 &reuseaddr,
    97                                 sizeof (reuseaddr));
    98 }
    99 
    100 static int
    101 CF_Socket_Local_CheckTimeout (const int sock,
    102                               const int timeout)
    103 {
    104     int             result = 0;
    105     fd_set          readfds;
    106     struct timeval  tv;
    107     int             error;
    108 
    109     if (!(timeout > CF_SOCKET_NO_TIMEOUT))
    110         return CF_OK;
    111 
    112     tv.tv_sec = timeout;
    113     tv.tv_usec = 0;
    114 
    115     FD_ZERO (&readfds);
    116     FD_SET (sock, &readfds);
    117 
    118     while ((result = select (sock + 1, &readfds, NULL, NULL, &tv)) < 0)
    119     {
    120         error = GET_SYSTEM_ERROR ();
    121         if (error != ERROR_INTR)
    122         {
    123             result = CF_ERROR_SOCKET_TIMEOUT;
    124             return result;
    125         }
    126     }
    127 
    128     if (FD_ISSET (sock, &readfds) == 0)
    129     {
    130         return CF_ERROR_SOCKET_INTERNAL;
    131     }
    132 
    133     return CF_OK;
    134 }
    135 
    136 /**
    137  * 소켓의 초기화 상태 확인
    138  *
    139  * \return 초기화 된 경우, CF_TRUE; 그렇지 않은 경우, CF_FALSE
    140  */
    141 CF_BOOL
    142 CF_Socket_IsInitialized (void)
    143 {
    144     return gInitialized;
    145 }
    146 
    147 /**
    148  * 소켓 초기화
    149  *
    150  * \return 성공 시, CF_OK; 실패 시, 오류 코드
    151  */
    152 int
    153 CF_Socket_Initialize (void)
    154 {
    155 #if defined(_WIN32) || defined(_WIN64)
    156     WSADATA winsockData;
    157     if (WSAStartup (MAKEWORD (2, 0), &winsockData))
    158        return CF_ERROR_SOCKET_INITIALIZE;
    159 #endif
    160     gInitialized = CF_TRUE;
    161    
    162     return CF_OK;
    163 }
    164 
    165 /**
    166  * 소켓 해제
    167  *
    168  * \return 성공 시, CF_OK; 실패 시, 오류 코드
    169  */
    170 int
    171 CF_Socket_Finalize (void)
    172 {
    173     ASSERT_INIT ();
    174 
    175 #if defined(_WIN32) || defined(_WIN64)
    176     if (WSACleanup ())
    177        return CF_ERROR_SOCKET_FINALIZE;
    178 #endif
    179 
    180     return CF_OK;
    181 }
    182 
    183 /**
    184  * 소켓 닫기
    185  *
    186  * \return 성공 시, CF_OK; 실패 시, 오류 코드
    187  *
    188  * \param sock 소켓
    189  */
    190 int
    191 CF_Socket_Close (const int sock)
    192 {
    193     int result = 0;
    194 
    195     ASSERT_SOCKET (sock);
    196 
    197     result = close (sock);
    198 
    199     if (result != 0)
    200         return CF_ERROR_SOCKET_CLOSE;
    201 
    202     return CF_OK;
    203 }
    204 
    205 /**
    206  * 소켓 옵션 설정
    207  *
    208  * \return 성공 시 CF_OK; 실패 시, 오류 코드
    209  *
    210  * \param sock      소켓
    211  * \param optname   옵션 이름
    212  * \param optval    설정할 옵션 값의 메모리
    213  * \param optlen    설정할 옵션의 길이
    214  */
    215 int
    216 CF_Socket_SetOption (const int      sock,
    217                      const int      optname,
    218                      const void     * optval,
    219                      const size_t   optlen)
     95CF_Socket_Local_SetOption (const int    sock,
     96                           const int    optname,
     97                           const void   * optval,
     98                           const size_t optlen)
    22099{
    221100    int result = 0;
     
    238117}
    239118
    240 /**
    241  * 소켓 옵션 얻기
    242  *
    243  * \return 성공 시 CF_OK; 실패 시, 오류 코드
    244  *
    245  * \param sock      소켓
    246  * \param optname   옵션 이름
    247  * \param optval    옵션 값을 가져올 메모리
    248  * \param optlen    옵션 길이를 가져올 메모리
    249  */
    250 int
    251 CF_Socket_GetOption (const int  sock,
    252                      const int  optname,
    253                      void       * optval,
    254                      size_t     * optlen)
     119static int
     120CF_Socket_Local_GetOption (const int    sock,
     121                           const int    optname,
     122                           void         * optval,
     123                           size_t       * optlen)
    255124{
    256125    int result = 0;
     
    273142}
    274143
     144static int
     145CF_Socket_Local_SetLinger (const int sock)
     146{
     147    struct linger linger;
     148
     149    linger.l_onoff = 1;
     150    linger.l_linger = 0;
     151
     152    return CF_Socket_Local_SetOption (sock,
     153                                      SO_LINGER,
     154                                      &linger,
     155                                      sizeof (linger));
     156}
     157
     158static int
     159CF_Socket_Local_SetReUseAddr (const int sock)
     160{
     161    int reuseaddr = 1;
     162
     163    return CF_Socket_Local_SetOption (sock,
     164                                      SO_REUSEADDR,
     165                                      &reuseaddr,
     166                                      sizeof (reuseaddr));
     167}
     168
     169static int
     170CF_Socket_Local_CheckTimeout (const int sock,
     171                              const int timeout)
     172{
     173    int             result = 0;
     174    fd_set          readfds;
     175    struct timeval  tv;
     176    int             error;
     177
     178    if (timeout <= CF_SOCKET_NO_TIMEOUT)
     179        return CF_OK;
     180
     181    tv.tv_sec = timeout;
     182    tv.tv_usec = 0;
     183
     184    FD_ZERO (&readfds);
     185    FD_SET (sock, &readfds);
     186
     187    while ((result = select (sock + 1, &readfds, NULL, NULL, &tv)) < 0)
     188    {
     189        error = CF_Util_GetSystemError ();
     190        if (error != ERROR_INTR)
     191        {
     192            result = CF_ERROR_SOCKET_TIMEOUT;
     193            return result;
     194        }
     195    }
     196
     197    if (FD_ISSET (sock, &readfds) == 0)
     198        return CF_ERROR_SOCKET_CHECK_DESC_SET;
     199
     200    return CF_OK;
     201}
     202
     203/**
     204 * 소켓의 초기화 상태 확인
     205 *
     206 * \return 초기화 된 경우, CF_TRUE; 그렇지 않은 경우, CF_FALSE
     207 */
     208static CF_BOOL
     209CF_Socket_IsInitialized (void)
     210{
     211    return gInitialized;
     212}
     213
     214/**
     215 * 소켓 초기화
     216 *
     217 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     218 */
     219int
     220CF_Socket_Initialize (void)
     221{
     222    if (!CF_Socket_IsInitialized ())
     223    {
     224#if defined(_WIN32) || defined(_WIN64)
     225        WSADATA winsockData;
     226        if (WSAStartup (MAKEWORD (2, 0), &winsockData))
     227            return CF_ERROR_SOCKET_INITIALIZE;
     228#endif
     229        gInitialized = CF_TRUE;
     230    }
     231   
     232    return CF_OK;
     233}
     234
     235/**
     236 * 소켓 해제
     237 *
     238 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     239 */
     240int
     241CF_Socket_Finalize (void)
     242{
     243    ASSERT_INIT ();
     244
     245#if defined(_WIN32) || defined(_WIN64)
     246    if (WSACleanup ())
     247       return CF_ERROR_SOCKET_FINALIZE;
     248#endif
     249
     250    return CF_OK;
     251}
     252
     253/**
     254 * 소켓 컨텍스트 생성
     255 *
     256 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     257 *
     258 * \param ctx   소켓 컨텍스트
     259 */
     260int
     261CF_Socket_Create (cf_ctx * ctx)
     262{
     263    int result = 0;
     264
     265    CF_SOCKET_CONTEXT * context = NULL;
     266
     267    ASSERT_CTX (ctx);
     268
     269    TRY
     270    {
     271        context = NEWCTX (CF_SOCKET_CONTEXT);
     272        if (context == NULL)
     273        {
     274            result = CF_ERROR_SOCKET_CREATE_CTX;
     275            TRY_BREAK;
     276        }
     277
     278        context->sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
     279        if (context->sock < 0)
     280        {
     281            result = CF_ERROR_SOCKET_CREATE;
     282            TRY_BREAK;
     283        }
     284
     285        context->timeout = CF_SOCKET_NO_TIMEOUT;
     286        *ctx = (CF_SOCKET_CONTEXT *) context;
     287    }
     288    CATCH_IF (result < 0)
     289    {
     290        CF_Socket_Close (context);
     291    }
     292
     293    return result;
     294}
     295
     296/**
     297 * 소켓 닫기
     298 *
     299 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     300 *
     301 * \param ctx   소켓 컨텍스트
     302 */
     303int
     304CF_Socket_Close (cf_ctx ctx)
     305{
     306    int result = 0;
     307    int sock = 0;
     308
     309    CF_SOCKET_CONTEXT * context = (CF_SOCKET_CONTEXT *) ctx;
     310
     311    ASSERT_CTX (context);
     312    ASSERT_SOCKET (context->sock);
     313
     314    sock = context->sock;
     315
     316    free (context);
     317
     318    result = close (sock);
     319
     320    if (result != 0)
     321        return CF_ERROR_SOCKET_CLOSE;
     322
     323    return CF_OK;
     324}
     325
    275326/**
    276327 * 소켓 연결
    277328 *
    278  * \return 성공 시, 연결된 소켓; 실패 시, 오류 코드
    279  *
    280  * \param ip        연결할 호스트의 주소 (도메인 이름 가능)
    281  * \param port      연결할 호스트의 포트번호
    282  */
    283 int
    284 CF_Socket_Connect (const char           * ip,
     329 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     330 *
     331 * \param ctx   소켓 컨텍스트
     332 * \param ip    연결할 호스트의 주소 (도메인 이름 가능)
     333 * \param port  연결할 호스트의 포트번호
     334 */
     335int
     336CF_Socket_Connect (cf_ctx               ctx,
     337                   const char           * ip,
    285338                   const unsigned short port)
    286 {
    287     return CF_Socket_ConnectTimeout (ip, port, CF_SOCKET_NO_TIMEOUT);
    288 }
    289 
    290 /**
    291  * 타임아웃 동안 소켓 연결
    292  *
    293  * \return 성공 시, 연결된 소켓; 실패 시, 오류 코드
    294  *
    295  * \param ip        연결할 호스트의 주소 (도메인 이름 가능)
    296  * \param port      연결할 호스트의 포트번호
    297  * \param timeout   타임아웃 (초)
    298  *
    299  * \see CF_SOCKET_NO_TIMEOUT
    300  */
    301 int
    302 CF_Socket_ConnectTimeout (const char            * ip,
    303                           const unsigned short  port,
    304                           const int             timeout)
    305339{
    306340    int                 result = 0;
    307341    int                 sock = 0;
     342    int                 timeout = 0;
    308343    struct sockaddr_in  address;
    309344    struct hostent      * hostEnt;
     
    315350    fd_set              writefds;
    316351
    317     ASSERT_INIT ();
    318 
    319     /* 1. create socket */
    320     sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
    321     if (sock < 0)
    322         return CF_ERROR_SOCKET_CREATE;
     352    CF_SOCKET_CONTEXT   * context = (CF_SOCKET_CONTEXT *) ctx;
     353
     354    ASSERT_CTX (ctx);
     355    TRY_INIT ();
     356
     357    /* 1. get and set socket info. */
     358    sock = context->sock;
     359    snprintf (context->ip, sizeof (context->ip), "%s", ip);
     360    context->port = port;
     361    timeout = context->timeout;
    323362
    324363    /* 2. set data */
     
    390429            retval = 0;
    391430
    392             result = CF_Socket_GetOption (sock, SO_ERROR, &retval, &length);
     431            result = CF_Socket_GetOption (ctx, SO_ERROR, &retval, &length);
    393432            if (result < 0)
    394433            {
     
    410449    CATCH_IF (result < 0)
    411450    {
    412         CF_Socket_Close (sock);
     451        CF_Socket_Close (ctx);
    413452        return result;
    414453    }
     
    417456        CF_Socket_Local_SetNonBlocking (sock, CF_FALSE);
    418457
    419     return sock;
     458    return result;
    420459}
    421460
     
    423462 * 서버 열기
    424463 *
    425  * \return 성공 시, 서버 소켓; 실패 시, 오류 코드
    426  *
     464 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     465 *
     466 * \param ctx       소켓 컨텍스트
    427467 * \param port      서버 포트
    428468 * \param backlog   listen 시의 backlog 수
    429469 */
    430470int
    431 CF_Socket_Server (const unsigned short  port,
     471CF_Socket_Server (cf_ctx                ctx,
     472                  const unsigned short  port,
    432473                  const int             backlog)
    433474{
     
    436477    struct sockaddr_in  address;
    437478
    438     ASSERT_INIT ();
    439 
    440     sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
    441     if (sock < 0)
    442         return CF_ERROR_SOCKET_CREATE;
     479    CF_SOCKET_CONTEXT   * context = (CF_SOCKET_CONTEXT *) ctx;
     480
     481    ASSERT_CTX (ctx);
     482    TRY_INIT ();
     483
     484    sock = context->sock;
     485    context->port = port;
    443486
    444487    address.sin_family      = AF_INET;
     
    462505        }
    463506
     507        snprintf (context->ip, sizeof (context->ip),
     508                  "%s",
     509                  inet_ntoa(address.sin_addr));
     510
    464511        result = listen (sock, backlog);
    465512        if (result < 0)
     
    471518    CATCH_IF (result < 0)
    472519    {
    473         CF_Socket_Close (sock);
    474         return result;
    475     }
    476 
    477     return sock;
    478 }
    479 
    480 /**
    481  * 소켓 연결
    482  *
    483  * \return 성공 시, 연결된 소켓; 실패 시, 오류 코드
    484  *
    485  * \param sock  서버 소켓
    486  */
    487 int
    488 CF_Socket_Accept (const int sock)
    489 {
    490     int                 sockClient = 0;
     520        CF_Socket_Close (ctx);
     521    }
     522
     523    return result;
     524}
     525
     526/**
     527 * 소켓 연결 받기
     528 *
     529 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     530 *
     531 * \param ctx       소켓 컨텍스트
     532 * \param client    연결을 수락할 client 소켓 컨텍스트
     533 */
     534int
     535CF_Socket_Accept (const cf_ctx  ctx,
     536                  cf_ctx        * client)
     537{
     538    int                 result = 0;
    491539    struct sockaddr_in  address;
    492540    socklen_t           len = sizeof (address);
    493541
    494     ASSERT_SOCKET (sock);
    495 
    496     sockClient = accept (sock, (struct sockaddr *) &address, &len);
    497     if (sockClient < 0)
     542    CF_SOCKET_CONTEXT   * clntctx = NULL;
     543    CF_SOCKET_CONTEXT   * context = (CF_SOCKET_CONTEXT *) ctx;
     544
     545    ASSERT_CTX (context);
     546    ASSERT_CTX (client);
     547    ASSERT_SOCKET (context->sock);
     548
     549    result = CF_Socket_Create ((cf_ctx *)&clntctx);
     550    if (result < 0)
     551        return result;
     552
     553    close (clntctx->sock);
     554
     555    clntctx->sock = accept (context->sock, (struct sockaddr *) &address, &len);
     556    if (clntctx->sock < 0)
     557    {
     558        CF_Socket_Close ((cf_ctx *)clntctx);
    498559        return CF_ERROR_SOCKET_ACCEPT;
    499 
    500     return sockClient;
     560    }
     561
     562    *client = (cf_ctx) clntctx;
     563
     564    return CF_OK;
    501565}
    502566
     
    506570 * \return 성공 시, CF_OK; 실패 시, 오류 코드
    507571 *
    508  * \param sock  소켓
    509  * \param buf   송신할 데이터
     572 * \param ctx   소켓 컨텍스트 * \param buf    송신할 데이터
    510573 * \param len   송신할 데이터의 길이
    511574 */
    512575int
    513 CF_Socket_Send (const int       sock,
     576CF_Socket_Send (const cf_ctx    ctx,
    514577                const void      * buf,
    515578                const size_t    len)
    516579{
    517     return CF_Socket_SendTimeout (sock, buf, len, CF_SOCKET_NO_TIMEOUT);
    518 }
    519 
    520 /**
    521  * 타임아웃 동안 데이터 송신
    522  *
    523  * \return 성공 시, CF_OK; 실패 시, 오류 코드
    524  *
    525  * \param sock      소켓
    526  * \param buf       송신할 데이터
    527  * \param len       송신할 데이터의 길이
    528  * \param timeout   타임아웃 (초)
    529  *
    530  * \see CF_SOCKET_NO_TIMEOUT
    531  */
    532 int
    533 CF_Socket_SendTimeout (const int    sock,
    534                        const void   * buf,
    535                        const size_t len,
    536                        const int    timeout)
    537 {
    538580    int result = 0;
    539581
    540     ASSERT_SOCKET (sock);
    541 
    542     result = (int) send (sock, buf, len, 0);
     582    CF_SOCKET_CONTEXT   * context = (CF_SOCKET_CONTEXT *) ctx;
     583
     584    ASSERT_CTX (context);
     585    ASSERT_SOCKET (context->sock);
     586
     587    result = (int) send (context->sock, buf, len, 0);
    543588    if (result != len)
    544589        return CF_ERROR_SOCKET_SEND;
     
    552597 * \return 성공 시, 수신한 데이터의 길이; 실패 시, 오류 코드
    553598 *
    554  * \param sock  소켓
     599 * \param ctx   소켓 컨텍스트
    555600 * \param buf   데이터를 수신할 버퍼
    556601 * \param len   데이터를 수신할 버퍼의 최대 크기
    557602 */
    558603int
    559 CF_Socket_Recv (const int       sock,
     604CF_Socket_Recv (const cf_ctx    ctx,
    560605                void            * buf,
    561606                const size_t    len)
    562607{
    563     return CF_Socket_RecvTimeout (sock, buf, len, CF_SOCKET_NO_TIMEOUT);
    564 }
    565 
    566 /**
    567  * 타임아웃 동안 데이터 수신
    568  *
    569  * \return 성공 시, 수신한 데이터의 길이; 실패 시, 오류 코드
    570  *
    571  * \param sock      소켓
    572  * \param buf       데이터를 수신할 버퍼
    573  * \param len       데이터를 수신할 버퍼의 최대 크기
    574  * \param timeout   타임아웃 (초)
    575  *
    576  * \see CF_SOCKET_NO_TIMEOUT
    577  */
    578 int
    579 CF_Socket_RecvTimeout (const int    sock,
    580                        void         * buf,
    581                        const size_t len,
    582                        const int    timeout)
    583 {
    584608    int result = 0;
    585609
    586     ASSERT_SOCKET (sock);
    587 
    588     result = CF_Socket_Local_CheckTimeout (sock, timeout);
     610    CF_SOCKET_CONTEXT   * context = (CF_SOCKET_CONTEXT *) ctx;
     611
     612    ASSERT_CTX (context);
     613    ASSERT_SOCKET (context->sock);
     614
     615    result = CF_Socket_Local_CheckTimeout (context->sock, context->timeout);
    589616    if (result < 0)
    590617        return result;
    591618
    592     result = (int) recv (sock, buf, len, 0);
     619    result = (int) recv (context->sock, buf, len, 0);
    593620    if (result < 0)
    594621        return CF_ERROR_SOCKET_RECV;
     
    596623    return result;
    597624}
     625
     626/**
     627 * 소켓 옵션 설정
     628 *
     629 * \return 성공 시 CF_OK; 실패 시, 오류 코드
     630 *
     631 * \param ctx       소켓 컨텍스트
     632 * \param optname   옵션 이름
     633 * \param optval    설정할 옵션 값의 메모리
     634 * \param optlen    설정할 옵션의 길이
     635 */
     636int
     637CF_Socket_SetOption (const cf_ctx   ctx,
     638                     const int      optname,
     639                     const void     * optval,
     640                     const size_t   optlen)
     641{
     642    CF_SOCKET_CONTEXT * context = (CF_SOCKET_CONTEXT *) ctx;
     643
     644    ASSERT_CTX (context);
     645
     646    return CF_Socket_Local_SetOption (context->sock,
     647                                      optname,
     648                                      optval,
     649                                      optlen);
     650}
     651
     652/**
     653 * 소켓 옵션 얻기
     654 *
     655 * \return 성공 시 CF_OK; 실패 시, 오류 코드
     656 *
     657 * \param ctx       소켓 컨텍스트
     658 * \param optname   옵션 이름
     659 * \param optval    옵션 값을 가져올 메모리
     660 * \param optlen    옵션 길이를 가져올 메모리
     661 */
     662int
     663CF_Socket_GetOption (const cf_ctx   ctx,
     664                     const int      optname,
     665                     void           * optval,
     666                     size_t         * optlen)
     667{
     668    CF_SOCKET_CONTEXT * context = (CF_SOCKET_CONTEXT *) ctx;
     669
     670    ASSERT_CTX (context);
     671
     672    return CF_Socket_Local_GetOption (context->sock,
     673                                      optname,
     674                                      optval,
     675                                      optlen);
     676}
     677
     678/**
     679 * 소켓 컨텍스트에 타임아웃 설정
     680 *
     681 * \return CF_OK 반환
     682 *
     683 * \param ctx       소켓 컨텍스트
     684 * \param timeout   설정할 타임아웃 시간
     685 */
     686int
     687CF_Socket_SetTimeOut (cf_ctx    ctx,
     688                      int       timeout)
     689{
     690    CF_SOCKET_CONTEXT * context = (CF_SOCKET_CONTEXT *) ctx;
     691
     692    ASSERT_CTX (context);
     693
     694    context->timeout = timeout;
     695
     696    return CF_OK;
     697}
     698
     699/**
     700 * 소켓 컨텍스트에 타임아웃 설정
     701 *
     702 * \return 성공 시 CF_OK; 실패 시, 오류 코드
     703 *
     704 * \param ctx   소켓 컨텍스트
     705 * \param ip    ip 주소를 저장할 충분한 공간의 메모리
     706 */
     707int
     708CF_Socket_GetIP (const cf_ctx   ctx,
     709                 char           * ip)
     710{
     711    CF_SOCKET_CONTEXT * context = (CF_SOCKET_CONTEXT *) ctx;
     712
     713    ASSERT_CTX (context);
     714    ASSERT_ARGS (ip == NULL);
     715
     716    snprintf (ip, sizeof (context->ip), "%s", context->ip);
     717
     718    return CF_OK;
     719}
     720
     721/**
     722 * 소켓 컨텍스트에 타임아웃 설정
     723 *
     724 * \return 성공 시 CF_OK; 실패 시, 오류 코드
     725 *
     726 * \param ctx   소켓 컨텍스트
     727 * \param port  포트 번호 저장할 포인터
     728 */
     729int
     730CF_Socket_GetPort (const cf_ctx     ctx,
     731                   unsigned short   * port)
     732{
     733    CF_SOCKET_CONTEXT * context = (CF_SOCKET_CONTEXT *) ctx;
     734
     735    ASSERT_ARGS (port == NULL);
     736
     737    *port = context->port;
     738
     739    return CF_OK;
     740}
  • trunk/src/cf_stack.c

    r128 r151  
    88#include "cf_stack.h"
    99#include "cf_list.h"
     10#include "cf_local.h"
    1011#include "cf_error.h"
    1112
    1213#include <stdio.h>
     14
     15static int
     16CF_Stack_Local_Get (cf_ctx  ctx,
     17                    void    ** element,
     18                    CF_BOOL removeFlag)
     19{
     20    int             result = 0;
     21    cf_ctx          list = (cf_ctx) ctx;
     22    cf_traverser    trav = NULL;
     23
     24    result = CF_List_Front (list, &trav);
     25    if (result < 0)
     26        return result;
     27
     28    result = CF_List_Get (trav, element);
     29    if (result < 0)
     30        return result;
     31
     32    result = (removeFlag == CF_TRUE)
     33           ? CF_List_Remove (ctx, &trav)
     34           : CF_OK;
     35
     36    return result;
     37}
    1338
    1439/**
     
    2045 */
    2146int
    22 CF_Stack_CreateCtx (CF_Stack_Ctx * ctx)
     47CF_Stack_Create (cf_ctx * ctx)
    2348{
    24     return CF_List_CreateCtx ((CF_List_Ctx *) ctx);
     49    return CF_List_Create ((cf_ctx *) ctx);
    2550}
    2651
     
    3358 */
    3459int
    35 CF_Stack_DestroyCtx (CF_Stack_Ctx ctx)
     60CF_Stack_Destroy (cf_ctx ctx)
    3661{
    37     return CF_List_DestroyCtx (ctx);
     62    return CF_List_Destroy (ctx);
    3863}
    3964
     
    4772 */
    4873int
    49 CF_Stack_Push (CF_Stack_Ctx ctx,
     74CF_Stack_Push (cf_ctx       ctx,
    5075               const void   * element)
    5176{
    52     int             result = 0;
    53     CF_List_Ctx     list = (CF_List_Ctx) ctx;
    54     CF_Traverser    traverser = NULL;
     77    cf_ctx list = (cf_ctx) ctx;
    5578
    56     result = CF_List_Front (list, &traverser);
    57     if (result < 0)
    58         return result;
    59 
    60     return CF_List_Insert (list, traverser, CF_DIRECTION_BEFORE, element);
     79    return CF_List_AddFront (list, element);
    6180}
    6281
     
    7089 */
    7190int
    72 CF_Stack_Pop (CF_Stack_Ctx  ctx,
    73               void          ** element)
     91CF_Stack_Pop (cf_ctx    ctx,
     92              void      ** element)
    7493{
    75     int         result = 0;
    76     CF_List_Ctx list = (CF_List_Ctx) ctx;
    77     CF_Traverser    traverser = NULL;
    78 
    79     result = CF_Stack_Top (ctx, element);
    80     if (result < 0)
    81         return result;
    82 
    83     result = CF_List_Front (list, &traverser);
    84     if (result < 0)
    85         return result;
    86 
    87     return CF_List_Remove (ctx, &traverser);
     94    return CF_Stack_Local_Get (ctx, element, CF_TRUE);
    8895}
    8996
     
    97104 */
    98105int
    99 CF_Stack_Top (CF_Stack_Ctx  ctx,
    100               void          ** element)
     106CF_Stack_Top (cf_ctx    ctx,
     107              void      ** element)
    101108{
    102     int             result = 0;
    103     CF_List_Ctx     list = (CF_List_Ctx) ctx;
    104     CF_Traverser    traverser = NULL;
    105 
    106     result = CF_List_Front (list, &traverser);
    107     if (result < 0)
    108         return result;
    109 
    110     return CF_List_GetElement (traverser, element);
     109    return CF_Stack_Local_Get (ctx, element, CF_FALSE);
    111110}
    112111
     
    119118 */
    120119int
    121 CF_Stack_GetSize (CF_Stack_Ctx ctx)
     120CF_Stack_GetSize (cf_ctx ctx)
    122121{
    123     return CF_List_GetSize ((CF_List_Ctx) ctx);
     122    return CF_List_GetSize ((cf_ctx) ctx);
    124123}
  • trunk/src/cf_thread.c

    r149 r151  
    77 */
    88#include "cf_thread.h"
     9#include "cf_local.h"
    910#include "cf_error.h"
    1011
     
    2425#endif // #if defined(_WIN32) || defined(_WIN64)
    2526
    26 #define ASSERT_THREAD_CTX(__ctx)    \
     27#define ASSERT_CTX(__ctx)   \
    2728    if (__ctx == NULL)              \
    2829        return CF_ERROR_THREAD_INVALID_CTX
     
    4748 */
    4849int
    49 CF_Thread_CreateCtx (CF_Thread_Ctx      * ctx,
    50                     CF_Thread_Function callback,
    51                      void               * arg)
     50CF_Thread_Create (cf_ctx                * ctx,
     51                  CF_Thread_Function    callback,
     52                  void                  * arg)
    5253{
    5354    CF_THREAD_CONTEXT * context = NULL;
    5455
    55     context = (CF_THREAD_CONTEXT *) calloc (sizeof (CF_THREAD_CONTEXT), 1);
     56    context = NEWCTX (CF_THREAD_CONTEXT);
    5657    if (context == NULL)
    5758        return CF_ERROR_THREAD_CREATE_CTX;
     
    6061    context->arg        = arg;
    6162
    62     *ctx = (CF_Thread_Ctx) context;
     63    *ctx = (cf_ctx) context;
    6364
    6465    return CF_OK;
     
    7475 * \remarks
    7576 * pthread에서 지원되는 스케줄링 정책은 SCHED_OTHER, SCHED_FIFO, SCHED_RR 등이 존재 <br />
    76  * 일반적으로 설정되는 스케줄링 정책의 기본값은 SCHED_OTHER이며, 솔라리스 환경에서 SCHED_OTHER는 TS(timesharing) 방식으로 명시되어 있음 <br />
    77  * 그러나 개발 단계에서 테스트된 동작은 SCHED_FIFO와 동일하였으며, 때문에 솔라리스 환경에서는 스케줄링 정책을 SCHED_RR로 명시하도록 함 <br />
     77 * 일반적으로 설정되는 스케줄링 정책의 기본값은 SCHED_OTHER이며,
     78 * 솔라리스 환경에서 SCHED_OTHER는 TS(timesharing) 방식으로 명시되어 있음 <br />
     79 * 그러나 개발 단계에서 테스트된 동작은 SCHED_FIFO와 동일하였으며,
     80 * 때문에 솔라리스 환경에서는 스케줄링 정책을 SCHED_RR로 명시하도록 함 <br />
    7881 * <br />
    7982 * 참고 url <br />
     
    8285 */
    8386int
    84 CF_Thread_Start (CF_Thread_Ctx ctx)
     87CF_Thread_Start (cf_ctx ctx)
    8588{
    8689    int result = 0;
     
    8891    CF_THREAD_CONTEXT * context = (CF_THREAD_CONTEXT *) ctx;
    8992
    90     ASSERT_THREAD_CTX (ctx);
     93    ASSERT_CTX (ctx);
    9194
    9295#if defined(_WIN32) || defined(_WIN64)
     
    138141 */
    139142int
    140 CF_Thread_DestroyCtx (CF_Thread_Ctx ctx)
     143CF_Thread_Destroy (cf_ctx ctx)
    141144{
    142145    CF_THREAD_CONTEXT * context = (CF_THREAD_CONTEXT *) ctx;
    143146
    144     ASSERT_THREAD_CTX (ctx);
     147    ASSERT_CTX (ctx);
    145148
    146149#if defined(_WIN32) || defined(_WIN64)
     
    163166 */
    164167int
    165 CF_Thread_Join (CF_Thread_Ctx ctx)
     168CF_Thread_Join (cf_ctx ctx)
    166169{
    167170    CF_THREAD_CONTEXT * context = (CF_THREAD_CONTEXT *) ctx;
     
    169172    char status[16] = {0x00,};
    170173
    171     ASSERT_THREAD_CTX (ctx);
     174    ASSERT_CTX (ctx);
    172175
    173176#if defined(_WIN32) || defined(_WIN64)
  • trunk/src/makefile

    r146 r151  
    2020                    cf_stack        \
    2121                    cf_bitwise      \
     22                    cf_util         \
    2223
    2324TARGET_NAME     = cf
     
    4647# link
    4748$(TARGET_PATH)/%.$(EXT_SHARED): $(OBJS)
    48     $(LD) $(SHARED_FLAGS) -o $@ $? $(LIBS)
     49    $(LD) $(SHARED_FLAGS) -o $@ $(LIBS) $?
    4950
    5051$(TARGET_PATH)/%.$(EXT_STATIC): $(OBJS)
     
    5354# compile
    5455$(OBJ_PATH)/%.o: %.$(SOURCE_TYPE)
    55     $(CC) $(CFLAGS) $(CDEFS) $(INCLUDES) -o $@ -c $?
     56    $(CC) $(CFLAGS) -c $(CDEFS) $(INCLUDES) -o $@ $?
    5657
    5758# doxygen
    5859$(DOXYGEN_OUTPUT_PATH):
    59 ifeq ($(DOXYGEN_CREATE),YES)
     60ifeq ($(DOXYGEN_CREATE),yes)
    6061ifeq ($(DOXYGEN),)
    6162    @echo "doxygen was not found."
     
    6970clean:
    7071    rm -rf $(TARGET_PATH) $(OBJ_PATH)
    71 ifeq ($(DOXYGEN_CREATE),YES)
     72ifeq ($(DOXYGEN_CREATE),yes)
    7273    rm -rf $(DOXYGEN_OUTPUT_PATH)
    7374endif
  • trunk/test/codec.c

    r96 r151  
    2525
    2626    CF_DEBUG_PRINT (stderr, "= Convert hex to binary =\n");
    27     if (CF_Codec_Hex_Decode (encode, bin, &length) < 0) {
    28         // error
    29     }
     27    if (CF_Codec_Hex_Decode (encode, bin, &length) < 0)
     28        CF_DEBUG_PRINT (stderr, "error\n");
    3029    else
    3130        CF_DEBUG_PRINT_BIN (stderr, bin, length, "bin    : %s\n", bin);
  • trunk/test/debug.c

    r135 r151  
    88#include <string.h>
    99
    10 void callee3 (void) {
     10void callee3 (void)
     11{
    1112    CF_DEBUG_BEGIN_FUNCTION;
    1213
     
    1819}
    1920
    20 void callee2 (void) {
     21void callee2 (void)
     22{
    2123    CF_DEBUG_BEGIN_FUNCTION;
    2224
     
    2830}
    2931
    30 void callee1 (void) {
     32void callee1 (void)
     33{
    3134    CF_DEBUG_BEGIN_FUNCTION;
    3235
     
    3841}
    3942
    40 int main (void) {
     43int main (void)
     44{
    4145    char data[] =
    4246        "【 曲名 : 사랑하고 있는데 】\n"
  • trunk/test/file.c

    r94 r151  
    66#include <stdio.h>
    77
    8 int main (void) {
    9     int fd = 0;
     8int main (void)
     9{
     10    cf_ctx fd = 0;
     11    int result = 0;
    1012    char *dir1 = "./dir1/test";
    1113    char *dir2 = "dir2/test";
     
    2123    }
    2224
    23     fd = CF_File_Create (name);
    24     if (fd < 0) {
     25    result = CF_File_Open (&fd, name, CF_FILE_CREATE|CF_FILE_RW|CF_FILE_TRUNC);
     26    if (result < 0) {
    2527        // error
    2628    }
     
    3032    CF_File_Close (fd);
    3133
    32     fd = CF_File_Open (name, CF_FILE_READ);
    33     if (fd < 0) {
     34    result = CF_File_Open (&fd, name, CF_FILE_READ);
     35    if (result < 0) {
    3436        // error
    3537    }
  • trunk/test/list.c

    r118 r151  
    1111    int             result = 0;
    1212    long long int   element = 0;
    13     CF_List_Ctx     ctx = NULL;
    14     CF_Traverser    trav = NULL;
     13    cf_ctx          list = NULL;
     14    cf_traverser    trav = NULL;
    1515
    16     result = CF_List_CreateCtx (&ctx);
     16    result = CF_List_Create (&list);
    1717    if (result < 0)
    1818        CF_DEBUG_PRINT (stderr, "error %d\n", result);
     
    2020    for (iter = 0 ; iter < 10 ; iter++)
    2121    {
    22         result = CF_List_Insert (ctx, trav, CF_DIRECTION_BEFORE, (void *)iter);
     22        result = CF_List_InsertBefore (list, trav, (void *)iter);
    2323        if (result < 0)
    2424            CF_DEBUG_PRINT (stderr, "error %d\n", result);
    2525    }
    2626
    27     result = CF_List_Front (ctx, &trav);
     27    result = CF_List_Front (list, &trav);
    2828    if (result < 0)
    2929        CF_DEBUG_PRINT (stderr, "error %d\n", result);
     
    3636    }
    3737
    38     result = CF_List_Insert (ctx, trav, CF_DIRECTION_BEFORE, (void *)100);
     38    result = CF_List_InsertBefore (list, trav, (void *)100);
    3939    if (result < 0)
    4040        CF_DEBUG_PRINT (stderr, "error %d\n", result);
    4141
    42     result = CF_List_Insert (ctx, trav, CF_DIRECTION_AFTER, (void *)200);
     42    result = CF_List_InsertAfter (list, trav, (void *)200);
    4343    if (result < 0)
    4444        CF_DEBUG_PRINT (stderr, "error %d\n", result);
    4545
    46     for (CF_List_Front (ctx, &trav) ; trav != NULL ; CF_List_Next (&trav))
     46    for (CF_List_Front (list, &trav) ; trav != NULL ; CF_List_Next (&trav))
    4747    {
    48         result = CF_List_GetElement (trav, (void **)&element);
     48        result = CF_List_Get (trav, (void **)&element);
    4949        if (result < 0)
    5050            CF_DEBUG_PRINT (stderr, "error %d\n", result);
     
    5353    }
    5454
    55     result = CF_List_DestroyCtx (ctx);
     55    result = CF_List_Destroy (list);
    5656    if (result < 0)
    5757        CF_DEBUG_PRINT (stderr, "error %d\n", result);
  • trunk/test/log.c

    r148 r151  
    1010#include <stdio.h>
    1111
    12 CF_Mutex_Ctx globalMutex;
     12#define COUNT   5
    1313
    14 #define LOG_MT      1
     14cf_ctx  globalLog;
     15cf_ctx  globalMutex;
    1516
    16 int test_log_mt (void * arg)
     17int worker (void * arg)
    1718{
    1819    static int cnt = 0;
     
    3233    for (i = 0 ; i < 10000 ; i++)
    3334    {
    34         CF_Log_Write (LOG_MT, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i);
     35        CF_Log_Write (globalLog, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i);
    3536    }
    3637
     
    4243int main (void)
    4344{
    44     int             i, j;
    45     char            idname[16] = {0x00,};
    46     CF_Thread_Ctx   tid[10];
     45    int     i, j;
     46    cf_ctx  tid[COUNT];
    4747
    4848    /* initialize */
    49     if (CF_Log_Initialize (10) < 0)
     49    if (CF_Log_Create (&globalLog, "log.txt", CF_LOG_NO_BUFFER) < 0)
     50        CF_DEBUG_PRINT (stderr, "failed to open log\n");
     51
     52    for (j = 0 ; j < 10000 ; j++)
    5053    {
    51         fprintf (stderr, "failed to init. log\n");
    52         return -1;
    53     }
    54 
    55     for (i = 0 ; i < 10 ; i++)
    56     {
    57         sprintf (idname, "logid%d.txt", i);
    58         if (CF_Log_Open (i, idname, CF_LOG_NO_BUFFER) < 0)
    59             fprintf (stderr, "failed to open log\n");
    60     }
    61 
    62     for (i = 0 ; i < 10 ; i++)
    63     {
    64         for (j = 0 ; j < 10000 ; j++)
    65         {
    66             int result = CF_Log_Write (i, "LOG_ID_TEST", "turn %d\n", j);
    67             if (result < 0)
    68                 fprintf (stderr, "failed to write log %d\n", result);
    69         }
    70 
    71         if (CF_Log_Close (i) < 0)
    72             fprintf (stderr, "failed to close log\n");
     54        int result = CF_Log_Write (globalLog, "LOG_ID_TEST", "turn %d\n", j);
     55        if (result < 0)
     56            CF_DEBUG_PRINT (stderr, "failed to write log %d\n", result);
    7357    }
    7458
    7559    /* mt {{{ */
    76     if (CF_Log_Open (LOG_MT, "log_mt.txt", CF_LOG_DEFAULT_BUFFER) < 0)
     60    if (CF_Log_Create (&globalLog, "log_mt.txt", CF_LOG_NO_BUFFER) < 0)
    7761        CF_DEBUG_PRINT (stderr, "create log ctx error\n");
    7862
    79     if (CF_Log_SetMT (LOG_MT, CF_TRUE) < 0)
    80         CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
    81 
    82     if (CF_Mutex_CreateCtx (&globalMutex) < 0) {
     63    if (CF_Mutex_Create (&globalMutex) < 0) {
    8364        // error
    8465    }
    8566
    86     for (i = 0 ; i < 10 ; i++)
     67    if (CF_Log_SetMultiThread (globalLog) < 0)
     68        CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
     69
     70    for (i = 0 ; i < COUNT ; i++)
    8771    {
    88         if (CF_Thread_CreateCtx (&tid[i], test_log_mt, &i) < 0)
     72        if (CF_Thread_Create (&tid[i], worker, &i) < 0)
    8973        {
    9074            CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", i);
     
    9377    }
    9478
    95     for (i = 0 ; i < 10 ; i++)
     79    for (i = 0 ; i < COUNT ; i++)
    9680    {
    9781        if (CF_Thread_Start (tid[i]) < 0)
     
    10286    }
    10387
    104     for (i = 0 ; i < 10 ; i++)
     88    for (i = 0 ; i < COUNT ; i++)
    10589    {
    10690        if (CF_Thread_Join (tid[i]) < 0)
    10791            CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i);
    108         if (CF_Thread_DestroyCtx (tid[i]) < 0)
     92        if (CF_Thread_Destroy (tid[i]) < 0)
    10993            CF_DEBUG_PRINT (stderr, "failed to release %dth thread\n", i);
    11094    }
    11195
    112     if (CF_Mutex_DestoryCtx (globalMutex) < 0) {
     96    if (CF_Log_UnsetMultiThread (globalLog) < 0)
     97        CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
     98
     99    if (CF_Mutex_Destory (globalMutex) < 0) {
    113100        // error
    114101    }
     
    116103
    117104    /* finalize */
    118     CF_Log_Finalize ();
     105    CF_Log_Destroy (globalLog);
    119106
    120107    return 0;
  • trunk/test/queue.c

    r118 r151  
    66#include "cf_debug.h"
    77
     8#define COUNT   5
     9
    810int main (void)
    911{
     
    1113    int             result = 0;
    1214    long long int   element = 0;
    13     CF_Queue_Ctx    ctx = NULL;
     15    cf_ctx          queue = NULL;
    1416
    1517    // create
    16     result = CF_Queue_CreateCtx (&ctx);
     18    result = CF_Queue_Create (&queue);
    1719    if (result < 0)
    1820        CF_DEBUG_PRINT (stderr, "error %d\n", result);
    1921
    2022    // insert
    21     for (iter = 0 ; iter < 10 ; iter++)
     23    for (iter = 0 ; iter < COUNT ; iter++)
    2224    {
    23         result = CF_Queue_Put (ctx, (void *)iter);
     25        result = CF_Queue_Put (queue, (void *)iter);
    2426        if (result < 0)
    2527            CF_DEBUG_PRINT (stderr, "error %d\n", result);
     
    2729
    2830    // move next
    29     for (iter = 0 ; iter < 10 ; iter++)
     31    for (iter = 0 ; iter < COUNT ; iter++)
    3032    {
    31         result = CF_Queue_Front (ctx, (void **)&element);
     33        result = CF_Queue_Front (queue, (void **)&element);
    3234        if (result < 0)
    3335            CF_DEBUG_PRINT (stderr, "error %d\n", result);
     
    3537            CF_DEBUG_PRINT (stderr, "front : %4d\n", element);
    3638
    37         result = CF_Queue_Get (ctx, (void **)&element);
     39        result = CF_Queue_Get (queue, (void **)&element);
    3840        if (result < 0)
    3941            CF_DEBUG_PRINT (stderr, "error %d\n", result);
     
    4547
    4648    // destroy
    47     result = CF_Queue_DestroyCtx (ctx);
     49    result = CF_Queue_Destroy (queue);
    4850    if (result < 0)
    4951        CF_DEBUG_PRINT (stderr, "error %d\n", result);
  • trunk/test/socket.c

    r122 r151  
    88#include <string.h>
    99
    10 #define LOG_SOCKET      0
    11 #define THREAD_POOL     1
     10#define THREAD_POOL     2
    1211#define PORT            1234
    13 
    14 int socket_echo_server (void * arg)
     12#define COUNT           2
     13
     14cf_ctx gLog;
     15
     16int server_worker (void * arg)
    1517{
    16     int srvsock = *((int *)arg);
    17     int clntsock = 0;
     18    cf_ctx srvsock = (cf_ctx)arg;
     19    cf_ctx clntsock = NULL;
     20    int result = 0;
    1821    int recvd = 0;
    1922    char buf[1024] = {0x00,};
     
    2124
    2225    /*------------------------------------------------------------*/
    23     clntsock = CF_Socket_Accept (srvsock);
    24     if (clntsock < 0)
     26    result = CF_Socket_Accept (srvsock, &clntsock);
     27    if (result < 0)
    2528    {
    2629        CF_DEBUG_PRINT (stderr, "failed to accept on server\n");
    2730        return -1;
    2831    }
    29     CF_Log_Write (LOG_SOCKET, "SERVER", "accepted\n");
    30 
    31     for (i = 0 ; i < 5 ; i++)
     32    CF_Log_Write (gLog, "SERVER", "accepted\n");
     33
     34    for (i = 0 ; i < COUNT ; i++)
    3235    {
    3336        if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0)
     
    3639            return -2;
    3740        }
    38         CF_Log_Write (LOG_SOCKET, "SERVER", "recv {%s}\n", buf);
    39         CF_DEBUG_PRINT (stderr, "[SERVER] recv {%s}\n", buf);
     41        CF_Log_Write (gLog, "SERVER", "recv {%s}\n", buf);
     42        CF_DEBUG_PRINT (stderr, "recv {%s}\n", buf);
    4043
    4144        if (CF_Socket_Send (clntsock, buf, (size_t)recvd) < 0)
     
    4346            return -3;
    4447        }
    45         CF_Log_Write (LOG_SOCKET, "SERVER", "resp {%s}\n", buf);
    46         CF_DEBUG_PRINT (stderr, "[SERVER] resp {%s}\n", buf);
     48        CF_Log_Write (gLog, "SERVER", "resp {%s}\n", buf);
     49        CF_DEBUG_PRINT (stderr, "resp {%s}\n", buf);
    4750    }
    4851
     
    5356}
    5457
    55 int socket_echo_client (void * arg)
     58int client_worker (void * arg)
    5659{
    57     int sock = 0;
     60    cf_ctx sock = NULL;
     61    int result = 0;
    5862    int recvd = 0;
    5963    char buf[1024] = {0x00,};
     
    6367
    6468    /*------------------------------------------------------------*/
    65     sock = CF_Socket_Connect ("localhost", PORT);
    66     if (sock < 0)
     69    result = CF_Socket_Create (&sock);
     70    if (result < 0)
     71    {
     72        CF_DEBUG_PRINT (stderr, "failed to create\n");
     73        return -1;
     74    }
     75
     76    result = CF_Socket_Connect (sock, "localhost", PORT);
     77    if (result < 0)
    6778    {
    6879        CF_DEBUG_PRINT (stderr, "failed to connect on client\n");
    69         return -1;
    70     }
    71     CF_Log_Write (LOG_SOCKET, "CLIENT", "connected\n");
    72 
    73     for (i = 0 ; i < 5 ; i++)
     80        return -2;
     81    }
     82    CF_Log_Write (gLog, "CLIENT", "connected\n");
     83
     84    for (i = 0 ; i < COUNT ; i++)
    7485    {
    7586        if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0)
    7687        {
    7788            CF_DEBUG_PRINT (stderr, "failed to send on client %d\n", errno);
    78             return -2;
    79         }
    80         CF_Log_Write (LOG_SOCKET, "CLIENT", "sent {%s}\n", buf);
    81         CF_DEBUG_PRINT (stderr, "[CLIENT] sent {%s}\n", buf);
     89            return -3;
     90        }
     91        CF_Log_Write (gLog, "CLIENT", "sent {%s}\n", buf);
     92        CF_DEBUG_PRINT (stderr, "sent {%s}\n", buf);
    8293
    8394        memset (buf, 0x00, sizeof (buf));
     
    8697        {
    8798            CF_DEBUG_PRINT (stderr, "failed to recv on client\n");
    88             return -3;
    89         }
    90         CF_Log_Write (LOG_SOCKET, "CLIENT", "recv {%s}\n", buf);
    91         CF_DEBUG_PRINT (stderr, "[CLIENT] recv {%s}\n", buf);
     99            return -4;
     100        }
     101        CF_Log_Write (gLog, "CLIENT", "recv {%s}\n", buf);
     102        CF_DEBUG_PRINT (stderr, "recv {%s}\n", buf);
    92103    }
    93104
     
    100111int main ()
    101112{
    102     CF_Thread_Ctx stid[THREAD_POOL];
    103     CF_Thread_Ctx ctid[THREAD_POOL];
    104 
    105     int sock = 0;
     113    cf_ctx stid[THREAD_POOL];
     114    cf_ctx ctid[THREAD_POOL];
     115
     116    char ip[256] = {0x00,};
     117    unsigned short port = 0;
     118    cf_ctx sock = 0;
     119    int result = 0;
    106120    int iter = 0;
    107121
    108122    /*------------------------------------------------------------*/
    109     if (CF_Log_Initialize (1) < 0)
    110     {
    111         fprintf (stderr, "failed to init. log\n");
     123    if (CF_Log_Create (&gLog, "socket.txt", CF_LOG_DEFAULT_BUFFER) < 0)
     124        CF_DEBUG_PRINT (stderr, "failed to open log\n");
     125
     126    CF_Log_Write (gLog, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
     127
     128    if (CF_Socket_Initialize () < 0)
     129    {
     130        CF_DEBUG_PRINT (stderr, "failed to initialize socket\n");
    112131        return -1;
    113132    }
    114 
    115     if (CF_Log_Open (LOG_SOCKET, "socket.txt", CF_LOG_DEFAULT_BUFFER) < 0)
    116         CF_DEBUG_PRINT (stderr, "failed to open log\n");
    117 
    118     CF_Log_Write (LOG_SOCKET, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
    119 
    120     if (CF_Socket_Initialize () < 0)
    121     {
    122         CF_DEBUG_PRINT (stderr, "failed to initialize socket\n");
     133    CF_Log_Write (gLog, "SOCKET", "socket initialized\n");
     134
     135    result = CF_Socket_Create (&sock);
     136    if (result < 0)
     137    {
     138        CF_DEBUG_PRINT (stderr, "failed to create\n");
    123139        return -2;
    124140    }
    125     CF_Log_Write (LOG_SOCKET, "SOCKET", "socket initialized\n");
    126 
    127     sock = CF_Socket_Server (PORT, 5);
     141
     142    result = CF_Socket_Server (sock, PORT, 5);
    128143    if (sock < 0)
    129144    {
     
    131146        return -3;
    132147    }
    133     CF_Log_Write (LOG_SOCKET, "SOCKET", "socket ready\n");
    134 
    135     for (iter = 0 ; iter < THREAD_POOL ; iter++)
    136     {
    137         if (CF_Thread_CreateCtx (&stid[iter], socket_echo_server, &sock) < 0)
     148    CF_Socket_GetIP (sock, ip);
     149    CF_Socket_GetPort (sock, &port);
     150    CF_Log_Write (gLog, "SOCKET", "socket ready [%s:%d]\n", ip, port);
     151    CF_DEBUG_PRINT (stderr, "socket ready [%s:%d]\n", ip, port);
     152 
     153
     154    for (iter = 0 ; iter < THREAD_POOL ; iter++)
     155    {
     156        if (CF_Thread_Create (&stid[iter], server_worker, sock) < 0)
    138157        {
    139158            CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
    140159            return -4;
    141160        }
    142         CF_Log_Write (LOG_SOCKET, "SOCKET", "create server thread-%d\n", iter);
    143     }
    144 
    145     for (iter = 0 ; iter < THREAD_POOL ; iter++)
    146     {
    147         if (CF_Thread_CreateCtx (&ctid[iter], socket_echo_client, &sock) < 0)
     161        CF_Log_Write (gLog, "SOCKET", "create server thread-%d\n", iter);
     162    }
     163
     164    for (iter = 0 ; iter < THREAD_POOL ; iter++)
     165    {
     166        if (CF_Thread_Create (&ctid[iter], client_worker, &sock) < 0)
    148167        {
    149168            CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
    150169            return -5;
    151170        }
    152         CF_Log_Write (LOG_SOCKET, "SOCKET", "create client thread-%d\n", iter);
     171        CF_Log_Write (gLog, "SOCKET", "create client thread-%d\n", iter);
    153172    }
    154173
     
    174193    {
    175194        CF_Thread_Join (ctid[iter]);
    176         CF_Thread_DestroyCtx (ctid[iter]);
     195        CF_Thread_Destroy (ctid[iter]);
    177196
    178197        CF_Thread_Join (stid[iter]);
    179         CF_Thread_DestroyCtx (stid[iter]);
    180 
    181         CF_Log_Write (LOG_SOCKET, "SOCKET", "join server thread-%d\n", iter);
     198        CF_Thread_Destroy (stid[iter]);
     199
     200        CF_Log_Write (gLog, "SOCKET", "join server thread-%d\n", iter);
    182201    }
    183202
     
    186205    CF_Socket_Finalize ();
    187206
    188     CF_Log_Close (LOG_SOCKET);
    189 
    190     CF_Log_Finalize ();
     207    CF_Log_Destroy (gLog);
    191208    /*------------------------------------------------------------*/
    192209
  • trunk/test/stack.c

    r118 r151  
    66#include "cf_debug.h"
    77
     8#define COUNT   5
     9
    810int main (void)
    911{
     
    1113    int             result = 0;
    1214    long long int   element = 0;
    13     CF_Stack_Ctx    ctx = NULL;
     15    cf_ctx          stack = NULL;
    1416
    1517    // create
    16     result = CF_Stack_CreateCtx (&ctx);
     18    result = CF_Stack_Create (&stack);
    1719    if (result < 0)
    1820        CF_DEBUG_PRINT (stderr, "error %d\n", result);
    1921
    2022    // insert
    21     for (iter = 0 ; iter < 10 ; iter++)
     23    for (iter = 0 ; iter < COUNT ; iter++)
    2224    {
    23         result = CF_Stack_Push (ctx, (void *)iter);
     25        result = CF_Stack_Push (stack, (void *)iter);
    2426        if (result < 0)
    2527            CF_DEBUG_PRINT (stderr, "error %d\n", result);
     
    2729
    2830    // move next
    29     for (iter = 0 ; iter < 10 ; iter++)
     31    for (iter = 0 ; iter < COUNT ; iter++)
    3032    {
    31         result = CF_Stack_Top (ctx, (void **)&element);
     33        result = CF_Stack_Top (stack, (void **)&element);
    3234        if (result < 0)
    3335            CF_DEBUG_PRINT (stderr, "error %d\n", result);
     
    3537            CF_DEBUG_PRINT (stderr, "top   : %4d\n", element);
    3638
    37         result = CF_Stack_Pop (ctx, (void **)&element);
     39        result = CF_Stack_Pop (stack, (void **)&element);
    3840        if (result < 0)
    3941            CF_DEBUG_PRINT (stderr, "error %d\n", result);
     
    4547
    4648    // destroy
    47     result = CF_Stack_DestroyCtx (ctx);
     49    result = CF_Stack_Destroy (stack);
    4850    if (result < 0)
    4951        CF_DEBUG_PRINT (stderr, "error %d\n", result);
  • trunk/test/thread.c

    r148 r151  
    88#include <stdio.h>
    99
    10 CF_Mutex_Ctx globalMutex;
     10#define COUNT   5
    1111
    12 int worker (void * arg) {
     12cf_ctx globalMutex;
     13
     14int worker (void * arg)
     15{
    1316    if (CF_Mutex_Lock (globalMutex) < 0) { // for critical section
    1417        // error
     
    2427}
    2528
    26 int main (void) {
    27     CF_Thread_Ctx tid[10];
    28     int i = 0;
     29int main (void)
     30{
     31    cf_ctx  tid[COUNT];
     32    int     i = 0;
    2933
    30     if (CF_Mutex_CreateCtx (&globalMutex) < 0) {
     34    if (CF_Mutex_Create (&globalMutex) < 0) {
    3135        // error
    3236    }
    3337
    34     for (i = 0 ; i < 10 ; i++) {
    35         if (CF_Thread_CreateCtx (&tid[i], worker, NULL) < 0) {
     38    for (i = 0 ; i < COUNT ; i++) {
     39        if (CF_Thread_Create (&tid[i], worker, NULL) < 0) {
    3640            // error
    3741        }
    3842    }
    3943
    40     for (i = 0 ; i < 10 ; i++) {
     44    for (i = 0 ; i < COUNT ; i++) {
    4145        if (CF_Thread_Start (tid[i]) < 0) {
    4246            // error
     
    4448    }
    4549
    46     for (i = 0 ; i < 10 ; i++) {
     50    for (i = 0 ; i < COUNT ; i++) {
    4751        if (CF_Thread_Join (tid[i]) < 0) { // block
    4852            // error
     
    5054    }
    5155
    52     for (i = 0 ; i < 10 ; i++) {
    53         if (CF_Thread_DestroyCtx (tid[i]) < 0) {
     56    for (i = 0 ; i < COUNT ; i++) {
     57        if (CF_Thread_Destroy (tid[i]) < 0) {
    5458            // error
    5559        }
    5660    }
    5761
    58     if (CF_Mutex_DestoryCtx (globalMutex) < 0) {
     62    if (CF_Mutex_Destory (globalMutex) < 0) {
    5963        // error
    6064    }
Note: See TracChangeset for help on using the changeset viewer.