File indexing completed on 2026-04-09 07:49:34
0001 #pragma once
0002
0003
0004
0005
0006
0007
0008 #include <csignal>
0009 #include <cassert>
0010 #include <sstream>
0011 #include <iostream>
0012 #include <vector>
0013 #include <cstring>
0014 #include <string>
0015 #include <algorithm>
0016
0017 #include "dirent.h"
0018
0019 struct SDir
0020 {
0021 static void List(std::vector<std::string>& names, const char* path, const char* ext, bool sigint=false );
0022 static void Trim(std::vector<std::string>& names, const char* ext );
0023 static std::string Desc(const std::vector<std::string>& names);
0024 };
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 inline void SDir::List(std::vector<std::string>& names, const char* path, const char* ext, bool sigint )
0035 {
0036 DIR* dir = opendir(path) ;
0037 if(!dir)
0038 {
0039 std::cout << "SDir::List FAILED TO OPEN DIR " << ( path ? path : "-" ) << std::endl ;
0040 if(sigint)
0041 {
0042 std::cout << "SDir::List std::raise(SIGINT) due to argument sigint:true " << std::endl ;
0043 std::raise(SIGINT) ;
0044 }
0045 return ;
0046 }
0047 struct dirent* entry ;
0048 while ((entry = readdir(dir)) != nullptr)
0049 {
0050 const char* name = entry->d_name ;
0051 if(strlen(name) > strlen(ext) && strcmp(name + strlen(name) - strlen(ext), ext)==0)
0052 {
0053 names.push_back(name);
0054 }
0055 }
0056 closedir (dir);
0057 std::sort( names.begin(), names.end() );
0058
0059 if(names.size() == 0 ) std::cout
0060 << "SDir::List"
0061 << " path " << ( path ? path : "-" )
0062 << " ext " << ( ext ? ext : "-" )
0063 << " NO ENTRIES FOUND "
0064 << std::endl
0065 ;
0066 }
0067
0068 inline void SDir::Trim(std::vector<std::string>& names, const char* ext)
0069 {
0070 for(int i=0 ; i < int(names.size()) ; i++)
0071 {
0072 std::string& name = names[i];
0073 const char* n = name.c_str();
0074 bool ends_with_ext = strlen(n) > strlen(ext) && strncmp(n + strlen(n) - strlen(ext), ext, strlen(ext) ) == 0 ;
0075 assert( ends_with_ext );
0076 if(!ends_with_ext) std::raise(SIGINT);
0077
0078 name = name.substr(0, strlen(n) - strlen(ext));
0079 }
0080 }
0081 inline std::string SDir::Desc(const std::vector<std::string>& names)
0082 {
0083 std::stringstream ss ;
0084 for(unsigned i=0 ; i < names.size() ; i++) ss << "[" << names[i] << "]" << std::endl ;
0085 std::string s = ss.str();
0086 return s ;
0087 }
0088
0089