File: GdlFeatures.h

package info (click to toggle)
grcompiler 5.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 24,020 kB
  • sloc: cpp: 48,200; ansic: 7,670; sh: 4,427; makefile: 197; xml: 190; perl: 127; sed: 21
file content (413 lines) | stat: -rw-r--r-- 10,751 bytes parent folder | download | duplicates (2)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*--------------------------------------------------------------------*//*:Ignore this sentence.
Copyright (C) 1999, 2001 SIL International. All rights reserved.

Distributable under the terms of either the Common Public License or the
GNU Lesser General Public License, as specified in the LICENSING.txt file.

File: GdlFeatures.h
Responsibility: Sharon Correll
Last reviewed: Not yet.

Description:
    Classes to implement the features mechanism.
-------------------------------------------------------------------------------*//*:End Ignore*/
#ifdef _MSC_VER
#pragma once
#endif
#ifndef FEATURES_INCLUDED
#define FEATURES_INCLUDED

#define LG_USENG (1033)
/*----------------------------------------------------------------------------------------------
Class: GdlExtName
Description: External name, that is, in natural language, suitable for display in
	a user-interface. Used by GdlFeatureDefn and GdlNameDefn
Hungarian: extname
----------------------------------------------------------------------------------------------*/

class GdlExtName : public GdlObject
{
	friend class GdlFeatureDefn;
	friend class GdlFeatureSetting;

public:
	//	Constructors:
	GdlExtName()
		:	m_wLanguageID(0)
	{
	}

	GdlExtName(std::wstring stu, utf16 wLangID)
		:	m_stuName(stu),
			m_wLanguageID(wLangID)
	{
	}

	//	Getters:
	std::wstring Name()		{ return m_stuName; }
	utf16 LanguageID()		{ return m_wLanguageID; }

	static std::wstring s_stuNoName;

protected:
	//	Instance variables:
	std::wstring	m_stuName;
	utf16			m_wLanguageID;
	
};

/*----------------------------------------------------------------------------------------------
Class: GdlFeatureSetting
Description: The information for a single feature setting.
Hungarian: fset
----------------------------------------------------------------------------------------------*/

class GdlFeatureSetting : public GdlObject
{
	friend class GdlFeatureDefn;
	friend class GdlLanguageDefn;

public:
	//	Constructor:
	GdlFeatureSetting()
	{
		m_nValue = 0;
		m_fHasValue = false;
	}

	//	Getters:
	int Value()				{ return m_nValue; }
	std::string Name()		{ return m_staName; }
	unsigned short NameTblId()		{ return m_wNameTblId; }

	//	Setters:
	void SetName(std::string sta)	{ m_staName = sta; }
	void SetValue(int n)			{ m_nValue = n; m_fHasValue = true; }
	void SetNameTblId(int nID)		{ m_wNameTblId = nID; }
	void AddExtName(utf16 wLangID, std::wstring stu)
	{
		m_vextname.push_back(GdlExtName(stu, wLangID));
	}
	void AddExtName(utf16 wLangID, GdlExpression * pexp)
	{
		GdlStringExpression * pexpString = dynamic_cast<GdlStringExpression*>(pexp);
		Assert(pexpString);

		std::wstring stu = pexpString->ConvertToUnicode();
		m_vextname.push_back(GdlExtName(stu, wLangID));
	}
	void SetNameTblId(utf16 w)		{ m_wNameTblId = w; }

	// compiler:
	void SetLabelFromNameTable(int nNameID, int nLangID, uint8 * pNameTbl);

	std::wstring FindBasicExtName(); 
	bool HasExtName(std::wstring stuName);

	// Debuggers:
	void DebugXmlFeatures(std::ofstream & strmOut, std::string staPathToCur);

protected:
	//	Instance variables:
	std::string				m_staName;
	std::vector<GdlExtName>	m_vextname;
	int						m_nValue;
	unsigned short			m_wNameTblId;
	bool					m_fHasValue;
};



/*----------------------------------------------------------------------------------------------
Class: GdlFeatureDefn
Description: A feature and its associated information
Hungarian: feat
----------------------------------------------------------------------------------------------*/

class GdlFeatureDefn : public GdlDefn
{
	friend class GdlFeatureSetting;

public:
	enum {
		kfidStdStyles = 0,	// whatever
		kfidStdLang = 1
	};

//	enum {	// standard style values
//		kstvPlain		= 0,
//		kstvBold		= 1,
//		kstvItalic		= 2,
//		kstvUnderscore	= 4,
//		kstvStrikeThru	= 8,
//		kstvCompressed	=16
//	};
//	enum { kstvLim = kstvPlain };	// for now, we don't support multiple styles

