"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/src/core/platform_fs.cpp" (12 May 2020, 9864 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 "platform_fs.cpp": 3.56_vs_3.57.

    1 
    2 /*
    3 This file is part of Highlight.
    4 
    5 Highlight is free software: you can redistribute it and/or modify
    6 it under the terms of the GNU General Public License as published by
    7 the Free Software Foundation, either version 3 of the License, or
    8 (at your option) any later version.
    9 
   10 Highlight is distributed in the hope that it will be useful,
   11 but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   13 GNU General Public License for more details.
   14 
   15 You should have received a copy of the GNU General Public License
   16 along with Highlight.  If not, see <http://www.gnu.org/licenses/>.
   17 */
   18 
   19 
   20 #include "platform_fs.h"
   21 
   22 // includes for recursive getFileNames() function
   23 #ifdef _WIN32
   24 #include <windows.h>
   25 #include <sys/stat.h>
   26 #else
   27 #include <dirent.h>
   28 #include <errno.h>
   29 #include <sys/stat.h>
   30 #ifdef __VMS
   31 #include <unixlib.h>
   32 #include <rms.h>
   33 #include <ssdef.h>
   34 #include <stsdef.h>
   35 #include <lib$routines.h>
   36 #include <starlet.h>
   37 #endif /* __VMS */
   38 #endif
   39 
   40 #include <algorithm>
   41 #include <iostream>
   42 #include <errno.h>
   43 
   44 using std::string;
   45 using std::vector;
   46 
   47 namespace Platform
   48 {
   49 
   50 #ifdef WIN32
   51 #include <windows.h>
   52 const char pathSeparator = '\\';
   53 
   54 #ifndef QT
   55 std::string getAppPath()
   56 {
   57     char pathAndName[MAX_PATH], path[MAX_PATH], drive[3];
   58     GetModuleFileName(NULL, pathAndName, MAX_PATH);
   59     _splitpath(pathAndName, drive, path, 0, 0);
   60     return std::string(drive)+path;
   61 }
   62 #endif
   63 
   64 std::string getHomePath()
   65 {
   66     return "";
   67 }
   68 
   69 int isColorEscCapable() {
   70     return 0;
   71 }
   72 
   73 
   74 std::string getTempFilePath(){
   75 
   76     char tmpPath[MAX_PATH], tmpFile[MAX_PATH];
   77     if (!GetTempPath(MAX_PATH, tmpPath))
   78         return "";
   79     
   80     if (!GetTempFileName(tmpPath, "hlt", 0, tmpFile))
   81         return "";
   82    
   83     return std::string(tmpFile);
   84 }
   85 
   86 #else
   87 #include <unistd.h>
   88 #include <sys/types.h>
   89 #include <pwd.h>
   90 #include <string.h>
   91 
   92 const char pathSeparator = '/';
   93 
   94 std::string getAppPath()
   95 {
   96     return "";
   97 }
   98 
   99 
  100 std::string getHomePath()
  101 {
  102     struct passwd *pw = getpwuid(getuid());
  103     return string(pw->pw_dir);
  104 }
  105 
  106 
  107 int isColorEscCapable() {
  108     
  109     if (!isatty(fileno(stdout)) || !isatty(fileno(stdin))){
  110         return 0;
  111     }
  112             
  113     char* colorOption=getenv("COLORTERM");
  114      
  115     if (colorOption!=NULL) {
  116         if (!strncmp(colorOption, "truecolor", 9)){
  117             return 2;
  118         }
  119     }
  120     colorOption=getenv("TERM");
  121     if (colorOption!=NULL) {
  122         if (!strncmp(colorOption, "xterm-256color", 14)){
  123             return 1;
  124         }
  125     }
  126     
  127     return 0;
  128 }
  129 
  130 std::string getTempFilePath(){
  131     std::string path("/tmp");
  132     
  133     char* tempOption=getenv("TEMP");
  134     if (tempOption) path=string(tempOption);
  135     char tmpPath[100];
  136     snprintf(tmpPath, sizeof tmpPath-1, "/highlight%d.lua", getpid());
  137     path += tmpPath;
  138     return path;
  139 }
  140 
  141 #endif
  142 
  143 bool getDirectoryEntries ( vector<string> &fileList,
  144                            string wildcard,
  145                            bool recursiveSearch )
  146 {
  147     if ( !wildcard.empty() ) {
  148         string directory_path;
  149         string::size_type Pos = wildcard.find_last_of ( pathSeparator );
  150         if ( Pos == string::npos ) {
  151             directory_path = ".";
  152         } else {
  153             directory_path = wildcard.substr ( 0, Pos + 1 );
  154             wildcard = wildcard.substr ( Pos + 1 );
  155         }
  156 
  157         getFileNames ( directory_path, wildcard, fileList );
  158     }
  159     return ! ( fileList.empty() );
  160 }
  161 
  162 
  163 #ifdef _WIN32  // Windows specific
  164 
  165 /**
  166  * WINDOWS function to resolve wildcards and recurse into sub directories.
  167  * The fileName vector is filled with the path and names of files to process.
  168  *
  169  * @param directory     The path of the directory to be processed.
  170  * @param wildcard      The wildcard to be processed (e.g. *.cpp).
  171  * @param filenam       An empty vector which will be filled with the path and names of files to process.
  172  */
  173 void getFileNames ( const string &directory,const string &wildcard, vector<string> &fileName )
  174 {
  175     vector<string> subDirectory;    // sub directories of directory
  176     WIN32_FIND_DATA FindFileData;   // for FindFirstFile and FindNextFile
  177 
  178     // Find the first file in the directory
  179     string firstFile = directory + "\\*";
  180     HANDLE hFind = FindFirstFile ( firstFile.c_str(), &FindFileData );
  181 
  182     if ( hFind == INVALID_HANDLE_VALUE )
  183         return;
  184 
  185     // save files and sub directories
  186     do {
  187         // skip hidden or read only
  188         if ( FindFileData.cFileName[0] == '.'
  189                 || ( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN )
  190                 || ( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_READONLY ) )
  191             continue;
  192 
  193         // if a sub directory and recursive, save sub directory
  194         if ( ( FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) && true /*g_isRecursive*/ ) {
  195             string subDirectoryPath = directory + pathSeparator + FindFileData.cFileName;
  196             subDirectory.push_back ( subDirectoryPath );
  197             continue;
  198         }
  199 
  200         // save the file name
  201         string filePathName = directory + pathSeparator + FindFileData.cFileName;
  202 
  203         // save file name if wildcard match
  204         if ( wildcmp ( wildcard.c_str(), FindFileData.cFileName ) ) {
  205             fileName.push_back ( filePathName );
  206         }
  207     } while ( FindNextFile ( hFind, &FindFileData ) != 0 );
  208 
  209     // check for processing error
  210     FindClose ( hFind );
  211     DWORD dwError = GetLastError();
  212     if ( dwError != ERROR_NO_MORE_FILES )
  213         return;
  214 
  215     // recurse into sub directories
  216     // if not doing recursive subDirectory is empty
  217     for ( unsigned i = 0; i < subDirectory.size(); i++ ) {
  218         getFileNames ( subDirectory[i], wildcard, fileName );
  219         continue;
  220     }
  221 
  222     return;
  223 }
  224 
  225 #else  // not _WIN32
  226 
  227 /**
  228 * LINUX function to resolve wildcards and recurse into sub directories.
  229 * The fileName vector is filled with the path and names of files to process.
  230 *
  231 * @param directory      The path of the directory to be processed.
  232 * @param wildcard       The wildcard to be processed (e.g. *.cpp).
  233 * @param filenam        An empty vector which will be filled with the path and names of files to process.
  234 */
  235 void getFileNames ( const string &directory,const string &wildcard, vector<string> &fileName )
  236 {
  237     struct dirent *entry;           // entry from readdir()
  238     struct stat statbuf;            // entry from stat()
  239     vector<string> subDirectory;    // sub directories of this directory
  240 
  241     // errno is defined in <errno.h> and is set for errors in opendir, readdir, or stat
  242     errno = 0;
  243 
  244     DIR *dp = opendir ( directory.c_str() );
  245     if ( errno ) return;
  246 
  247     // save the first fileName entry for this recursion
  248     //const unsigned firstEntry = fileName.size();
  249 
  250     // save files and sub directories
  251     while ( ( entry = readdir ( dp ) ) != NULL ) {
  252         // get file status
  253         string entryFilepath = directory + pathSeparator + entry->d_name;
  254         if ( -1 == stat ( entryFilepath.c_str(), &statbuf ) || errno) {
  255             closedir ( dp );
  256             return;
  257         }
  258 
  259         // skip hidden
  260         if ( entry->d_name[0] == '.' )
  261             continue;
  262 
  263         // if a sub directory and recursive, save sub directory
  264         if ( S_ISDIR ( statbuf.st_mode ) && /*g_isRecursive*/ true ) { ///TODO
  265             subDirectory.push_back ( entryFilepath );
  266             continue;
  267         }
  268 
  269         // if a file, save file name
  270         if ( S_ISREG ( statbuf.st_mode ) ) {
  271             // save file name if wildcard match
  272             if ( wildcmp ( wildcard.c_str(), entry->d_name ) ) {
  273                 fileName.push_back ( entryFilepath );
  274             }
  275         }
  276     }
  277     closedir ( dp );
  278 
  279     if ( errno ) return;
  280 
  281     // sort the current entries for fileName
  282     // https://gitlab.com/saalen/highlight/issues/84
  283     // coredump with GLIBCXX_ASSERTIONS compiler option, use iterators if sort is needed
  284     //if ( firstEntry < fileName.size() )
  285     //    sort ( &fileName[firstEntry], &fileName[fileName.size() ] );
  286 
  287     // recurse into sub directories
  288     // if not doing recursive, subDirectory is empty
  289     if ( subDirectory.size() > 1 )
  290         sort ( subDirectory.begin(), subDirectory.end() );
  291     for ( unsigned i = 0; i < subDirectory.size(); i++ ) {
  292         getFileNames ( subDirectory[i], wildcard, fileName );
  293         continue;
  294     }
  295 
  296     return;
  297 }
  298 
  299 #endif
  300 
  301 // From The Code Project http://www.codeproject.com/string/wildcmp.asp
  302 // Written by Jack Handy - jakkhandy@hotmail.com
  303 // Modified to compare case insensitive for Windows (the LC macro)
  304 
  305 int wildcmp ( const char *wild, const char *data )
  306 {
  307     const char *cp = NULL, *mp = NULL;
  308     bool cmpval;
  309 
  310     while ( ( *data ) && ( *wild != '*' ) ) {
  311 #ifdef _WIN32
  312         cmpval = ( tolower ( *wild ) != tolower ( *data ) ) && ( *wild != '?' );
  313 #else
  314         cmpval = ( *wild != *data ) && ( *wild != '?' );
  315 #endif
  316 
  317         if ( cmpval ) {
  318             return 0;
  319         }
  320         wild++;
  321         data++;
  322     }
  323 
  324     while ( *data ) {
  325         if ( *wild == '*' ) {
  326             if ( !*++wild ) {
  327                 return 1;
  328             }
  329             mp = wild;
  330             cp = data+1;
  331         } else {
  332 #ifdef _WIN32
  333             cmpval = ( tolower ( *wild ) == tolower ( *data ) || ( *wild == '?' ) );
  334 #else
  335             cmpval = ( *wild == *data ) || ( *wild == '?' );
  336 #endif
  337 
  338             if ( cmpval ) {
  339                 wild++;
  340                 data++;
  341             } else {
  342                 wild = mp;
  343                 data = cp++;
  344             }
  345         }
  346     }
  347 
  348     while ( *wild == '*' ) {
  349         wild++;
  350     }
  351     return !*wild;
  352 }
  353 
  354 
  355 bool fileExists(const string &fName)
  356 {
  357     struct stat fileInfo;
  358     return !stat(fName.c_str(),&fileInfo);
  359 }
  360 
  361 //-D_FILE_OFFSET_BITS=64
  362 //268435456 256 MB
  363 
  364 off_t fileSize(const string& fName) {
  365     struct stat fileInfo;
  366     if(stat(fName.c_str(), &fileInfo) != 0) {
  367         return 0;
  368     }
  369     return fileInfo.st_size;   
  370 }
  371 
  372 }