libcrn  3.9.5
A document image processing library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNIO.h
Go to the documentation of this file.
1 /* Copyright 2006-2012 Yann LEYDIER, CoReNum, INSA Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn 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 Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNIO.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNIO_HEADER
23 #define CRNIO_HEADER
24 
25 #include <CRNString.h>
26 #include <CRNIO/CRNPath.h>
27 #ifdef _MSC_VER
28 # include <io.h>
29 #endif
30 #include <CRNIO/CRNMessenger.h>
31 
34 namespace crn
35 {
43  class IO
44  {
45  /*************************************************************************/
46  /* Messages **************************************************************/
47  /*************************************************************************/
48  public:
50  static void Debug(const String &msg);
52  static void Warning(const String &msg);
54  static void Verbose(const String &msg);
56  static void Error(const String &msg);
57 
59  static std::shared_ptr<Messenger>& CurrentMessenger();
60 
62  static bool& IsQuiet();
64  static bool& IsVerbose();
65 
66  /*************************************************************************/
67  /* Files and directories *************************************************/
68  /*************************************************************************/
69  public:
71  static void Mkdir(const Path &name);
73  static void Rm(const Path &name);
75  static void ShieldRm(const Path &name);
77  static void Rmdir(const Path &name);
79  enum {
80  EXISTS =
81 #ifdef F_OK
82  F_OK,
83 #else
84  0,
85 #endif
86  READ =
87 #ifdef R_OK
88  R_OK,
89 #else
90  4,
91 #endif
93 #ifdef W_OK
94  W_OK,
95 #else
96  2,
97 #endif
99 #ifdef X_OK
100  X_OK
101 #else
102  1
103 #endif
104  };
106  static bool Access(const Path &name, int mode);
108  static void Copy(const Path &src, const Path &dst);
110  static void ShieldCopy(const Path &src, const Path &dst);
111 
116  class Directory
117  {
118  public:
120  Directory(const Path &path);
122  inline const std::vector<Path>& GetFiles() const { return files; }
124  inline const std::vector<Path>& GetDirs() const { return directories; }
125 
126  private:
127  std::vector<Path> files;
128  std::vector<Path> directories;
129  };
130 
131  };
132 }
133 
136 #ifdef DEBUG
137 # define CRNDebug(x) crn::IO::Debug(x)
138 # include <iostream>
139 # define CRNdout std::cout
140 #else
141 # define CRNDebug(x)
142 # include <fstream>
143 # define CRNdout std::ofstream()
144 #endif
145 #define CRNWarning(x) crn::IO::Warning(x)
146 #define CRNVerbose(x) crn::IO::Verbose(x)
147 #define CRNError(x) crn::IO::Error(x)
148 #ifdef PRINTOPTIM
149 # define CRNOptimNeeded(x) crn::IO::Warning(String(U"Optimization needed: ") + x)
150 #else
151 # define CRNOptimNeeded(x)
152 #endif
153 
154 #endif
155 
156 
static void Warning(const String &msg)
Prints warning messages. Please use CRNWarning() macro instead.
Definition: CRNIO.cpp:94
static void ShieldRm(const Path &name)
Removes a file and protects it with mutex.
Definition: CRNIO.cpp:193
static void Debug(const String &msg)
Prints debug messages. Please use CRNDebug() macro instead.
Definition: CRNIO.cpp:80
static std::shared_ptr< Messenger > & CurrentMessenger()
Delegate that will print the messages.
Definition: CRNIO.cpp:68
static void Copy(const Path &src, const Path &dst)
Copies a file.
Definition: CRNIO.cpp:263
static void Mkdir(const Path &name)
Creates a directory.
Definition: CRNIO.cpp:137
A handler to the content of a directory.
Definition: CRNIO.h:116
A UTF32 character string class.
Definition: CRNString.h:61
static bool & IsQuiet()
If true, Debug, Warning, Verbose and Error don't print anything.
Definition: CRNIO.cpp:48
static bool & IsVerbose()
Controls whether CRNVerbose prints something or not.
Definition: CRNIO.cpp:58
static void Verbose(const String &msg)
Prints verbose messages. Please use CRNVerbose() macro instead.
Definition: CRNIO.cpp:108
A convenience class for file paths.
Definition: CRNPath.h:39
General purpose IO class.
Definition: CRNIO.h:43
static void Error(const String &msg)
Prints error messages. Please use CRNError() macro instead.
Definition: CRNIO.cpp:122
static void Rmdir(const Path &name)
Recursively removes a directory.
Definition: CRNIO.cpp:205
const std::vector< Path > & GetDirs() const
Returns the list of directories.
Definition: CRNIO.h:124
static void ShieldCopy(const Path &src, const Path &dst)
Copies a file and protects source and destination with mutex.
Definition: CRNIO.cpp:295
const std::vector< Path > & GetFiles() const
Returns the list of files.
Definition: CRNIO.h:122
static void Rm(const Path &name)
Removes a file.
Definition: CRNIO.cpp:175
static bool Access(const Path &name, int mode)
Checks rights on a file.
Definition: CRNIO.cpp:161
Directory(const Path &path)
Constructor.
Definition: CRNIO.cpp:311