LCOV - code coverage report
Current view: top level - ugbase/common/util - file_util.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 2 0
Test Date: 2025-09-21 23:31:46 Functions: - 0 0

            Line data    Source code
       1              : /*
       2              :  * Copyright (c) 2011-2014:  G-CSC, Goethe University Frankfurt
       3              :  * Author: Sebastian Reiter, Torbjörn Klatt
       4              :  * 
       5              :  * This file is part of UG4.
       6              :  * 
       7              :  * UG4 is free software: you can redistribute it and/or modify it under the
       8              :  * terms of the GNU Lesser General Public License version 3 (as published by the
       9              :  * Free Software Foundation) with the following additional attribution
      10              :  * requirements (according to LGPL/GPL v3 §7):
      11              :  * 
      12              :  * (1) The following notice must be displayed in the Appropriate Legal Notices
      13              :  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
      14              :  * 
      15              :  * (2) The following notice must be displayed at a prominent place in the
      16              :  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
      17              :  * 
      18              :  * (3) The following bibliography is recommended for citation and must be
      19              :  * preserved in all covered files:
      20              :  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
      21              :  *   parallel geometric multigrid solver on hierarchically distributed grids.
      22              :  *   Computing and visualization in science 16, 4 (2013), 151-164"
      23              :  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
      24              :  *   flexible software system for simulating pde based models on high performance
      25              :  *   computers. Computing and visualization in science 16, 4 (2013), 165-179"
      26              :  * 
      27              :  * This program is distributed in the hope that it will be useful,
      28              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      29              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      30              :  * GNU Lesser General Public License for more details.
      31              :  */
      32              : 
      33              : /**
      34              :  * \file file_util.h
      35              :  * \date 2012-05-15
      36              :  * \brief File utility functions
      37              :  * \details Utility function, which depend on functionality specific to
      38              :  * operating systems are implemented in common/os_dependent/file_util_*.cpp
      39              :  */
      40              : 
      41              : #ifndef __H__UG__FILE_UTIL__
      42              : #define __H__UG__FILE_UTIL__
      43              : 
      44              : #include <fstream>
      45              : #include <vector>
      46              : #include <string>
      47              : #include "common/ug_config.h"
      48              : 
      49              : namespace ug
      50              : {
      51              : 
      52              : /// \addtogroup ugbase_common_io
      53              : /// \{
      54              : 
      55              : /**
      56              :  * \brief Returns a list of all directories in a directory
      57              :  *
      58              :  * The returned list contains as well the directory scanned as <tt>.</tt> and its
      59              :  * parent directory as <tt>..</tt>.
      60              :  *
      61              :  * \param[out]  dirsOut string vector for holding the list of directories
      62              :  * \param[in]   dir     path and name of the directory to scan
      63              :  *
      64              :  * \note The implementation relies on OS specific instructions.
      65              :  *       Implementations for POSIX-UNIX and Windows are available.
      66              :  */
      67              : UG_API bool GetDirectoriesInDirectory(std::vector<std::string>& dirsOut, const char* dir);
      68              : 
      69              : /**
      70              :  * \brief Returns a list of all files in a directory
      71              :  *
      72              :  * \param[out]  filesOut  string vector for holding the list of files
      73              :  * \param[in]   dir       path and name of the directory to scan
      74              :  *
      75              :  * \note The implementation relies on OS specific instructions.
      76              :  *       Implementations for POSIX-UNIX and Windows are available.
      77              :  */
      78              : UG_API bool GetFilesInDirectory(std::vector<std::string>& filesOut, const char* dir);
      79              : 
      80              : /**
      81              :  * \brief Checks the existence of a given directory
      82              :  *
      83              :  * \param[in] name of the directory to be checked
      84              :  * \return \c true if the specified directory exists, \c false otherwise
      85              :  */
      86              : UG_API bool DirectoryExists(const char* dirname);
      87              : static inline bool DirectoryExists( std::string filename)
      88              : {
      89              :         return DirectoryExists(filename.c_str());
      90              : }
      91              : /**
      92              :  * \brief Checks the existence of a given file
      93              :  *
      94              :  * \param[in] filename path and name of the file to be checked
      95              :  * \return \c true if the specified file exists, \c false otherwise
      96              :  */
      97              : UG_API bool FileExists( const char *filename );
      98              : static inline bool FileExists( std::string filename)
      99              : {
     100            0 :         return FileExists(filename.c_str());
     101              : }
     102              : 
     103              : /**
     104              :  * \brief Returns size of the specified file in bytes
     105              :  *
     106              :  * \param[in] filename Path and name of the file
     107              :  * \return Size of \c filename in bytes
     108              :  * \throws std::runtime_error if the specified file does not exist or could not
     109              :  *                            be opened for reading.
     110              :  */
     111              : UG_API size_t FileSize( const char *filename );
     112              : static inline size_t FileSize( std::string filename)
     113              : {
     114              :         return FileSize(filename.c_str());
     115              : }
     116              : 
     117              : /**
     118              :  * \brief Check filename extension
     119              :  * 
     120              :  * \param[in] filename path and anme of the file
     121              :  * \param[in] extension desired file extension (including leading dot)
     122              :  * \return \c true if \c filename has given extension, \c false otherwise
     123              :  */
     124              : UG_API bool FileTypeIs( const char *filename, const char *extension );
     125              : 
     126              : /**
     127              :  * \brief Creates a directory
     128              :  *
     129              :  * This is a hack. This function does the same as CreateDirectory.
     130              :  * But there is a linking problem in MS VC, so that the name CreateDirectory
     131              :  * cannot be used.
     132              :  *
     133              :  * \param[in] directory name of the directory
     134              :  * \return true if successfull
     135              :  *
     136              :  */
     137              : UG_API bool CreateDirectoryTMP(const char *directory);
     138              : 
     139              : /**
     140              :  * \brief Creates a directory
     141              :  *
     142              :  * \param[in] directory name of the directory
     143              :  * \param[in] mode      (optional, default 0777) Sets ownership options for the file.
     144              :  *                                      Ignored on windows.
     145              :  * \return true if successfull
     146              :  */
     147              : UG_API bool CreateDirectory(const char *directory);
     148              : 
     149              : /**
     150              :  * \brief Creates a directory
     151              :  *
     152              :  * \param[in] directory name of the directory
     153              :  * \param[in] mode      (optional, default 0777) Sets ownership options for the file.
     154              :  *                                      Ignored on windows.
     155              :  * \return true if successfull
     156              :  */
     157              : UG_API bool CreateDirectory(const char *directory, int mode);
     158              : 
     159              : /**
     160              :  * \brief Creates a directory
     161              :  *
     162              :  * \param[in] directory name of the directory
     163              :  * \return true if successfull
     164              :  */
     165              : static inline bool CreateDirectory(std::string directory)
     166              : {
     167            0 :         return CreateDirectoryTMP(directory.c_str());
     168              : }
     169              : 
     170              : /**
     171              :  * \brief Compares two files by their content
     172              :  *
     173              :  * Comparison is first done on file size using ug::FileSize.
     174              :  * If the two files have same size, then the comparison is done line wise based
     175              :  * on std::string comparison.
     176              :  *
     177              :  * \param[in] file1 Path and name of the first file
     178              :  * \param[in] file2 Path and name of the second file
     179              :  * \return \c true if they are the same one or different ones but with same
     180              :  *         content; \c false otherwise
     181              :  * \throws std::runtim_error if one or both files can not be found or opened for
     182              :  *                           reading.
     183              :  */
     184              : UG_API bool FileCompare( const char *file1, const char *file2 );
     185              : 
     186              : /**
     187              :  * \brief Returns a path to which an application may write temporary files.
     188              :  *
     189              :  * On unix systems, this is normally "/tmp". On windows the users AppData path
     190              :  * or something similar is returned.
     191              :  */
     192              : UG_API std::string GetTmpPath();
     193              : 
     194              : /**
     195              :  * \param filename filename to read
     196              :  * \param file vector to put whole file into
     197              :  * \param bText if true, open file with r, otherwise rb
     198              :  * used in \sa ParallelReadFile and Script
     199              :  */
     200              : UG_API bool ReadFile(const char* filename, std::vector<char> &file, bool bText);
     201              : 
     202              : 
     203              : /**
     204              :  * \param filename filename including path (e.g. /Users/horst/file1)
     205              :  * \param extension the extension to be added to the file e.g. txt
     206              :  * \param bSuccess true if successful, false otherwise
     207              :  * \return filenameXXXXXX.extension, where XXXXXX is some number between 1 and 999999,
     208              :  *                      so that the file doesn't exist yet.
     209              :  */
     210              : UG_API std::string MakeTmpFile(std::string filename, const std::string &extension,
     211              :                 bool &bSuccess);
     212              : 
     213              : ///     Changes the current directory
     214              : UG_API void ChangeDirectory(std::string dir);
     215              : 
     216              : 
     217              : ////////////////////////////////////////////////////////////////////////////////
     218              : ///     searches the file in the standard paths.
     219              : /**     The checking order is the following:
     220              :  *              - absolute or relative path to working directory
     221              :  *              - relative path regarding the current script path (PathProvider::get_current_path())
     222              :  *
     223              :  * \return      the full path (including the filename) at which the file was found, or an
     224              :  *                      empty string if no matching file was found.
     225              :  */
     226              : std::string FindFileInStandardPaths(const char* filename);
     227              : 
     228              : ////////////////////////////////////////////////////////////////////////////////
     229              : ///     searches the directory in the standard paths.
     230              : /**     The checking order is the following:
     231              :  *              - absolute or relative path to working directory
     232              :  *              - relative path regarding the current script path (PathProvider::get_current_path())
     233              :  *
     234              :  * \return      the full path (including the dir name) at which the dir was found, or an
     235              :  *                      empty string if no matching file was found.
     236              :  */
     237              : std::string FindDirInStandardPaths(const char* dirname);
     238              : 
     239              : /** Current working directory
     240              :  *
     241              :  * \return      the current working directory as a string
     242              :  */
     243              : std::string CurrentWorkingDirectory();
     244              : 
     245              : // end group ugbase_common_io
     246              : /// \}
     247              : 
     248              : } // namespace ug
     249              : 
     250              : #endif // __H__UG__FILE_UTIL__
     251              : 
     252              : // EOF
        

Generated by: LCOV version 2.0-1