summaryrefslogtreecommitdiffstats
path: root/branches/1.0/tinySAK/src/tsk_ragel_state.h
blob: 6adb5717c35f648fb593f69dd9670a25a008c866 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright (C) 2009-2010 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*	
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/

/**@file tsk_ragel_state.h
 * @brief Ragel state for SIP, HTTP and MSRP parsing.
 *
 * @author Mamadou Diop <diopmamadou(at)doubango.org>
 *
 * @date Created: Sat Nov 8 16:54:58 2009 mdiop
 */
#ifndef TINYSAK_RAGEL_STATE_H
#define TINYSAK_RAGEL_STATE_H

#include "tinysak_config.h"
#include "tsk_params.h"

#include <string.h>

TSK_BEGIN_DECLS


#if defined(_MSC_VER)
#	define atoi64	_atoi64
#else
#	define atoi64	atoll
#endif

/**@ingroup tsk_ragel_state_group
*/
#define TSK_SCANNER_SET_STRING(string) \
	if(!string) \
	{ \
		int len = (int)(te  - ts);  \
		if(len >0) \
		{ \
			string = tsk_calloc(len+1, sizeof(char)), memcpy(string, ts, len); \
		} \
	}

/**@ingroup tsk_ragel_state_group
*/
#define TSK_PARSER_SET_STRING(string) \
	{  \
		int len = (int)(p  - tag_start);  \
		if(len && tag_start){ \
			if(string){ \
				TSK_FREE(string); \
			} \
			string = tsk_calloc(len+1, sizeof(char)), memcpy(string, tag_start, len); \
		}  \
	}

/**@ingroup tsk_ragel_state_group
*/
#define TSK_SCANNER_SET_INTEGER(integer) \
	{ \
		int len = (int)(te  - ts); \
		if(len>=0) \
		{ \
			char* tmp = tsk_calloc(len+1, sizeof(char)); \
			memcpy(tmp, ts, len); \
			integer = atoi(tmp); \
			free(tmp); \
		} \
	}

/**@ingroup tsk_ragel_state_group
*/
#define TSK_PARSER_SET_INTEGER_EX(retval, type, func) \
	{ \
		int len = (int)(p  - tag_start); \
		if(len>=0) \
		{ \
			char* tmp = tsk_calloc(len+1, sizeof(char)); \
			memcpy(tmp, tag_start, len); \
			retval = (type) func(tmp); \
			free(tmp); \
		} \
	}
/**@ingroup tsk_ragel_state_group
* @def TSK_PARSER_SET_INTEGER
*/
/**@ingroup tsk_ragel_state_group
* @def TSK_PARSER_SET_INT
*/
/**@ingroup tsk_ragel_state_group
* @def TSK_PARSER_SET_UINT
*/
/**@ingroup tsk_ragel_state_group
* @def TSK_PARSER_SET_FLOAT
*/
/**@ingroup tsk_ragel_state_group
* @def TSK_PARSER_SET_DOUBLE
*/
#define TSK_PARSER_SET_INTEGER(retval) TSK_PARSER_SET_INTEGER_EX(retval, int, atoi)
#define TSK_PARSER_SET_INT(retval) TSK_PARSER_SET_INTEGER(retval)
#define TSK_PARSER_SET_UINT(retval) TSK_PARSER_SET_INTEGER_EX(retval, uint32_t, atoi64)
#define TSK_PARSER_SET_FLOAT(retval) TSK_PARSER_SET_INTEGER_EX(retval, float, atof)
#define TSK_PARSER_SET_DOUBLE(retval) TSK_PARSER_SET_INTEGER_EX(retval, double, atof)

/**@ingroup tsk_ragel_state_group
*/
#define TSK_PARSER_ADD_PARAM(dest) \
	{ \
		tsk_size_t len = (tsk_size_t)(p  - tag_start); \
		tsk_param_t *param = tsk_params_parse_param(tag_start, len); \
		if(param) \
		{ \
			if(!dest) dest = tsk_list_create(); \
			tsk_list_push_back_data(dest, ((void**) &param)); \
		} \
	}

/**@ingroup tsk_ragel_state_group
*/
#define TSK_SACANNER_ADD_PARAM(dest) \
	{ \
		int len = (int)(te  - ts); \
		if(len >0) \
		{ \
			tsk_param_t *param = tsk_params_parse_param(ts, len); \
			if(param) \
			{ \
				if(!dest) dest = tsk_list_create(); \
				tsk_list_push_back_data(dest, ((void**) &param)); \
			} \
		} \
	}

/**@ingroup tsk_ragel_state_group
*/
#define TSK_PARSER_ADD_STRING(dest) \
	{ \
		tsk_size_t len = (tsk_size_t)(p  - tag_start); \
		tsk_string_t *string = tsk_string_create(tsk_null); \
		string->value = tsk_calloc(len+1, sizeof(char)), memcpy(string->value, tag_start, len); \
		if(!dest)  \
		{  \
			dest = tsk_list_create(); \
		} \
		tsk_list_push_back_data(dest, ((void**) &string)); \
	}

/**@ingroup tsk_ragel_state_group
* Ragel state.
*/
typedef struct tsk_ragel_state_s
{
	int cs; /**< Ragel current state. */
	const char *p; /**< Data pointing to the buffer to parse. */
	const char *pe; /**< Data end pointer. */
	const char *eof; /**< End of the file (in our cas data) pointer. */
		
	const char* tag_start; /**< Last tag start position set by ragel machine. */
	const char* tag_end; /**< The end of the ragel tag. */
}
tsk_ragel_state_t;


TINYSAK_API void tsk_ragel_state_init(tsk_ragel_state_t *state, const char *data, tsk_size_t size);

TSK_END_DECLS

#endif /* TINYSAK_RAGEL_STATE_H */

OpenPOWER on IntegriCloud