a Code for the Combination of Indirect and Direct Constraints on High Energy Physics Models Logo
FilePlaceholderManager.cpp
Go to the documentation of this file.
1/*
2 * FilePlaceholderManager.cpp
3 *
4 * Created on: Oct 10, 2013
5 * Author: Ben O'Leary (benjamin.oleary@gmail.com)
6 *
7 * This file is part of BOLlib, released under the
8 * GNU General Public License. Please see the accompanying
9 * README.BOLlib.txt file for a full list of files, brief documentation
10 * on how to use these classes, and further details on the license.
11 */
12
13#include "../include/FilePlaceholderManager.hpp"
14#include <cstdio>
15#include <iostream>
16
17namespace BOL
18{
19
21 std::string const inputSuffix,
22 std::string const placeholderSuffix,
23 std::string const outputSuffix ):
24 inputSuffix( inputSuffix ),
25 placeholderSuffix( placeholderSuffix ),
26 outputSuffix( outputSuffix ),
27 filenameTriples(),
28 whichTriple(),
29 currentTriple(),
30 currentSuffix( "" ),
31 lastPlaceholder( "" )
32 {
33 // just an initialization list.
34 }
35
37 {
38 // does nothing.
39 }
40
41
42 void
43 FilePlaceholderManager::prepareFilenames( std::string const& inputDirectory,
44 std::string const& placeholderDirectory,
45 std::string const& outputDirectory )
46 /* this takes all the names of all the files in the directory given by
47 * inputDirectory which end in inputSuffixToStrip, and places each in a
48 * FilenameTriple with the placeholder filename given by replacing
49 * inputSuffixToStrip with placeholderSuffixToAdd, replacing the directory
50 * path with placeholderDirectory if placeholderDirectory is not empty, and
51 * the same for the output file using outputSuffixToAdd and outputDirectory
52 * if not empty.
53 */
54 {
55 std::vector< std::string > validBaseFilenames;
56 DIR* directoryPointer( opendir( inputDirectory.c_str() ) );
57 if( NULL == directoryPointer )
58 {
59 throw std::runtime_error( "Could not read directory!" );
60 }
61 std::string currentFile;
62 struct dirent* structPointer( readdir( directoryPointer ) );
63 while( NULL != structPointer )
64 {
65 currentFile.assign( structPointer->d_name );
66 structPointer = readdir( directoryPointer );
67 if( ( 0 == currentFile.compare( "." ) )
68 ||
69 ( 0 == currentFile.compare( ".." ) )
70 ||
71 !( inputSuffix.size() < currentFile.size() ) )
72 {
73 continue;
74 }
75 currentSuffix.assign( currentFile.substr( currentFile.size()
76 - inputSuffix.size() ) );
77 if( 0 == currentSuffix.compare( inputSuffix ) )
78 {
79 validBaseFilenames.push_back( currentFile.substr( 0,
80 ( currentFile.size() - inputSuffix.size() ) ) );
81 }
82 }
83 prepareFilenames( validBaseFilenames,
84 inputDirectory,
85 placeholderDirectory,
86 outputDirectory );
87 }
88
89 void
91 std::vector< std::string > const& baseFilenames,
92 std::string const inputDirectory,
93 std::string placeholderDirectory,
94 std::string outputDirectory )
95 /* this takes all the names of all the files in baseFilenames and places
96 * each in a FilenameTriple with inputSuffixToAdd appended as the input
97 * filename, with placeholderSuffixToAdd appended as the placeholder
98 * filename, replacing the directory path with placeholderDirectory if
99 * placeholderDirectory is not empty, and with outputSuffixToAdd appended
100 * as the output file, replacing the directory path with outputDirectory if
101 * outputDirectory is not empty.
102 */
103 {
104 filenameTriples.clear();
105 if( placeholderDirectory.empty() )
106 {
107 placeholderDirectory.assign( inputDirectory );
108 }
109 else
110 {
111 UsefulStuff::runSystemCommand( "mkdir -p " + placeholderDirectory );
112 }
113 if( outputDirectory.empty() )
114 {
115 outputDirectory.assign( inputDirectory );
116 }
117 else
118 {
119 UsefulStuff::runSystemCommand( "mkdir -p " + outputDirectory );
120 }
121 for( std::vector< std::string >::const_iterator
122 whichBaseFilename( baseFilenames.begin() );
123 baseFilenames.end() > whichBaseFilename;
124 ++whichBaseFilename )
125 {
126 currentTriple.inputFile.assign( inputDirectory + "/"
127 + (*whichBaseFilename) + inputSuffix );
128 currentTriple.placeholderFile.assign( placeholderDirectory + "/"
129 + (*whichBaseFilename) + placeholderSuffix );
130 currentTriple.outputFile.assign( outputDirectory + "/"
131 + (*whichBaseFilename) + outputSuffix );
132 filenameTriples.push_back( currentTriple );
133 }
135 }
136
137 bool
138 FilePlaceholderManager::holdNextPlace( bool const deleteLastPlaceholder )
139 /* this looks to find the first FilenameTriple in filenameTriples which
140 * has both a placeholder filename and an output filename which do not yet
141 * exist in the file system, and returns true if there was such a triple.
142 * if deleteLastPlaceholder is true, the previous placeholder is also
143 * deleted, obviously.
144 */
145 {
146 while( ( filenameTriples.end() > whichTriple )
147 &&
148 ( UsefulStuff::fileExists( whichTriple->outputFile )
149 ||
150 UsefulStuff::fileExists( whichTriple->placeholderFile )
151 ||
152 !(UsefulStuff::fileExists( whichTriple->inputFile )) ) )
153 {
154 ++whichTriple;
155 }
156 if( deleteLastPlaceholder )
157 {
159 }
160 if( !( filenameTriples.end() > whichTriple ) )
161 {
162 return false;
163 }
164 lastPlaceholder.assign( whichTriple->placeholderFile );
165 std::ofstream placeholderStream( lastPlaceholder.c_str() );
166 placeholderStream << "placeholder" << std::endl;
167 placeholderStream.close();
168 return true;
169 }
170
171} /* namespace BOL */
bool holdNextPlace(bool const deleteLastPlaceholder=true)
FilePlaceholderManager(std::string const inputSuffix="", std::string const placeholderSuffix="", std::string const outputSuffix="")
std::vector< FilenameTriple > filenameTriples
std::vector< FilenameTriple >::const_iterator whichTriple
void prepareFilenames(std::string const &inputDirectory, std::string const &placeholderDirectory="", std::string const &outputDirectory="")
static bool fileExists(std::string const &fileName)
static void runSystemCommand(std::string const &systemCommand)