00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "data/directory.hpp"
00035 #include "data/pointer.hpp"
00036 #include "data/list.hpp"
00037 #include "data/file.hpp"
00038
00039 #include <cstdio>
00040 #include <cstring>
00041 #include <cerrno>
00042
00043 #ifdef WIN32
00044 #define WIN32_LEAN_AND_MEAN
00045 #include <windows.h>
00046 #include <direct.h>
00047 #include <io.h>
00048 #endif
00049
00050 namespace gsgl
00051 {
00052
00053 namespace io
00054 {
00055
00056 #ifdef WIN32
00057 const string directory::SEPARATOR(L"\\");
00058 #else
00059 #error Must define directory separator!
00060 #endif
00061
00062
00063
00064 directory::directory()
00065 : data_object()
00066 {
00067 data::smart_pointer<char, true> buf(new char[MAX_PATH_SIZE]);
00068 ::_getcwd(buf, MAX_PATH_SIZE);
00069
00070 name = string(buf);
00071 get_paths();
00072 }
00073
00074 directory::directory(const string & name)
00075 : data_object(), name(name)
00076 {
00077 get_paths();
00078 }
00079
00080 directory::directory(const directory & d)
00081 : data_object(), name(d.name)
00082 {
00083 get_paths();
00084 }
00085
00086 directory::~directory()
00087 {
00088 }
00089
00090 directory & directory::operator= (const directory & d)
00091 {
00092 files.clear();
00093 dirs.clear();
00094
00095 name = d.name;
00096 get_paths();
00097
00098 return *this;
00099 }
00100
00101
00102 int directory::compare(const comparable & d) const
00103 {
00104 const directory *dp = dynamic_cast<const directory *>(&d);
00105 if (dp)
00106 {
00107 return full_path.compare(dp->full_path);
00108 }
00109 else
00110 {
00111 throw internal_exception(__FILE__, __LINE__, L"Comparing different types.");
00112 }
00113 }
00114
00115
00116 void directory::get_paths()
00117 {
00118 #ifdef WIN32
00119 data::smart_pointer<wchar_t, true> buf(new wchar_t[MAX_PATH_SIZE]);
00120 wchar_t *fname;
00121 GetFullPathName(name.w_string(), MAX_PATH_SIZE, buf, &fname);
00122
00123 full_path = string(buf);
00124
00125 string end = full_path.right_substring(1);
00126
00127 if (!(end == L"\\" || end == L"/"))
00128 {
00129 full_path += SEPARATOR;
00130 }
00131 #endif
00132 }
00133
00134
00135 const string & directory::get_name() const
00136 {
00137 return name;
00138 }
00139
00140
00141 const string & directory::get_full_path() const
00142 {
00143 return full_path;
00144 }
00145
00146
00147 const data::list<file> & directory::get_files() const
00148 {
00149 files.clear();
00150
00151 #ifdef WIN32
00152 string wild = full_path + L"*";
00153
00154 WIN32_FIND_DATA data;
00155 HANDLE fh = FindFirstFile(wild.w_string(), &data);
00156 bool finding = fh != INVALID_HANDLE_VALUE;
00157 while (finding)
00158 {
00159 if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
00160 files.append(file(full_path + string(data.cFileName)));
00161 finding = FindNextFile(fh, &data) != 0;
00162 }
00163 if (fh != INVALID_HANDLE_VALUE)
00164 FindClose(fh);
00165 #else
00166 #error directory::get_files() not implemented
00167 #endif
00168
00169 return files;
00170 }
00171
00172
00173 const data::list<directory> & directory::get_dirs() const
00174 {
00175 dirs.clear();
00176
00177 #ifdef WIN32
00178 string wild = full_path + L"*";
00179
00180 WIN32_FIND_DATA data;
00181 HANDLE fh = FindFirstFile(wild.w_string(), &data);
00182 bool finding = fh != INVALID_HANDLE_VALUE;
00183 while (finding)
00184 {
00185 string entry_name(data.cFileName);
00186
00187 if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 && entry_name != string(L".") && entry_name != string(L".."))
00188 dirs.append(directory(full_path + string(data.cFileName)));
00189 finding = FindNextFile(fh, &data) != 0;
00190 }
00191 if (fh != INVALID_HANDLE_VALUE)
00192 FindClose(fh);
00193 #else
00194 #error directory::get_dirs() not implemented
00195 #endif
00196
00197 return dirs;
00198 }
00199
00200
00201 bool directory::exists(const string & path)
00202 {
00203 return ::access(path.c_string(), 00) == 0;
00204 }
00205
00206
00207 void directory::create(const string & dir)
00208 {
00209 if (::mkdir(dir.c_string()) == -1)
00210 throw io_exception(L"unable to create directory %ls: %hs", dir.w_string(), ::strerror(errno));
00211 }
00212
00213 }
00214
00215 }