Changeset 151 in libcf
- Timestamp:
- 10/31/13 10:17:24 (11 years ago)
- Location:
- trunk
- Files:
-
- 3 added
- 34 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/docs/doxyfile
r119 r151 39 39 # a quick idea about the purpose of the project. Keep the description short. 40 40 41 PROJECT_BRIEF = CFLibrary41 PROJECT_BRIEF = "CFLibrary (Cheese Foundation Library)" 42 42 43 43 # With the PROJECT_LOGO tag one can specify an logo or icon that is … … 53 53 # where doxygen was started. If left blank the current directory will be used. 54 54 55 OUTPUT_DIRECTORY = ../ documentation55 OUTPUT_DIRECTORY = ../Documentation 56 56 57 57 # If the CREATE_SUBDIRS tag is set to YES, then doxygen will create -
trunk/include/cf_base.h
r145 r151 10 10 * \mainpage 11 11 12 <h2 style="line-height: 10%;">Welcome to documentation page of libCF</h2>13 - CFLibrary (cheese's foundation library)12 <h2 style="line-height: 10%;">Welcome to documentation for libcf</h2> 13 :: CFLibrary (Cheese Foundation Library) 14 14 15 15 This documentation (suggested by nvdark) includes only manual of APIs. … … 40 40 } CF_BOOL; 41 41 42 /** 공용 건텍스트 */ 43 typedef void * cf_ctx; 44 42 45 /** 성공 시, 반환 값 */ 43 46 #define CF_OK 0 -
trunk/include/cf_error.h
r128 r151 21 21 #define CF_ERROR_FILE_READ CF_ERROR_FILE - 3 22 22 #define CF_ERROR_FILE_WRITE CF_ERROR_FILE - 4 23 #define CF_ERROR_FILE_CREATE 23 #define CF_ERROR_FILE_CREATE_CTX CF_ERROR_FILE - 5 24 24 #define CF_ERROR_FILE_CLOSE CF_ERROR_FILE - 6 25 25 #define CF_ERROR_FILE_GET_SIZE CF_ERROR_FILE - 7 26 26 #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 27 29 /* }}} file */ 28 30 … … 47 49 #define CF_ERROR_SOCKET_SEND CF_ERROR_SOCKET - 16 48 50 #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 50 54 /* }}} socket */ 51 55 … … 85 89 #define CF_ERROR_LOG CF_ERROR_BASE * 6 86 90 /*------------------------------------------------------------*/ 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 102 99 /* }}} log */ 103 100 … … 128 125 /* }}} bitwise */ 129 126 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 130 149 #endif // #ifndef __CF_ERROR_H__ -
trunk/include/cf_file.h
r128 r151 31 31 32 32 CF_EXPORT int 33 CF_File_Open (const char * path, 33 CF_File_Open (cf_ctx * ctx, 34 const char * path, 34 35 const CF_FILE_FLAG flag); 35 36 36 37 CF_EXPORT int 37 CF_File_C reate (const char * path);38 CF_File_Close (cf_ctx ctx); 38 39 39 40 CF_EXPORT int 40 CF_File_Close (const int fd); 41 42 CF_EXPORT int 43 CF_File_Read (const int fd, 41 CF_File_Read (const cf_ctx ctx, 44 42 void * buf, 45 43 const size_t len); 46 44 47 45 CF_EXPORT int 48 CF_File_Write (const int fd,46 CF_File_Write (const cf_ctx ctx, 49 47 const void * buf, 50 48 const size_t len); … … 59 57 CF_File_MakeDirectory (const char * path); 60 58 59 CF_EXPORT int 60 CF_File_GetPath (const cf_ctx ctx, 61 char * path); 62 61 63 #ifdef __cplusplus 62 64 } -
trunk/include/cf_list.h
r128 r151 13 13 #include "cf_base.h" 14 14 15 /** 리스트 컨텍스트 (Opaque) */16 typedef void * CF_List_Ctx;17 18 15 /** 리스트 탐색자 (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; 16 typedef cf_ctx cf_traverser; 27 17 28 18 #ifdef __cplusplus … … 31 21 32 22 CF_EXPORT int 33 CF_List_Create Ctx (CF_List_Ctx * ctx);23 CF_List_Create (cf_ctx * ctx); 34 24 35 25 CF_EXPORT int 36 CF_List_Destroy Ctx (CF_List_Ctx ctx);26 CF_List_Destroy (cf_ctx ctx); 37 27 38 28 CF_EXPORT int 39 CF_List_Front ( CF_List_Ctxctx,40 CF_Traverser * traverser);29 CF_List_Front (cf_ctx ctx, 30 cf_traverser * traverser); 41 31 42 32 CF_EXPORT int 43 CF_List_Rear ( CF_List_Ctxctx,44 CF_Traverser * traverser);33 CF_List_Rear (cf_ctx ctx, 34 cf_traverser * traverser); 45 35 46 36 CF_EXPORT int 47 CF_List_Insert (CF_List_Ctx ctx, 48 const CF_Traverser traverser, 49 const CF_DIRECTION direction, 37 CF_List_AddFront (cf_ctx ctx, 38 const void * element); 39 40 CF_EXPORT int 41 CF_List_AddRear (cf_ctx ctx, 42 const void * element); 43 44 CF_EXPORT int 45 CF_List_InsertBefore (cf_ctx ctx, 46 const cf_traverser traverser, 50 47 const void * element); 51 48 52 49 CF_EXPORT int 53 CF_List_Remove (CF_List_Ctx ctx, 54 CF_Traverser * traverser); 50 CF_List_InsertAfter (cf_ctx ctx, 51 const cf_traverser traverser, 52 const void * element); 55 53 56 54 CF_EXPORT int 57 CF_List_RemoveAll (CF_List_Ctx ctx); 55 CF_List_Set (cf_traverser traverser, 56 const void * element); 58 57 59 58 CF_EXPORT int 60 CF_List_Get Element (const CF_Traverser traverser,59 CF_List_Get (const cf_traverser traverser, 61 60 void ** element); 62 61 63 62 CF_EXPORT int 64 CF_List_Prev (CF_Traverser * traverser); 63 CF_List_Remove (cf_ctx ctx, 64 cf_traverser * traverser); 65 65 66 66 CF_EXPORT int 67 CF_List_ Next (CF_Traverser * traverser);67 CF_List_RemoveAll (cf_ctx ctx); 68 68 69 69 CF_EXPORT int 70 CF_List_GetSize (CF_List_Ctx ctx); 70 CF_List_Prev (cf_traverser * traverser); 71 72 CF_EXPORT int 73 CF_List_Next (cf_traverser * traverser); 74 75 CF_EXPORT int 76 CF_List_GetSize (cf_ctx ctx); 71 77 72 78 #ifdef __cplusplus -
trunk/include/cf_log.h
r128 r151 29 29 30 30 CF_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, 31 CF_Log_Create (cf_ctx * ctx, 38 32 const char * path, 39 33 const int memsize); 40 34 41 35 CF_EXPORT int 42 CF_Log_ Close (const int mapid);36 CF_Log_Destroy (cf_ctx ctx); 43 37 44 38 CF_EXPORT int 45 CF_Log_Write (c onst int mapid,39 CF_Log_Write (cf_ctx ctx, 46 40 const char * prefix, 47 41 const char * fmt, ...); 48 42 49 43 CF_EXPORT int 50 CF_Log_Flush (c onst int mapid);44 CF_Log_Flush (cf_ctx ctx); 51 45 52 46 CF_EXPORT int 53 CF_Log_SetMT (const int mapid, 54 const CF_BOOL flag); 47 CF_Log_SetMultiThread (cf_ctx ctx); 48 49 CF_EXPORT int 50 CF_Log_UnsetMultiThread (cf_ctx ctx); 55 51 56 52 #ifdef __cplusplus -
trunk/include/cf_mutex.h
r128 r151 5 5 * 6 6 * \brief 뮤텍스 지원 7 *8 * \example thread.c9 7 */ 10 8 #ifndef __CF_MUTEX_H__ … … 13 11 #include "cf_base.h" 14 12 15 /** 뮤텍스 컨텍스트 */16 typedef void * CF_Mutex_Ctx;17 18 13 #ifdef __cplusplus 19 14 extern "C" { … … 21 16 22 17 CF_EXPORT int 23 CF_Mutex_Create Ctx (CF_Mutex_Ctx * ctx);18 CF_Mutex_Create (cf_ctx * ctx); 24 19 25 20 CF_EXPORT int 26 CF_Mutex_Destory Ctx (CF_Mutex_Ctx ctx);21 CF_Mutex_Destory (cf_ctx ctx); 27 22 28 23 CF_EXPORT int 29 CF_Mutex_Lock ( CF_Mutex_Ctx ctx);24 CF_Mutex_Lock (cf_ctx ctx); 30 25 31 26 CF_EXPORT int 32 CF_Mutex_Unlock ( CF_Mutex_Ctx ctx);27 CF_Mutex_Unlock (cf_ctx ctx); 33 28 34 29 #ifdef __cplusplus -
trunk/include/cf_queue.h
r128 r151 13 13 #include "cf_base.h" 14 14 15 /** 큐 컨텍스트 (Opaque) */16 typedef void * CF_Queue_Ctx;17 18 15 #ifdef __cplusplus 19 16 extern "C" { … … 21 18 22 19 CF_EXPORT int 23 CF_Queue_Create Ctx (CF_Queue_Ctx * ctx);20 CF_Queue_Create (cf_ctx * ctx); 24 21 25 22 CF_EXPORT int 26 CF_Queue_Destroy Ctx (CF_Queue_Ctx ctx);23 CF_Queue_Destroy (cf_ctx ctx); 27 24 28 25 CF_EXPORT int 29 CF_Queue_Put ( CF_Queue_Ctxctx,30 const void 26 CF_Queue_Put (cf_ctx ctx, 27 const void * element); 31 28 32 29 CF_EXPORT int 33 CF_Queue_Get ( CF_Queue_Ctx ctx,34 void 30 CF_Queue_Get (cf_ctx ctx, 31 void ** element); 35 32 36 33 CF_EXPORT int 37 CF_Queue_Front ( CF_Queue_Ctx ctx,38 void 34 CF_Queue_Front (cf_ctx ctx, 35 void ** element); 39 36 40 37 CF_EXPORT int 41 CF_Queue_GetSize ( CF_Queue_Ctx ctx);38 CF_Queue_GetSize (cf_ctx ctx); 42 39 43 40 #ifdef __cplusplus -
trunk/include/cf_socket.h
r128 r151 16 16 17 17 /** 타임아웃을 설정하지 않음 */ 18 #define CF_SOCKET_NO_TIMEOUT -118 #define CF_SOCKET_NO_TIMEOUT 0 19 19 20 20 #ifdef __cplusplus 21 21 extern "C" { 22 22 #endif 23 24 CF_EXPORT CF_BOOL25 CF_Socket_IsInitialized (void);26 23 27 24 CF_EXPORT int … … 32 29 33 30 CF_EXPORT int 34 CF_Socket_C lose (const int sock);31 CF_Socket_Create (cf_ctx * ctx); 35 32 36 33 CF_EXPORT int 37 CF_Socket_SetOption (const int sock, 34 CF_Socket_Close (cf_ctx ctx); 35 36 CF_EXPORT int 37 CF_Socket_Connect (cf_ctx ctx, 38 const char * ip, 39 const unsigned short port); 40 41 CF_EXPORT int 42 CF_Socket_Server (cf_ctx ctx, 43 const unsigned short port, 44 const int backlog); 45 46 CF_EXPORT int 47 CF_Socket_Accept (const cf_ctx ctx, 48 cf_ctx * client); 49 50 CF_EXPORT int 51 CF_Socket_Send (const cf_ctx ctx, 52 const void * buf, 53 const size_t len); 54 55 CF_EXPORT int 56 CF_Socket_Recv (const cf_ctx ctx, 57 void * buf, 58 const size_t len); 59 60 CF_EXPORT int 61 CF_Socket_SetOption (const cf_ctx ctx, 38 62 const int optname, 39 63 const void * optval, … … 41 65 42 66 CF_EXPORT int 43 CF_Socket_GetOption (const int sock,44 const int optname,45 void * optval,46 size_t * optlen);67 CF_Socket_GetOption (const cf_ctx ctx, 68 const int optname, 69 void * optval, 70 size_t * optlen); 47 71 48 72 CF_EXPORT int 49 CF_Socket_ Connect (const char * ip,50 const unsigned short port);73 CF_Socket_SetTimeOut (cf_ctx ctx, 74 int timeout); 51 75 52 76 CF_EXPORT int 53 CF_Socket_ConnectTimeout (const char * ip, 54 const unsigned short port, 55 const int timeout); 77 CF_Socket_GetIP (const cf_ctx ctx, 78 char * ip); 56 79 57 80 CF_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); 81 CF_Socket_GetPort (const cf_ctx ctx, 82 unsigned short * port); 85 83 86 84 #ifdef __cplusplus -
trunk/include/cf_stack.h
r128 r151 13 13 #include "cf_base.h" 14 14 15 /** 스택 컨텍스트 (Opaque) */16 typedef void * CF_Stack_Ctx;17 18 15 #ifdef __cplusplus 19 16 extern "C" { … … 21 18 22 19 CF_EXPORT int 23 CF_Stack_Create Ctx (CF_Stack_Ctx * ctx);20 CF_Stack_Create (cf_ctx * ctx); 24 21 25 22 CF_EXPORT int 26 CF_Stack_Destroy Ctx (CF_Stack_Ctx ctx);23 CF_Stack_Destroy (cf_ctx ctx); 27 24 28 25 CF_EXPORT int 29 CF_Stack_Push ( CF_Stack_Ctxctx,30 const void 26 CF_Stack_Push (cf_ctx ctx, 27 const void * element); 31 28 32 29 CF_EXPORT int 33 CF_Stack_Pop ( CF_Stack_Ctx ctx,34 void 30 CF_Stack_Pop (cf_ctx ctx, 31 void ** element); 35 32 36 33 CF_EXPORT int 37 CF_Stack_Top ( CF_Stack_Ctxctx,38 void 34 CF_Stack_Top (cf_ctx ctx, 35 void ** element); 39 36 40 37 CF_EXPORT int 41 CF_Stack_GetSize ( CF_Stack_Ctx ctx);38 CF_Stack_GetSize (cf_ctx ctx); 42 39 43 40 #ifdef __cplusplus -
trunk/include/cf_thread.h
r147 r151 16 16 typedef int (* CF_Thread_Function) (void *); 17 17 18 /** 스레드 컨텍스트 */19 typedef void * CF_Thread_Ctx;20 21 18 #ifdef __cplusplus 22 19 extern "C" { … … 24 21 25 22 CF_EXPORT int 26 CF_Thread_Create Ctx (CF_Thread_Ctx* ctx,23 CF_Thread_Create (cf_ctx * ctx, 27 24 CF_Thread_Function callback, 28 25 void * arg); 29 26 30 27 CF_EXPORT int 31 CF_Thread_Start ( CF_Thread_Ctx ctx);28 CF_Thread_Start (cf_ctx ctx); 32 29 33 30 CF_EXPORT int 34 CF_Thread_Destroy Ctx (CF_Thread_Ctx ctx);31 CF_Thread_Destroy (cf_ctx ctx); 35 32 36 33 CF_EXPORT int 37 CF_Thread_Join ( CF_Thread_Ctx ctx);34 CF_Thread_Join (cf_ctx ctx); 38 35 39 36 #ifdef __cplusplus -
trunk/makeinclude/platform.mk
r134 r151 9 9 DOXYGEN := $(shell which doxygen) 10 10 11 DOXYGEN_OUTPUT_PATH = ../ documentation11 DOXYGEN_OUTPUT_PATH = ../Documentation 12 12 DOXYGEN_PATH = ../docs 13 13 DOXYGEN_FILE = $(DOXYGEN_PATH)/doxyfile … … 16 16 # set options 17 17 #------------------------------------------------------------ 18 ifeq ($(DEBUG), YES)18 ifeq ($(DEBUG), yes) 19 19 DEFS = -D_DEBUG 20 20 FLAG = -g -
trunk/makeinclude/userdefine.mk
r119 r151 9 9 # compile with debugging flag and definition 10 10 # > YES or NO 11 DEBUG = YES11 DEBUG = yes 12 12 13 13 # create doxygen documents automatically 14 14 # > YES or NO 15 DOXYGEN_CREATE = YES15 DOXYGEN_CREATE = yes -
trunk/src/cf_codec.c
r129 r151 13 13 #include <stdio.h> 14 14 15 #define BASE64_PADDING_CHAR_INDEX 64 16 15 17 #define ASSERT_ARGS(x) \ 16 18 if ((x)) \ 17 19 return CF_ERROR_CODEC_INVALID_ARGS 18 20 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 6441 '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 73 21 /** 74 22 * hex-encode … … 88 36 char * hex) 89 37 { 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 }; 99 45 100 46 ASSERT_ARGS (bin == NULL); 101 47 ASSERT_ARGS (hex == NULL); 102 48 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'; 109 55 110 56 return CF_OK; … … 114 60 * hex-decode 115 61 * 116 * \return 성공 시, 디코딩된 바이너리 데이터의 길이; 실패 시, 오류 코드62 * \return 성공 시, CF_OK; 실패 시, 오류 코드 117 63 * 118 64 * \param hex 16진수 문자열 … … 129 75 { 130 76 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; 135 80 char buf = 0; 136 81 cf_byte val = 0; 137 82 cf_byte asciiHex = 0; 138 83 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 139 103 ASSERT_ARGS (hex == NULL); 140 104 ASSERT_ARGS (bin == NULL); 141 105 ASSERT_ARGS (len == NULL); 142 106 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) 148 110 return CF_ERROR_CODEC_NOT_HEXSTRING; 149 111 150 binlen = length / 2; 151 152 for (iter = 0, ptr = hex ; *ptr ; iter++) 112 for (src = hex ; *src ; ) 153 113 { 154 114 val = 0; /* init/re-init docoding-buffer */ 155 115 156 116 /* 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) \ 164 124 (buf - asciiHex + (asciiHex == '0' ? 0 : 10)); \ 165 125 } while (0) 166 126 167 127 /* 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; 175 134 176 135 return CF_OK; … … 196 155 const cf_byte * src = bin; 197 156 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 }; 198 169 199 170 ASSERT_ARGS (src == NULL); … … 207 178 #define SEXTUPLE_E_6(__x) (((__x[2]) ) & 0x3f) 208 179 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)]; 222 191 223 192 if (src - bin == len - 1) 224 193 { 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]; 227 196 } 228 197 else 229 198 { 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)]; 233 201 } 234 202 235 *dst++ = g_table_Base64Encode[BASE64_PADDING_CHAR_INDEX];203 *dst++ = base64enc[BASE64_PADDING_CHAR_INDEX]; 236 204 } 237 205 *dst = '\0'; … … 243 211 * Base64-decode 244 212 * 245 * \return 성공 시, 디코딩된 바이너리 데이터의 길이; 실패 시, 오류 코드213 * \return 성공 시, CF_OK; 실패 시, 오류 코드 246 214 * 247 215 * \param base64 base64 문자열 … … 259 227 const char * src = base64; 260 228 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 }; 263 250 264 251 ASSERT_ARGS (src == NULL); … … 266 253 ASSERT_ARGS (len == NULL); 267 254 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++; 271 256 272 257 if (*src == 0xff) 273 258 return CF_ERROR_CODEC_NOT_BASE64; 274 259 275 remain = ( int)(src - base64);260 remain = (size_t)(src - base64); 276 261 binlen = ((remain + 2/* max padding length */) / 4) * 3; 277 262 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]] ) 284 269 285 270 for (src = base64 ; remain > 4 ; remain -= 4, src += 4) … … 297 282 *dst++ = (cf_byte)(SEXTUPLE_D_5 (src) | SEXTUPLE_D_6 (src)); 298 283 299 return binlen - (4 - remain); 300 } 284 *len = binlen - (4 - remain); 285 286 return CF_OK; 287 } -
trunk/src/cf_debug.c
r147 r151 28 28 return CF_ERROR_DEBUG_INVALID_CTX 29 29 30 /** 디버그 컨텍스트 (Opaque) */31 typedef void * CF_Debug_Ctx;32 33 30 /** 콜스택 인터페이스 */ 34 typedef struct cf_debug_callStack { 31 typedef struct cf_debug_callStack 32 { 35 33 char file[NAME_LENGTH + 1]; /* *< 파일 이름 */ 36 34 char function[NAME_LENGTH + 1]; /* *< 함수 이름 */ … … 50 48 } CF_DEBUG_CALLSTACK; 51 49 52 /** 디버그 컨텍스트 ( CF_Debug_Ctx의 구현) */50 /** 디버그 컨텍스트 (cf_ctx의 구현) */ 53 51 typedef struct __cf_debug_ctx__ 54 52 { 55 int 56 CF_Mutex_Ctx mutex;53 int fd; 54 cf_ctx mutex; 57 55 58 56 CF_DEBUG_CALLSTACK callstack; 59 } CF_DEBUG_C TX;60 61 static CF_Debug_Ctx gDebugSingleCtx = NULL;57 } CF_DEBUG_CONTEXT; 58 59 static cf_ctx gDebugSingleCtx = NULL; 62 60 63 61 static int … … 187 185 */ 188 186 static int 189 CF_Debug_CallStackPush ( CF_Debug_Ctxctx,190 const char 191 const char 192 const int 193 { 194 CF_DEBUG_C TX * context = (CF_DEBUG_CTX*) ctx;187 CF_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; 195 193 CF_DEBUG_CALLSTACK * push = NULL; 196 194 197 195 ASSERT_CTX (ctx); 198 196 199 push = (CF_DEBUG_CALLSTACK *) calloc (sizeof (CF_DEBUG_CALLSTACK), 1);197 push = NEWCTX (CF_DEBUG_CALLSTACK); 200 198 if (push == NULL) 201 199 return CF_ERROR_DEBUG_PUSH_CALLSTACK; … … 223 221 */ 224 222 static int 225 CF_Debug_CallStackPeek ( CF_Debug_Ctxctx,223 CF_Debug_CallStackPeek (cf_ctx ctx, 226 224 CF_Debug_CallStack * callstack) 227 225 { 228 CF_DEBUG_C TX * context = (CF_DEBUG_CTX*) ctx;226 CF_DEBUG_CONTEXT * context = (CF_DEBUG_CONTEXT *) ctx; 229 227 CF_DEBUG_CALLSTACK * pop = NULL; 230 228 … … 254 252 */ 255 253 static int 256 CF_Debug_CallStackPop ( CF_Debug_Ctxctx,254 CF_Debug_CallStackPop (cf_ctx ctx, 257 255 CF_Debug_CallStack * callstack) 258 256 { 259 CF_DEBUG_C TX * context = (CF_DEBUG_CTX*) ctx;257 CF_DEBUG_CONTEXT * context = (CF_DEBUG_CONTEXT *) ctx; 260 258 CF_DEBUG_CALLSTACK * pop = NULL; 261 259 … … 283 281 */ 284 282 static int 285 CF_Debug_Destroy Ctx (CF_Debug_Ctx ctx)286 { 287 CF_DEBUG_C TX * context = (CF_DEBUG_CTX*) ctx;283 CF_Debug_Destroy (cf_ctx ctx) 284 { 285 CF_DEBUG_CONTEXT * context = (CF_DEBUG_CONTEXT *) ctx; 288 286 289 287 ASSERT_CTX (ctx); … … 293 291 294 292 if (context->mutex) 295 CF_Mutex_Destory Ctx(context->mutex);293 CF_Mutex_Destory (context->mutex); 296 294 297 295 free (context); … … 303 301 * 디버그 컨텍스트를 생성 304 302 * 305 * \return 성공 시, CF_Debug_Ctx 형태의 컨텍스트; 실패 시, NULL303 * \return 성공 시, cf_ctx 형태의 컨텍스트; 실패 시, NULL 306 304 * 307 305 * \param ctx 디버그 컨텍스트 308 306 */ 309 307 static int 310 CF_Debug_CreateCtx (CF_Debug_Ctx * ctx) 311 { 312 int result = 0; 313 CF_DEBUG_CTX * context = NULL; 308 CF_Debug_Create (cf_ctx * ctx) 309 { 310 int result = 0; 311 312 CF_DEBUG_CONTEXT * context = NULL; 314 313 315 314 TRY 316 315 { 317 context = (CF_DEBUG_CTX *) calloc (sizeof (CF_DEBUG_CTX), 1);316 context = NEWCTX (CF_DEBUG_CONTEXT); 318 317 if (context == NULL) 319 318 { … … 322 321 } 323 322 324 result = CF_Mutex_Create Ctx(&context->mutex);323 result = CF_Mutex_Create (&context->mutex); 325 324 if (result < 0) 326 325 { … … 328 327 } 329 328 330 *ctx = ( CF_Debug_Ctx) context;329 *ctx = (cf_ctx) context; 331 330 } 332 331 CATCH_IF (result < 0) 333 332 { 334 CF_Debug_Destroy Ctx(context);333 CF_Debug_Destroy (context); 335 334 } 336 335 … … 349 348 350 349 if (gDebugSingleCtx == NULL) 351 result = CF_Debug_Create Ctx(&gDebugSingleCtx);350 result = CF_Debug_Create (&gDebugSingleCtx); 352 351 353 352 return result; … … 362 361 CF_Debug_Finalize (void) 363 362 { 364 CF_Debug_Ctx ctx = gDebugSingleCtx;363 cf_ctx ctx = gDebugSingleCtx; 365 364 366 365 gDebugSingleCtx = NULL; 367 366 368 return CF_Debug_Destroy Ctx(ctx);367 return CF_Debug_Destroy (ctx); 369 368 } 370 369 … … 383 382 const int line) 384 383 { 385 CF_DEBUG_C TX * ctx = (CF_DEBUG_CTX*)gDebugSingleCtx;384 CF_DEBUG_CONTEXT * ctx = (CF_DEBUG_CONTEXT *)gDebugSingleCtx; 386 385 387 386 ASSERT_CTX (ctx); … … 402 401 CF_Debug_LeaveFunction (void) 403 402 { 404 CF_DEBUG_C TX * ctx = (CF_DEBUG_CTX*)gDebugSingleCtx;403 CF_DEBUG_CONTEXT * ctx = (CF_DEBUG_CONTEXT *)gDebugSingleCtx; 405 404 406 405 ASSERT_CTX (ctx); … … 425 424 int iter = 0; 426 425 427 CF_DEBUG_C TX* ctx = gDebugSingleCtx;426 CF_DEBUG_CONTEXT * ctx = gDebugSingleCtx; 428 427 CF_DEBUG_CALLSTACK * callstack = NULL; 429 428 -
trunk/src/cf_file.c
r128 r151 11 11 12 12 #include <stdio.h> 13 #include <string.h> 13 14 #include <fcntl.h> 14 15 #include <errno.h> … … 51 52 #define FILE_MODE S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH 52 53 54 #define ASSERT_CTX(__ctx) \ 55 if (__ctx == NULL) \ 56 return CF_ERROR_FILE_INVALID_CTX 57 53 58 #define ASSERT_ARGS(x) \ 54 59 if ((x)) \ 55 60 return CF_ERROR_FILE_INVALID_ARGS 61 62 typedef struct __cf_file_ctx__ 63 { 64 int fd; 65 char path[1024]; 66 CF_FILE_FLAG flag; 67 } CF_FILE_CONTEXT; 56 68 57 69 static int … … 73 85 * 파일 열기 74 86 * 75 * \return 성공 시, 파일 디스크립터; 실패 시, 오류 코드 76 * 87 * \return 성공 시, CF_OK; 실패 시, 오류 코드 88 * 89 * \param ctx 파일 컨텍스트 77 90 * \param path 파일 경로 78 91 * \param flag 파일 열기 플래그 … … 81 94 */ 82 95 int 83 CF_File_Open (const char * path, 96 CF_File_Open (cf_ctx * ctx, 97 const char * path, 84 98 const CF_FILE_FLAG flag) 85 99 { 100 int fd = 0; 86 101 int result = 0; 87 102 int osflag = 0; 88 103 104 CF_FILE_CONTEXT * context = NULL; 105 106 ASSERT_CTX (ctx); 89 107 ASSERT_ARGS (path == NULL); 90 108 91 osflag = CF_File_Local_ConvertFlag (flag) | O_BINARY; 92 93 result = open (path, osflag, FILE_MODE); 94 if (result < 0) 95 return CF_ERROR_FILE_OPEN; 109 TRY 110 { 111 context = NEWCTX (CF_FILE_CONTEXT); 112 if (context == NULL) 113 { 114 result = CF_ERROR_FILE_CREATE_CTX; 115 TRY_BREAK; 116 } 117 context->flag = flag; 118 context->fd = -1; 119 120 osflag = CF_File_Local_ConvertFlag (flag) | O_BINARY; 121 122 fd = open (path, osflag, FILE_MODE); 123 if (fd < 0) 124 { 125 result = CF_ERROR_FILE_OPEN; 126 TRY_BREAK; 127 } 128 context->fd = fd; 129 130 *ctx = context; 131 } 132 CATCH_IF (result < 0) 133 { 134 CF_File_Close ((cf_ctx) context); 135 } 96 136 97 137 return result; … … 99 139 100 140 /** 101 * 파일 생성102 *103 * \return 성공 시, 파일 디스크립터; 실패 시, 오류 코드104 *105 * \param path 파일 경로106 */107 int108 CF_File_Create (const char * path)109 {110 int result = 0;111 const int flag = CF_FILE_CREATE|CF_FILE_WRITE|CF_FILE_TRUNC;112 113 ASSERT_ARGS (path == NULL);114 115 result = CF_File_Open (path, flag);116 if (result < 0)117 return CF_ERROR_FILE_CREATE;118 119 return result;120 }121 122 /**123 141 * 파일 닫기 124 142 * 125 143 * \return 성공 시, CF_OK; 실패 시, 오류 코드 126 144 * 127 * \param fd 파일 디스크립터 128 */ 129 int 130 CF_File_Close (const int fd) 131 { 132 int result = 0; 133 134 ASSERT_ARGS (fd < 0); 145 * \param ctx 파일 컨텍스트 146 */ 147 int 148 CF_File_Close (cf_ctx ctx) 149 { 150 int result = 0; 151 int fd = 0; 152 153 CF_FILE_CONTEXT * context = (CF_FILE_CONTEXT *) ctx; 154 155 ASSERT_CTX (context); 156 ASSERT_ARGS (context->fd < 0); 157 158 fd = context->fd; 159 160 free (context); 135 161 136 162 result = close (fd); … … 146 172 * \return 성공 시, 읽은 바이트 수; 실패 시, 오류 코드 147 173 * 148 * \param fd 파일 디스크립터174 * \param ctx 파일 컨텍스트 149 175 * \param buf 데이터를 저장할 메모리 150 176 * \param len 데이터를 저장할 메모리의 크기 151 177 */ 152 178 int 153 CF_File_Read (const int fd,179 CF_File_Read (const cf_ctx ctx, 154 180 void * buf, 155 181 const size_t len) 156 182 { 157 183 int result = 0; 158 159 ASSERT_ARGS (fd < 0); 184 185 CF_FILE_CONTEXT * context = (CF_FILE_CONTEXT *) ctx; 186 187 ASSERT_CTX (context); 188 ASSERT_ARGS (context->fd < 0); 160 189 ASSERT_ARGS (buf == NULL); 161 190 162 result = (int) read ( fd, buf, len);191 result = (int) read (context->fd, buf, len); 163 192 if (result < 0) 164 193 return CF_ERROR_FILE_READ; … … 172 201 * \return 성공 시, CF_OK; 실패 시, 오류 코드 173 202 * 174 * \param fd 파일 디스크립터203 * \param ctx 파일 컨텍스트 175 204 * \param buf 데이터가 저장된 메모리 176 205 * \param len 쓸 데이터의 길이 177 206 */ 178 207 int 179 CF_File_Write (const int fd,208 CF_File_Write (const cf_ctx ctx, 180 209 const void * buf, 181 210 const size_t len) 182 211 { 183 212 int result = 0; 184 185 ASSERT_ARGS (fd < 0); 213 214 CF_FILE_CONTEXT * context = (CF_FILE_CONTEXT *) ctx; 215 216 ASSERT_CTX (context); 217 ASSERT_ARGS (context->fd < 0); 186 218 ASSERT_ARGS (buf == NULL); 187 219 188 result = (int) write ( fd, buf, len);220 result = (int) write (context->fd, buf, len); 189 221 if (result != len) 190 222 return CF_ERROR_FILE_WRITE; … … 206 238 int fd = 0; 207 239 240 CF_FILE_CONTEXT * context = NULL; 241 208 242 ASSERT_ARGS (path == NULL); 209 243 210 fd = CF_File_Open ( path, CF_FILE_READ);244 fd = CF_File_Open ((cf_ctx *)&context, path, CF_FILE_READ); 211 245 if (fd < 0) 212 246 return CF_ERROR_FILE_OPEN; … … 216 250 return CF_ERROR_FILE_GET_SIZE; 217 251 218 CF_File_Close ( fd);252 CF_File_Close ((cf_ctx)context); 219 253 220 254 return length; … … 245 279 { 246 280 int result = 0; 247 char fullPath[1024] = {0x00,}; 281 size_t length = 0; 282 char * fullpath = NULL; 248 283 char stepPath[256] = {0x00,}; 249 284 250 char * f = fullPath;251 char * d = stepPath;285 char * f = NULL; 286 char * d = NULL; 252 287 253 288 ASSERT_ARGS (path == NULL); 254 289 255 snprintf (fullPath, sizeof (fullPath) - 1, "%s%c", path, DELIMITER); 290 length = strlen (path); 291 fullpath = (char *) calloc (length + 2, 1); 292 if (!fullpath) 293 return CF_ERROR_FILE_ALLOCATE_BUFFER; 294 295 f = fullpath; 296 d = stepPath; 297 298 snprintf (fullpath, length + 1, "%s%c", path, DELIMITER); 256 299 257 300 for (*d++ = *f++ ; *f ; *d++ = *f++) … … 270 313 return CF_OK; 271 314 } 315 316 /** 317 * 컨텍스트가 열고 있는 파일의 경로를 가져옴 318 * 319 * \return 성공 시, CF_OK; 실패 시, 오류 코드 320 * 321 * \param ctx 파일 컨텍스트 322 * \param path 파일 경로를 저장할 충분한 공간의 메모리 323 */ 324 int 325 CF_File_GetPath (const cf_ctx ctx, 326 char * path) 327 { 328 CF_FILE_CONTEXT * context = (CF_FILE_CONTEXT *) ctx; 329 330 ASSERT_CTX (context); 331 ASSERT_ARGS (path == NULL); 332 333 snprintf (path, sizeof (context->path), "%s", context->path); 334 335 return CF_OK; 336 } -
trunk/src/cf_list.c
r149 r151 24 24 return CF_ERROR_DS_INVALID_ARGS 25 25 26 /** 리스트 노드 ( CF_Traverser의 구현) */26 /** 리스트 노드 (cf_traverser의 구현) */ 27 27 typedef struct __cf_node__ 28 28 { … … 32 32 } CF_NODE; 33 33 34 /** 리스트 컨텍스트 ( CF_List_Ctx의 구현) */34 /** 리스트 컨텍스트 (cf_ctx의 구현) */ 35 35 typedef struct __cf_list__ 36 36 { … … 48 48 */ 49 49 int 50 CF_List_Create Ctx (CF_List_Ctx * ctx)50 CF_List_Create (cf_ctx * ctx) 51 51 { 52 52 CF_LIST_CONTEXT * context = NULL; 53 53 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); 57 57 if (context == NULL) 58 58 return CF_ERROR_DS_CREATE_CTX; 59 59 60 *ctx = ( CF_List_Ctx) context;60 *ctx = (cf_ctx) context; 61 61 62 62 return CF_OK; … … 71 71 */ 72 72 int 73 CF_List_Destroy Ctx (CF_List_Ctx ctx)73 CF_List_Destroy (cf_ctx ctx) 74 74 { 75 75 CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx; … … 85 85 86 86 /** 87 * 리스트 탐색자( Traverser)를 Front 위치로 설정87 * 리스트 탐색자(traverser)를 Front 위치로 설정 88 88 * 89 89 * \return 성공 시, CF_OK; 실패 시, 오류 코드 … … 93 93 */ 94 94 int 95 CF_List_Front ( CF_List_Ctxctx,96 CF_Traverser * traverser)95 CF_List_Front (cf_ctx ctx, 96 cf_traverser * traverser) 97 97 { 98 98 CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx; … … 101 101 ASSERT_TRAVERSER (traverser); 102 102 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 위치로 설정 110 110 * 111 111 * \return 성공 시, CF_OK; 실패 시, 오류 코드 … … 115 115 */ 116 116 int 117 CF_List_Rear ( CF_List_Ctxctx,118 CF_Traverser * traverser)117 CF_List_Rear (cf_ctx ctx, 118 cf_traverser * traverser) 119 119 { 120 120 CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx; … … 123 123 ASSERT_TRAVERSER (traverser); 124 124 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 */ 138 int 139 CF_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 */ 162 int 163 CF_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)의 앞에 새 데이터 삽입 132 180 * 133 181 * \return 성공 시, CF_OK; 실패 시, 오류 코드 … … 135 183 * \param ctx 리스트 컨텍스트 136 184 * \param traverser 리스트 탐색자 137 * \param direction 탐색자의 전/후를 지정138 185 * \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 */ 187 int 188 CF_List_InsertBefore (cf_ctx ctx, 189 const cf_traverser traverser, 190 const void * element) 150 191 { 151 192 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); 158 199 if (newnode == NULL) 159 200 return CF_ERROR_DS_CREATE_NODE; … … 169 210 } 170 211 171 if (direction == CF_DIRECTION_BEFORE) 212 node = traverser ? (CF_NODE *) traverser : context->front; 213 214 newnode->next = node; 215 if (node) 172 216 { 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; 186 221 } 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 */ 241 int 242 CF_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) 188 261 { 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; 202 264 } 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; 203 279 } NO_CATCH; 204 280 … … 209 285 210 286 /** 287 * 리스트 탐색자(traverser)의 자리에 새 데이터 삽입 288 * 289 * \return 성공 시, CF_OK; 실패 시, 오류 코드 290 * 291 * \param traverser 리스트 탐색자 292 * \param element 할당할 데이터 주소 293 */ 294 int 295 CF_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 */ 315 int 316 CF_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 /** 211 330 * 리스트에서 탐색자 위치의 항목을 삭제 212 331 * … … 217 336 */ 218 337 int 219 CF_List_Remove ( CF_List_Ctxctx,220 CF_Traverser * traverser)338 CF_List_Remove (cf_ctx ctx, 339 cf_traverser * traverser) 221 340 { 222 341 CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx; … … 263 382 */ 264 383 int 265 CF_List_RemoveAll ( CF_List_Ctx ctx)266 { 267 CF_Traverser traverser = NULL;384 CF_List_RemoveAll (cf_ctx ctx) 385 { 386 cf_traverser traverser = NULL; 268 387 269 388 ASSERT_CTX (ctx); … … 279 398 280 399 /** 281 * 탐색자 위치의 데이터를 가져옴282 *283 * \return 성공 시, CF_OK; 실패 시, 오류 코드284 *285 * \param traverser 리스트 탐색자286 * \param element 데이터 주소287 */288 int289 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 /**303 400 * 탐색자 위치를 이전으로 이동 304 401 * … … 308 405 */ 309 406 int 310 CF_List_Prev ( CF_Traverser * traverser)407 CF_List_Prev (cf_traverser * traverser) 311 408 { 312 409 CF_NODE * node = (CF_NODE *) *traverser; … … 316 413 317 414 node = node->prev; 318 *traverser = ( CF_Traverser *) node;415 *traverser = (cf_traverser *) node; 319 416 if (node == NULL) 320 417 return CF_ERROR_DS_NO_MORE; … … 331 428 */ 332 429 int 333 CF_List_Next ( CF_Traverser * traverser)430 CF_List_Next (cf_traverser * traverser) 334 431 { 335 432 CF_NODE * node = (CF_NODE *) *traverser; … … 339 436 340 437 node = node->next; 341 *traverser = ( CF_Traverser *) node;438 *traverser = (cf_traverser *) node; 342 439 if (node == NULL) 343 440 return CF_ERROR_DS_NO_MORE; … … 354 451 */ 355 452 int 356 CF_List_GetSize ( CF_List_Ctx ctx)453 CF_List_GetSize (cf_ctx ctx) 357 454 { 358 455 CF_LIST_CONTEXT * context = (CF_LIST_CONTEXT *) ctx; -
trunk/src/cf_local.h
r128 r151 6 6 #ifndef __CF_LOCAL_H__ 7 7 #define __CF_LOCAL_H__ 8 9 #include <stdlib.h> 8 10 9 11 /** 파일 이름 및 경로 길이 */ … … 18 20 #define CATCH_ELSE else 19 21 22 /** 23 * 유틸 매크로 24 * 25 * \remarks 26 * header는 별도로 include 해야 함 27 */ 28 #define NEWCTX(__ctx) ((__ctx *) calloc (sizeof (__ctx), 1)) 29 20 30 /** non-compatible function */ 21 31 #if defined(_WIN32) || defined(_WIN64) -
trunk/src/cf_log.c
r149 r151 19 19 #include "cf_local.h" 20 20 #include "cf_error.h" 21 #include "cf_util.h" 21 22 22 23 #include <stdio.h> … … 29 30 # include <Windows.h> 30 31 #else 31 # include <sys/time.h>32 32 #endif 33 34 #define LOG_BUFFER_DEFAULT_SIZE 128 * 1024 33 35 34 36 #define ASSERT_CTX(__ctx) \ … … 36 38 return CF_ERROR_LOG_INVALID_CTX 37 39 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의 구현) */ 76 45 typedef struct __cf_log_ctx__ 77 46 { 78 47 char path[NAME_LENGTH + 1]; 79 int fd;48 cf_ctx file; 80 49 char * buffer; 81 50 size_t size; /* entire size of buffer */ 82 51 size_t length; /* data length in current */ 83 CF_Mutex_Ctx mutex;52 cf_ctx mutex; 84 53 } 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 timezone97 {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 Greenwich136 tz->tz_minuteswest = _timezone / 60;137 tz->tz_dsttime = _daylight;138 }139 */140 141 return 0;142 }143 /* }}} */144 #endif145 146 static int147 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 int170 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 }182 54 183 55 static int 184 56 CF_Log_Local_Flush (CF_LOG_CONTEXT * ctx) 185 57 { 186 if (CF_File_Write (ctx->f d, ctx->buffer, ctx->length) < 0)58 if (CF_File_Write (ctx->file, ctx->buffer, ctx->length) < 0) 187 59 return CF_ERROR_LOG_FLUSH; 188 60 … … 208 80 const size_t demandSize) 209 81 { 210 int result = CF_OK; 82 int result = 0; 83 84 size_t writeSize; 85 size_t remainSize; 211 86 212 87 if (ctx->size > 0) /* 버퍼단위 버퍼링.... */ 213 88 { 214 size_t writeSize;215 size_t remainSize;216 217 89 remainSize = demandSize; 218 90 while (remainSize) 219 91 { 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); 223 99 ctx->length += writeSize; 224 100 … … 247 123 * \param ctx 로그 컨텍스트 248 124 */ 249 staticint250 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_Create Ctx(&context->mutex) < 0)125 int 126 CF_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) 257 133 return CF_ERROR_LOG_SET_MULTITHREAD; 258 134 … … 267 143 * \param ctx 로그 컨텍스트 268 144 */ 269 staticint270 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_Destory Ctx(context->mutex) < 0)145 int 146 CF_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) 277 153 return CF_ERROR_LOG_UNSET_MULTITHREAD; 278 154 … … 293 169 */ 294 170 static int 295 CF_Log_Local_WriteVA (CF_L og_Ctx ctx,296 const char * prefix,297 const char * fmt,298 va_list valist)171 CF_Log_Local_WriteVA (CF_LOG_CONTEXT * context, 172 const char * prefix, 173 const char * fmt, 174 va_list valist) 299 175 { 300 176 #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); 310 188 length = snprintf (buffer, BUF_LEN, "[%s][%s] ", datetime, prefix); 311 189 vsnprintf (&buffer[length], BUF_LEN - (size_t)length, fmt, valist); … … 329 207 */ 330 208 int 331 CF_Log_WriteCtx (CF_Log_Ctx ctx, 332 const char * prefix, 333 const char * fmt, ...) 334 { 209 CF_Log_Write (cf_ctx ctx, 210 const char * prefix, 211 const char * fmt, ...) 212 { 213 CF_LOG_CONTEXT * context = (CF_LOG_CONTEXT *) ctx; 214 335 215 int result = 0; 336 216 va_list valist; 337 217 338 218 va_start (valist, fmt); 339 result = CF_Log_Local_WriteVA (c tx, prefix, fmt, valist);219 result = CF_Log_Local_WriteVA (context, prefix, fmt, valist); 340 220 va_end (valist); 341 221 … … 350 230 * \param ctx 로그 컨텍스트 351 231 */ 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; 232 int 233 CF_Log_Flush (cf_ctx ctx) 234 { 235 int result = 0; 236 237 CF_LOG_CONTEXT * context = (CF_LOG_CONTEXT *) ctx; 357 238 358 239 ASSERT_CTX (ctx); … … 372 253 * \param ctx 로그 컨텍스트 373 254 */ 374 staticint375 CF_Log_Destroy Ctx (CF_Log_Ctx ctx)255 int 256 CF_Log_Destroy (cf_ctx ctx) 376 257 { 377 258 CF_LOG_CONTEXT * context = (CF_LOG_CONTEXT *) ctx; … … 381 262 if (context->size > 0) 382 263 { 383 CF_Log_Flush Ctx(ctx);264 CF_Log_Flush (ctx); 384 265 free (context->buffer); 385 266 context->buffer = NULL; … … 387 268 } 388 269 389 CF_File_Close (context->f d);390 391 CF_Mutex_Destory Ctx(context->mutex);270 CF_File_Close (context->file); 271 272 CF_Mutex_Destory (context->mutex); 392 273 context->mutex = NULL; 274 275 free (context); 393 276 394 277 return CF_OK; … … 406 289 * \see CF_LOG_DEFAULT_BUFFER, CF_LOG_NO_BUFFER 407 290 */ 408 staticint409 CF_Log_Create Ctx (CF_Log_Ctx* ctx,410 411 const intmemsize)291 int 292 CF_Log_Create (cf_ctx * ctx, 293 const char * path, 294 const int memsize) 412 295 { 413 296 int result = 0; … … 417 300 : memsize; 418 301 419 CF_LOG_CONTEXT 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); 423 306 424 307 TRY 425 308 { 426 context = (CF_LOG_CONTEXT *) calloc (sizeof (CF_LOG_CONTEXT), 1);309 context = NEWCTX (CF_LOG_CONTEXT); 427 310 if (context == NULL) 428 311 { … … 431 314 } 432 315 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) 435 318 { 436 319 result = CF_ERROR_LOG_CREATE_FILE; 437 320 TRY_BREAK; 438 321 } 439 snprintf (context->path, sizeof (context->path) - 1, "%s", path);322 snprintf (context->path, sizeof (context->path) - 1, "%s", path); 440 323 441 324 if (size > 0) … … 450 333 } 451 334 452 *ctx = ( CF_Log_Ctx) context;335 *ctx = (cf_ctx) context; 453 336 } 454 337 CATCH_IF (result < 0) 455 338 { 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 21 21 #endif // #if defined(_WIN32) || defined(_WIN64) 22 22 23 #define ASSERT_ MUTEX_CTX(__ctx) \24 if (__ctx == NULL) 23 #define ASSERT_CTX(__ctx) \ 24 if (__ctx == NULL) \ 25 25 return CF_ERROR_MUTEX_INVALID_CTX 26 26 … … 38 38 */ 39 39 int 40 CF_Mutex_Create Ctx (CF_Mutex_Ctx * ctx)40 CF_Mutex_Create (cf_ctx * ctx) 41 41 { 42 42 int result = 0; … … 44 44 CF_MUTEX_CONTEXT * context = NULL; 45 45 46 context = (CF_MUTEX_CONTEXT *) calloc (sizeof (CF_MUTEX_CONTEXT), 1); 46 ASSERT_CTX (ctx); 47 48 context = NEWCTX (CF_MUTEX_CONTEXT); 47 49 if (context == NULL) 48 50 return CF_ERROR_MUTEX_CREATE_CTX; … … 70 72 CATCH_IF (result < 0) 71 73 { 72 CF_Mutex_Destory Ctx ((CF_Mutex_Ctx) context);74 CF_Mutex_Destory ((cf_ctx) context); 73 75 } 74 76 … … 84 86 */ 85 87 int 86 CF_Mutex_Destory Ctx (CF_Mutex_Ctx ctx)88 CF_Mutex_Destory (cf_ctx ctx) 87 89 { 88 CF_MUTEX_CONTEXT * context = ctx;90 CF_MUTEX_CONTEXT * context = (CF_MUTEX_CONTEXT *) ctx; 89 91 90 ASSERT_ MUTEX_CTX (ctx);92 ASSERT_CTX (ctx); 91 93 92 94 #if defined(_WIN32) || defined(_WIN64) … … 112 114 */ 113 115 int 114 CF_Mutex_Lock ( CF_Mutex_Ctx ctx)116 CF_Mutex_Lock (cf_ctx ctx) 115 117 { 116 CF_MUTEX_CONTEXT * context = ctx;118 CF_MUTEX_CONTEXT * context = (CF_MUTEX_CONTEXT *) ctx; 117 119 118 ASSERT_ MUTEX_CTX (ctx);120 ASSERT_CTX (ctx); 119 121 120 122 #if defined(_WIN32) || defined(_WIN64) … … 135 137 */ 136 138 int 137 CF_Mutex_Unlock ( CF_Mutex_Ctx ctx)139 CF_Mutex_Unlock (cf_ctx ctx) 138 140 { 139 CF_MUTEX_CONTEXT * context = ctx;141 CF_MUTEX_CONTEXT * context = (CF_MUTEX_CONTEXT *) ctx; 140 142 141 ASSERT_ MUTEX_CTX (ctx);143 ASSERT_CTX (ctx); 142 144 143 145 #if defined(_WIN32) || defined(_WIN64) -
trunk/src/cf_queue.c
r128 r151 8 8 #include "cf_queue.h" 9 9 #include "cf_list.h" 10 #include "cf_local.h" 10 11 #include "cf_error.h" 11 12 12 13 #include <stdio.h> 14 15 static int 16 CF_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 } 13 38 14 39 /** … … 20 45 */ 21 46 int 22 CF_Queue_Create Ctx (CF_Queue_Ctx * ctx)47 CF_Queue_Create (cf_ctx * ctx) 23 48 { 24 return CF_List_Create Ctx ((CF_List_Ctx *) ctx);49 return CF_List_Create ((cf_ctx *) ctx); 25 50 } 26 51 … … 33 58 */ 34 59 int 35 CF_Queue_Destroy Ctx (CF_Queue_Ctx ctx)60 CF_Queue_Destroy (cf_ctx ctx) 36 61 { 37 return CF_List_Destroy Ctx(ctx);62 return CF_List_Destroy (ctx); 38 63 } 39 64 … … 47 72 */ 48 73 int 49 CF_Queue_Put ( CF_Queue_Ctxctx,74 CF_Queue_Put (cf_ctx ctx, 50 75 const void * element) 51 76 { 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; 55 78 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); 61 80 } 62 81 … … 70 89 */ 71 90 int 72 CF_Queue_Get ( CF_Queue_Ctx ctx,73 void 91 CF_Queue_Get (cf_ctx ctx, 92 void ** element) 74 93 { 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); 88 95 } 89 96 … … 97 104 */ 98 105 int 99 CF_Queue_Front ( CF_Queue_Ctx ctx,100 void 106 CF_Queue_Front (cf_ctx ctx, 107 void ** element) 101 108 { 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); 111 110 } 112 111 … … 119 118 */ 120 119 int 121 CF_Queue_GetSize ( CF_Queue_Ctx ctx)120 CF_Queue_GetSize (cf_ctx ctx) 122 121 { 123 return CF_List_GetSize (( CF_List_Ctx) ctx);122 return CF_List_GetSize ((cf_ctx) ctx); 124 123 } -
trunk/src/cf_socket.c
r142 r151 9 9 #include "cf_local.h" 10 10 #include "cf_error.h" 11 #include "cf_util.h" 11 12 12 13 #include <stdio.h> … … 33 34 #endif 34 35 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 35 44 #define ASSERT_INIT() \ 36 45 if (!CF_Socket_IsInitialized ()) \ 46 return CF_ERROR_SOCKET_NOT_INITIALIZED 47 48 #define TRY_INIT() \ 49 if (CF_Socket_Initialize ()) \ 37 50 return CF_ERROR_SOCKET_NOT_INITIALIZED 38 51 … … 45 58 # define sa_family_t unsigned short 46 59 # define close(__sock) closesocket(__sock) 47 # define GET_SYSTEM_ERROR() WSAGetLastError ()48 60 # define ERROR_INTR WSAEINTR 49 61 #else 50 # define GET_SYSTEM_ERROR() errno51 62 # define ERROR_INTR EINTR 52 63 #endif 64 65 #define MAX_IP_LENGTH 256 66 67 typedef 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; 53 74 54 75 static CF_BOOL gInitialized = CF_FALSE; … … 64 85 int flags = fcntl (sock, F_GETFL, 0); 65 86 66 if (boolean) 67 flags |= O_NONBLOCK; 68 else 69 flags &= ~O_NONBLOCK; 87 if (boolean) flags |= O_NONBLOCK; 88 else flags &= ~O_NONBLOCK; 70 89 71 90 fcntl (sock, F_SETFL, flags); … … 74 93 75 94 static 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) 95 CF_Socket_Local_SetOption (const int sock, 96 const int optname, 97 const void * optval, 98 const size_t optlen) 220 99 { 221 100 int result = 0; … … 238 117 } 239 118 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) 119 static int 120 CF_Socket_Local_GetOption (const int sock, 121 const int optname, 122 void * optval, 123 size_t * optlen) 255 124 { 256 125 int result = 0; … … 273 142 } 274 143 144 static int 145 CF_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 158 static int 159 CF_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 169 static int 170 CF_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 */ 208 static CF_BOOL 209 CF_Socket_IsInitialized (void) 210 { 211 return gInitialized; 212 } 213 214 /** 215 * 소켓 초기화 216 * 217 * \return 성공 시, CF_OK; 실패 시, 오류 코드 218 */ 219 int 220 CF_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 */ 240 int 241 CF_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 */ 260 int 261 CF_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 */ 303 int 304 CF_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 275 326 /** 276 327 * 소켓 연결 277 328 * 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 */ 335 int 336 CF_Socket_Connect (cf_ctx ctx, 337 const char * ip, 285 338 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_TIMEOUT300 */301 int302 CF_Socket_ConnectTimeout (const char * ip,303 const unsigned short port,304 const int timeout)305 339 { 306 340 int result = 0; 307 341 int sock = 0; 342 int timeout = 0; 308 343 struct sockaddr_in address; 309 344 struct hostent * hostEnt; … … 315 350 fd_set writefds; 316 351 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; 323 362 324 363 /* 2. set data */ … … 390 429 retval = 0; 391 430 392 result = CF_Socket_GetOption ( sock, SO_ERROR, &retval, &length);431 result = CF_Socket_GetOption (ctx, SO_ERROR, &retval, &length); 393 432 if (result < 0) 394 433 { … … 410 449 CATCH_IF (result < 0) 411 450 { 412 CF_Socket_Close ( sock);451 CF_Socket_Close (ctx); 413 452 return result; 414 453 } … … 417 456 CF_Socket_Local_SetNonBlocking (sock, CF_FALSE); 418 457 419 return sock;458 return result; 420 459 } 421 460 … … 423 462 * 서버 열기 424 463 * 425 * \return 성공 시, 서버 소켓; 실패 시, 오류 코드 426 * 464 * \return 성공 시, CF_OK; 실패 시, 오류 코드 465 * 466 * \param ctx 소켓 컨텍스트 427 467 * \param port 서버 포트 428 468 * \param backlog listen 시의 backlog 수 429 469 */ 430 470 int 431 CF_Socket_Server (const unsigned short port, 471 CF_Socket_Server (cf_ctx ctx, 472 const unsigned short port, 432 473 const int backlog) 433 474 { … … 436 477 struct sockaddr_in address; 437 478 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; 443 486 444 487 address.sin_family = AF_INET; … … 462 505 } 463 506 507 snprintf (context->ip, sizeof (context->ip), 508 "%s", 509 inet_ntoa(address.sin_addr)); 510 464 511 result = listen (sock, backlog); 465 512 if (result < 0) … … 471 518 CATCH_IF (result < 0) 472 519 { 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 */ 534 int 535 CF_Socket_Accept (const cf_ctx ctx, 536 cf_ctx * client) 537 { 538 int result = 0; 491 539 struct sockaddr_in address; 492 540 socklen_t len = sizeof (address); 493 541 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); 498 559 return CF_ERROR_SOCKET_ACCEPT; 499 500 return sockClient; 560 } 561 562 *client = (cf_ctx) clntctx; 563 564 return CF_OK; 501 565 } 502 566 … … 506 570 * \return 성공 시, CF_OK; 실패 시, 오류 코드 507 571 * 508 * \param sock 소켓 509 * \param buf 송신할 데이터 572 * \param ctx 소켓 컨텍스트 * \param buf 송신할 데이터 510 573 * \param len 송신할 데이터의 길이 511 574 */ 512 575 int 513 CF_Socket_Send (const int sock,576 CF_Socket_Send (const cf_ctx ctx, 514 577 const void * buf, 515 578 const size_t len) 516 579 { 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_TIMEOUT531 */532 int533 CF_Socket_SendTimeout (const int sock,534 const void * buf,535 const size_t len,536 const int timeout)537 {538 580 int result = 0; 539 581 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); 543 588 if (result != len) 544 589 return CF_ERROR_SOCKET_SEND; … … 552 597 * \return 성공 시, 수신한 데이터의 길이; 실패 시, 오류 코드 553 598 * 554 * \param sock 소켓599 * \param ctx 소켓 컨텍스트 555 600 * \param buf 데이터를 수신할 버퍼 556 601 * \param len 데이터를 수신할 버퍼의 최대 크기 557 602 */ 558 603 int 559 CF_Socket_Recv (const int sock,604 CF_Socket_Recv (const cf_ctx ctx, 560 605 void * buf, 561 606 const size_t len) 562 607 { 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_TIMEOUT577 */578 int579 CF_Socket_RecvTimeout (const int sock,580 void * buf,581 const size_t len,582 const int timeout)583 {584 608 int result = 0; 585 609 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); 589 616 if (result < 0) 590 617 return result; 591 618 592 result = (int) recv ( sock, buf, len, 0);619 result = (int) recv (context->sock, buf, len, 0); 593 620 if (result < 0) 594 621 return CF_ERROR_SOCKET_RECV; … … 596 623 return result; 597 624 } 625 626 /** 627 * 소켓 옵션 설정 628 * 629 * \return 성공 시 CF_OK; 실패 시, 오류 코드 630 * 631 * \param ctx 소켓 컨텍스트 632 * \param optname 옵션 이름 633 * \param optval 설정할 옵션 값의 메모리 634 * \param optlen 설정할 옵션의 길이 635 */ 636 int 637 CF_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 */ 662 int 663 CF_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 */ 686 int 687 CF_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 */ 707 int 708 CF_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 */ 729 int 730 CF_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 8 8 #include "cf_stack.h" 9 9 #include "cf_list.h" 10 #include "cf_local.h" 10 11 #include "cf_error.h" 11 12 12 13 #include <stdio.h> 14 15 static int 16 CF_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 } 13 38 14 39 /** … … 20 45 */ 21 46 int 22 CF_Stack_Create Ctx (CF_Stack_Ctx * ctx)47 CF_Stack_Create (cf_ctx * ctx) 23 48 { 24 return CF_List_Create Ctx ((CF_List_Ctx *) ctx);49 return CF_List_Create ((cf_ctx *) ctx); 25 50 } 26 51 … … 33 58 */ 34 59 int 35 CF_Stack_Destroy Ctx (CF_Stack_Ctx ctx)60 CF_Stack_Destroy (cf_ctx ctx) 36 61 { 37 return CF_List_Destroy Ctx(ctx);62 return CF_List_Destroy (ctx); 38 63 } 39 64 … … 47 72 */ 48 73 int 49 CF_Stack_Push ( CF_Stack_Ctxctx,74 CF_Stack_Push (cf_ctx ctx, 50 75 const void * element) 51 76 { 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; 55 78 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); 61 80 } 62 81 … … 70 89 */ 71 90 int 72 CF_Stack_Pop ( CF_Stack_Ctx ctx,73 void 91 CF_Stack_Pop (cf_ctx ctx, 92 void ** element) 74 93 { 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); 88 95 } 89 96 … … 97 104 */ 98 105 int 99 CF_Stack_Top ( CF_Stack_Ctx ctx,100 void 106 CF_Stack_Top (cf_ctx ctx, 107 void ** element) 101 108 { 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); 111 110 } 112 111 … … 119 118 */ 120 119 int 121 CF_Stack_GetSize ( CF_Stack_Ctx ctx)120 CF_Stack_GetSize (cf_ctx ctx) 122 121 { 123 return CF_List_GetSize (( CF_List_Ctx) ctx);122 return CF_List_GetSize ((cf_ctx) ctx); 124 123 } -
trunk/src/cf_thread.c
r149 r151 7 7 */ 8 8 #include "cf_thread.h" 9 #include "cf_local.h" 9 10 #include "cf_error.h" 10 11 … … 24 25 #endif // #if defined(_WIN32) || defined(_WIN64) 25 26 26 #define ASSERT_ THREAD_CTX(__ctx) \27 #define ASSERT_CTX(__ctx) \ 27 28 if (__ctx == NULL) \ 28 29 return CF_ERROR_THREAD_INVALID_CTX … … 47 48 */ 48 49 int 49 CF_Thread_Create Ctx (CF_Thread_Ctx* ctx,50 51 void* arg)50 CF_Thread_Create (cf_ctx * ctx, 51 CF_Thread_Function callback, 52 void * arg) 52 53 { 53 54 CF_THREAD_CONTEXT * context = NULL; 54 55 55 context = (CF_THREAD_CONTEXT *) calloc (sizeof (CF_THREAD_CONTEXT), 1);56 context = NEWCTX (CF_THREAD_CONTEXT); 56 57 if (context == NULL) 57 58 return CF_ERROR_THREAD_CREATE_CTX; … … 60 61 context->arg = arg; 61 62 62 *ctx = ( CF_Thread_Ctx) context;63 *ctx = (cf_ctx) context; 63 64 64 65 return CF_OK; … … 74 75 * \remarks 75 76 * 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 /> 78 81 * <br /> 79 82 * 참고 url <br /> … … 82 85 */ 83 86 int 84 CF_Thread_Start ( CF_Thread_Ctx ctx)87 CF_Thread_Start (cf_ctx ctx) 85 88 { 86 89 int result = 0; … … 88 91 CF_THREAD_CONTEXT * context = (CF_THREAD_CONTEXT *) ctx; 89 92 90 ASSERT_ THREAD_CTX (ctx);93 ASSERT_CTX (ctx); 91 94 92 95 #if defined(_WIN32) || defined(_WIN64) … … 138 141 */ 139 142 int 140 CF_Thread_Destroy Ctx (CF_Thread_Ctx ctx)143 CF_Thread_Destroy (cf_ctx ctx) 141 144 { 142 145 CF_THREAD_CONTEXT * context = (CF_THREAD_CONTEXT *) ctx; 143 146 144 ASSERT_ THREAD_CTX (ctx);147 ASSERT_CTX (ctx); 145 148 146 149 #if defined(_WIN32) || defined(_WIN64) … … 163 166 */ 164 167 int 165 CF_Thread_Join ( CF_Thread_Ctx ctx)168 CF_Thread_Join (cf_ctx ctx) 166 169 { 167 170 CF_THREAD_CONTEXT * context = (CF_THREAD_CONTEXT *) ctx; … … 169 172 char status[16] = {0x00,}; 170 173 171 ASSERT_ THREAD_CTX (ctx);174 ASSERT_CTX (ctx); 172 175 173 176 #if defined(_WIN32) || defined(_WIN64) -
trunk/src/makefile
r146 r151 20 20 cf_stack \ 21 21 cf_bitwise \ 22 cf_util \ 22 23 23 24 TARGET_NAME = cf … … 46 47 # link 47 48 $(TARGET_PATH)/%.$(EXT_SHARED): $(OBJS) 48 $(LD) $(SHARED_FLAGS) -o $@ $ ? $(LIBS)49 $(LD) $(SHARED_FLAGS) -o $@ $(LIBS) $? 49 50 50 51 $(TARGET_PATH)/%.$(EXT_STATIC): $(OBJS) … … 53 54 # compile 54 55 $(OBJ_PATH)/%.o: %.$(SOURCE_TYPE) 55 $(CC) $(CFLAGS) $(CDEFS) $(INCLUDES) -o $@ -c$?56 $(CC) $(CFLAGS) -c $(CDEFS) $(INCLUDES) -o $@ $? 56 57 57 58 # doxygen 58 59 $(DOXYGEN_OUTPUT_PATH): 59 ifeq ($(DOXYGEN_CREATE), YES)60 ifeq ($(DOXYGEN_CREATE),yes) 60 61 ifeq ($(DOXYGEN),) 61 62 @echo "doxygen was not found." … … 69 70 clean: 70 71 rm -rf $(TARGET_PATH) $(OBJ_PATH) 71 ifeq ($(DOXYGEN_CREATE), YES)72 ifeq ($(DOXYGEN_CREATE),yes) 72 73 rm -rf $(DOXYGEN_OUTPUT_PATH) 73 74 endif -
trunk/test/codec.c
r96 r151 25 25 26 26 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"); 30 29 else 31 30 CF_DEBUG_PRINT_BIN (stderr, bin, length, "bin : %s\n", bin); -
trunk/test/debug.c
r135 r151 8 8 #include <string.h> 9 9 10 void callee3 (void) { 10 void callee3 (void) 11 { 11 12 CF_DEBUG_BEGIN_FUNCTION; 12 13 … … 18 19 } 19 20 20 void callee2 (void) { 21 void callee2 (void) 22 { 21 23 CF_DEBUG_BEGIN_FUNCTION; 22 24 … … 28 30 } 29 31 30 void callee1 (void) { 32 void callee1 (void) 33 { 31 34 CF_DEBUG_BEGIN_FUNCTION; 32 35 … … 38 41 } 39 42 40 int main (void) { 43 int main (void) 44 { 41 45 char data[] = 42 46 "【 曲名 : 사랑하고 있는데 】\n" -
trunk/test/file.c
r94 r151 6 6 #include <stdio.h> 7 7 8 int main (void) { 9 int fd = 0; 8 int main (void) 9 { 10 cf_ctx fd = 0; 11 int result = 0; 10 12 char *dir1 = "./dir1/test"; 11 13 char *dir2 = "dir2/test"; … … 21 23 } 22 24 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) { 25 27 // error 26 28 } … … 30 32 CF_File_Close (fd); 31 33 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) { 34 36 // error 35 37 } -
trunk/test/list.c
r118 r151 11 11 int result = 0; 12 12 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; 15 15 16 result = CF_List_Create Ctx (&ctx);16 result = CF_List_Create (&list); 17 17 if (result < 0) 18 18 CF_DEBUG_PRINT (stderr, "error %d\n", result); … … 20 20 for (iter = 0 ; iter < 10 ; iter++) 21 21 { 22 result = CF_List_Insert (ctx, trav, CF_DIRECTION_BEFORE, (void *)iter);22 result = CF_List_InsertBefore (list, trav, (void *)iter); 23 23 if (result < 0) 24 24 CF_DEBUG_PRINT (stderr, "error %d\n", result); 25 25 } 26 26 27 result = CF_List_Front ( ctx, &trav);27 result = CF_List_Front (list, &trav); 28 28 if (result < 0) 29 29 CF_DEBUG_PRINT (stderr, "error %d\n", result); … … 36 36 } 37 37 38 result = CF_List_Insert (ctx, trav, CF_DIRECTION_BEFORE, (void *)100);38 result = CF_List_InsertBefore (list, trav, (void *)100); 39 39 if (result < 0) 40 40 CF_DEBUG_PRINT (stderr, "error %d\n", result); 41 41 42 result = CF_List_Insert (ctx, trav, CF_DIRECTION_AFTER, (void *)200);42 result = CF_List_InsertAfter (list, trav, (void *)200); 43 43 if (result < 0) 44 44 CF_DEBUG_PRINT (stderr, "error %d\n", result); 45 45 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)) 47 47 { 48 result = CF_List_Get Element(trav, (void **)&element);48 result = CF_List_Get (trav, (void **)&element); 49 49 if (result < 0) 50 50 CF_DEBUG_PRINT (stderr, "error %d\n", result); … … 53 53 } 54 54 55 result = CF_List_Destroy Ctx (ctx);55 result = CF_List_Destroy (list); 56 56 if (result < 0) 57 57 CF_DEBUG_PRINT (stderr, "error %d\n", result); -
trunk/test/log.c
r148 r151 10 10 #include <stdio.h> 11 11 12 CF_Mutex_Ctx globalMutex; 12 #define COUNT 5 13 13 14 #define LOG_MT 1 14 cf_ctx globalLog; 15 cf_ctx globalMutex; 15 16 16 int test_log_mt(void * arg)17 int worker (void * arg) 17 18 { 18 19 static int cnt = 0; … … 32 33 for (i = 0 ; i < 10000 ; i++) 33 34 { 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); 35 36 } 36 37 … … 42 43 int main (void) 43 44 { 44 int i, j; 45 char idname[16] = {0x00,}; 46 CF_Thread_Ctx tid[10]; 45 int i, j; 46 cf_ctx tid[COUNT]; 47 47 48 48 /* 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++) 50 53 { 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); 73 57 } 74 58 75 59 /* 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) 77 61 CF_DEBUG_PRINT (stderr, "create log ctx error\n"); 78 62 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) { 83 64 // error 84 65 } 85 66 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++) 87 71 { 88 if (CF_Thread_Create Ctx (&tid[i], test_log_mt, &i) < 0)72 if (CF_Thread_Create (&tid[i], worker, &i) < 0) 89 73 { 90 74 CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", i); … … 93 77 } 94 78 95 for (i = 0 ; i < 10; i++)79 for (i = 0 ; i < COUNT ; i++) 96 80 { 97 81 if (CF_Thread_Start (tid[i]) < 0) … … 102 86 } 103 87 104 for (i = 0 ; i < 10; i++)88 for (i = 0 ; i < COUNT ; i++) 105 89 { 106 90 if (CF_Thread_Join (tid[i]) < 0) 107 91 CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i); 108 if (CF_Thread_Destroy Ctx(tid[i]) < 0)92 if (CF_Thread_Destroy (tid[i]) < 0) 109 93 CF_DEBUG_PRINT (stderr, "failed to release %dth thread\n", i); 110 94 } 111 95 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) { 113 100 // error 114 101 } … … 116 103 117 104 /* finalize */ 118 CF_Log_ Finalize ();105 CF_Log_Destroy (globalLog); 119 106 120 107 return 0; -
trunk/test/queue.c
r118 r151 6 6 #include "cf_debug.h" 7 7 8 #define COUNT 5 9 8 10 int main (void) 9 11 { … … 11 13 int result = 0; 12 14 long long int element = 0; 13 CF_Queue_Ctx ctx= NULL;15 cf_ctx queue = NULL; 14 16 15 17 // create 16 result = CF_Queue_Create Ctx (&ctx);18 result = CF_Queue_Create (&queue); 17 19 if (result < 0) 18 20 CF_DEBUG_PRINT (stderr, "error %d\n", result); 19 21 20 22 // insert 21 for (iter = 0 ; iter < 10; iter++)23 for (iter = 0 ; iter < COUNT ; iter++) 22 24 { 23 result = CF_Queue_Put ( ctx, (void *)iter);25 result = CF_Queue_Put (queue, (void *)iter); 24 26 if (result < 0) 25 27 CF_DEBUG_PRINT (stderr, "error %d\n", result); … … 27 29 28 30 // move next 29 for (iter = 0 ; iter < 10; iter++)31 for (iter = 0 ; iter < COUNT ; iter++) 30 32 { 31 result = CF_Queue_Front ( ctx, (void **)&element);33 result = CF_Queue_Front (queue, (void **)&element); 32 34 if (result < 0) 33 35 CF_DEBUG_PRINT (stderr, "error %d\n", result); … … 35 37 CF_DEBUG_PRINT (stderr, "front : %4d\n", element); 36 38 37 result = CF_Queue_Get ( ctx, (void **)&element);39 result = CF_Queue_Get (queue, (void **)&element); 38 40 if (result < 0) 39 41 CF_DEBUG_PRINT (stderr, "error %d\n", result); … … 45 47 46 48 // destroy 47 result = CF_Queue_Destroy Ctx (ctx);49 result = CF_Queue_Destroy (queue); 48 50 if (result < 0) 49 51 CF_DEBUG_PRINT (stderr, "error %d\n", result); -
trunk/test/socket.c
r122 r151 8 8 #include <string.h> 9 9 10 #define LOG_SOCKET 0 11 #define THREAD_POOL 1 10 #define THREAD_POOL 2 12 11 #define PORT 1234 13 14 int socket_echo_server (void * arg) 12 #define COUNT 2 13 14 cf_ctx gLog; 15 16 int server_worker (void * arg) 15 17 { 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; 18 21 int recvd = 0; 19 22 char buf[1024] = {0x00,}; … … 21 24 22 25 /*------------------------------------------------------------*/ 23 clntsock = CF_Socket_Accept (srvsock);24 if ( clntsock< 0)26 result = CF_Socket_Accept (srvsock, &clntsock); 27 if (result < 0) 25 28 { 26 29 CF_DEBUG_PRINT (stderr, "failed to accept on server\n"); 27 30 return -1; 28 31 } 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++) 32 35 { 33 36 if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0) … … 36 39 return -2; 37 40 } 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); 40 43 41 44 if (CF_Socket_Send (clntsock, buf, (size_t)recvd) < 0) … … 43 46 return -3; 44 47 } 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); 47 50 } 48 51 … … 53 56 } 54 57 55 int socket_echo_client(void * arg)58 int client_worker (void * arg) 56 59 { 57 int sock = 0; 60 cf_ctx sock = NULL; 61 int result = 0; 58 62 int recvd = 0; 59 63 char buf[1024] = {0x00,}; … … 63 67 64 68 /*------------------------------------------------------------*/ 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) 67 78 { 68 79 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++) 74 85 { 75 86 if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0) 76 87 { 77 88 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); 82 93 83 94 memset (buf, 0x00, sizeof (buf)); … … 86 97 { 87 98 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); 92 103 } 93 104 … … 100 111 int main () 101 112 { 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; 106 120 int iter = 0; 107 121 108 122 /*------------------------------------------------------------*/ 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"); 112 131 return -1; 113 132 } 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"); 123 139 return -2; 124 140 } 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); 128 143 if (sock < 0) 129 144 { … … 131 146 return -3; 132 147 } 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) 138 157 { 139 158 CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter); 140 159 return -4; 141 160 } 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_Create Ctx (&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) 148 167 { 149 168 CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter); 150 169 return -5; 151 170 } 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); 153 172 } 154 173 … … 174 193 { 175 194 CF_Thread_Join (ctid[iter]); 176 CF_Thread_Destroy Ctx(ctid[iter]);195 CF_Thread_Destroy (ctid[iter]); 177 196 178 197 CF_Thread_Join (stid[iter]); 179 CF_Thread_Destroy Ctx(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); 182 201 } 183 202 … … 186 205 CF_Socket_Finalize (); 187 206 188 CF_Log_Close (LOG_SOCKET); 189 190 CF_Log_Finalize (); 207 CF_Log_Destroy (gLog); 191 208 /*------------------------------------------------------------*/ 192 209 -
trunk/test/stack.c
r118 r151 6 6 #include "cf_debug.h" 7 7 8 #define COUNT 5 9 8 10 int main (void) 9 11 { … … 11 13 int result = 0; 12 14 long long int element = 0; 13 CF_Stack_Ctx ctx= NULL;15 cf_ctx stack = NULL; 14 16 15 17 // create 16 result = CF_Stack_Create Ctx (&ctx);18 result = CF_Stack_Create (&stack); 17 19 if (result < 0) 18 20 CF_DEBUG_PRINT (stderr, "error %d\n", result); 19 21 20 22 // insert 21 for (iter = 0 ; iter < 10; iter++)23 for (iter = 0 ; iter < COUNT ; iter++) 22 24 { 23 result = CF_Stack_Push ( ctx, (void *)iter);25 result = CF_Stack_Push (stack, (void *)iter); 24 26 if (result < 0) 25 27 CF_DEBUG_PRINT (stderr, "error %d\n", result); … … 27 29 28 30 // move next 29 for (iter = 0 ; iter < 10; iter++)31 for (iter = 0 ; iter < COUNT ; iter++) 30 32 { 31 result = CF_Stack_Top ( ctx, (void **)&element);33 result = CF_Stack_Top (stack, (void **)&element); 32 34 if (result < 0) 33 35 CF_DEBUG_PRINT (stderr, "error %d\n", result); … … 35 37 CF_DEBUG_PRINT (stderr, "top : %4d\n", element); 36 38 37 result = CF_Stack_Pop ( ctx, (void **)&element);39 result = CF_Stack_Pop (stack, (void **)&element); 38 40 if (result < 0) 39 41 CF_DEBUG_PRINT (stderr, "error %d\n", result); … … 45 47 46 48 // destroy 47 result = CF_Stack_Destroy Ctx (ctx);49 result = CF_Stack_Destroy (stack); 48 50 if (result < 0) 49 51 CF_DEBUG_PRINT (stderr, "error %d\n", result); -
trunk/test/thread.c
r148 r151 8 8 #include <stdio.h> 9 9 10 CF_Mutex_Ctx globalMutex; 10 #define COUNT 5 11 11 12 int worker (void * arg) { 12 cf_ctx globalMutex; 13 14 int worker (void * arg) 15 { 13 16 if (CF_Mutex_Lock (globalMutex) < 0) { // for critical section 14 17 // error … … 24 27 } 25 28 26 int main (void) { 27 CF_Thread_Ctx tid[10]; 28 int i = 0; 29 int main (void) 30 { 31 cf_ctx tid[COUNT]; 32 int i = 0; 29 33 30 if (CF_Mutex_Create Ctx(&globalMutex) < 0) {34 if (CF_Mutex_Create (&globalMutex) < 0) { 31 35 // error 32 36 } 33 37 34 for (i = 0 ; i < 10; i++) {35 if (CF_Thread_Create Ctx(&tid[i], worker, NULL) < 0) {38 for (i = 0 ; i < COUNT ; i++) { 39 if (CF_Thread_Create (&tid[i], worker, NULL) < 0) { 36 40 // error 37 41 } 38 42 } 39 43 40 for (i = 0 ; i < 10; i++) {44 for (i = 0 ; i < COUNT ; i++) { 41 45 if (CF_Thread_Start (tid[i]) < 0) { 42 46 // error … … 44 48 } 45 49 46 for (i = 0 ; i < 10; i++) {50 for (i = 0 ; i < COUNT ; i++) { 47 51 if (CF_Thread_Join (tid[i]) < 0) { // block 48 52 // error … … 50 54 } 51 55 52 for (i = 0 ; i < 10; i++) {53 if (CF_Thread_Destroy Ctx(tid[i]) < 0) {56 for (i = 0 ; i < COUNT ; i++) { 57 if (CF_Thread_Destroy (tid[i]) < 0) { 54 58 // error 55 59 } 56 60 } 57 61 58 if (CF_Mutex_Destory Ctx(globalMutex) < 0) {62 if (CF_Mutex_Destory (globalMutex) < 0) { 59 63 // error 60 64 }
Note:
See TracChangeset
for help on using the changeset viewer.