"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/src/core/datadir.cpp" (12 May 2020, 8037 Bytes) of package /windows/www/highlight-3.57-x64.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "datadir.cpp": 3.54_vs_3.55.

    1 /***************************************************************************
    2                           dataDir.cpp  -  description
    3                              -------------------
    4     begin                : Sam March 1 2003
    5     copyright            : (C) 2003-2020 by Andre Simon
    6     email                : a.simon@mailbox.org
    7  ***************************************************************************/
    8 
    9 
   10 /*
   11 This file is part of Highlight.
   12 
   13 Highlight is free software: you can redistribute it and/or modify
   14 it under the terms of the GNU General Public License as published by
   15 the Free Software Foundation, either version 3 of the License, or
   16 (at your option) any later version.
   17 
   18 Highlight is distributed in the hope that it will be useful,
   19 but WITHOUT ANY WARRANTY; without even the implied warranty of
   20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   21 GNU General Public License for more details.
   22 
   23 You should have received a copy of the GNU General Public License
   24 along with Highlight.  If not, see <http://www.gnu.org/licenses/>.
   25 */
   26 
   27 
   28 #include <string>
   29 #include <fstream>
   30 #include <vector>
   31 #include "platform_fs.h"
   32 
   33 #include "datadir.h"
   34 
   35 
   36 string DataDir::LSB_DATA_DIR="/usr/share/highlight/";
   37 string DataDir::LSB_CFG_DIR="/etc/highlight/";
   38 string DataDir::LSB_DOC_DIR="/usr/share/doc/highlight/";
   39 
   40 
   41 void DataDir::initSearchDirectories ( const string &userDefinedDir )
   42 {
   43 
   44 #ifndef WIN32
   45     possibleDirs.push_back ( Platform::getHomePath() + "/.highlight/" );
   46 #endif
   47     
   48     if ( !userDefinedDir.empty() ) possibleDirs.push_back ( userDefinedDir );
   49 
   50     char* hlEnvPath=getenv("HIGHLIGHT_DATADIR");
   51     if (hlEnvPath!=NULL) {
   52         possibleDirs.push_back ( hlEnvPath );
   53     }
   54 
   55 #ifndef WIN32
   56 
   57 #ifdef HL_DATA_DIR
   58     possibleDirs.push_back ( HL_DATA_DIR );
   59 #else
   60     possibleDirs.push_back ( LSB_DATA_DIR );
   61 #endif
   62 
   63 #ifdef HL_CONFIG_DIR
   64     possibleDirs.push_back ( HL_CONFIG_DIR);
   65 #else
   66     possibleDirs.push_back ( LSB_CFG_DIR);
   67 #endif
   68 
   69 #else
   70     possibleDirs.push_back(Platform::getAppPath()); //not needed because of fallback in searchFile
   71 #endif
   72 }
   73 
   74 void DataDir::searchDataDir( const string &userDefinedDir )
   75 {
   76     initSearchDirectories(userDefinedDir);
   77 }
   78 
   79 const string DataDir::searchFile(const string path)
   80 {
   81     for ( unsigned int i=0; i<possibleDirs.size(); i++ ) {
   82     
   83         //cerr << "Searching "<<possibleDirs[i]<< path<<"\n";
   84         if ( Platform::fileExists ( possibleDirs[i] + path ) )
   85             return possibleDirs[i]+ path;
   86     }
   87     return path;
   88 }
   89 
   90 const void DataDir::printConfigPaths()
   91 {
   92     for ( unsigned int i=0; i<possibleDirs.size(); i++ ) {
   93         if ( Platform::fileExists ( possibleDirs[i] ) )
   94             cout <<possibleDirs[i]<<"\n";
   95     }
   96 }
   97 
   98 const string DataDir::getLangPath ( const string & file )
   99 {
  100     return searchFile(string("langDefs")+Platform::pathSeparator+file);
  101 }
  102 
  103 const string DataDir::getThemePath ( const string & file, bool base16)
  104 {
  105     string themesPath=string("themes")+Platform::pathSeparator;
  106     
  107     if (base16)
  108         themesPath=themesPath + "base16" + Platform::pathSeparator;
  109     
  110     return searchFile(themesPath+file);
  111 }
  112 
  113 const string DataDir::getPluginPath ( const string & file)
  114 {
  115     return searchFile(string("plugins")+Platform::pathSeparator+file);
  116 }
  117 
  118 const string DataDir::getFiletypesConfPath (const string & file)
  119 {
  120     return searchFile(file + ".conf");
  121 }
  122 
  123 const string DataDir::getThemePath ( )
  124 {
  125     return getSystemDataPath()+"themes"+Platform::pathSeparator;
  126 }
  127 
  128 const string DataDir::getLangPath ( )
  129 {
  130     return getSystemDataPath()+"langDefs"+Platform::pathSeparator;
  131 }
  132 
  133 const string DataDir::getPluginPath ( )
  134 {
  135     return getSystemDataPath()+"plugins"+Platform::pathSeparator;
  136 }
  137 
  138 const string DataDir::getSystemDataPath ( )
  139 {
  140 #ifndef WIN32
  141 #ifdef HL_DATA_DIR
  142     return HL_DATA_DIR;
  143 #else
  144     return LSB_DATA_DIR;
  145 #endif
  146 #else
  147     return Platform::getAppPath();
  148 #endif
  149 }
  150 
  151 const string  DataDir::getI18nDir()
  152 {
  153     return getSystemDataPath()+"gui_files"+Platform::pathSeparator+"i18n"+Platform::pathSeparator;
  154 }
  155 
  156 const string  DataDir::getExtDir()
  157 {
  158     return getSystemDataPath() +"gui_files"+Platform::pathSeparator+"ext"+Platform::pathSeparator;
  159 }
  160 
  161 const string DataDir::getDocDir()
  162 {
  163     return getSystemDataPath();
  164 }
  165 
  166 const string DataDir::getEncodingHint (const string &syntax) {
  167         
  168     return encodingHint[getFileBaseName(syntax)];
  169 }
  170 
  171 string DataDir::getFileSuffix(const string& fileName)
  172 {
  173     size_t ptPos=fileName.rfind(".");
  174     size_t psPos = fileName.rfind ( Platform::pathSeparator );
  175     if (ptPos == string::npos) {
  176         return  (psPos==string::npos) ? fileName : fileName.substr(psPos+1, fileName.length());
  177     }
  178     return (psPos!=string::npos && psPos>ptPos) ? "" : fileName.substr(ptPos+1, fileName.length());
  179 }
  180 
  181 string DataDir::guessFileType ( const string& suffix, const string &inputFile, bool useUserSuffix, bool forceShebangCheckStdin )
  182 {   
  183     string baseName = getFileBaseName(inputFile);
  184     if (assocByFilename.count(baseName)) return assocByFilename.find(baseName)->second;
  185    
  186     string lcSuffix = StringTools::change_case(suffix);
  187     if (assocByExtension.count(lcSuffix)) {
  188         return assocByExtension[lcSuffix];
  189     }
  190 
  191     if (!useUserSuffix) {
  192         string shebang =  analyzeFile(forceShebangCheckStdin ? "" : inputFile);
  193         if (!shebang.empty()) return shebang;
  194     }
  195 
  196     return lcSuffix;
  197 }
  198 
  199 bool DataDir::loadFileTypeConfig (const string& path )
  200 {
  201     string confPath=searchFile(path+".conf");
  202     try {
  203         Diluculum::LuaState ls;
  204         Diluculum::LuaValueList ret= ls.doFile (confPath);
  205 
  206         int idx=1;
  207         string langName;
  208         Diluculum::LuaValue mapEntry;
  209         while ((mapEntry = ls["FileMapping"][idx].value()) !=Diluculum::Nil) {
  210             langName = mapEntry["Lang"].asString();
  211             if (mapEntry["Extensions"] !=Diluculum::Nil) {
  212                 readLuaList("Extensions", langName, mapEntry,  &assocByExtension);
  213             } else if (mapEntry["Filenames"] !=Diluculum::Nil) {
  214                 readLuaList("Filenames", langName, mapEntry,  &assocByFilename);
  215             } else if (mapEntry["Shebang"] !=Diluculum::Nil) {
  216                 assocByShebang.insert ( make_pair ( mapEntry["Shebang"].asString(),  langName ) );
  217             } else if (mapEntry["EncodingHint"] !=Diluculum::Nil) {
  218                 encodingHint.insert ( make_pair ( langName, mapEntry["EncodingHint"].asString() ) );
  219             } 
  220             idx++;
  221         }
  222 
  223     } catch (Diluculum::LuaError &err) {
  224         cerr <<err.what()<<"\n";
  225         return false;
  226     }
  227     return true;
  228 }
  229 
  230 void DataDir::readLuaList(const string& paramName, const string& langName,Diluculum::LuaValue &luaVal, StringMap* extMap){
  231     int extIdx=1;
  232     string val;
  233     while (luaVal[paramName][extIdx] !=Diluculum::Nil) {
  234         val = luaVal[paramName][extIdx].asString();
  235         extMap->insert ( make_pair ( val,  langName ) );
  236         extIdx++;
  237     }
  238 }
  239 
  240 string DataDir::analyzeFile ( const string& file )
  241 {
  242     string firstLine;
  243     if ( !file.empty() ) {
  244         ifstream inFile ( file.c_str() );
  245         getline ( inFile, firstLine );
  246     } else {
  247         
  248         //  This copies all the data to a new buffer, uses the data to get the
  249         //  first line, then makes cin use the new buffer that underlies the
  250         //  stringstream instance
  251         cin_bufcopy << cin.rdbuf();
  252         getline ( cin_bufcopy, firstLine );
  253         cin_bufcopy.seekg ( 0, ios::beg );
  254         cin.rdbuf ( cin_bufcopy.rdbuf() );
  255     }
  256     StringMap::iterator it;
  257     boost::xpressive::sregex rex;
  258     boost::xpressive::smatch what;
  259     for ( it=assocByShebang.begin(); it!=assocByShebang.end(); it++ ) {
  260         rex = boost::xpressive::sregex::compile( it->first );
  261         if ( boost::xpressive::regex_search( firstLine, what, rex )  ) return it->second;
  262     }
  263     return "";
  264 }
  265 
  266 string DataDir::getFileBaseName(const string& fileName){
  267     size_t psPos = fileName.rfind ( /*Platform::pathSeparator*/ '/' );
  268     return  (psPos==string::npos) ? fileName : fileName.substr(psPos+1, fileName.length());
  269 }
  270