FDArray::FDArray(std::string const& input, std::string const& output,
std::string const& errout)
- : _input(input), _output(output), _errout(errout), _fdArray(),
- imap {{"", -1},
- {"cin", STDIN_FILENO},
- {"stdin", STDIN_FILENO}},
- oemap{{"", -1},
- {"cout", STDOUT_FILENO},
- {"stdout", STDOUT_FILENO},
- {"cerr", STDERR_FILENO},
- {"stderr", STDERR_FILENO}}
+ : _fdArray(), _input(input), _output(output), _errout(errout),
+ _imap {{"", -1},
+ {"cin", STDIN_FILENO},
+ {"stdin", STDIN_FILENO}},
+ _oemap{{"", -1},
+ {"cout", STDOUT_FILENO},
+ {"stdout", STDOUT_FILENO},
+ {"cerr", STDERR_FILENO},
+ {"stderr", STDERR_FILENO}}
{
int sim_fd;
std::map<std::string, int>::iterator it;
* Search through the input options and setup the default fd if match is
* found; otherwise, open an input file and seek to location.
*/
- if ((it = imap.find(input)) != imap.end())
+ if ((it = _imap.find(input)) != _imap.end())
sim_fd = it->second;
else
sim_fd = openInputFile(input);
* Search through the output/error options and setup the default fd if
* match is found; otherwise, open an output file and seek to location.
*/
- if ((it = oemap.find(output)) != oemap.end())
+ if ((it = _oemap.find(output)) != _oemap.end())
sim_fd = it->second;
else
sim_fd = openOutputFile(output);
if (output == errout)
; /* Reuse the same file descriptor if these match. */
- else if ((it = oemap.find(errout)) != oemap.end())
+ else if ((it = _oemap.find(errout)) != _oemap.end())
sim_fd = it->second;
else
sim_fd = openOutputFile(errout);
stdin_ffd->setFileOffset(0);
}
- if ((it = imap.find(stdin_ffd->getFileName())) != imap.end()) {
+ if ((it = _imap.find(stdin_ffd->getFileName())) != _imap.end()) {
stdin_ffd->setSimFD(it->second);
} else {
stdin_ffd->setSimFD(openInputFile(stdin_ffd->getFileName()));
stdout_ffd->setFileOffset(0);
}
- if ((it = oemap.find(stdout_ffd->getFileName())) != oemap.end()) {
+ if ((it = _oemap.find(stdout_ffd->getFileName())) != _oemap.end()) {
stdout_ffd->setSimFD(it->second);
} else {
stdout_ffd->setSimFD(openOutputFile(stdout_ffd->getFileName()));
if (stdout_ffd->getFileName() == stderr_ffd->getFileName()) {
/* Reuse the same sim_fd file descriptor if these match. */
stderr_ffd->setSimFD(stdout_ffd->getSimFD());
- } else if ((it = oemap.find(stderr_ffd->getFileName())) != oemap.end()) {
+ } else if ((it = _oemap.find(stderr_ffd->getFileName())) != _oemap.end()) {
stderr_ffd->setSimFD(it->second);
} else {
stderr_ffd->setSimFD(openOutputFile(stderr_ffd->getFileName()));
class FDArray
{
- private:
- static const int NUM_FDS = 1024;
-
public:
/**
* Initialize the file descriptor array and set the standard file
FDArray(std::string const& input, std::string const& output,
std::string const& errout);
- std::string _input;
- std::string _output;
- std::string _errout;
-
/**
* Figure out the file offsets for all currently open files and save them
* the offsets during the calls to drain by the owning process.
*/
void restoreFileOffsets();
+ /**
+ * Put the pointer specified by fdep into the _fdArray entry indexed
+ * by tgt_fd.
+ * @param tgt_fd Use target file descriptors to index the array.
+ * @param fdep Incoming pointer used to set the entry pointed to by tgt_fd.
+ */
+ void setFDEntry(int tgt_fd, std::shared_ptr<FDEntry> fdep);
+
/**
* Treat this object like a normal array in using the subscript operator
* to pull entries out of it.
* @param tgt_fd Use target file descriptors to index the array.
*/
- inline std::shared_ptr<FDEntry>
+ std::shared_ptr<FDEntry>
operator[](int tgt_fd)
{
return getFDEntry(tgt_fd);
}
+ /**
+ * Return the size of the _fdArray field
+ */
+ int getSize() const { return _fdArray.size(); }
+
/**
* Step through the file descriptor array and find the first available
* entry which is denoted as being free by being a 'nullptr'. That file
*/
int allocFD(std::shared_ptr<FDEntry> fdp);
- /**
- * Return the size of the _fdArray field
- */
- int getSize() const { return _fdArray.size(); }
-
- /**
- * Put the pointer specified by fdep into the _fdArray entry indexed
- * by tgt_fd.
- * @param tgt_fd Use target file descriptors to index the array.
- * @param fdep Incoming pointer used to set the entry pointed to by tgt_fd.
- */
- void setFDEntry(int tgt_fd, std::shared_ptr<FDEntry> fdep);
-
/**
* Try to close the host file descriptor. If successful, set the
* specified file descriptor entry object pointer to nullptr.
* Hold pointers to the file descriptor entries. The array size is
* statically defined by the operating system.
*/
- std::array<std::shared_ptr<FDEntry>, NUM_FDS> _fdArray;
+ static constexpr size_t _numFDs {1024};
+ std::array<std::shared_ptr<FDEntry>, _numFDs> _fdArray;
+
+ /**
+ * Hold param strings passed from the Process class which indicate
+ * the filename for each of the corresponding files or some keyword
+ * indicating the use of standard file descriptors.
+ */
+ std::string _input;
+ std::string _output;
+ std::string _errout;
/**
* Hold strings which represent the default values which are checked
* provided doesn't hit against these maps, then a file is opened on the
* host instead of using the host's standard file descriptors.
*/
- std::map<std::string, int> imap;
- std::map<std::string, int> oemap;
+ std::map<std::string, int> _imap;
+ std::map<std::string, int> _oemap;
};
#endif // __FD_ARRAY_HH__