ITK  4.6.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 namespace itk
23 {
24 
30 class FileTools
31 {
32 public:
33 
35  static void CreateDirectory( const char* fn );
36 
38  static void CreateFile( const char* fn );
39 };
40 
41 } // namespace itk
42 
43 // here comes the implementation
44 
45 #include <string>
46 #include "itksys/SystemTools.hxx"
47 #include "itkMacro.h"
48 
49 namespace itk
50 {
51 
53 inline void
54 FileTools::CreateDirectory( const char* dir )
55 {
56  if ( dir == ITK_NULLPTR || itksys::SystemTools::FileExists(dir,true) )
57  {
58  ExceptionObject eo( __FILE__, __LINE__, "directory cannot be created" );
59  throw eo;
60  }
62 
63  // do nothing if it already exists
64  std::string s(dir);
65  if ( s == "" || s == "." || itksys::SystemTools::FileIsDirectory(dir) )
66  {
67  return;
68  }
69 
70  // create it
71  itksys::SystemTools::MakeDirectory( dir );
72 
73  // check successful or not
74  if ( !itksys::SystemTools::FileIsDirectory(dir) )
75  {
76  ExceptionObject eo( __FILE__, __LINE__, "directory cannot be created" );
77  throw eo;
78  }
79 }
80 
82 inline void
83 FileTools::CreateFile( const char* fn )
84 {
85  if ( fn == ITK_NULLPTR || *fn == 0 || itksys::SystemTools::FileIsDirectory(fn) )
86  {
87  ExceptionObject eo( __FILE__, __LINE__, "file cannot be created" );
88  throw eo;
89  }
91 
92  // do nothing if it already exists
93  if ( itksys::SystemTools::FileExists(fn,true) )
94  {
95  return;
96  }
97 
98  // make sure the directory exists
99  std::string dir = itksys::SystemTools::GetFilenamePath( fn );
100  FileTools::CreateDirectory( dir.c_str() );
101 
102  // create the file
103  itksys::SystemTools::Touch( fn, true );
104 
105  // check successful or not
106  if ( !itksys::SystemTools::FileExists(fn,true) )
107  {
108  ExceptionObject eo( __FILE__, __LINE__, "file cannot be created" );
109  throw eo;
110  }
111 }
112 
113 } // namespace itk
114 
115 #endif // __itkFileTools_h
static void CreateFile(const char *fn)
Definition: itkFileTools.h:83
Standard exception handling object.
This is a helper class to provide file/directory manipulations such as file creation, directory creation, etc.
Definition: itkFileTools.h:30
static void CreateDirectory(const char *fn)
Definition: itkFileTools.h:54