add files: char_converter.h and char_converter.c

pull/10/head
yuqing 2016-11-25 09:02:03 +08:00
parent 100ae31704
commit e2735189a5
4 changed files with 174 additions and 5 deletions

View File

@ -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

View File

@ -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)

87
src/char_converter.c Normal file
View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#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; i<count; i++)
{
src = charPairs[i].src;
pCharConverter->char_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; i<SPACE_CHAR_PAIR_COUNT; i++) {
pairs[i].dest = dest_base + i;
}
return char_converter_init(pCharConverter, pairs, SPACE_CHAR_PAIR_COUNT);
}
int fast_char_convert(FastCharConverter *pCharConverter,
char *text, const int text_len)
{
int count;
unsigned char *p;
unsigned char *end;
if (pCharConverter->count <= 0)
{
return 0;
}
count = 0;
end = (unsigned char *)text + text_len;
for (p=(unsigned char *)text; p<end; p++)
{
if (pCharConverter->char_table[*p] != 0)
{
*p = pCharConverter->char_table[*p];
++count;
}
}
return count;
}

80
src/char_converter.h Normal file
View File

@ -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 <syslog.h>
#include <sys/time.h>
#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