ITK  6.0.0
Insight Toolkit
itkFileTools.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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  * https://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
38  CreateDirectory(const std::string & dir);
39 
41  static void
42  CreateFile(const std::string & fn);
43 };
44 
45 } // namespace itk
46 
47 // here comes the implementation
48 
49 #include "itksys/SystemTools.hxx"
50 #include "itkMacro.h"
51 
52 namespace itk
53 {
54 
56 inline void
57 FileTools::CreateDirectory(const std::string & dir)
58 {
59  if (dir.empty() || itksys::SystemTools::FileExists(dir.c_str(), true))
60  {
61  ExceptionObject eo(__FILE__, __LINE__, "directory cannot be created");
62  throw eo;
63  }
66  // do nothing if it already exists
67  if (dir.empty() || "." == dir || itksys::SystemTools::FileIsDirectory(dir.c_str()))
68  {
69  return;
70  }
71 
72  // create it
73  itksys::SystemTools::MakeDirectory(dir.c_str());
74 
75  // check successful or not
76  if (!itksys::SystemTools::FileIsDirectory(dir.c_str()))
77  {
78  ExceptionObject eo(__FILE__, __LINE__, "directory cannot be created");
79  throw eo;
80  }
81 }
82 
84 inline void
85 FileTools::CreateFile(const std::string & fn)
86 {
87  if (fn.empty() || itksys::SystemTools::FileIsDirectory(fn.c_str()))
88  {
89  ExceptionObject eo(__FILE__, __LINE__, "file cannot be created");
90  throw eo;
91  }
94  // do nothing if it already exists
95  if (itksys::SystemTools::FileExists(fn.c_str(), true))
96  {
97  return;
98  }
99 
100  // make sure the directory exists
101  std::string dir = itksys::SystemTools::GetFilenamePath(fn.c_str());
103 
104  // create the file
105  itksys::SystemTools::Touch(fn.c_str(), true);
106 
107  // check successful or not
108  if (!itksys::SystemTools::FileExists(fn.c_str(), true))
109  {
110  ExceptionObject eo(__FILE__, __LINE__, "file cannot be created");
111  throw eo;
112  }
113 }
114 
115 } // namespace itk
116 
117 #endif // itkFileTools_h
itk::FileTools::CreateDirectory
static void CreateDirectory(const std::string &dir)
Definition: itkFileTools.h:57
itkMacro.h
itk::FileTools
This is a helper class to provide file/directory manipulations such as file creation,...
Definition: itkFileTools.h:32
itk::FileTools::CreateFile
static void CreateFile(const std::string &fn)
Definition: itkFileTools.h:85
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnatomicalOrientation.h:29