diff --git a/HISTORY b/HISTORY index 2dbf9b0..87e4846 100644 --- a/HISTORY +++ b/HISTORY @@ -1,9 +1,9 @@ -Version 1.31 2016-11-13 +Version 1.31 2016-11-25 * move SET_SOCKOPT_NOSIGPIPE from sockopt.c to sockopt.h * add function get_time_item_from_str * add file trylock functions - * logger context add field: use_file_write_lock + * add files: char_converter.h and char_converter.c Version 1.30 2016-10-31 * modify php-fastcommon/test.php diff --git a/src/Makefile.in b/src/Makefile.in index 9711902..50d3849 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -11,7 +11,8 @@ FAST_SHARED_OBJS = hash.lo chain.lo shared_func.lo ini_file_reader.lo \ fast_timer.lo process_ctrl.lo fast_mblock.lo \ connection_pool.lo fast_mpool.lo fast_allocator.lo \ fast_buffer.lo multi_skiplist.lo flat_skiplist.lo \ - system_info.lo fast_blocked_queue.lo id_generator.lo + system_info.lo fast_blocked_queue.lo id_generator.lo \ + char_converter.lo FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \ logger.o sockopt.o base64.o sched_thread.o \ @@ -20,7 +21,8 @@ FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \ fast_timer.o process_ctrl.o fast_mblock.o \ connection_pool.o fast_mpool.o fast_allocator.o \ fast_buffer.o multi_skiplist.o flat_skiplist.o \ - system_info.o fast_blocked_queue.o id_generator.o + system_info.o fast_blocked_queue.o id_generator.o \ + char_converter.o HEADER_FILES = common_define.h hash.h chain.h logger.h base64.h \ shared_func.h pthread_func.h ini_file_reader.h _os_define.h \ @@ -30,7 +32,7 @@ HEADER_FILES = common_define.h hash.h chain.h logger.h base64.h \ connection_pool.h fast_mpool.h fast_allocator.h \ fast_buffer.h skiplist.h multi_skiplist.h flat_skiplist.h \ skiplist_common.h system_info.h fast_blocked_queue.h \ - php7_ext_wrapper.h id_generator.h + php7_ext_wrapper.h id_generator.h char_converter.h ALL_OBJS = $(FAST_STATIC_OBJS) $(FAST_SHARED_OBJS) diff --git a/src/char_converter.c b/src/char_converter.c new file mode 100644 index 0000000..ba33f8f --- /dev/null +++ b/src/char_converter.c @@ -0,0 +1,87 @@ +/** +* Copyright (C) 2008 Happy Fish / YuQing +* +* FastDFS may be copied only under the terms of the GNU General +* Public License V3, which may be found in the FastDFS source kit. +* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. +**/ + +#include +#include +#include +#include +#include +#include "logger.h" +#include "shared_func.h" +#include "char_converter.h" + +int char_converter_init(FastCharConverter *pCharConverter, + const FastCharPair *charPairs, const int count) +{ + int i; + unsigned char src; + if (count > FAST_MAX_CHAR_COUNT) + { + logError("file: "__FILE__", line: %d, " + "count: %d is too large, exceeds %d!", __LINE__, + count, FAST_MAX_CHAR_COUNT); + return EINVAL; + } + + pCharConverter->count = count; + for (i=0; ichar_table[src] = charPairs[i].dest; + } + return 0; +} + +int std_space_char_converter_init(FastCharConverter *pCharConverter, + const unsigned char dest_base) +{ +#define SPACE_CHAR_PAIR_COUNT 7 + int i; + FastCharPair pairs[SPACE_CHAR_PAIR_COUNT]; + + pairs[0].src = '\0'; + pairs[1].src = '\t'; + pairs[2].src = '\n'; + pairs[3].src = '\v'; + pairs[4].src = '\f'; + pairs[5].src = '\r'; + pairs[6].src = ' '; + + for (i=0; icount <= 0) + { + return 0; + } + + count = 0; + end = (unsigned char *)text + text_len; + for (p=(unsigned char *)text; pchar_table[*p] != 0) + { + *p = pCharConverter->char_table[*p]; + ++count; + } + } + + return count; +} + diff --git a/src/char_converter.h b/src/char_converter.h new file mode 100644 index 0000000..770955c --- /dev/null +++ b/src/char_converter.h @@ -0,0 +1,80 @@ +/** +* Copyright (C) 2008 Happy Fish / YuQing +* +* FastDFS may be copied only under the terms of the GNU General +* Public License V3, which may be found in the FastDFS source kit. +* Please visit the FastDFS Home Page http://www.csource.org/ for more detail. +**/ + +//char_converter.h +#ifndef CHAR_CONVERTER_H +#define CHAR_CONVERTER_H + +#include +#include +#include "common_define.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define FAST_MAX_CHAR_COUNT 256 + +typedef struct fast_char_pair +{ + unsigned char src; + unsigned char dest; +} FastCharPair; + +typedef struct fast_char_converter +{ + /* + * char pairs count + * */ + int count; + + /* + * char table to convert + * */ + unsigned char char_table[FAST_MAX_CHAR_COUNT]; +} FastCharConverter; + +/** + * char converter init function + * parameters: + * pCharConverter: the char converter + * charPairs: the char pairs + * count: the count of char pairs + * return: 0 for success, != 0 fail +*/ +int char_converter_init(FastCharConverter *pCharConverter, + const FastCharPair *charPairs, const int count); + +/** + * standard space chars to convert + * parameters: + * pCharConverter: the char converter + * dest_base: the dest base char + * return: 0 for success, != 0 fail +*/ +int std_space_char_converter_init(FastCharConverter *pCharConverter, + const unsigned char dest_base); + +/** + * char convert function + * parameters: + * pCharConverter: the char converter + * text: the text to convert + * text_len: the length of text + * return: converted char count +*/ +int fast_char_convert(FastCharConverter *pCharConverter, + char *text, const int text_len); + + +#ifdef __cplusplus +} +#endif + +#endif +