diff options
Diffstat (limited to 'src/FbTk')
-rw-r--r-- | src/FbTk/FileUtil.cc (renamed from src/FbTk/Directory.cc) | 88 | ||||
-rw-r--r-- | src/FbTk/FileUtil.hh (renamed from src/FbTk/Directory.hh) | 52 | ||||
-rw-r--r-- | src/FbTk/Makefile.am | 2 | ||||
-rw-r--r-- | src/FbTk/Theme.cc | 8 |
4 files changed, 103 insertions, 47 deletions
diff --git a/src/FbTk/Directory.cc b/src/FbTk/FileUtil.cc index 0535dc0..b70eff3 100644 --- a/src/FbTk/Directory.cc +++ b/src/FbTk/FileUtil.cc | |||
@@ -1,5 +1,5 @@ | |||
1 | // Directory.cc | 1 | // FileUtil.cc |
2 | // Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net) | 2 | // Copyright (c) 2002 - 2004 Henrik Kinnunen (fluxgen at users.sourceforge.net) |
3 | // | 3 | // |
4 | // Permission is hereby granted, free of charge, to any person obtaining a | 4 | // Permission is hereby granted, free of charge, to any person obtaining a |
5 | // copy of this software and associated documentation files (the "Software"), | 5 | // copy of this software and associated documentation files (the "Software"), |
@@ -21,13 +21,70 @@ | |||
21 | 21 | ||
22 | // $Id$ | 22 | // $Id$ |
23 | 23 | ||
24 | #include "Directory.hh" | 24 | #include "FileUtil.hh" |
25 | 25 | ||
26 | #include <sys/stat.h> | 26 | #include <sys/stat.h> |
27 | #include <unistd.h> | 27 | #include <unistd.h> |
28 | 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 | |||
29 | namespace FbTk { | 37 | namespace FbTk { |
30 | 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 | |||
31 | Directory::Directory(const char *dir):m_dir(0), | 88 | Directory::Directory(const char *dir):m_dir(0), |
32 | m_num_entries(0) { | 89 | m_num_entries(0) { |
33 | if (dir != 0) | 90 | if (dir != 0) |
@@ -89,30 +146,5 @@ bool Directory::open(const char *dir) { | |||
89 | return true; | 146 | return true; |
90 | } | 147 | } |
91 | 148 | ||
92 | bool Directory::isDirectory(const std::string &filename) { | ||
93 | struct stat statbuf; | ||
94 | if (stat(filename.c_str(), &statbuf) != 0) | ||
95 | return false; | ||
96 | |||
97 | return S_ISDIR(statbuf.st_mode); | ||
98 | } | ||
99 | |||
100 | bool Directory::isRegularFile(const std::string &filename) { | ||
101 | struct stat statbuf; | ||
102 | if (stat(filename.c_str(), &statbuf) != 0) | ||
103 | return false; | ||
104 | |||
105 | return S_ISREG(statbuf.st_mode); | ||
106 | } | ||
107 | |||
108 | bool Directory::isExecutable(const std::string &filename) { | ||
109 | struct stat statbuf; | ||
110 | if (stat(filename.c_str(), &statbuf) != 0) | ||
111 | return false; | ||
112 | |||
113 | return statbuf.st_mode & S_IXUSR || | ||
114 | statbuf.st_mode & S_IXGRP || | ||
115 | statbuf.st_mode & S_IXOTH; | ||
116 | } | ||
117 | 149 | ||
118 | }; // end namespace FbTk | 150 | }; // end namespace FbTk |
diff --git a/src/FbTk/Directory.hh b/src/FbTk/FileUtil.hh index f138ce7..718ccb8 100644 --- a/src/FbTk/Directory.hh +++ b/src/FbTk/FileUtil.hh | |||
@@ -1,5 +1,5 @@ | |||
1 | // Directory.hh | 1 | // FileUtil.hh |
2 | // Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net) | 2 | // Copyright (c) 2002 - 2004 Henrik Kinnunen (fluxgen at users.sourceforge.net) |
3 | // | 3 | // |
4 | // Permission is hereby granted, free of charge, to any person obtaining a | 4 | // Permission is hereby granted, free of charge, to any person obtaining a |
5 | // copy of this software and associated documentation files (the "Software"), | 5 | // copy of this software and associated documentation files (the "Software"), |
@@ -21,19 +21,49 @@ | |||
21 | 21 | ||
22 | // $Id$ | 22 | // $Id$ |
23 | 23 | ||
24 | #ifndef FBTK_DIRECTORY_HH | 24 | #ifndef FBTK_FILEUTIL_HH |
25 | #define FBTK_DIRECTORY_HH | 25 | #define FBTK_FILEUTIL_HH |
26 | |||
27 | #include "NotCopyable.hh" | ||
28 | 26 | ||
27 | #ifdef HAVE_CONFIG_H | ||
28 | #include "config.h" | ||
29 | #endif // HAVE_CONFIG_H | ||
30 | #ifdef HAVE_CTIME | ||
31 | #include <ctime> | ||
32 | #else | ||
33 | #include <time.h> | ||
34 | #endif | ||
29 | #include <sys/types.h> | 35 | #include <sys/types.h> |
30 | #include <dirent.h> | 36 | #include <dirent.h> |
37 | |||
31 | #include <string> | 38 | #include <string> |
32 | 39 | ||
40 | #include "NotCopyable.hh" | ||
41 | |||
33 | namespace FbTk { | 42 | namespace FbTk { |
34 | 43 | ||
44 | /// Wrapper for file routines | ||
45 | |||
46 | namespace FileUtil { | ||
47 | |||
48 | /// @return true if file is a directory | ||
49 | bool isDirectory(const char* filename); | ||
50 | /// @return true if a file is a regular file | ||
51 | bool isRegularFile(const char* filename); | ||
52 | /// @return true if a file executable for user | ||
53 | bool isExecutable(const char* filename); | ||
54 | |||
55 | /// gets timestamp of last status change | ||
56 | /// @return timestamp | ||
57 | /// @return -1 (failure) | ||
58 | time_t getLastStatusChangeTimestamp(const char* filename); | ||
59 | |||
60 | /// copies file 'from' to 'to' | ||
61 | bool copyFile(const char* from, const char* to); | ||
62 | |||
63 | }; // end of File namespace | ||
64 | |||
35 | /// Wrapper class for DIR * routines | 65 | /// Wrapper class for DIR * routines |
36 | class Directory: private FbTk::NotCopyable { | 66 | class Directory : private FbTk::NotCopyable { |
37 | public: | 67 | public: |
38 | explicit Directory(const char *dir = 0); | 68 | explicit Directory(const char *dir = 0); |
39 | ~Directory(); | 69 | ~Directory(); |
@@ -52,12 +82,6 @@ public: | |||
52 | bool open(const char *dir); | 82 | bool open(const char *dir); |
53 | /// @return number of entries in the directory | 83 | /// @return number of entries in the directory |
54 | size_t entries() const { return m_num_entries; } | 84 | size_t entries() const { return m_num_entries; } |
55 | /// @return true if file is a directory | ||
56 | static bool isDirectory(const std::string &filename); | ||
57 | /// @return true if a file is a regular file | ||
58 | static bool isRegularFile(const std::string &filename); | ||
59 | /// @return true if a file executable for user | ||
60 | static bool isExecutable(const std::string &filename); | ||
61 | 85 | ||
62 | private: | 86 | private: |
63 | std::string m_name; | 87 | std::string m_name; |
@@ -67,4 +91,4 @@ private: | |||
67 | 91 | ||
68 | } // end namespace FbTk | 92 | } // end namespace FbTk |
69 | 93 | ||
70 | #endif // FBTK_DIRECTORY_HH | 94 | #endif // FBTK_FILEUTIL_HH |
diff --git a/src/FbTk/Makefile.am b/src/FbTk/Makefile.am index 49217bc..be9b3ef 100644 --- a/src/FbTk/Makefile.am +++ b/src/FbTk/Makefile.am | |||
@@ -13,7 +13,7 @@ xpm_SOURCE= ImageXPM.hh ImageXPM.cc | |||
13 | endif | 13 | endif |
14 | 14 | ||
15 | libFbTk_a_SOURCES = App.hh App.cc Color.cc Color.hh Command.hh \ | 15 | libFbTk_a_SOURCES = App.hh App.cc Color.cc Color.hh Command.hh \ |
16 | Directory.hh Directory.cc \ | 16 | FileUtil.hh FileUtil.cc \ |
17 | EventHandler.hh EventManager.hh EventManager.cc \ | 17 | EventHandler.hh EventManager.hh EventManager.cc \ |
18 | FbWindow.hh FbWindow.cc Font.cc Font.hh FontImp.hh \ | 18 | FbWindow.hh FbWindow.cc Font.cc Font.hh FontImp.hh \ |
19 | I18n.cc I18n.hh \ | 19 | I18n.cc I18n.hh \ |
diff --git a/src/FbTk/Theme.cc b/src/FbTk/Theme.cc index 40c68e9..5225686 100644 --- a/src/FbTk/Theme.cc +++ b/src/FbTk/Theme.cc | |||
@@ -26,7 +26,7 @@ | |||
26 | #include "XrmDatabaseHelper.hh" | 26 | #include "XrmDatabaseHelper.hh" |
27 | #include "App.hh" | 27 | #include "App.hh" |
28 | #include "StringUtil.hh" | 28 | #include "StringUtil.hh" |
29 | #include "Directory.hh" | 29 | #include "FileUtil.hh" |
30 | #include "I18n.hh" | 30 | #include "I18n.hh" |
31 | #include "Image.hh" | 31 | #include "Image.hh" |
32 | 32 | ||
@@ -87,14 +87,14 @@ bool ThemeManager::load(const std::string &filename, int screen_num) { | |||
87 | std::string location = FbTk::StringUtil::expandFilename(filename); | 87 | std::string location = FbTk::StringUtil::expandFilename(filename); |
88 | std::string prefix = ""; | 88 | std::string prefix = ""; |
89 | 89 | ||
90 | if (Directory::isDirectory(filename)) { | 90 | if (FileUtil::isDirectory(filename.c_str())) { |
91 | prefix = location; | 91 | prefix = location; |
92 | 92 | ||
93 | location.append("/theme.cfg"); | 93 | location.append("/theme.cfg"); |
94 | if (!Directory::isRegularFile(location)) { | 94 | if (!FileUtil::isRegularFile(location.c_str())) { |
95 | location = prefix; | 95 | location = prefix; |
96 | location.append("/style.cfg"); | 96 | location.append("/style.cfg"); |
97 | if (!Directory::isRegularFile(location)) { | 97 | if (!FileUtil::isRegularFile(location.c_str())) { |
98 | cerr<<"Error loading theme file "<<location<<": not a regular file"<<endl; | 98 | cerr<<"Error loading theme file "<<location<<": not a regular file"<<endl; |
99 | return false; | 99 | return false; |
100 | } | 100 | } |