~ubuntu-branches/ubuntu/trusty/fluxbox/trusty-proposed

« back to all changes in this revision

Viewing changes to src/FbTk/FileUtil.cc

  • Committer: Bazaar Package Importer
  • Author(s): Dmitry E. Oboukhov
  • Date: 2008-07-01 10:38:14 UTC
  • mfrom: (2.1.12 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080701103814-khx2b6il152x9p93
Tags: 1.0.0+deb1-8
* x-dev has been removed from build-depends (out-of-date package).
* Standards-Version bumped to 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// FileUtil.cc
2
 
// Copyright (c) 2002 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
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: FileUtil.cc 3864 2005-01-24 18:02:34Z mathias $
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