Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:16:11

0001 #include "InputFileHandler.h"
0002 
0003 #include <phool/phool.h>
0004 
0005 #include <algorithm>
0006 #include <filesystem>
0007 #include <fstream>
0008 #include <iostream>
0009 
0010 int InputFileHandler::AddFile(const std::string &filename)
0011 {
0012   if (GetVerbosity() > 0)
0013   {
0014     std::cout << "Adding " << filename << " to list of input files" << std::endl;
0015   }
0016   m_FileList.push_back(filename);
0017   m_FileListCopy.push_back(filename);
0018   return 0;
0019 }
0020 
0021 int InputFileHandler::AddListFile(const std::string &filename)
0022 {
0023   // checking filesize to see if we have a text file
0024   if (std::filesystem::exists(filename.c_str()))
0025   {
0026     if (std::filesystem::is_regular_file(filename.c_str()))
0027     {
0028       //      uintmax_t fsize = std::filesystem::file_size(filename.c_str());
0029     }
0030     else
0031     {
0032       std::cout << filename << " is not a regular file" << std::endl;
0033       return -1;
0034     }
0035   }
0036   else
0037   {
0038     std::cout << PHWHERE << "Could not open " << filename << std::endl;
0039     return -1;
0040   }
0041   std::ifstream infile;
0042   infile.open(filename, std::ios_base::in);
0043   if (!infile)
0044   {
0045     std::cout << PHWHERE << "Could not open " << filename << std::endl;
0046     return -1;
0047   }
0048   std::string FullLine;
0049   int nfiles = 0;
0050   getline(infile, FullLine);
0051   while (!infile.eof())
0052   {
0053     if (!std::all_of(FullLine.begin(), FullLine.end(), ::isprint))
0054     {
0055       std::cout << PHWHERE << "file " << filename
0056                 << " contains non printable characters, it is likely a binary file" << std::endl;
0057       return -1;
0058     }
0059     if (!FullLine.empty() && FullLine[0] != '#')  // remove comments
0060     {
0061       AddFile(FullLine);
0062       nfiles++;
0063     }
0064     else if (!FullLine.empty())
0065     {
0066       if (GetVerbosity() > 0)
0067       {
0068         std::cout << "Found Comment: " << FullLine << std::endl;
0069       }
0070     }
0071     getline(infile, FullLine);
0072   }
0073   infile.close();
0074   if (nfiles == 0)
0075   {
0076     std::cout << " listfile " << filename << " does not contain filenames "
0077               << "if this is the only list you load into this Input Manager your code will exit very soon" << std::endl;
0078   }
0079   return 0;
0080 }
0081 
0082 int InputFileHandler::OpenNextFile()
0083 {
0084   while (!m_FileList.empty())
0085   {
0086     std::list<std::string>::const_iterator iter = m_FileList.begin();
0087     if (GetVerbosity())
0088     {
0089       std::cout << PHWHERE << " opening next file: " << *iter << std::endl;
0090     }
0091     if (fileopen(*iter))
0092     {
0093       std::cout << PHWHERE << " could not open file: " << *iter << std::endl;
0094       m_FileList.pop_front();
0095     }
0096     else
0097     {
0098       return 1;
0099     }
0100   }
0101   return 0;
0102 }
0103 
0104 void InputFileHandler::Print(const std::string & /* what */) const
0105 {
0106   std::cout << "file list: " << std::endl;
0107   for (const auto &iter : m_FileList)
0108   {
0109     std::cout << iter << std::endl;
0110   }
0111 }
0112 
0113 void InputFileHandler::UpdateFileList()
0114 {
0115   if (!m_FileList.empty())
0116   {
0117     if (m_Repeat)
0118     {
0119       m_FileList.push_back(*(m_FileList.begin()));
0120       if (m_Repeat > 0)
0121       {
0122         m_Repeat--;
0123       }
0124     }
0125     m_FileList.pop_front();
0126   }
0127   return;
0128 }