LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
DictionaryData.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 * Copyright (C) 2004-2012 by CEA LIST *
8 * *
9 ***************************************************************************/
10#include "DictionaryData.h"
11
12#include "common/LimaCommon.h"
14
15#include <QtGlobal>
16#include <QtCore/QFileInfo>
17
18#include <iostream>
19#include <fstream>
20
21
22using namespace std;
23
24namespace Lima
25{
26
27namespace LinguisticProcessing
28{
29
30namespace AnalysisDict
31{
32
34 m_data(0),
35 m_entriesAddr(),
36 m_lingPropertiesAddr()
37{}
38
39
41{
42 if (m_data)
43 {
44 delete [] m_data;
45 }
46}
47
48void DictionaryData::loadBinaryFile(const std::string& file)
49{
51 LDEBUG << "DictionaryData::loadBinaryFile" << file;
52 if( !QFileInfo::exists(file.c_str()))
53// if( !boost::filesystem3::exists(file))
54 {
55 std::string mess = "DictionaryData::loadBinaryFile file ";
56 mess.append(file).append(" not found!");
57 throw( std::logic_error( mess ) );
58 }
59 uint64_t dataSize = QFileInfo(file.c_str()).size();
60 LDEBUG << "DictionaryData::loadBinaryFile data size: " << dataSize;
61 m_data = new unsigned char [dataSize];
62 if (m_data == NULL)
63 {
64 std::string mess = "DictionaryData::loadBinaryFile memory allocation error";
65 throw( std::logic_error( mess ) );
66 }
67
68 // load data
69 FILE *dataFile = fopen(file.c_str(), "rb");
70 if (dataFile == NULL)
71 {
72 LIMA_EXCEPTION( "DictionaryData::loadBinaryFile error cannot open data file"
73 << file.c_str() );
74 }
75 uint64_t readSize = fread(m_data, 1, dataSize, dataFile); //_dataSize = max
76 fclose(dataFile);
77 if (readSize != dataSize)
78 {
79 std::string mess = "DictionaryData::loadBinaryFile totalDataReadSize != _dataSize ";
80 throw( std::logic_error( mess ) );
81 }
82
83 // parseEntries
84 unsigned char* p=m_data;
85 uint64_t nbEntries=readCodedInt(p);
86 m_entriesAddr.resize(nbEntries);
87 uint64_t read;
88 for (vector<unsigned char*>::iterator entryItr=m_entriesAddr.begin();
89 entryItr!=m_entriesAddr.end();
90 entryItr++)
91 {
92 *entryItr = p;
93 // go to next entry
94 read=readCodedInt(p);
95 if (read == 1)
96 {
97 // 1 means delete, next in is length
98 read=readCodedInt(p);
99 }
100 p += read;
101 }
102 LDEBUG << "read " << nbEntries << " entries";
103
104 // parseLingProperties
105 uint64_t nbLingProp=readCodedInt(p);
106 m_lingPropertiesAddr.resize(nbLingProp);
107 for(vector<unsigned char*>::iterator lingItr=m_lingPropertiesAddr.begin();
108 lingItr!=m_lingPropertiesAddr.end();
109 lingItr++)
110 {
111 *lingItr=p;
112 read = readCodedInt(p);
113 p += read;
114 }
115 LDEBUG << "read " << nbLingProp << " lingPropsSet";
116 Q_ASSERT((uint64_t)(p-m_data) == dataSize);
117}
118
119uint64_t DictionaryData::readCodedInt(unsigned char* &p)
120{
121 uint64_t val = 0;
122// cerr << "start read" << endl;
123 do
124 {
125// cerr << "val = " << val << " *p = " << (int) *p << endl;
126 val = (val <<7) + ((*p >> 1) & 0x7F);
127 }
128 while (*(p++) & 0x1);
129// cerr << "end read val=" << val << endl;
130 return(val);
131}
132
133}
134
135}
136
137}
#define LIMA_EXCEPTION(X)
This macro writes the message X to a previously configured error stream before throwing a LimaExcepti...
Definition LimaCommon.h:293
#define LDEBUG
Definition LimaCommon.h:157
#define ANALYSISDICTLOGINIT
NAUTITIA.
STL namespace.