1 /**
2 * Copyright (C) 2008 Happy Fish / YuQing
3 *
4 * FastDFS may be copied only under the terms of the GNU General
5 * Public License V3, which may be found in the FastDFS source kit.
6 * Please visit the FastDFS Home Page http://www.fastken.com/ for more detail.
7 **/
8
9 //char_converter.h
10 #ifndef CHAR_CONVERTER_H
11 #define CHAR_CONVERTER_H
12
13 #include <syslog.h>
14 #include <sys/time.h>
15 #include "common_define.h"
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 #define FAST_MAX_CHAR_COUNT 256
22
23 #define FAST_CHAR_OP_NONE 0
24 #define FAST_CHAR_OP_ADD_BACKSLASH 1
25 #define FAST_CHAR_OP_NO_BACKSLASH 2
26
27 typedef struct fast_char_pair
28 {
29 unsigned char src;
30 unsigned char dest;
31 } FastCharPair;
32
33 typedef struct fast_char_target
34 {
35 unsigned char op;
36 unsigned char dest;
37 } FastCharTarget;
38
39 typedef struct fast_char_converter
40 {
41 /*
42 * char pairs count
43 * */
44 int count;
45
46 /*
47 * char table to convert
48 * */
49 FastCharTarget char_table[FAST_MAX_CHAR_COUNT];
50 } FastCharConverter;
51
52 /**
53 * char converter init function
54 * parameters:
55 * pCharConverter: the char converter
56 * charPairs: the char pairs
57 * count: the count of char pairs
58 * op: the operator type
59 * return: 0 for success, != 0 fail
60 */
61 int char_converter_init_ex(FastCharConverter *pCharConverter,
62 const FastCharPair *charPairs, const int count,
63 const unsigned op);
64
65 /**
66 * char converter init function
67 * parameters:
68 * pCharConverter: the char converter
69 * charPairs: the char pairs
70 * count: the count of char pairs
71 * return: 0 for success, != 0 fail
72 */
char_converter_init(FastCharConverter * pCharConverter,const FastCharPair * charPairs,const int count)73 static inline int char_converter_init(FastCharConverter *pCharConverter,
74 const FastCharPair *charPairs, const int count)
75 {
76 return char_converter_init_ex(pCharConverter, charPairs, count,
77 FAST_CHAR_OP_NO_BACKSLASH);
78 }
79
80 /**
81 * standard space chars to convert
82 * parameters:
83 * pCharConverter: the char converter
84 * dest_base: the dest base char
85 * return: 0 for success, != 0 fail
86 */
87 int std_space_char_converter_init(FastCharConverter *pCharConverter,
88 const unsigned char dest_base);
89
90 /**
91 * standard space chars init to add backslash
92 * parameters:
93 * pCharConverter: the char converter
94 * return: 0 for success, != 0 fail
95 */
96 int std_spaces_add_backslash_converter_init(FastCharConverter *pCharConverter);
97
98 /**
99 * set char pair to converter
100 * parameters:
101 * pCharConverter: the char converter
102 * src: the src char
103 * dest: the dest char
104 * return: none
105 */
106 void char_converter_set_pair(FastCharConverter *pCharConverter,
107 const unsigned char src, const unsigned char dest);
108
109 /**
110 * set char pair to converter
111 * parameters:
112 * pCharConverter: the char converter
113 * src: the src char
114 * op: the operator type
115 * dest: the dest char
116 * return: none
117 */
118 void char_converter_set_pair_ex(FastCharConverter *pCharConverter,
119 const unsigned char src, const unsigned op, const unsigned char dest);
120
121 /**
122 * char convert function
123 * parameters:
124 * pCharConverter: the char converter
125 * input: the input to convert
126 * input_len: the length of input
127 * output: the input to convert
128 * out_len: the length of output
129 * out_size: output buff size
130 * return: converted char count
131 */
132 int fast_char_convert(FastCharConverter *pCharConverter,
133 const char *input, const int input_len,
134 char *output, int *out_len, const int out_size);
135
136 #ifdef __cplusplus
137 }
138 #endif
139
140 #endif
141
142