ITK  4.13.0
Insight Segmentation and Registration Toolkit
itkFileTools.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 
19 #ifndef itkFileTools_h
20 #define itkFileTools_h
21 
22 #include <string>
23 
24 namespace itk
25 {
26 
32 class FileTools
33 {
34 public:
35 
37  static void CreateDirectory( const std::string &fn );
38 
40  static void CreateFile( const std::string &fn );
41 
42 };
43 
44 } // namespace itk
45 
46 // here comes the implementation
47 
48 #include "itksys/SystemTools.hxx"
49 #include "itkMacro.h"
50 
51 namespace itk
52 {
53 
55 inline void
56 FileTools::CreateDirectory( const std::string &dir )
57 {
58  if ( dir.empty() || itksys::SystemTools::FileExists(dir.c_str(),true) )
59  {
60  ExceptionObject eo( __FILE__, __LINE__, "directory cannot be created" );
61  throw eo;
62  }
64 
65  // do nothing if it already exists
66  if ( "" == dir || "." == dir || itksys::SystemTools::FileIsDirectory(dir.c_str()) )
67  {
68  return;
69  }
70 
71  // create it
72  itksys::SystemTools::MakeDirectory( dir.c_str() );
73 
74  // check successful or not
75  if ( !itksys::SystemTools::FileIsDirectory( dir.c_str() ) )
76  {
77  ExceptionObject eo( __FILE__, __LINE__, "directory cannot be created" );
78  throw eo;
79  }
80 }
81 
83 inline void
84 FileTools::CreateFile( const std::string &fn )
85 {
86  if ( fn.empty() || itksys::SystemTools::FileIsDirectory( fn.c_str() ) )
87  {
88  ExceptionObject eo( __FILE__, __LINE__, "file cannot be created" );
89  throw eo;
90  }
92 
93  // do nothing if it already exists
94  if ( itksys::SystemTools::FileExists(fn.c_str() ,true) )
95  {
96  return;
97  }
98 
99  // make sure the directory exists
100  std::string dir = itksys::SystemTools::GetFilenamePath( fn.c_str() );
102 
103  // create the file
104  itksys::SystemTools::Touch( fn.c_str(), true );
105 
106  // check successful or not
107  if ( !itksys::SystemTools::FileExists( fn.c_str(), true) )
108  {
109  ExceptionObject eo( __FILE__, __LINE__, "file cannot be created" );
110  throw eo;
111  }
112 }
113 
114 } // namespace itk
115 
116 #endif // itkFileTools_h
static void CreateDirectory(const std::string &fn)
Definition: itkFileTools.h:56
static void CreateFile(const std::string &fn)
Definition: itkFileTools.h:84
Standard exception handling object.
This is a helper class to provide file/directory manipulations such as file creation, directory creation, etc.
Definition: itkFileTools.h:32