File: doc_lang.cpp

package info (click to toggle)
faust 0.9.46-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 15,256 kB
  • ctags: 9,961
  • sloc: cpp: 47,746; sh: 2,254; ansic: 1,503; makefile: 1,211; ruby: 950; yacc: 468; objc: 459; lex: 200; xml: 177
file content (262 lines) | stat: -rw-r--r-- 7,215 bytes parent folder | download | duplicates (3)
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
/************************************************************************
 ************************************************************************
 FAUST compiler
 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
 ---------------------------------------------------------------------
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 
 This program 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 General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 ************************************************************************
 ************************************************************************/


#include <iostream>
#include <fstream>
#include <set>
#include <string>
#include <time.h>
#include <cstdlib>
#include <errno.h>

#include "doc_lang.hh"
#include "doc_notice.hh"
#include "doc_autodoc.hh"
#include "doc_metadatas.hh"
#include "lateq.hh"
#include "enrobage.hh"
#include "compatibility.hh"



extern map<string, string>	gDocNoticeStringMap;
extern set<string>			gDocNoticeKeySet;

extern map<string, string>	gDocAutodocStringMap;
extern set<string>			gDocAutodocKeySet;

extern map<string, string>	gDocMathStringMap;
extern set<string>			gDocMathKeySet;

extern map<string, string>	gDocMetadatasStringMap;
extern set<string>			gDocMetadatasKeySet;

static const string			gDocTextsDefaultFile = "mathdoctexts-default.txt";

static void			importDocStrings(const string& filename);
static void			getKey(string& s, string& key, size_t& pt1);
static void			getText(string& s, size_t& pt1, string& text);
static void			storePair(const string& key, const string& text);

static void			printStringMapContent(map<string,string>& map, const string& name);

static istream*		openArchFile (const string& filename);
static void			getCurrentDir();
static int			cholddir();

static string 		gCurrentDir;	///< Room to save current directory name.




/*****************************************************************************
							Public functions
 *****************************************************************************/


void loadTranslationFile(const string& lang)
{
	initDocMath();
	initDocNotice();
	initDocAutodoc();
	initDocMetadatas();
	
	/** First ensure that the default file is loaded a least. */
	importDocStrings(gDocTextsDefaultFile);
	
	/** Then try and load the target file. */
	if ( ! lang.empty() ) {
		importDocStrings( "mathdoctexts-" + lang + ".txt" );
	}
}



/*****************************************************************************
								Static functions
 *****************************************************************************/



/**
 * @brief Feed the content of doc texts maps from a file.
 *
 * This mecchanism allows to load different files for translation.
 *
 * "mathdoctexts" files must have been formatted as follows :
 * - a line beginning by ':' immediately declares a keyword,
 * - a line beginning by '"' contains text until the last '"',
 * - text can directly follow a keyword, if separated by one or
 * many separator characters (space or tab).
 * - a direct line break between two double quoted strings
 * will insert a '\n' line break in the resulting notice string.
 */
static void importDocStrings(const string& filename)
{	
	string s;
	string key, text;
	istream* file = openArchFile(filename);
	
	while ( getline(*file, s) ) {
		size_t pt1; // Text pointer.
		
		/* The first character determines whether will follow a key or a text. */
		switch (s[0]) {
			case ':':
				text = "";
				getKey(s, key, pt1);
				if (pt1==string::npos) continue;
				break;
			case '\"':
				pt1 = 0;
				break;
			default:
				continue;
		}
		getText(s, pt1, text);
		storePair(key, text);
	}
	printStringMapContent(gDocNoticeStringMap, "gDocNoticeStringMap");
	printStringMapContent(gDocAutodocStringMap, "gDocAutodocStringMap");
	printStringMapContent(gDocMathStringMap, "gDocMathStringMap");
	printStringMapContent(gDocMetadatasStringMap, "gDocMetadatasStringMap");
}


static void getKey(string& s, string& key, size_t& pt1) 
{
	/* Initialisation. */
	key = "";
	string separators = " \t";
	size_t pk1 = 1;
	size_t pk2 = s.find_first_of(separators);
	
	/* Immediate '\n' after keyword case. */
	if (pk2==string::npos) pk2 = s.size();
	
	/* Capture and check the keyword. */
	key = s.substr(pk1, pk2-1);
	
	/* Prepare text capture. */
	pt1 = s.find_first_of("\"", pk2);
}


static void getText(string& s, size_t& pt1, string& text)
{
	/* Capture the text on the current line. */
	size_t pt2;
	pt2 = s.find_last_not_of("\"");
	if (pt2!=string::npos) {
		if (text.size() > 0) text += "\n"; // Handle line breaks.
		text += s.substr(pt1+1, pt2-pt1);
	}
}


static void storePair(const string& key, const string& text)
{
	/* Store the current pair. */
	if(!key.empty() && !text.empty()) {
		
		if (gDocNoticeKeySet.find(key) != gDocNoticeKeySet.end()) {
			gDocNoticeStringMap[key] = text;
		} 
		else if (gDocAutodocKeySet.find(key) != gDocAutodocKeySet.end()) {
			gDocAutodocStringMap[key] = text;
		}
		else if (gDocMathKeySet.find(key) != gDocMathKeySet.end()) {
			gDocMathStringMap[key] = text;
		}
		else if (gDocMetadatasKeySet.find(key) != gDocMetadatasKeySet.end()) {
			gDocMetadatasStringMap[key] = text;
		}
		else {
			cerr << "Documentator : importDocStings : " << "warning : unknown key \"" << key << "\"" << endl;
		}
		//cerr << "gDocNoticeStringMap[\"" << key << "\"] = \"" << gDocNoticeStringMap[key] << "\"" << endl;
	}
}


/** 
 * Simple trace function.
 */
static void printStringMapContent(map<string,string>& m, const string& name) {
	bool trace = false;
	if(trace) {
		cout << name << ".size() = " << m.size() << endl;
		map<string,string>::iterator it;
		int i = 1;
		for(it = m.begin(); it!=m.end(); ++it)
			cout << i++ << ".\t" << name << "[" << it->first << "] \t= '" << it->second << "'" << endl;
	}
}




//------------------------ file managment -------------------------


/**
 * Open architecture file.
 */
static istream* openArchFile (const string& filename)
{
	istream* file;
	getCurrentDir();	// Save the current directory.
	if ( (file = open_arch_stream(filename.c_str())) ) {
		//cerr << "Documentator : openArchFile : Opening '" << filename << "'" << endl;
	} else {
		cerr << "ERROR : can't open architecture file " << filename << endl;
		exit(1);
	}
	cholddir();			// Return to current directory.
	return file;
}


/**
 * Switch back to the previously stored current directory
 */
static int cholddir ()
{
	if (chdir(gCurrentDir.c_str()) == 0) {
		return 0;
	} else {
		perror("cholddir");
		exit(errno);
	}
}


/**
 * Get current directory and store it in gCurrentDir.
 */
static void getCurrentDir ()
{
	char	buffer[FAUST_PATH_MAX];
    gCurrentDir = getcwd (buffer, FAUST_PATH_MAX);
}