forked from varnamproject/libvarnam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.c
151 lines (128 loc) · 3.68 KB
/
token.c
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
/* Methods related to token creation and pooling
*
* Copyright (C) Navaneeth.K.N
*
* This is part of libvarnam. See LICENSE.txt for the license
*/
#include <string.h>
#include "token.h"
#include "vtypes.h"
#include "util.h"
#include "varray.h"
#include "result-codes.h"
void
initialize_token (vtoken *tok,
int id,
int type,
int match_type,
const char* pattern,
const char* value1,
const char* value2,
const char* value3,
const char* tag,
int priority,
int accept_condition,
int flags)
{
tok->id = id;
tok->type = type;
tok->match_type = match_type;
tok->priority = priority;
tok->accept_condition = accept_condition;
tok->flags = flags;
strncpy( tok->pattern, pattern, VARNAM_SYMBOL_MAX);
strncpy( tok->value1, value1, VARNAM_SYMBOL_MAX);
if (value2 != NULL)
strncpy( tok->value2, value2, VARNAM_SYMBOL_MAX);
else
tok->value2[0] = '\0';
if (value3 != NULL)
strncpy( tok->value3, value3, VARNAM_SYMBOL_MAX);
else
tok->value3[0] = '\0';
if (tag != NULL)
strncpy( tok->tag, tag, VARNAM_SYMBOL_MAX);
else
tok->tag[0] = '\0';
}
struct token*
Token(int id, int type, int match_type, const char* pattern, const char* value1,
const char* value2, const char* value3, const char* tag, int priority, int accept_condition, int flags)
{
struct token* tok = (struct token*) xmalloc(sizeof(struct token));
initialize_token (tok, id, type, match_type, pattern, value1, value2, value3, tag, priority, accept_condition, flags);
return tok;
}
struct token*
token_new()
{
struct token* tok = (struct token*) xmalloc(sizeof(struct token));
return tok;
}
struct token*
get_pooled_token (
varnam *handle,
int id,
int type,
int match_type,
const char* pattern,
const char* value1,
const char* value2,
const char* value3,
const char* tag,
int priority,
int accept_condition,
int flags)
{
vtoken *tok;
if (v_->tokens_pool == NULL)
v_->tokens_pool = vpool_init ();
tok = vpool_get (v_->tokens_pool);
if (tok == NULL)
{
tok = Token (id, type, match_type, pattern, value1, value2, value3, tag, priority, accept_condition, flags);
vpool_add (v_->tokens_pool, tok);
}
else
initialize_token (tok, id, type, match_type, pattern, value1, value2, value3, tag, priority, accept_condition, flags);
return tok;
}
varray*
product_tokens(varnam *handle, varray *tokens)
{
int array_cnt, *offsets, i, last_array_offset;
varray *product, *array, *tmp;
array_cnt = varray_length (tokens);
offsets = xmalloc(sizeof(int) * (size_t) array_cnt);
product = get_pooled_array (handle);
varray_clear (product);
for (i = 0; i < array_cnt; i++) offsets[i] = 0;
for (;;)
{
array = get_pooled_array (handle);
for (i = 0; i < array_cnt; i++) {
tmp = varray_get (tokens, i);
varray_push (array, varray_get (tmp, offsets[i]));
}
varray_push (product, array);
last_array_offset = array_cnt - 1;
offsets[last_array_offset]++;
while (offsets[last_array_offset] == varray_length ((varray*) varray_get (tokens, last_array_offset)))
{
offsets[last_array_offset] = 0;
if (--last_array_offset < 0) goto finished;
offsets[last_array_offset]++;
}
}
finished:
xfree (offsets);
return product;
}
void
destroy_token(void *token)
{
if (token != NULL)
{
xfree((vtoken*) token);
}
}