	//	Constructors & destructors:
	GdlFeatureDefn()
	{
		m_nID = 0;
		m_fStdStyles = false;
		m_fStdLang = false;
		m_fIDSet = false;
		m_fDefaultSet = false;
		m_fFatalError = false;
		m_pfsetDefault = NULL;
		m_wNameTblId = 0xFFFF;
		m_fHasPublicID = false;
	}

	~GdlFeatureDefn()
	{
		for (size_t i = 0; i < m_vpfset.size(); ++i)
			delete m_vpfset[i];
	}

	//	Setters:
	void SetName(std::string sta)	{ m_staName = sta; }
	void SetID(unsigned int n)		{ m_nID = n; m_fIDSet = true; }
	void SetDefault(int n)			{ m_nDefault = n; m_fDefaultSet = true; }
	void SetNameTblId(int nID)		{ m_wNameTblId = nID; }
	void AddExtName(utf16 wLangID, std::wstring stu)
	{
		m_vextname.push_back(GdlExtName(stu, wLangID));
	}
	void AddExtName(utf16 wLangID, GdlExpression * pexp)
	{
		GdlStringExpression * pexpString = dynamic_cast<GdlStringExpression*>(pexp);
		Assert(pexpString);

		std::wstring stu = pexpString->ConvertToUnicode();
		m_vextname.push_back(GdlExtName(stu, wLangID));
	}
	void MarkAsLanguageFeature()
	{
		m_fStdLang = true;
		m_nID = kfidStdLang;
		AddAltID(kfidStdLang);
	}

	utf16 SetNameTblIds(utf16 wFirst, uint8 * pNameTbl, // return next id to use
		std::vector<GdlFeatureDefn *> & vpfeatInput);

	void AddAltID(unsigned int n)
	{
		m_vnIDs.push_back(n);
	}

	void SetHasPublicID(bool f) { m_fHasPublicID = f;  }


	//	Getters:
	std::string Name()
	{
		return m_staName;
	}
	size_t NumberOfSettings()
	{
		return m_vpfset.size();
	}
	unsigned int ID()
	{
		return m_nID;
	}
	bool IsLanguageFeature()
	{
		return m_fStdLang;
	}
	unsigned short NameTblId()	{ return m_wNameTblId; }
	bool NameTblInfo(std::vector<std::wstring> & vstuExtNames, std::vector<utf16> & vwLangIds, 
		std::vector<utf16> & vwNameTblIds, size_t & cchwStringData,
		unsigned int nNameTblIdMinNew, int nNameIdNoName);
	void AltIDs(std::vector<unsigned int> & vn)
	{
		vn = m_vnIDs;
	}
	size_t NumAltIDs() { return m_vnIDs.size(); }

	GdlFeatureSetting * FindSetting(std::string sta);
	GdlFeatureSetting * FindOrAddSetting(std::string, GrpLineAndFile & lnf);
	GdlFeatureSetting * FindSettingWithValue(int n);

	bool HasPublicID() { return m_fHasPublicID; }

public:
	//	Pre-compiler:
	bool ErrorCheck();
	void SortFeatIDs();
	void SortFeatSettings();
	void SetStdStyleFlag();
	void FillInBoolean(GrcSymbolTable * psymtbl);
	void ErrorCheckContd();
	void CalculateDefault();
	void AssignInternalID(int n)
	{
		m_nInternalID = n;
	}
	void RecordDebugInfo();

	void KludgeDebugSettings() {
		std::cout << this->Name() << " : ";
		if (m_vpfset.size() > 0) {
			for (size_t ifset = 0; ifset < m_vpfset.size(); ifset++)
				std::cout << m_vpfset[ifset]->Name() << " " << m_vpfset[ifset]->Value() << " | ";
		}
		std::cout << "\n";
	}

	//	Compiler
	int InternalID()
	{
		return m_nInternalID;
	}

	void OutputSettings(GrcBinaryStream * pbstrm);

	void PushFeatureSetting(GdlFeatureSetting * pfset)
	{
		m_vpfset.push_back(pfset);
	}

	void SetLabelFromNameTable(int nNameID, int nLangID, uint8 * pNameTbl);
	static std::wstring GetLabelFromNameTable(int nNameID, int nLangID, uint8 * pNameTbl);

