forked from UnitexGramLab/unitex-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractFst2Load.cpp
218 lines (178 loc) · 6.12 KB
/
AbstractFst2Load.cpp
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* Unitex
*
* Copyright (C) 2001-2018 Université Paris-Est Marne-la-Vallée <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*
*/
/*
* File created and contributed by Gilles Vollant (Ergonotics SAS)
* as part of an UNITEX optimization and reliability effort
*
* additional information: http://www.ergonotics.com/unitex-contribution/
* contact : [email protected]
*
*/
#include "Unicode.h"
#include "Fst2.h"
#include "AbstractFst2Load.h"
#include "AbstractFst2PlugCallback.h"
#include "Persistence.h"
#ifndef HAS_UNITEX_NAMESPACE
#define HAS_UNITEX_NAMESPACE 1
#endif
//namespace unitex {
using namespace unitex;
struct AbstractFst2Space {
t_persistent_fst2_func_array func_array;
void* privateSpacePtr;
} ;
struct List_AbstractFst2Space {
AbstractFst2Space afs;
List_AbstractFst2Space* next;
} ;
struct List_AbstractFst2Space* p_abstract_fst2_space_list=NULL;
UNITEX_FUNC int UNITEX_CALL AddAbstractFst2Space(const t_persistent_fst2_func_array* func_array,void* privateSpacePtr)
{
struct List_AbstractFst2Space* new_item;
new_item = (struct List_AbstractFst2Space*)malloc(sizeof(struct List_AbstractFst2Space));
if (new_item == NULL)
return 0;
new_item->afs.func_array = *func_array;
new_item->afs.privateSpacePtr = privateSpacePtr;
new_item->next = NULL;
if (p_abstract_fst2_space_list == NULL)
p_abstract_fst2_space_list = new_item;
else {
struct List_AbstractFst2Space* tmp = p_abstract_fst2_space_list;
while ((tmp->next) != NULL)
tmp = tmp->next;
tmp->next = new_item;
}
if ((new_item->afs.func_array.fnc_Init_Fst2Space) != NULL)
(*(new_item->afs.func_array.fnc_Init_Fst2Space))(new_item->afs.privateSpacePtr);
return 1;
}
UNITEX_FUNC int UNITEX_CALL RemoveAbstractFst2Space(const t_persistent_fst2_func_array* func_array,void* privateSpacePtr)
{
struct List_AbstractFst2Space* tmp = p_abstract_fst2_space_list;
struct List_AbstractFst2Space* tmp_previous = NULL;
while (tmp != NULL)
{
if ((memcmp(&tmp->afs.func_array,func_array,sizeof(t_persistent_fst2_func_array))==0) &&
(tmp->afs.privateSpacePtr == privateSpacePtr))
{
if (tmp_previous == NULL)
p_abstract_fst2_space_list = tmp->next;
else
tmp_previous->next = tmp->next;
if ((tmp->afs.func_array.fnc_Uninit_Fst2Space) != NULL)
(*(tmp->afs.func_array.fnc_Uninit_Fst2Space))(tmp->afs.privateSpacePtr);
free(tmp);
return 1;
}
tmp_previous = tmp;
tmp = tmp->next;
}
return 0;
}
UNITEX_FUNC int UNITEX_CALL GetNbAbstractFst2SpaceInstalled()
{
int count=0;
struct List_AbstractFst2Space* tmp = p_abstract_fst2_space_list;
while (tmp != NULL)
{
count++;
tmp = tmp->next;
}
return count;
}
const AbstractFst2Space * GetFst2SpaceForFileName(const char*name)
{
const struct List_AbstractFst2Space* tmp = p_abstract_fst2_space_list;
while (tmp != NULL)
{
const AbstractFst2Space * test_afs = &(tmp->afs);
if (tmp->afs.func_array.fnc_is_filename_object(name,tmp->afs.privateSpacePtr) != 0)
return test_afs;
tmp = tmp->next;
}
return NULL;
}
/*******************************/
Fst2* load_abstract_fst2(const VersatileEncodingConfig* vec,const char* filename,int read_names,struct FST2_free_info* p_fst2_free_info)
{
Fst2* res = NULL;
const AbstractFst2Space * pads = GetFst2SpaceForFileName(filename) ;
if (pads == NULL)
{
res = load_fst2(vec, filename, read_names);
if ((res != NULL) && (p_fst2_free_info != NULL))
{
p_fst2_free_info->must_be_free = 1;
p_fst2_free_info->func_free_fst2 = NULL;
p_fst2_free_info->private_ptr = NULL;
}
return res;
}
else
{
if (p_fst2_free_info != NULL)
{
p_fst2_free_info->must_be_free = 0;
p_fst2_free_info->private_ptr = NULL;
p_fst2_free_info->privateSpacePtr = pads->privateSpacePtr;
p_fst2_free_info->func_free_fst2 = (void*)(pads->func_array.fnc_free_abstract_fst2);
}
res = (*(pads->func_array.fnc_load_abstract_fst2))(vec, filename,read_names,p_fst2_free_info,pads->privateSpacePtr);
return res;
}
}
int is_abstract_fst2_filename(const char* filename)
{
return ((GetFst2SpaceForFileName(filename) == NULL) ? 0 : 1);
}
int is_abstract_or_persistent_fst2_filename(const char* filename)
{
if (GetFst2SpaceForFileName(filename))
return 1;
if (get_persistent_structure(filename))
return 1;
return 0;
}
void free_abstract_Fst2(Fst2* fst2,struct FST2_free_info* p_fst2_free_info)
{
if (fst2 != NULL)
{
if (p_fst2_free_info == NULL)
free_Fst2(fst2);
else
{
if (p_fst2_free_info->must_be_free != 0)
{
if (p_fst2_free_info->func_free_fst2 == NULL)
free_Fst2(fst2);
else
{
t_fnc_free_abstract_fst2 fnc_free_abstract_fst2 = (t_fnc_free_abstract_fst2)(p_fst2_free_info->func_free_fst2);
if (fnc_free_abstract_fst2 != NULL)
(*fnc_free_abstract_fst2)(fst2,p_fst2_free_info,p_fst2_free_info->privateSpacePtr);
}
}
}
}
}
//} // namespace unitex