Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / utility / file.h
index 8f01a7445a83ef9344fdcae7e81080584755baa9..ef837b4a842b5357bc6f38c68c0a56aff5a984e8 100644 (file)
@@ -1,38 +1,42 @@
 /*
+ * This file is part of the GROMACS molecular simulation package.
  *
- *                This source code is part of
+ * Copyright (c) 2012,2013,2014, 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.
  *
- *                 G   R   O   M   A   C   S
- *
- *          GROningen MAchine for Chemical Simulations
+ * 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.
  *
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
- * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2009, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
+ * 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.
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * 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, 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 www.gromacs.org.
+ * 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 papers on the package - you can find them in the top README file.
- *
- * For more info, check our website at http://www.gromacs.org
+ * the research papers on the package. Check out http://www.gromacs.org.
  */
 /*! \file
  * \brief
- * Declares functions for file handling.
+ * Declares gmx::File.
  *
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \inpublicapi
  * \ingroup module_utility
  */
@@ -43,7 +47,7 @@
 
 #include <string>
 
-#include "common.h"
+#include "gromacs/utility/common.h"
 
 namespace gmx
 {
@@ -102,6 +106,17 @@ class File
          */
         void close();
 
+        /*! \brief
+         * Returns whether the file is an interactive terminal.
+         *
+         * Only works on Unix, otherwise always returns true.
+         * It only makes sense to call this for File::standardInput() and
+         * friends.
+         *
+         * Thie file must be open.
+         * Does not throw.
+         */
+        bool isInteractive() const;
         /*! \brief
          * Returns a file handle for interfacing with C functions.
          *
@@ -120,6 +135,41 @@ class File
          * The file must be open.
          */
         void readBytes(void *buffer, size_t bytes);
+        /*! \brief
+         * Reads a single line from the file.
+         *
+         * \param[out] line    String to receive the line.
+         * \returns    false if nothing was read because the file ended.
+         * \throws     std::bad_alloc if out of memory.
+         * \throws     FileIOError on any I/O error.
+         *
+         * On error or when false is returned, \p line will be empty.
+         * Trailing space will be removed from the line.
+         * To loop over all lines in the file, use:
+         * \code
+           std::string line;
+           while (file.readLine(&line))
+           {
+               // ...
+           }
+           \endcode
+         */
+        bool readLine(std::string *line);
+        /*! \brief
+         * Reads a single line from the file.
+         *
+         * \param[out] line    String to receive the line.
+         * \returns    false if nothing was read because the file ended.
+         * \throws     std::bad_alloc if out of memory.
+         * \throws     FileIOError on any I/O error.
+         *
+         * On error or when false is returned, \p line will be empty.
+         * Works as readLine(), except that terminating newline will be present
+         * in \p line if it was present in the file.
+         *
+         * \see readLine()
+         */
+        bool readLineWithTrailingSpace(std::string *line);
 
         /*! \brief
          * Writes a string to the file.
@@ -153,17 +203,41 @@ class File
          */
         void writeLine();
 
+        /*! \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
+         * Returns a File object for accessing stdin.
+         *
+         * \throws    std::bad_alloc if out of memory (only on first call).
+         */
+        static File &standardInput();
+        /*! \brief
+         * Returns a File object for accessing stdout.
+         *
+         * \throws    std::bad_alloc if out of memory (only on first call).
+         */
+        static File &standardOutput();
         /*! \brief
          * Returns a File object for accessing stderr.
          *
-         * \throws    std::bad_alloc if out of memory.
+         * \throws    std::bad_alloc if out of memory (only on first call).
          */
         static File &standardError();
 
         /*! \brief
          * Reads contents of a file to a std::string.
          *
-         * \param[in] filename  File to read.
+         * \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.
@@ -171,16 +245,27 @@ class File
         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:
         /*! \brief
          * Initialize file object from an existing file handle.
          *
-         * \param[in]  fp     File handle to use (may be NULL).
+         * \param[in]  fp     %File handle to use (may be NULL).
          * \param[in]  bClose Whether this object should close its file handle.
          * \throws     std::bad_alloc if out of memory.
          *
-         * Used internally to implement standardError().
+         * Used internally to implement standardOutput() and standardError().
          */
         File(FILE *fp, bool bClose);