From 33085e9bf6a80f3a8c22826f58ab7779f60c69c0 Mon Sep 17 00:00:00 2001 From: yuqing Date: Sat, 26 Nov 2016 12:20:43 +0800 Subject: [PATCH] add files: char_convert_loader.h and char_convert_loader.c --- HISTORY | 3 +- src/Makefile.in | 7 +- src/char_convert_loader.c | 145 ++++++++++++++++++++++++++++++++++++++ src/char_convert_loader.h | 61 ++++++++++++++++ src/char_converter.c | 13 +++- 5 files changed, 223 insertions(+), 6 deletions(-) create mode 100644 src/char_convert_loader.c create mode 100644 src/char_convert_loader.h diff --git a/HISTORY b/HISTORY index 87e4846..3242bcc 100644 --- a/HISTORY +++ b/HISTORY @@ -1,9 +1,10 @@ -Version 1.31 2016-11-25 +Version 1.31 2016-11-26 * move SET_SOCKOPT_NOSIGPIPE from sockopt.c to sockopt.h * add function get_time_item_from_str * add file trylock functions * add files: char_converter.h and char_converter.c + * add files: char_convert_loader.h and char_convert_loader.c Version 1.30 2016-10-31 * modify php-fastcommon/test.php diff --git a/src/Makefile.in b/src/Makefile.in index 50d3849..2fbf27c 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -12,7 +12,7 @@ FAST_SHARED_OBJS = hash.lo chain.lo shared_func.lo ini_file_reader.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 \ - char_converter.lo + char_converter.lo char_convert_loader.lo FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.o \ logger.o sockopt.o base64.o sched_thread.o \ @@ -22,7 +22,7 @@ FAST_STATIC_OBJS = hash.o chain.o shared_func.o ini_file_reader.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 \ - char_converter.o + char_converter.o char_convert_loader.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 \ @@ -32,7 +32,8 @@ 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 char_converter.h + php7_ext_wrapper.h id_generator.h char_converter.h \ + char_convert_loader.h ALL_OBJS = $(FAST_STATIC_OBJS) $(FAST_SHARED_OBJS) diff --git a/src/char_convert_loader.c b/src/char_convert_loader.c new file mode 100644 index 0000000..69b8c1b --- /dev/null +++ b/src/char_convert_loader.c @@ -0,0 +1,145 @@ +/** +* 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 +#include "logger.h" +#include "shared_func.h" +#include "char_convert_loader.h" + +int char_convert_loader_init(FastCharConverter *pCharConverter, + const IniItem *items, const int count) +{ + const IniItem *pItem; + const IniItem *pEnd; + int result; + + char_convert_loader_init(pCharConverter, NULL, 0); + pEnd = items + count; + for (pItem=items; pItemname, pItem->value); + if (result != 0) { + return result; + } + } + return 0; +} + +static int char_convert_loader_parse(const char *s, unsigned char *out_char) +{ + int len; + len = strlen(s); + if (len == 1) { + *out_char = *s; + return 0; + } + + if (*s != '\\') { + logError("file: "__FILE__", line: %d, " + "invalid char string: %s", __LINE__, s); + return EINVAL; + } + + if (len == 2) { + switch (s[1]) { + case '0': + *out_char = '\0'; + break; + case 'a': + *out_char = '\a'; + break; + case 'b': + *out_char = '\b'; + break; + case 't': + *out_char = '\t'; + break; + case 'n': + *out_char = '\n'; + break; + case 'v': + *out_char = '\v'; + break; + case 'f': + *out_char = '\f'; + break; + case 'r': + *out_char = '\r'; + break; + case 's': + *out_char = ' '; + break; + default: + logError("file: "__FILE__", line: %d, " + "invalid char string: %s", __LINE__, s); + return EINVAL; + } + return 0; + } + + if (len != 4 || s[1] != 'x' || !isxdigit(s[2]) || !isxdigit(s[3])) { + logError("file: "__FILE__", line: %d, " + "invalid char string: %s, correct format: \\x##, " + "## for hex digital. eg. \\x20 for the SPACE char", + __LINE__, s); + return EINVAL; + } + + *out_char = (unsigned char)strtol(s+2, NULL, 16); + return 0; +} + +int char_convert_loader_set_pair(FastCharConverter *pCharConverter, + const char *src, const char *dest) +{ + unsigned char src_char; + unsigned char dest_char; + int result; + + if (src == NULL || *src == '\0') { + logError("file: "__FILE__", line: %d, " + "empty src string", __LINE__); + return EINVAL; + } + if (dest == NULL || *dest == '\0') { + logError("file: "__FILE__", line: %d, " + "empty dest string, src string: %s", + __LINE__, src); + return EINVAL; + } + + if ((result=char_convert_loader_parse(src, &src_char)) != 0) { + return result; + } + + if (*dest == '"') { + if (strlen(dest) != 4 || dest[1] != '\\' || dest[3] != '"') { + logError("file: "__FILE__", line: %d, " + "invalid dest string: %s, correct format: \"\\c\", " + "eg. \"\\t\"", __LINE__, src); + return EINVAL; + } + dest_char = dest[2]; + char_converter_set_pair_ex(pCharConverter, + src_char, FAST_CHAR_OP_ADD_BACKSLASH, dest_char); + return 0; + } + + if ((result=char_convert_loader_parse(dest, &dest_char)) != 0) { + return result; + } + char_converter_set_pair_ex(pCharConverter, + src_char, FAST_CHAR_OP_NO_BACKSLASH, dest_char); + return 0; +} + diff --git a/src/char_convert_loader.h b/src/char_convert_loader.h new file mode 100644 index 0000000..b4900c1 --- /dev/null +++ b/src/char_convert_loader.h @@ -0,0 +1,61 @@ +/** +* 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_convert_loader.h +#ifndef CHAR_CONVERT_LOADER_H +#define CHAR_CONVERT_LOADER_H + +#include +#include +#include "common_define.h" +#include "ini_file_reader.h" +#include "char_converter.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * char converter init function + * parameters: + * pCharConverter: the char converter + * items: the char key value pairs + * count: the count of kv pairs + * return: 0 for success, != 0 fail +*/ +int char_convert_loader_init(FastCharConverter *pCharConverter, + const IniItem *items, const int count); + +/** + * set char src and dest pair + * parameters: + * pCharConverter: the char converter + * src: the src string to parse + * dest: the dest string to parse + * + * Note: + * src and dest can be ASCII code as \x##, ## for hex digital, + * such as \x20 for the SPACE char + * + * dest can be a printable char, ASCII code as \x##, + * or quoted two chars as backslash follow by a char, such as "\t" + * + * extended backslash char pairs: + * \0 for the ASCII 0 character + * \s for the SPACE character + * return: 0 for success, != 0 fail +*/ +int char_convert_loader_set_pair(FastCharConverter *pCharConverter, + const char *src, const char *dest); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/src/char_converter.c b/src/char_converter.c index 111acdc..b60116c 100644 --- a/src/char_converter.c +++ b/src/char_converter.c @@ -70,6 +70,16 @@ void char_converter_set_pair(FastCharConverter *pCharConverter, void char_converter_set_pair_ex(FastCharConverter *pCharConverter, const unsigned char src, const unsigned op, const unsigned char dest) { + if (op == FAST_CHAR_OP_NONE) { + if (pCharConverter->char_table[src].op != FAST_CHAR_OP_NONE) { + --pCharConverter->count; + } + } else { + if (pCharConverter->char_table[src].op == FAST_CHAR_OP_NONE) { + ++pCharConverter->count; + } + } + pCharConverter->char_table[src].op = op; pCharConverter->char_table[src].dest = dest; } @@ -86,8 +96,7 @@ int fast_char_convert(FastCharConverter *pCharConverter, int max_size_sub1; int remain_len; - if (pCharConverter->count <= 0) - { + if (pCharConverter->count <= 0) { return 0; }