1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3  * Pan - A Newsreader for Gtk+
4  * Copyright (C) 2002-2006  Charles Kerr <charles@rebelbase.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 
21 #ifndef _FileUtil_h_
22 #define _FileUtil_h_
23 
24 #include <string>
25 extern "C" {
26   #include <stddef.h>
27   #include <stdio.h>
28   #include <glib.h>
29   #include <glib/gstdio.h>
30 }
31 #include <pan/general/string-view.h>
32 
33 #define g_freopen freopen
34 #define g_fopen fopen
35 #define g_rmdir rmdir
36 #define g_remove remove
37 #define g_unlink unlink
38 #define g_lstat lstat
39 #define g_stat stat
40 #define g_rename rename
41 #define g_open open
42 
43 namespace pan
44 {
45   /**
46    * Collection of file utilities.
47    *
48    * @ingroup general
49    */
50   namespace file
51   {
52     /** Stats a file and prints out some useful info. Umask etc.... */
53     std::ostream& print_file_info (std::ostream&, const char*);
54 
55     /** just like strerror but never returns NULL */
56     const char * pan_strerror (int error_number);
57 
58     /**
59      * Returns the home pan directory, which falls back to $HOME/.pan2
60      * if the PAN_HOME environmental variable isn't set.
61      */
62     const std::string & get_pan_home ();
63 
64     /**
65 	 * Returns an absolute filename of a file
66 	 */
67     std::string absolute_fn(const std::string &dir, const std::string &base);
68 
69     /**
70      * If the specified directory doesn't exist, Pan tries to create it.
71      * @param path
72      * @return true if the directory already existed or was created; false otherwise
73      */
74     bool ensure_dir_exists (const StringView& path);
75 
76     /**
77      * Makes a unique filename given an optional path and a starting file name.
78      * The filename is sanitized before looking for uniqueness.
79      */
80     gchar* get_unique_fname( const gchar *path, const gchar *fname);
81 
82     /**
83      * Attempt to make a filename safe for use.
84      * <ol>
85      * <li>Replacing illegal characters with '_'.
86      * <li>Ensure the resulting string is UTF8-safe
87      * <li>This function assumes the input is UTF8 since gmime uses UTF8 interface.
88      * <li>Return value must be g_free'd.
89      * </ol>
90      */
91     std::string sanitize (const StringView& filename);
92 
93     /**
94      * Check to see if the specifiled file exists.
95      * @return true if the file exists, false otherwise
96      * @param filename the file to check
97      */
98     bool file_exists (const char * filename);
99 
100     /**
101      * Removes extra '/' characters from the specified filename
102      * @param filename
103      * @return the filename pointer.
104      */
105     char* normalize_inplace (char * filename);
106 
107      /**
108       * Given the location of a text file, read it in and massage it
109       * to the point where it should be usable -- convert CR/LF to LF,
110       * and ensure that it's UTF8-clean.
111       */
112      bool get_text_file_contents (const StringView  & filename,
113                                   std::string       & setme,
114                                   const char        * fallback_charset_1=0,
115                                   const char        * fallback_charset_2=0);
116 
117   };
118 }
119 
120 #endif // _FileUtil_h_
121