diff options
author | mathias <mathias> | 2004-12-18 01:29:22 (GMT) |
---|---|---|
committer | mathias <mathias> | 2004-12-18 01:29:22 (GMT) |
commit | b3fa5c242881b55a76c0e25b3cebaf15d0744f69 (patch) | |
tree | bcdb8e0df23428f3f3107e27e6af008d6a0409cc /src/FbTk/FileUtil.cc | |
parent | 6458b1b485c7481da6d2d1bf235b44fe3b700bd3 (diff) | |
download | fluxbox-b3fa5c242881b55a76c0e25b3cebaf15d0744f69.zip fluxbox-b3fa5c242881b55a76c0e25b3cebaf15d0744f69.tar.bz2 |
* moved FbTk/Directory.cc/hh over to FbTk/FileUtil.cc/hh which contain now
file and directory - helproutines.
* created the FileUtil-namespace which contains file-related functions, moved
those functions out of Directory - code
* changes to the rest of the files to follow those changes
Diffstat (limited to 'src/FbTk/FileUtil.cc')
-rw-r--r-- | src/FbTk/FileUtil.cc | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/src/FbTk/FileUtil.cc b/src/FbTk/FileUtil.cc new file mode 100644 index 0000000..b70eff3 --- /dev/null +++ b/src/FbTk/FileUtil.cc | |||
@@ -0,0 +1,150 @@ | |||
1 | // FileUtil.cc | ||
2 | // Copyright (c) 2002 - 2004 Henrik Kinnunen (fluxgen at users.sourceforge.net) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | // $Id$ | ||
23 | |||
24 | #include "FileUtil.hh" | ||
25 | |||
26 | #include <sys/stat.h> | ||
27 | #include <unistd.h> | ||
28 | |||
29 | #include <iostream> | ||
30 | #include <fstream> | ||
31 | |||
32 | using std::ifstream; | ||
33 | using std::ofstream; | ||
34 | using std::cerr; | ||
35 | using std::endl; | ||
36 | |||
37 | namespace FbTk { | ||
38 | |||
39 | time_t FileUtil::getLastStatusChangeTimestamp(const char* filename) { | ||
40 | struct stat buf; | ||
41 | if (filename && !stat(filename, &buf)) { | ||
42 | return buf.st_ctime; | ||
43 | } else | ||
44 | return (time_t)-1; | ||
45 | } | ||
46 | |||
47 | bool FileUtil::isDirectory(const char* filename) { | ||
48 | struct stat buf; | ||
49 | if (!filename || stat(filename, &buf)) | ||
50 | return false; | ||
51 | |||
52 | return S_ISDIR(buf.st_mode); | ||
53 | } | ||
54 | |||
55 | bool FileUtil::isRegularFile(const char* filename) { | ||
56 | struct stat buf; | ||
57 | if (!filename || stat(filename, &buf)) | ||
58 | return false; | ||
59 | |||
60 | return S_ISREG(buf.st_mode); | ||
61 | } | ||
62 | |||
63 | bool FileUtil::isExecutable(const char* filename) { | ||
64 | struct stat buf; | ||
65 | if (!filename || !stat(filename, &buf)) | ||
66 | return false; | ||
67 | |||
68 | return buf.st_mode & S_IXUSR || | ||
69 | buf.st_mode & S_IXGRP || | ||
70 | buf.st_mode & S_IXOTH; | ||
71 | } | ||
72 | |||
73 | bool FileUtil::copyFile(const char* from, const char* to) { | ||
74 | ifstream from_file(from); | ||
75 | ofstream to_file(to); | ||
76 | |||
77 | if (!to_file.good()) | ||
78 | cerr << "Can't write file '"<<to<<"'."<<endl; | ||
79 | else if (from_file.good()) { | ||
80 | to_file<<from_file.rdbuf(); | ||
81 | return true; | ||
82 | } else | ||
83 | cerr << "Can't copy from '"<<from<<"' to '"<<to<<"'."<<endl; | ||
84 | |||
85 | return false; | ||
86 | } | ||
87 | |||
88 | Directory::Directory(const char *dir):m_dir(0), | ||
89 | m_num_entries(0) { | ||
90 | if (dir != 0) | ||
91 | open(dir); | ||
92 | } | ||
93 | |||
94 | Directory::~Directory() { | ||
95 | close(); | ||
96 | } | ||
97 | |||
98 | void Directory::rewind() { | ||
99 | if (m_dir != 0) | ||
100 | rewinddir(m_dir); | ||
101 | } | ||
102 | |||
103 | struct dirent *Directory::read() { | ||
104 | if (m_dir == 0) | ||
105 | return 0; | ||
106 | |||
107 | return readdir(m_dir); | ||
108 | } | ||
109 | |||
110 | std::string Directory::readFilename() { | ||
111 | dirent *ent = read(); | ||
112 | if (ent == 0) | ||
113 | return ""; | ||
114 | return (ent->d_name ? ent->d_name : ""); | ||
115 | } | ||
116 | |||
117 | void Directory::close() { | ||
118 | if (m_dir != 0) { | ||
119 | closedir(m_dir); | ||
120 | m_name = ""; | ||
121 | m_dir = 0; | ||
122 | m_num_entries = 0; | ||
123 | } | ||
124 | } | ||
125 | |||
126 | |||
127 | bool Directory::open(const char *dir) { | ||
128 | if (dir == 0) | ||
129 | return false; | ||
130 | |||
131 | if (m_dir != 0) | ||
132 | close(); | ||
133 | |||
134 | m_dir = opendir(dir); | ||
135 | if (m_dir == 0) // successfull loading? | ||
136 | return false; | ||
137 | |||
138 | m_name= dir; | ||
139 | |||
140 | // get number of entries | ||
141 | while (read()) | ||
142 | m_num_entries++; | ||
143 | |||
144 | rewind(); // go back to start | ||
145 | |||
146 | return true; | ||
147 | } | ||
148 | |||
149 | |||
150 | }; // end namespace FbTk | ||