	std::wstring FindBasicExtName();
	bool HasExtName(std::wstring stuName);

	// Debuggers
	void DebugXmlFeatures(std::ofstream & strmOut, std::string staPathToCur);

protected:
	//	Instance variables:
	std::string					m_staName;
	unsigned int				m_nID;
	std::vector<GdlExtName>		m_vextname;
	std::vector<GdlFeatureSetting *>	m_vpfset;
	int					 		m_nDefault;

	std::vector<unsigned int>	m_vnIDs;  // including alternate IDs; main ID must be first

	bool m_fIDSet;
	bool m_fDefaultSet;		// if still false at the end, we need to
							// figure out what the default should be

	bool m_fStdStyles;		// true for the one feature that corresponds
							// to the set of standard styles

	bool m_fStdLang;		// true if this is the special system "lang" feature whose
							// possible values are language IDs.

	bool m_fHasPublicID;	// set to true if there is a non-hidden ID

	//	for compiler:
	bool m_fFatalError;		// fatal error in this feature definition
	int	m_nInternalID;
	GdlFeatureSetting * m_pfsetDefault;
	unsigned short m_wNameTblId;
};

/*----------------------------------------------------------------------------------------------
Class: GdlLanguageDefn
Description: A mapping of a language identifier onto a set of feature values.
Hungarian: lang
----------------------------------------------------------------------------------------------*/
class GdlLanguageDefn : public GdlDefn
{
	friend class GdlFeatureDefn;
	friend class GdlFeatureSetting;

public:
	// General:
	unsigned int Code()
	{
		unsigned int nCode;
		memcpy(&nCode, m_rgchID, sizeof(m_rgchID));
		return nCode;
	}

	void SetCode(std::string staCode)
	{
		Assert(staCode.length() <= 4);
		staCode = staCode.substr(0, 4);
		memset(m_rgchID, 0, sizeof(m_rgchID));
		memcpy(m_rgchID, staCode.data(), staCode.size() * sizeof(char));
	}

	size_t NumberOfSettings()
	{
		return m_vpfset.size();
	}

	// Pre-compiler:
	void AddFeatureValue(GdlFeatureDefn * pfeat, GdlFeatureSetting * pfset,
		int nFset, GrpLineAndFile & lnf);

	// Compiler:
	void OutputSettings(GrcBinaryStream * pbstrm);

protected:
	//	Instance variables:
	char m_rgchID[4];
	std::vector<GdlFeatureDefn*> m_vpfeat;
	std::vector<GdlFeatureSetting*> m_vpfset;
	std::vector<int> m_vnFset;
};


/*----------------------------------------------------------------------------------------------
Class: GdlLangClass
Description: A collection of language maps and the feature settings for them.
Hungarian: lcls
----------------------------------------------------------------------------------------------*/
class GdlLangClass
{
	friend class GrcManager;

	GdlLangClass(std::string sta)
	{
		m_staLabel = sta;
	}

	~GdlLangClass()
	{
		for (size_t i = 0; i < m_vpexpVal.size(); i++)
			delete m_vpexpVal[i];
	}

	void AddLanguage(GdlLanguageDefn * plang)
	{
		for (size_t i = 0; i < m_vplang.size(); i++)
		{
			if (m_vplang[i] == plang)
				return;
		}
		m_vplang.push_back(plang);
	}

	void AddFeatureValue(std::string staFeat, std::string staVal,
		GdlExpression * pexpVal, GrpLineAndFile lnf)
	{
		m_vstaFeat.push_back(staFeat);
		m_vstaVal.push_back(staVal);
		m_vpexpVal.push_back(pexpVal);
		m_vlnf.push_back(lnf);
		Assert(m_vstaFeat.size() == m_vstaVal.size());
		Assert(m_vstaFeat.size() == m_vpexpVal.size());
		Assert(m_vstaFeat.size() == m_vlnf.size());
	}

	bool PreCompile(GrcManager * pcman);

protected:
	GrpLineAndFile m_lnf;
	std::string m_staLabel;
	std::vector<GdlLanguageDefn *> m_vplang;
	// These four are parallel vectors, each item corresponding to a feature assignment:
	std::vector<std::string> m_vstaFeat;		// name of feature
	std::vector<std::string> m_vstaVal;		// text of value
	std::vector<GdlExpression*> m_vpexpVal;	// expression, if not an identifer
	std::vector<GrpLineAndFile> m_vlnf;
};

#endif // FEATURES_INCLUDED