Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:34

0001 #pragma once
0002 
0003 #include <cstring>
0004 #include <cstdlib>
0005 #include <string>
0006 #include <sys/stat.h>
0007 #include <errno.h> 
0008 #include <vector>
0009 #include <algorithm>
0010 #include "dirent.h"
0011 
0012 struct sdirectory
0013 {
0014     static std::string DirName( const char* filepath );
0015     static int MakeDirsForFile(const char* filepath, int mode=0 );  
0016     static int MakeDirs(const char* dirpath, int mode=0 );  
0017     static void DirList( std::vector<std::string>& names, const char* path, const char* pfx=nullptr, const char* ext=nullptr); 
0018 };
0019 
0020 
0021 inline std::string sdirectory::DirName( const char* filepath )
0022 {
0023     std::string p = filepath ; 
0024     std::size_t pos = p.find_last_of("/") ; 
0025     return pos == std::string::npos ? "" : p.substr(0, pos); 
0026 }
0027 
0028 inline int sdirectory::MakeDirsForFile( const char* filepath, int mode_ )
0029 {
0030     if(filepath == nullptr) return 1 ; 
0031     std::string dirpath = DirName(filepath);
0032     return MakeDirs(dirpath.c_str(), mode_ );
0033 }
0034 
0035 
0036 /**
0037 sdirectory::MakeDirs
0038 ----------------------
0039 
0040 While loop is needed because mkdir can only create one 
0041 level of directory at once. 
0042 This follows NPU.hh U::MakeDirs
0043 
0044 **/
0045 
0046 inline int sdirectory::MakeDirs( const char* dirpath_, int mode_ )
0047 {
0048     mode_t default_mode = S_IRWXU | S_IRGRP |  S_IXGRP | S_IROTH | S_IXOTH ;
0049     mode_t mode = mode_ == 0 ? default_mode : mode_ ;
0050 
0051     char* dirpath = strdup(dirpath_);
0052     char* p = dirpath ;
0053     int rc = 0 ;
0054 
0055     while (*p != '\0' && rc == 0)
0056     {
0057         p++;   // advance past leading character, probably slash, and subsequent slashes the next line gets to  
0058         while(*p != '\0' && *p != '/') p++;  // advance p until subsequent slash 
0059         char v = *p;                         // store the slash      
0060         *p = '\0' ;                          // replace slash with string terminator
0061         //printf("%s\n", path );                   
0062         rc = mkdir(dirpath, mode) == -1 && errno != EEXIST ? 1 : 0 ;  // set rc non-zero for mkdir errors other than exists already  
0063         *p = v;                              // put back the slash  
0064     }   
0065     free(dirpath);
0066     return rc ; 
0067 }
0068 
0069 /**
0070 sdirectory::DirList
0071 --------------------
0072 
0073 **/
0074 
0075 
0076 inline void sdirectory::DirList( std::vector<std::string>& names, const char* path, const char* pfx, const char* ext)
0077 {
0078     DIR* dir = opendir(path) ;
0079     if(!dir) std::cout << "sdirectory::DirList FAILED TO OPEN DIR " << ( path ? path : "-" ) << std::endl ; 
0080     if(!dir) return ; 
0081     struct dirent* entry ;
0082     while ((entry = readdir(dir)) != nullptr) 
0083     {   
0084         const char* name = entry->d_name ;
0085         bool dot_name = strcmp(name,".") == 0 || strcmp(name,"..") == 0 ; 
0086         if(dot_name) continue ; 
0087  
0088         bool pfx_match = pfx == nullptr ? true : ( strlen(name) > strlen(pfx) && strncmp(name, pfx, strlen(pfx)) == 0 ) ; 
0089         bool ext_match = ext == nullptr ? true : ( strlen(name) > strlen(ext) && strcmp(name + strlen(name) - strlen(ext), ext)==0)  ;
0090         if(ext_match && pfx_match) names.push_back(name); 
0091     }   
0092     closedir (dir);
0093     std::sort( names.begin(), names.end() );  
0094 
0095     if(names.size() == 0 ) std::cout 
0096         << "sdirectory::DirList" 
0097         << " path " << ( path ? path : "-" ) 
0098         << " pfx " << ( pfx ? pfx : "-" ) 
0099         << " ext " << ( ext ? ext : "-" ) 
0100         << " NO ENTRIES FOUND "
0101         << std::endl
0102         ;   
0103 }
0104 
0105 
0106 
0107