1 /************************************************************************
2 ************************************************************************
4 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
5 ---------------------------------------------------------------------
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ************************************************************************
20 ************************************************************************/
31 #include "doc_lang.hh"
32 #include "doc_notice.hh"
33 #include "doc_autodoc.hh"
34 #include "doc_metadatas.hh"
36 #include "enrobage.hh"
37 #include "compatibility.hh"
41 extern map
<string
, string
> gDocNoticeStringMap
;
42 extern set
<string
> gDocNoticeKeySet
;
44 extern map
<string
, string
> gDocAutodocStringMap
;
45 extern set
<string
> gDocAutodocKeySet
;
47 extern map
<string
, string
> gDocMathStringMap
;
48 extern set
<string
> gDocMathKeySet
;
50 extern map
<string
, string
> gDocMetadatasStringMap
;
51 extern set
<string
> gDocMetadatasKeySet
;
53 static const string gDocTextsDefaultFile
= "mathdoctexts-default.txt";
55 static void importDocStrings(const string
& filename
);
56 static void getKey(string
& s
, string
& key
, size_t& pt1
);
57 static void getText(string
& s
, size_t& pt1
, string
& text
);
58 static void storePair(const string
& key
, const string
& text
);
60 static void printStringMapContent(map
<string
,string
>& map
, const string
& name
);
62 static istream
* openArchFile (const string
& filename
);
63 static void getCurrentDir();
64 static int cholddir();
66 static string gCurrentDir
; ///< Room to save current directory name.
71 /*****************************************************************************
73 *****************************************************************************/
76 void loadTranslationFile(const string
& lang
)
83 /** First ensure that the default file is loaded a least. */
84 importDocStrings(gDocTextsDefaultFile
);
86 /** Then try and load the target file. */
87 if ( ! lang
.empty() ) {
88 importDocStrings( "mathdoctexts-" + lang
+ ".txt" );
94 /*****************************************************************************
96 *****************************************************************************/
101 * @brief Feed the content of doc texts maps from a file.
103 * This mecchanism allows to load different files for translation.
105 * "mathdoctexts" files must have been formatted as follows :
106 * - a line beginning by ':' immediately declares a keyword,
107 * - a line beginning by '"' contains text until the last '"',
108 * - text can directly follow a keyword, if separated by one or
109 * many separator characters (space or tab).
110 * - a direct line break between two double quoted strings
111 * will insert a '\n' line break in the resulting notice string.
113 static void importDocStrings(const string
& filename
)
117 istream
* file
= openArchFile(filename
);
119 while ( getline(*file
, s
) ) {
120 size_t pt1
; // Text pointer.
122 /* The first character determines whether will follow a key or a text. */
127 if (pt1
==string::npos
) continue;
135 getText(s
, pt1
, text
);
136 storePair(key
, text
);
138 printStringMapContent(gDocNoticeStringMap
, "gDocNoticeStringMap");
139 printStringMapContent(gDocAutodocStringMap
, "gDocAutodocStringMap");
140 printStringMapContent(gDocMathStringMap
, "gDocMathStringMap");
141 printStringMapContent(gDocMetadatasStringMap
, "gDocMetadatasStringMap");
145 static void getKey(string
& s
, string
& key
, size_t& pt1
)
147 /* Initialisation. */
149 string separators
= " \t";
151 size_t pk2
= s
.find_first_of(separators
);
153 /* Immediate '\n' after keyword case. */
154 if (pk2
==string::npos
) pk2
= s
.size();
156 /* Capture and check the keyword. */
157 key
= s
.substr(pk1
, pk2
-1);
159 /* Prepare text capture. */
160 pt1
= s
.find_first_of("\"", pk2
);
164 static void getText(string
& s
, size_t& pt1
, string
& text
)
166 /* Capture the text on the current line. */
168 pt2
= s
.find_last_not_of("\"");
169 if (pt2
!=string::npos
) {
170 if (text
.size() > 0) text
+= "\n"; // Handle line breaks.
171 text
+= s
.substr(pt1
+1, pt2
-pt1
);
176 static void storePair(const string
& key
, const string
& text
)
178 /* Store the current pair. */
179 if(!key
.empty() && !text
.empty()) {
181 if (gDocNoticeKeySet
.find(key
) != gDocNoticeKeySet
.end()) {
182 gDocNoticeStringMap
[key
] = text
;
184 else if (gDocAutodocKeySet
.find(key
) != gDocAutodocKeySet
.end()) {
185 gDocAutodocStringMap
[key
] = text
;
187 else if (gDocMathKeySet
.find(key
) != gDocMathKeySet
.end()) {
188 gDocMathStringMap
[key
] = text
;
190 else if (gDocMetadatasKeySet
.find(key
) != gDocMetadatasKeySet
.end()) {
191 gDocMetadatasStringMap
[key
] = text
;
194 cerr
<< "Documentator : importDocStings : " << "warning : unknown key \"" << key
<< "\"" << endl
;
196 //cerr << "gDocNoticeStringMap[\"" << key << "\"] = \"" << gDocNoticeStringMap[key] << "\"" << endl;
202 * Simple trace function.
204 static void printStringMapContent(map
<string
,string
>& m
, const string
& name
) {
207 cout
<< name
<< ".size() = " << m
.size() << endl
;
208 map
<string
,string
>::iterator it
;
210 for(it
= m
.begin(); it
!=m
.end(); ++it
)
211 cout
<< i
++ << ".\t" << name
<< "[" << it
->first
<< "] \t= '" << it
->second
<< "'" << endl
;
218 //------------------------ file managment -------------------------
222 * Open architecture file.
224 static istream
* openArchFile (const string
& filename
)
227 getCurrentDir(); // Save the current directory.
228 if ( (file
= open_arch_stream(filename
.c_str())) ) {
229 //cerr << "Documentator : openArchFile : Opening '" << filename << "'" << endl;
231 cerr
<< "ERROR : can't open architecture file " << filename
<< endl
;
234 cholddir(); // Return to current directory.
240 * Switch back to the previously stored current directory
242 static int cholddir ()
244 if (chdir(gCurrentDir
.c_str()) == 0) {
254 * Get current directory and store it in gCurrentDir.
256 static void getCurrentDir ()
258 char buffer
[FAUST_PATH_MAX
];
259 gCurrentDir
= getcwd (buffer
, FAUST_PATH_MAX
);