LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
FsaAccessHeader.cpp
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/***************************************************************************
7 FsaAccessHeader.cpp - description
8 -------------------
9 begin : mon nov 7 2005
10 copyright : (C) 2003-2005 by CEA
11 email : olivier.mesnard@cea.fr
12 ***************************************************************************/
13
14/***************************************************************************
15 * *
16 * header for files *
17 * compact dictionnary based on finite state automata *
18 * implemented with Boost Graph library *
19 * *
20 ***************************************************************************/
21
22 // pour la definitin de one_byte, two_bytes, four_bytes
23#include "FsaAccessHeader.h"
26#include <sstream>
27#include <string.h>
28
29
30namespace Lima {
31namespace Common {
32namespace FsaAccess {
33
35 friend class FsaAccessHeader;
36
37 FsaAccessHeaderPrivate(bool trie_direction_fwd);
39
40 uint16_t m_majorVersion;
41 uint16_t m_minorVersion;
42 uint8_t m_charType;
43 uint8_t m_packing;
44 uint8_t m_charOrder;
45 uint64_t m_nbVertices;
46 uint64_t m_nbEdges;
47// uint64_t m_maxSimpleId;
48// uint64_t m_compoundsDataOffset;
49// uint64_t m_nbCompounds;
58 bool m_trie_direction_fwd;
59// long m_compoundsDataPos;
60};
61
62FsaAccessHeaderPrivate::FsaAccessHeaderPrivate(bool trie_direction_fwd)
63 : m_majorVersion( MAJOR_VERSION_16 ),
64 m_minorVersion( MINOR_VERSION_16 ),
65 m_packing(BUILDER),
66 m_charOrder(FORWARD),
67 m_nbVertices(0),
68 m_nbEdges(0),
69// m_maxSimpleId(MAX_SIMPLE_TID),
70// m_compoundsDataOffset(0)
71// m_nbCompounds(0),
72 m_trie_direction_fwd( trie_direction_fwd )
73{
74 if( sizeof(LimaChar) == 2 ) {
75 m_charType = 2;
76 }
77 else {
78 std::string mess = "FsaAccessBuilder::FsaAccessHeader: sizeof(LimaChar) must be 2!!!" ;
79#ifdef DEBUG_CD
81 LERROR << mess.c_str();
82#endif
83 throw( FsaNotSaved( mess ) );
84 }
85}
86
87FsaAccessHeaderPrivate::~FsaAccessHeaderPrivate()
88{
89}
90
91FsaAccessHeader::FsaAccessHeader(bool trie_direction_fwd) :
92 m_d(new FsaAccessHeaderPrivate(trie_direction_fwd))
93{
94}
95
100
105
107
108bool FsaAccessHeader::getTrieDirectionForward() const { return m_d->m_trie_direction_fwd; }
109uint64_t FsaAccessHeader::getNbVertices() const { return m_d->m_nbVertices; }
110uint64_t FsaAccessHeader::getNbEdges() const { return m_d->m_nbEdges; }
111void FsaAccessHeader::setPackingStatus(uint8_t packingStatus) { m_d->m_packing = packingStatus; }
112void FsaAccessHeader::setNbVertices(uint64_t nbVerts) { m_d->m_nbVertices = nbVerts; }
113void FsaAccessHeader::setNbEdges(uint64_t nbEdges) { m_d->m_nbEdges = nbEdges; }
114// uint64_t getMaxSimpleId() const { return m_maxSimpleId; }
115// uint64_t int getCompoundsDataOffset() const { return m_compoundsDataOffset; }
116// uint64_t getNbCompounds() const { return m_nbCompounds; }
117// void setStreamPos(std::ifstream& is);
118// const long& getStreamPos() { return m_compoundsDataPos; }
119
121#ifdef DEBUG_CD
123 LDEBUG << "FsaAccessHeader::read()";
124#endif
125
126 char headBuf[HEADER_SIZE+1];
127 iw.readData( headBuf, HEADER_SIZE );
128 char *dstptr = headBuf;
129
130 char magicNumber[13];
131 strncpy(magicNumber, dstptr, 12 );
132 dstptr += 12;
133 magicNumber[sizeof(magicNumber)-1] = '\0';
134 if( strcmp(magicNumber,MAGIC_NUMBER) ) {
135 std::string mess = "FsaAccessHeader::read: bad magic number '"
136 + std::string(magicNumber) + "'" ;
137#ifdef DEBUG_CD
138 LERROR << mess.c_str();
139#endif
140 throw( AccessByStringNotInitialized(mess) );
141 }
142
143 char numBuf[6];
144 strncpy(numBuf, dstptr, 5 );
145 dstptr += 5;
146 numBuf[sizeof(numBuf)-1] = '\0';
147 sscanf(numBuf, "%hu.%hu", &m_d->m_majorVersion, &m_d->m_minorVersion);
148#ifdef DEBUG_CD
149 LDEBUG << "FsaAccessHeader::read: m_majorVersion = "
150 << m_d->m_majorVersion << " m_minorVersion = " << m_d->m_minorVersion
151 ;
152#endif
153 uint16_t majorVersion = MAJOR_VERSION_16;
154// uint16_t minorVersion = MINOR_VERSION_16;
155 if( m_d->m_majorVersion > majorVersion) {
156/*
157 if( (m_majorVersion > majorVersion)
158 ||((m_majorVersion == majorVersion)
159 && (m_minorVersion > minorVersion) ) ) {
160*/
161 std::string mess = "FsaAccessHeader::read: bad version = "
162 + std::string(numBuf)
163 + ". Get a release of your library or convert your file !!" ;
164#ifdef DEBUG_CD
165 LERROR << mess.c_str();
166#endif
167 throw( AccessByStringNotInitialized(mess ) );
168 }
169
170 uint8_t cVal= *dstptr++;
171 m_d->m_charType = cVal;
172 if( (m_d->m_charType != 1)
173 && (m_d->m_charType != 2)
174 && (m_d->m_charType != 4) ) {
175 std::string mess = "FsaAccessHeader::read: byte_per_char must be 1, 2 or 4!!!" ;
176#ifdef DEBUG_CD
177 LERROR << mess.c_str();
178#endif
179 throw( AccessByStringNotInitialized(mess) );
180 }
181
182#ifdef DEBUG_CD
183 LDEBUG << "FsaAccessHeader::read: m_charType = "
184 << (int)cVal;
185#endif
186
187 cVal= *dstptr++;
188 m_d->m_packing = cVal;
189 if( (m_d->m_packing != BUILDER)
190 && (m_d->m_packing != BUILT)
191 && (m_d->m_packing != SPARE)) {
192 std::string mess = "FsaAccessHeader::read: m_packing type is not recognized!!!" ;
193#ifdef DEBUG_CD
194 LERROR << mess.c_str();
195#endif
196 throw( AccessByStringNotInitialized(mess) );
197 }
198#ifdef DEBUG_CD
199 LDEBUG << "FsaAccessHeader::read: m_packing = "
200 << (m_d->m_packing == BUILDER ? "BUILDER" : (m_d->m_packing == BUILT ? "BUILT" : "SPARE" ));
201#endif
202
203 cVal= *dstptr++;
204#ifdef DEBUG_CD
205 LDEBUG << "FsaAccessHeader::read: m_charOrder = "
206 << (int)cVal;
207#endif
208 m_d->m_charOrder = cVal;
209 if( (m_d->m_charOrder != FORWARD)
210 &&(m_d->m_charOrder != REVERSE) ) {
211 std::string mess = "FsaAccessHeader::read: character order is not recognized!!!" ;
212#ifdef DEBUG_CD
213 LERROR << mess.c_str();
214#endif
215 throw( AccessByStringNotInitialized(mess) );
216 }
217 m_d->m_trie_direction_fwd = (m_d->m_charOrder == FORWARD);
218#ifdef DEBUG_CD
219 LDEBUG << "FsaAccessHeader::read: m_charOrder = "
220 << (m_d->m_charOrder == FORWARD ? "FORWARD" : "REVERSE" );
221#ifdef DEBUG_CD
222 LDEBUG << "FsaAccessHeader::read: m_charOrder = "
223 << (int)cVal;
224#endif
225#endif
226
227 char numBuf2[21];
228 strncpy(numBuf2, dstptr, 20 );
229 dstptr += 20;
230 numBuf2[sizeof(numBuf2)-1] = '\0';
231 sscanf(numBuf2, "%lu %lu ", &(m_d->m_nbVertices), &(m_d->m_nbEdges) );
232#ifdef DEBUG_CD
233 LDEBUG << "FsaAccessHeader::read: nbVert = " << m_d->m_nbVertices
234 << "m_nbEdges = " << m_d->m_nbEdges;
235#endif
236
237}
238
239
241#ifdef DEBUG_CD
243 LDEBUG << "FsaAccessHeader::write(";
244#endif
245
246 char headBuf[HEADER_SIZE];
247 char *dstptr = headBuf;
248 memset(dstptr, 0, HEADER_SIZE );
249 strncpy(dstptr, MAGIC_NUMBER, 12 );
250 dstptr += 12;
251
252 sprintf(dstptr, "%02d.%02d", m_d->m_majorVersion, m_d->m_minorVersion);
253 dstptr += 5;
254
255 *dstptr++ = m_d->m_charType;
256 *dstptr++ = m_d->m_packing;
257 *dstptr++ = m_d->m_charOrder;
258
259 sprintf(dstptr, "%09lu ", m_d->m_nbVertices);
260 dstptr += 10;
261
262 sprintf(dstptr, "%09lu ", m_d->m_nbEdges);
263 dstptr += 10;
264
265 ow.writeData( headBuf, HEADER_SIZE );
266#ifdef DEBUG_CD
267 LDEBUG << "FsaAccessHeader::write: os.write headbuf ";
268#endif
269
270}
271
272
273
274} // namespace FsaAccess
275} // namespace Common
276} // namespace Lima
#define MAJOR_VERSION_16
#define HEADER_SIZE
#define MAGIC_NUMBER
#define MINOR_VERSION_16
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define FSAALOGINIT
Definition LimaCommon.h:206
#define FSAAIOLOGINIT
Definition LimaCommon.h:205
Use this exception to signal the used of a wrongly initialized LIMA dictionary.
Definition LimaCommon.h:428
virtual void readData(char *ptr, size_t n)=0
virtual void writeData(const char *ptr, size_t n)=0
FsaDictionaries (FsaDictBuilder and FsaDictSpare) share the same binary file format:
FsaAccessHeader & operator=(const FsaAccessHeader &h)
void write(AbstractFsaAccessOStreamWrapper &ow)
void read(AbstractFsaAccessIStreamWrapper &iw)
void setPackingStatus(uint8_t packingStatus)
NAUTITIA.
QChar LimaChar
Definition LimaString.h:30