file.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2008 Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 #ifndef CXXTOOLS_FILE_H
29 #define CXXTOOLS_FILE_H
30 
31 #include <cxxtools/fileinfo.h>
32 #include <cxxtools/ioerror.h>
33 #include <string>
34 
35 namespace cxxtools
36 {
37 
40 class File
41 {
42  public:
48  explicit File(const std::string& path);
49 
55  explicit File(const FileInfo& fi);
56 
58  File(const File& file);
59 
61  ~File();
62 
64  File& operator=(const File& file);
65 
71  const std::string& path() const
72  { return _path; }
73 
75  std::size_t size() const;
76 
85  std::string dirName() const;
86 
88  std::string name() const;
89 
91  std::string baseName() const;
92 
94  std::string extension() const;
95 
97  void resize(std::size_t n);
98 
103  void remove();
104 
110  void move(const std::string& to);
111 
112  void link(const std::string& newpath);
113 
114  void symlink(const std::string& newpath);
115 
116  void copy(const std::string& newpath);
117 
118  public:
120  static File create(const std::string& path);
121 
123  static bool exists(const std::string& path);
124 
125  protected:
127  File();
128 
129  private:
131  std::string _path;
132 };
133 
134 inline bool operator<(const File& a, const File& b)
135 {
136  return a.path() < b.path();
137 }
138 
139 inline bool operator==(const File& a, const File& b)
140 {
141  return a.path() == b.path();
142 }
143 
144 inline bool operator!=(const File& a, const File& b)
145 {
146  return !(a == b);
147 }
148 
149 } // namespace cxxtools
150 
151 #endif