From: Teemu Murtola Date: Fri, 26 Jun 2015 04:16:48 +0000 (+0300) Subject: Remove gmx::File (except for File::exists()) X-Git-Url: http://biod.pnpi.spb.ru/gitweb/?p=alexxy%2Fgromacs.git;a=commitdiff_plain;h=1d597ac0415f9a847715ef65daa55fe1312bd59f Remove gmx::File (except for File::exists()) Move remaining static methods to either path.h, or to the file stream classes. Change-Id: I53e910051b9ef57e501adad4cec1a4b5295d24a7 --- diff --git a/src/gromacs/commandline/cmdlineprogramcontext.cpp b/src/gromacs/commandline/cmdlineprogramcontext.cpp index 5d98a4ae51..8420424687 100644 --- a/src/gromacs/commandline/cmdlineprogramcontext.cpp +++ b/src/gromacs/commandline/cmdlineprogramcontext.cpp @@ -60,7 +60,6 @@ #include "buildinfo.h" #include "gromacs/utility/exceptions.h" -#include "gromacs/utility/file.h" #include "gromacs/utility/gmxassert.h" #include "gromacs/utility/path.h" #include "gromacs/utility/stringutil.h" diff --git a/src/gromacs/commandline/tests/cmdlinehelpmodule.cpp b/src/gromacs/commandline/tests/cmdlinehelpmodule.cpp index 7be3fb5ed7..5fada52c35 100644 --- a/src/gromacs/commandline/tests/cmdlinehelpmodule.cpp +++ b/src/gromacs/commandline/tests/cmdlinehelpmodule.cpp @@ -48,7 +48,7 @@ #include "gromacs/commandline/cmdlinemodulemanager.h" #include "gromacs/options/basicoptions.h" #include "gromacs/options/options.h" -#include "gromacs/utility/file.h" +#include "gromacs/utility/textwriter.h" #include "gromacs/onlinehelp/tests/mock_helptopic.h" #include "testutils/cmdlinetest.h" @@ -123,7 +123,7 @@ TEST_F(CommandLineHelpModuleTest, ExportsHelp) "test", "help", "-export", "rst" }; // TODO: Find a more elegant solution, or get rid of the links.dat altogether. - gmx::File::writeFileFromString("links.dat", ""); + gmx::TextWriter::writeFileFromString("links.dat", ""); CommandLine args(cmdline); initManager(args, "test"); MockOptionsModule &mod1 = addOptionsModule("module", "First module"); diff --git a/src/gromacs/commandline/tests/pargs.cpp b/src/gromacs/commandline/tests/pargs.cpp index d74f12f987..53ea895be1 100644 --- a/src/gromacs/commandline/tests/pargs.cpp +++ b/src/gromacs/commandline/tests/pargs.cpp @@ -51,9 +51,9 @@ #include #include "gromacs/utility/arrayref.h" -#include "gromacs/utility/file.h" #include "gromacs/utility/path.h" #include "gromacs/utility/stringutil.h" +#include "gromacs/utility/textwriter.h" #include "testutils/cmdlinetest.h" #include "testutils/testasserts.h" @@ -108,7 +108,7 @@ class ParseCommonArgsTest : public ::testing::Test FileArgumentType type) { std::string filename(tempFiles_.getTemporaryFilePath(extension)); - gmx::File::writeFileFromString(filename, "Dummy file"); + gmx::TextWriter::writeFileFromString(filename, "Dummy file"); if (name != NULL) { args_.append(name); diff --git a/src/gromacs/gmxpreprocess/fflibutil.cpp b/src/gromacs/gmxpreprocess/fflibutil.cpp index f986f5e545..11e1f5ae84 100644 --- a/src/gromacs/gmxpreprocess/fflibutil.cpp +++ b/src/gromacs/gmxpreprocess/fflibutil.cpp @@ -47,7 +47,6 @@ #include "gromacs/utility/directoryenumerator.h" #include "gromacs/utility/exceptions.h" #include "gromacs/utility/fatalerror.h" -#include "gromacs/utility/file.h" #include "gromacs/utility/futil.h" #include "gromacs/utility/path.h" #include "gromacs/utility/smalloc.h" diff --git a/src/gromacs/gmxpreprocess/pdb2top.cpp b/src/gromacs/gmxpreprocess/pdb2top.cpp index 920c3a38f9..49b7316358 100644 --- a/src/gromacs/gmxpreprocess/pdb2top.cpp +++ b/src/gromacs/gmxpreprocess/pdb2top.cpp @@ -70,7 +70,6 @@ #include "gromacs/utility/dir_separator.h" #include "gromacs/utility/exceptions.h" #include "gromacs/utility/fatalerror.h" -#include "gromacs/utility/file.h" #include "gromacs/utility/futil.h" #include "gromacs/utility/path.h" #include "gromacs/utility/programcontext.h" diff --git a/src/gromacs/onlinehelp/helpformat.h b/src/gromacs/onlinehelp/helpformat.h index 99e16425b5..bf99fdd7c4 100644 --- a/src/gromacs/onlinehelp/helpformat.h +++ b/src/gromacs/onlinehelp/helpformat.h @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by + * Copyright (c) 2012,2013,2014,2015, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -50,7 +50,6 @@ namespace gmx { -class File; class HelpWriterContext; /*! \libinternal \brief diff --git a/src/gromacs/utility/CMakeLists.txt b/src/gromacs/utility/CMakeLists.txt index 1933e660a6..1ce2ce4b20 100644 --- a/src/gromacs/utility/CMakeLists.txt +++ b/src/gromacs/utility/CMakeLists.txt @@ -44,7 +44,6 @@ gmx_install_headers( errorcodes.h exceptions.h fatalerror.h - file.h flags.h futil.h gmxassert.h diff --git a/src/gromacs/utility/datafilefinder.cpp b/src/gromacs/utility/datafilefinder.cpp index 2bc6a9b875..e773fff84b 100644 --- a/src/gromacs/utility/datafilefinder.cpp +++ b/src/gromacs/utility/datafilefinder.cpp @@ -51,7 +51,7 @@ #include "buildinfo.h" #include "gromacs/utility/directoryenumerator.h" #include "gromacs/utility/exceptions.h" -#include "gromacs/utility/file.h" +#include "gromacs/utility/filestream.h" #include "gromacs/utility/path.h" #include "gromacs/utility/programcontext.h" #include "gromacs/utility/stringutil.h" @@ -133,7 +133,7 @@ FILE *DataFileFinder::openFile(const DataFileOptions &options) const fprintf(debug, "Opening library file %s\n", fn); } #endif - return File::openRawHandle(filename, "r"); + return TextInputFile::openRawHandle(filename); } std::string DataFileFinder::findFile(const DataFileOptions &options) const diff --git a/src/gromacs/utility/file.cpp b/src/gromacs/utility/file.cpp deleted file mode 100644 index b5415a1c9c..0000000000 --- a/src/gromacs/utility/file.cpp +++ /dev/null @@ -1,299 +0,0 @@ -/* - * This file is part of the GROMACS molecular simulation package. - * - * Copyright (c) 2012,2013,2014,2015, by the GROMACS development team, led by - * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, - * and including many others, as listed in the AUTHORS file in the - * top-level source directory and at http://www.gromacs.org. - * - * GROMACS is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2.1 - * of the License, or (at your option) any later version. - * - * GROMACS is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with GROMACS; if not, see - * http://www.gnu.org/licenses, or write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * If you want to redistribute modifications to GROMACS, please - * consider that scientific software is very special. Version - * control is crucial - bugs must be traceable. We will be happy to - * consider code for inclusion in the official distribution, but - * derived work must not be called official GROMACS. Details are found - * in the README & COPYING files - if they are missing, get the - * official version at http://www.gromacs.org. - * - * To help us fund GROMACS development, we humbly ask that you cite - * the research papers on the package. Check out http://www.gromacs.org. - */ -/*! \internal \file - * \brief - * Implements gmx::File. - * - * \author Teemu Murtola - * \ingroup module_utility - */ -#include "gmxpre.h" - -#include "file.h" - -#include "config.h" - -#include -#include -#include - -#include -#include -#include - -#include - -#include "gromacs/utility/exceptions.h" -#include "gromacs/utility/gmxassert.h" -#include "gromacs/utility/stringutil.h" - -namespace gmx -{ - -/*! \internal \brief - * Private implementation class for File. - * - * \ingroup module_utility - */ -class File::Impl -{ - public: - /*! \brief - * Initialize a file object with the given handle. - * - * \param[in] fp %File handle to use (may be NULL). - * \param[in] bClose Whether this object should close its file handle. - */ - Impl(FILE *fp, bool bClose); - ~Impl(); - - //! File handle for this object (may be NULL). - FILE *fp_; - /*! \brief - * Whether \p fp_ should be closed by this object. - * - * Can be true if \p fp_ is NULL. - */ - bool bClose_; -}; - -File::Impl::Impl(FILE *fp, bool bClose) - : fp_(fp), bClose_(bClose) -{ -} - -File::Impl::~Impl() -{ - if (fp_ != NULL && bClose_) - { - if (fclose(fp_) != 0) - { - // TODO: Log the error somewhere - } - } -} - -// static -FILE *File::openRawHandle(const char *filename, const char *mode) -{ - FILE *fp = fopen(filename, mode); - if (fp == NULL) - { - GMX_THROW_WITH_ERRNO( - FileIOError(formatString("Could not open file '%s'", filename)), - "fopen", errno); - } - return fp; -} - -// static -FILE *File::openRawHandle(const std::string &filename, const char *mode) -{ - return openRawHandle(filename.c_str(), mode); -} - -File::File(const char *filename, const char *mode) - : impl_(new Impl(NULL, true)) -{ - open(filename, mode); -} - -File::File(const std::string &filename, const char *mode) - : impl_(new Impl(NULL, true)) -{ - open(filename, mode); -} - -File::~File() -{ -} - -void File::open(const char *filename, const char *mode) -{ - GMX_RELEASE_ASSERT(impl_->fp_ == NULL, - "Attempted to open the same file object twice"); - // TODO: Port all necessary functionality from gmx_ffopen() here. - impl_->fp_ = openRawHandle(filename, mode); -} - -void File::open(const std::string &filename, const char *mode) -{ - open(filename.c_str(), mode); -} - -void File::close() -{ - GMX_RELEASE_ASSERT(impl_->fp_ != NULL, - "Attempted to close a file object that is not open"); - GMX_RELEASE_ASSERT(impl_->bClose_, - "Attempted to close a file object that should not be"); - bool bOk = (fclose(impl_->fp_) == 0); - impl_->fp_ = NULL; - if (!bOk) - { - GMX_THROW_WITH_ERRNO( - FileIOError("Error while closing file"), "fclose", errno); - } -} - -FILE *File::handle() -{ - GMX_RELEASE_ASSERT(impl_->fp_ != NULL, - "Attempted to access a file object that is not open"); - return impl_->fp_; -} - -void File::readBytes(void *buffer, size_t bytes) -{ - errno = 0; - FILE *fp = handle(); - // TODO: Retry based on errno or something else? - size_t bytesRead = std::fread(buffer, 1, bytes, fp); - if (bytesRead != bytes) - { - if (feof(fp)) - { - GMX_THROW(FileIOError( - formatString("Premature end of file\n" - "Attempted to read: %d bytes\n" - "Successfully read: %d bytes", - static_cast(bytes), - static_cast(bytesRead)))); - } - else - { - GMX_THROW_WITH_ERRNO(FileIOError("Error while reading file"), - "fread", errno); - } - } -} - -void File::writeString(const char *str) -{ - if (fprintf(handle(), "%s", str) < 0) - { - GMX_THROW_WITH_ERRNO(FileIOError("Writing to file failed"), - "fprintf", errno); - } -} - -// static -bool File::exists(const char *filename) -{ - if (filename == NULL) - { - return false; - } - FILE *test = fopen(filename, "r"); - if (test == NULL) - { - return false; - } - else - { - fclose(test); - // Windows doesn't allow fopen of directory, so we don't need to check - // this separately. -#ifndef GMX_NATIVE_WINDOWS - struct stat st_buf; - int status = stat(filename, &st_buf); - if (status != 0 || !S_ISREG(st_buf.st_mode)) - { - return false; - } -#endif - return true; - } -} - -// static -bool File::exists(const std::string &filename) -{ - return exists(filename.c_str()); -} - -// static -std::string File::readToString(const char *filename) -{ - // Binary mode is required on Windows to be able to determine a size - // that can be passed to fread(). - File file(filename, "rb"); - FILE *fp = file.handle(); - - if (std::fseek(fp, 0L, SEEK_END) != 0) - { - GMX_THROW_WITH_ERRNO(FileIOError("Seeking to end of file failed"), - "fseek", errno); - } - long len = std::ftell(fp); - if (len == -1) - { - GMX_THROW_WITH_ERRNO(FileIOError("Reading file length failed"), - "ftell", errno); - } - if (std::fseek(fp, 0L, SEEK_SET) != 0) - { - GMX_THROW_WITH_ERRNO(FileIOError("Seeking to start of file failed"), - "fseek", errno); - } - - std::vector data(len); - file.readBytes(&data[0], len); - file.close(); - - std::string result(&data[0], len); - // The below is necessary on Windows to make newlines stay as '\n' on a - // roundtrip. - result = replaceAll(result, "\r\n", "\n"); - - return result; -} - -// static -std::string File::readToString(const std::string &filename) -{ - return readToString(filename.c_str()); -} - -// static -void File::writeFileFromString(const std::string &filename, - const std::string &text) -{ - File file(filename, "w"); - file.writeString(text); - file.close(); -} - -} // namespace gmx diff --git a/src/gromacs/utility/file.h b/src/gromacs/utility/file.h deleted file mode 100644 index 9a6b9565d1..0000000000 --- a/src/gromacs/utility/file.h +++ /dev/null @@ -1,196 +0,0 @@ -/* - * This file is part of the GROMACS molecular simulation package. - * - * Copyright (c) 2012,2013,2014,2015, by the GROMACS development team, led by - * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, - * and including many others, as listed in the AUTHORS file in the - * top-level source directory and at http://www.gromacs.org. - * - * GROMACS is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 2.1 - * of the License, or (at your option) any later version. - * - * GROMACS is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with GROMACS; if not, see - * http://www.gnu.org/licenses, or write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * If you want to redistribute modifications to GROMACS, please - * consider that scientific software is very special. Version - * control is crucial - bugs must be traceable. We will be happy to - * consider code for inclusion in the official distribution, but - * derived work must not be called official GROMACS. Details are found - * in the README & COPYING files - if they are missing, get the - * official version at http://www.gromacs.org. - * - * To help us fund GROMACS development, we humbly ask that you cite - * the research papers on the package. Check out http://www.gromacs.org. - */ -/*! \file - * \brief - * Declares gmx::File. - * - * \author Teemu Murtola - * \inpublicapi - * \ingroup module_utility - */ -#ifndef GMX_UTILITY_FILE_H -#define GMX_UTILITY_FILE_H - -#include - -#include - -#include "gromacs/utility/classhelpers.h" - -namespace gmx -{ - -/*! \brief - * Basic file object. - * - * This class provides basic file I/O functionality and uses exceptions - * (FileIOError) for error reporting. - * - * \inpublicapi - * \ingroup module_utility - */ -class File -{ - public: - /*! \brief - * Opens a file and returns a `FILE` handle. - * - * \param[in] filename Path of the file to open. - * \param[in] mode Mode to open the file in (for fopen()). - * \throws FileIOError on any I/O error. - * - * Instead of returning `NULL` on errors, throws an exception with - * additional details (including the file name and `errno`). - */ - static FILE *openRawHandle(const char *filename, const char *mode); - //! \copydoc openRawHandle(const char *, const char *) - static FILE *openRawHandle(const std::string &filename, const char *mode); - /*! \brief - * Creates a file object and opens a file. - * - * \param[in] filename Path of the file to open. - * \param[in] mode Mode to open the file in (for fopen()). - * \throws std::bad_alloc if out of memory. - * \throws FileIOError on any I/O error. - * - * \see open(const char *, const char *) - */ - File(const char *filename, const char *mode); - //! \copydoc File(const char *, const char *) - File(const std::string &filename, const char *mode); - /*! \brief - * Destroys the file object. - * - * If the file is still open, it is closed. - * Any error conditions will be ignored. - */ - ~File(); - - /*! \brief - * Opens a file. - * - * \param[in] filename Path of the file to open. - * \param[in] mode Mode to open the file in (for fopen()). - * \throws FileIOError on any I/O error. - * - * The file object must not be open. - */ - void open(const char *filename, const char *mode); - //! \copydoc open(const char *, const char *) - void open(const std::string &filename, const char *mode); - /*! \brief - * Closes the file object. - * - * \throws FileIOError on any I/O error. - * - * The file must be open. - */ - void close(); - - /*! \brief - * Returns a file handle for interfacing with C functions. - * - * The file must be open. - * Does not throw. - */ - FILE *handle(); - - /*! \brief - * Reads given number of bytes from the file. - * - * \param[out] buffer Pointer to buffer that receives the bytes. - * \param[in] bytes Number of bytes to read. - * \throws FileIOError on any I/O error. - * - * The file must be open. - */ - void readBytes(void *buffer, size_t bytes); - - /*! \brief - * Writes a string to the file. - * - * \param[in] str String to write. - * \throws FileIOError on any I/O error. - * - * The file must be open. - */ - void writeString(const char *str); - //! \copydoc writeString(const char *) - void writeString(const std::string &str) { writeString(str.c_str()); } - - /*! \brief - * Checks whether a file exists and is a regular file. - * - * \param[in] filename Path to the file to check. - * \returns true if \p filename exists and is accessible. - * - * Does not throw. - */ - static bool exists(const char *filename); - //! \copydoc exists(const char *) - static bool exists(const std::string &filename); - - /*! \brief - * Reads contents of a file to a std::string. - * - * \param[in] filename Name of the file to read. - * \returns The contents of \p filename. - * \throws std::bad_alloc if out of memory. - * \throws FileIOError on any I/O error. - */ - static std::string readToString(const char *filename); - //! \copydoc readToString(const char *) - static std::string readToString(const std::string &filename); - /*! \brief - * Convenience method for writing a file from a string in a single call. - * - * \param[in] filename Name of the file to read. - * \param[in] text String to write to \p filename. - * \throws FileIOError on any I/O error. - * - * If \p filename exists, it is overwritten. - */ - static void writeFileFromString(const std::string &filename, - const std::string &text); - - private: - class Impl; - - PrivateImplPointer impl_; -}; - -} // namespace gmx - -#endif diff --git a/src/gromacs/utility/fileredirector.cpp b/src/gromacs/utility/fileredirector.cpp index 054d7c6495..68e595c477 100644 --- a/src/gromacs/utility/fileredirector.cpp +++ b/src/gromacs/utility/fileredirector.cpp @@ -43,8 +43,8 @@ #include "fileredirector.h" -#include "gromacs/utility/file.h" #include "gromacs/utility/filestream.h" +#include "gromacs/utility/path.h" namespace gmx { diff --git a/src/gromacs/utility/filestream.cpp b/src/gromacs/utility/filestream.cpp index 7a2cf25dc7..5f245858a5 100644 --- a/src/gromacs/utility/filestream.cpp +++ b/src/gromacs/utility/filestream.cpp @@ -188,6 +188,25 @@ StandardInputStream &StandardInputStream::instance() * TextInputFile */ +// static +FILE *TextInputFile::openRawHandle(const char *filename) +{ + FILE *fp = fopen(filename, "r"); + if (fp == NULL) + { + GMX_THROW_WITH_ERRNO( + FileIOError(formatString("Could not open file '%s'", filename)), + "fopen", errno); + } + return fp; +} + +// static +FILE *TextInputFile::openRawHandle(const std::string &filename) +{ + return openRawHandle(filename.c_str()); +} + TextInputFile::TextInputFile(const std::string &filename) : impl_(new FileStreamImpl(filename.c_str(), "r")) { diff --git a/src/gromacs/utility/filestream.h b/src/gromacs/utility/filestream.h index f1e49339ca..23c354c344 100644 --- a/src/gromacs/utility/filestream.h +++ b/src/gromacs/utility/filestream.h @@ -103,6 +103,19 @@ class StandardInputStream : public TextInputStream class TextInputFile : public TextInputStream { public: + /*! \brief + * Opens a file and returns a `FILE` handle. + * + * \param[in] filename Path of the file to open. + * \throws FileIOError on any I/O error. + * + * Instead of returning `NULL` on errors, throws an exception with + * additional details (including the file name and `errno`). + */ + static FILE *openRawHandle(const char *filename); + //! \copydoc openRawHandle(const char *, const char *) + static FILE *openRawHandle(const std::string &filename); + /*! \brief * Opens a text file as a stream. * diff --git a/src/gromacs/utility/path.cpp b/src/gromacs/utility/path.cpp index d6aa17140d..60c0e0cd7a 100644 --- a/src/gromacs/utility/path.cpp +++ b/src/gromacs/utility/path.cpp @@ -47,10 +47,12 @@ #include #include +#include #include #include #include +#include #include @@ -386,6 +388,44 @@ std::string Path::resolveSymlinks(const std::string &path) return result; } +/******************************************************************** + * File + */ + +// static +bool File::exists(const char *filename) +{ + if (filename == NULL) + { + return false; + } + FILE *test = std::fopen(filename, "r"); + if (test == NULL) + { + return false; + } + else + { + std::fclose(test); + // Windows doesn't allow fopen of directory, so we don't need to check + // this separately. +#ifndef GMX_NATIVE_WINDOWS + struct stat st_buf; + int status = stat(filename, &st_buf); + if (status != 0 || !S_ISREG(st_buf.st_mode)) + { + return false; + } +#endif + return true; + } +} + +// static +bool File::exists(const std::string &filename) +{ + return exists(filename.c_str()); +} /******************************************************************** * Directory diff --git a/src/gromacs/utility/path.h b/src/gromacs/utility/path.h index ef2b3e8848..8dba41e84c 100644 --- a/src/gromacs/utility/path.h +++ b/src/gromacs/utility/path.h @@ -85,6 +85,25 @@ class Path Path(); }; +class File +{ + public: + /*! \brief + * Checks whether a file exists and is a regular file. + * + * \param[in] filename Path to the file to check. + * \returns `true` if \p filename exists and is accessible. + * + * Does not throw. + */ + static bool exists(const char *filename); + //! \copydoc exists(const char *) + static bool exists(const std::string &filename); + + private: + // Disallow instantiation. + File(); +}; class Directory { diff --git a/src/gromacs/utility/textreader.cpp b/src/gromacs/utility/textreader.cpp index 4eef63e4fc..436614964b 100644 --- a/src/gromacs/utility/textreader.cpp +++ b/src/gromacs/utility/textreader.cpp @@ -50,6 +50,26 @@ namespace gmx { +// static +std::string TextReader::readFileToString(const char *filename) +{ + std::string result; + TextReader reader(filename); + std::string line; + while (reader.readLine(&line)) + { + result.append(line); + } + reader.close(); + return result; +} + +// static +std::string TextReader::readFileToString(const std::string &filename) +{ + return readFileToString(filename.c_str()); +} + class TextReader::Impl { public: diff --git a/src/gromacs/utility/textreader.h b/src/gromacs/utility/textreader.h index 0310f1a061..62607c6499 100644 --- a/src/gromacs/utility/textreader.h +++ b/src/gromacs/utility/textreader.h @@ -67,6 +67,18 @@ namespace gmx class TextReader { public: + /*! \brief + * Reads contents of a file to a std::string. + * + * \param[in] filename Name of the file to read. + * \returns The contents of \p filename. + * \throws std::bad_alloc if out of memory. + * \throws FileIOError on any I/O error. + */ + static std::string readFileToString(const char *filename); + //! \copydoc readFileToString(const char *) + static std::string readFileToString(const std::string &filename); + /*! \brief * Creates a reader that reads from specified file. * diff --git a/src/gromacs/utility/textwriter.cpp b/src/gromacs/utility/textwriter.cpp index aef0d5cc7b..6e52e0a949 100644 --- a/src/gromacs/utility/textwriter.cpp +++ b/src/gromacs/utility/textwriter.cpp @@ -63,6 +63,15 @@ class TextWriter::Impl TextOutputStreamPointer stream_; }; +// static +void TextWriter::writeFileFromString(const std::string &filename, + const std::string &text) +{ + TextWriter file(filename); + file.writeString(text); + file.close(); +} + TextWriter::TextWriter(const std::string &filename) : impl_(new Impl(TextOutputStreamPointer(new TextOutputFile(filename)))) { diff --git a/src/gromacs/utility/textwriter.h b/src/gromacs/utility/textwriter.h index c473b8c279..de83c98d8a 100644 --- a/src/gromacs/utility/textwriter.h +++ b/src/gromacs/utility/textwriter.h @@ -66,6 +66,19 @@ namespace gmx class TextWriter { public: + /*! \brief + * Convenience method for writing a file from a string in a single call. + * + * \param[in] filename Name of the file to read. + * \param[in] text String to write to \p filename. + * \throws std::bad_alloc if out of memory. + * \throws FileIOError on any I/O error. + * + * If \p filename exists, it is overwritten. + */ + static void writeFileFromString(const std::string &filename, + const std::string &text); + /*! \brief * Creates a writer that writes to specified file. * diff --git a/src/programs/mdrun/tests/moduletest.cpp b/src/programs/mdrun/tests/moduletest.cpp index 845aa58ac0..b793af46c5 100644 --- a/src/programs/mdrun/tests/moduletest.cpp +++ b/src/programs/mdrun/tests/moduletest.cpp @@ -50,8 +50,8 @@ #include "gromacs/options/options.h" #include "gromacs/utility/basedefinitions.h" #include "gromacs/utility/basenetwork.h" -#include "gromacs/utility/file.h" #include "gromacs/utility/gmxmpi.h" +#include "gromacs/utility/textwriter.h" #include "programs/mdrun/mdrun_main.h" #include "testutils/cmdlinetest.h" @@ -133,13 +133,13 @@ SimulationRunner::useStringAsMdpFile(const char *mdpString) void SimulationRunner::useStringAsMdpFile(const std::string &mdpString) { - gmx::File::writeFileFromString(mdpInputFileName_, mdpString); + gmx::TextWriter::writeFileFromString(mdpInputFileName_, mdpString); } void SimulationRunner::useStringAsNdxFile(const char *ndxString) { - gmx::File::writeFileFromString(ndxFileName_, ndxString); + gmx::TextWriter::writeFileFromString(ndxFileName_, ndxString); } void diff --git a/src/testutils/cmdlinetest.cpp b/src/testutils/cmdlinetest.cpp index af2e5b2390..6b336b27f3 100644 --- a/src/testutils/cmdlinetest.cpp +++ b/src/testutils/cmdlinetest.cpp @@ -55,9 +55,9 @@ #include "gromacs/commandline/cmdlineoptionsmodule.h" #include "gromacs/commandline/cmdlineprogramcontext.h" #include "gromacs/utility/arrayref.h" -#include "gromacs/utility/file.h" #include "gromacs/utility/gmxassert.h" #include "gromacs/utility/stringutil.h" +#include "gromacs/utility/textreader.h" #include "gromacs/utility/textwriter.h" #include "testutils/refdata.h" @@ -298,7 +298,7 @@ void CommandLineTestHelper::setInputFileContents( GMX_ASSERT(extension[0] != '.', "Extension should not contain a dot"); std::string fullFilename = impl_->fileManager_.getTemporaryFilePath( formatString("%d.%s", args->argc(), extension)); - File::writeFileFromString(fullFilename, contents); + TextWriter::writeFileFromString(fullFilename, contents); args->addOption(option, fullFilename); } @@ -346,7 +346,7 @@ void CommandLineTestHelper::checkOutputFiles(TestReferenceChecker checker) const outfile != impl_->outputFiles_.end(); ++outfile) { - std::string output = File::readToString(outfile->path); + std::string output = TextReader::readFileToString(outfile->path); outputChecker.checkStringBlock(output, outfile->option.c_str()); } } diff --git a/src/testutils/integrationtests.cpp b/src/testutils/integrationtests.cpp index 20fa85f566..9913f6053e 100644 --- a/src/testutils/integrationtests.cpp +++ b/src/testutils/integrationtests.cpp @@ -1,7 +1,7 @@ /* * This file is part of the GROMACS molecular simulation package. * - * Copyright (c) 2013,2014, by the GROMACS development team, led by + * Copyright (c) 2013,2014,2015, by the GROMACS development team, led by * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl, * and including many others, as listed in the AUTHORS file in the * top-level source directory and at http://www.gromacs.org. @@ -46,7 +46,7 @@ #include #include "gromacs/utility/exceptions.h" -#include "gromacs/utility/file.h" +#include "gromacs/utility/textwriter.h" namespace gmx { @@ -69,7 +69,7 @@ void IntegrationTestFixture::redirectStringToStdin(const char* theString) { std::string fakeStdin("fake-stdin"); - gmx::File::writeFileFromString(fakeStdin, theString); + gmx::TextWriter::writeFileFromString(fakeStdin, theString); if (NULL == std::freopen(fakeStdin.c_str(), "r", stdin)) { GMX_THROW_WITH_ERRNO(FileIOError("Failed to redirect a string to stdin"), diff --git a/src/testutils/stringtest.cpp b/src/testutils/stringtest.cpp index 8c005295e0..ce37bdfee5 100644 --- a/src/testutils/stringtest.cpp +++ b/src/testutils/stringtest.cpp @@ -49,7 +49,7 @@ #include "gromacs/options/basicoptions.h" #include "gromacs/options/options.h" -#include "gromacs/utility/file.h" +#include "gromacs/utility/textreader.h" #include "testutils/refdata.h" #include "testutils/testoptions.h" @@ -136,7 +136,7 @@ StringTestBase::checkText(const std::string &text, const char *id) void StringTestBase::checkFileContents(const std::string &filename, const char *id) { - const std::string text = File::readToString(filename); + const std::string text = TextReader::readFileToString(filename); checkText(text, id); }