FAUST compiler  0.9.9.6b8
doc_lang.cpp
Go to the documentation of this file.
00001 /************************************************************************
00002  ************************************************************************
00003  FAUST compiler
00004  Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
00005  ---------------------------------------------------------------------
00006  This program is free software; you can redistribute it and/or modify
00007  it under the terms of the GNU General Public License as published by
00008  the Free Software Foundation; either version 2 of the License, or
00009  (at your option) any later version.
00010  
00011  This program is distributed in the hope that it will be useful,
00012  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  GNU General Public License for more details.
00015  
00016  You should have received a copy of the GNU General Public License
00017  along with this program; if not, write to the Free Software
00018  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  ************************************************************************
00020  ************************************************************************/
00021 
00022 
00023 #include <iostream>
00024 #include <fstream>
00025 #include <set>
00026 #include <string>
00027 #include <time.h>
00028 #include <cstdlib>
00029 #include <errno.h>
00030 
00031 #include "doc_lang.hh"
00032 #include "doc_notice.hh"
00033 #include "doc_autodoc.hh"
00034 #include "doc_metadatas.hh"
00035 #include "lateq.hh"
00036 #include "enrobage.hh"
00037 #include "compatibility.hh"
00038 
00039 
00040 
00041 extern map<string, string>  gDocNoticeStringMap;
00042 extern set<string>          gDocNoticeKeySet;
00043 
00044 extern map<string, string>  gDocAutodocStringMap;
00045 extern set<string>          gDocAutodocKeySet;
00046 
00047 extern map<string, string>  gDocMathStringMap;
00048 extern set<string>          gDocMathKeySet;
00049 
00050 extern map<string, string>  gDocMetadatasStringMap;
00051 extern set<string>          gDocMetadatasKeySet;
00052 
00053 static const string         gDocTextsDefaultFile = "mathdoctexts-default.txt";
00054 
00055 static void         importDocStrings(const string& filename);
00056 static void         getKey(string& s, string& key, size_t& pt1);
00057 static void         getText(string& s, size_t& pt1, string& text);
00058 static void         storePair(const string& key, const string& text);
00059 
00060 static void         printStringMapContent(map<string,string>& map, const string& name);
00061 
00062 static istream*     openArchFile (const string& filename);
00063 static void         getCurrentDir();
00064 static int          cholddir();
00065 
00066 static string       gCurrentDir;    
00067 
00068 
00069 
00070 
00071 /*****************************************************************************
00072                             Public functions
00073  *****************************************************************************/
00074 
00075 
00076 void loadTranslationFile(const string& lang)
00077 {
00078     initDocMath();
00079     initDocNotice();
00080     initDocAutodoc();
00081     initDocMetadatas();
00082     
00084     importDocStrings(gDocTextsDefaultFile);
00085     
00087     if ( ! lang.empty() ) {
00088         importDocStrings( "mathdoctexts-" + lang + ".txt" );
00089     }
00090 }
00091 
00092 
00093 
00094 /*****************************************************************************
00095                                 Static functions
00096  *****************************************************************************/
00097 
00098 
00099 
00113 static void importDocStrings(const string& filename)
00114 {   
00115     string s;
00116     string key, text;
00117     istream* file = openArchFile(filename);
00118     
00119     while ( getline(*file, s) ) {
00120         size_t pt1; // Text pointer.
00121         
00122         /* The first character determines whether will follow a key or a text. */
00123         switch (s[0]) {
00124             case ':':
00125                 text = "";
00126                 getKey(s, key, pt1);
00127                 if (pt1==string::npos) continue;
00128                 break;
00129             case '\"':
00130                 pt1 = 0;
00131                 break;
00132             default:
00133                 continue;
00134         }
00135         getText(s, pt1, text);
00136         storePair(key, text);
00137     }
00138     printStringMapContent(gDocNoticeStringMap, "gDocNoticeStringMap");
00139     printStringMapContent(gDocAutodocStringMap, "gDocAutodocStringMap");
00140     printStringMapContent(gDocMathStringMap, "gDocMathStringMap");
00141     printStringMapContent(gDocMetadatasStringMap, "gDocMetadatasStringMap");
00142 }
00143 
00144 
00145 static void getKey(string& s, string& key, size_t& pt1) 
00146 {
00147     /* Initialisation. */
00148     key = "";
00149     string separators = " \t";
00150     size_t pk1 = 1;
00151     size_t pk2 = s.find_first_of(separators);
00152     
00153     /* Immediate '\n' after keyword case. */
00154     if (pk2==string::npos) pk2 = s.size();
00155     
00156     /* Capture and check the keyword. */
00157     key = s.substr(pk1, pk2-1);
00158     
00159     /* Prepare text capture. */
00160     pt1 = s.find_first_of("\"", pk2);
00161 }
00162 
00163 
00164 static void getText(string& s, size_t& pt1, string& text)
00165 {
00166     /* Capture the text on the current line. */
00167     size_t pt2;
00168     pt2 = s.find_last_not_of("\"");
00169     if (pt2!=string::npos) {
00170         if (text.size() > 0) text += "\n"; // Handle line breaks.
00171         text += s.substr(pt1+1, pt2-pt1);
00172     }
00173 }
00174 
00175 
00176 static void storePair(const string& key, const string& text)
00177 {
00178     /* Store the current pair. */
00179     if(!key.empty() && !text.empty()) {
00180         
00181         if (gDocNoticeKeySet.find(key) != gDocNoticeKeySet.end()) {
00182             gDocNoticeStringMap[key] = text;
00183         } 
00184         else if (gDocAutodocKeySet.find(key) != gDocAutodocKeySet.end()) {
00185             gDocAutodocStringMap[key] = text;
00186         }
00187         else if (gDocMathKeySet.find(key) != gDocMathKeySet.end()) {
00188             gDocMathStringMap[key] = text;
00189         }
00190         else if (gDocMetadatasKeySet.find(key) != gDocMetadatasKeySet.end()) {
00191             gDocMetadatasStringMap[key] = text;
00192         }
00193         else {
00194             cerr << "Documentator : importDocStings : " << "warning : unknown key \"" << key << "\"" << endl;
00195         }
00196         //cerr << "gDocNoticeStringMap[\"" << key << "\"] = \"" << gDocNoticeStringMap[key] << "\"" << endl;
00197     }
00198 }
00199 
00200 
00204 static void printStringMapContent(map<string,string>& m, const string& name) {
00205     bool trace = false;
00206     if(trace) {
00207         cout << name << ".size() = " << m.size() << endl;
00208         map<string,string>::iterator it;
00209         int i = 1;
00210         for(it = m.begin(); it!=m.end(); ++it)
00211             cout << i++ << ".\t" << name << "[" << it->first << "] \t= '" << it->second << "'" << endl;
00212     }
00213 }
00214 
00215 
00216 
00217 
00218 //------------------------ file managment -------------------------
00219 
00220 
00224 static istream* openArchFile (const string& filename)
00225 {
00226     istream* file;
00227     getCurrentDir();    // Save the current directory.
00228     if ( (file = open_arch_stream(filename.c_str())) ) {
00229         //cerr << "Documentator : openArchFile : Opening '" << filename << "'" << endl;
00230     } else {
00231         cerr << "ERROR : can't open architecture file " << filename << endl;
00232         exit(1);
00233     }
00234     cholddir();         // Return to current directory.
00235     return file;
00236 }
00237 
00238 
00242 static int cholddir ()
00243 {
00244     if (chdir(gCurrentDir.c_str()) == 0) {
00245         return 0;
00246     } else {
00247         perror("cholddir");
00248         exit(errno);
00249     }
00250 }
00251 
00252 
00256 static void getCurrentDir ()
00257 {
00258     char    buffer[FAUST_PATH_MAX];
00259     gCurrentDir = getcwd (buffer, FAUST_PATH_MAX);
00260 }
00261 
00262