00001 #ifndef DIRECTORY_IMPLEMENTATION_FILE
00002 #define DIRECTORY_IMPLEMENTATION_FILE
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "directory.h"
00019 #include "filename.h"
00020
00021 #include <basis/convert_utf.h>
00022 #include <basis/function.h>
00023 #include <basis/istring.h>
00024 #include <basis/log_base.h>
00025 #include <basis/portable.h>
00026 #include <basis/shell_sort.h>
00027 #include <basis/string_array.h>
00028
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <sys/stat.h>
00032 #ifdef __UNIX__
00033 #include <dirent.h>
00034 #include <fnmatch.h>
00035 #include <string.h>
00036 #endif
00037 #ifdef __WIN32__
00038 #include <direct.h>
00039 #endif
00040
00041 #ifdef __WIN32__
00042 const int MAX_ABS_PATH = 2048;
00043 #else
00044 const int MAX_ABS_PATH = PATH_MAX;
00045 #endif
00046
00047 #undef LOG
00048 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger(), s)
00049
00050 directory::directory(const istring &path, const char *pattern)
00051 : _scanned_okay(false),
00052 _path(new istring),
00053 _files(new string_array),
00054 _folders(new string_array),
00055 _pattern(new istring(pattern))
00056 { reset(path, pattern); }
00057
00058 directory::directory(const directory &to_copy)
00059 : _scanned_okay(false),
00060 _path(new istring),
00061 _files(new string_array),
00062 _folders(new string_array),
00063 _pattern(new istring)
00064 { reset(*to_copy._path, to_copy._pattern->observe()); }
00065
00066 directory::~directory()
00067 {
00068 _scanned_okay = false;
00069 WHACK(_path);
00070 WHACK(_files);
00071 WHACK(_folders);
00072 WHACK(_pattern);
00073 }
00074
00075 const istring &directory::path() const { return *_path; }
00076
00077 const istring &directory::pattern() const { return *_pattern; }
00078
00079 directory &directory::operator =(const directory &to_copy)
00080 {
00081 if (this == &to_copy) return *this;
00082 _scanned_okay = false;
00083 reset(*to_copy._path, to_copy._pattern->observe());
00084 return *this;
00085 }
00086
00087 istring directory::absolute_path(const istring &rel_path)
00088 {
00089 char abs_path[MAX_ABS_PATH + 1];
00090 abs_path[0] = '\0';
00091 #ifdef __WIN32__
00092 if (!_fullpath(abs_path, rel_path.s(), MAX_ABS_PATH)) return "";
00093 return abs_path;
00094 #else
00095 if (!realpath(rel_path.s(), abs_path)) return "";
00096 return abs_path;
00097 #endif
00098 }
00099
00100 istring directory::current()
00101 {
00102 istring to_return(".");
00103 #ifdef __WIN32__
00104 flexichar buffer[MAX_ABS_PATH + 1] = { '\0' };
00105 GetCurrentDirectory(MAX_ABS_PATH, buffer);
00106 to_return = from_unicode_temp(buffer);
00107 #else
00108 char buffer[MAX_ABS_PATH + 1] = { '\0' };
00109 if (realpath(".", buffer)) to_return = buffer;
00110 #endif
00111 return to_return;
00112 }
00113
00114 bool directory::reset(const istring &path, const char *pattern)
00115 { *_path = path; *_pattern = pattern; return rescan(); }
00116
00117 bool directory::move_up(const char *pattern)
00118 {
00119 istring currdir = current();
00120 return reset(currdir + "/..", pattern);
00121 }
00122
00123 bool directory::move_down(const istring &subdir, const char *pattern)
00124 {
00125 istring currdir = current();
00126 return reset(currdir + "/" + subdir, pattern);
00127 }
00128
00129 const string_array &directory::files() const { return *_files; }
00130
00131 const string_array &directory::directories() const { return *_folders; }
00132
00133 bool directory::rescan()
00134 {
00135 FUNCDEF("rescan");
00136 _scanned_okay = false;
00137 _files->reset();
00138 _folders->reset();
00139 istring cur_dir = ".";
00140 istring par_dir = "..";
00141 #ifdef __WIN32__
00142
00143 WIN32_FIND_DATA wfd;
00144 istring real_path_spec = *_path + "/" + *_pattern;
00145 HANDLE search_handle = FindFirstFile(to_unicode_temp(real_path_spec), &wfd);
00146 if (search_handle == INVALID_HANDLE_VALUE) return false;
00147 do {
00148
00149 istring filename_transcoded(from_unicode_temp(wfd.cFileName));
00150 if (!strcmp(filename_transcoded.s(), cur_dir.s())) continue;
00151 if (!strcmp(filename_transcoded.s(), par_dir.s())) continue;
00152
00153 #ifdef UNICODE
00154
00155 to_unicode_persist(fudgemart, filename_transcoded);
00156 if (memcmp((wchar_t*)fudgemart, wfd.cFileName, wcslen(wfd.cFileName)*2))
00157 printf("failed to compare the string before and after transcoding\n");
00158 #endif
00159
00160
00161
00162 filename temp_name(*_path, filename_transcoded.s());
00163
00164
00165 if (temp_name.is_directory()) {
00166 _folders->concatenate(filename_transcoded);
00167 } else {
00168 _files->concatenate(filename_transcoded);
00169
00170 #ifdef UNICODE
00171 to_unicode_persist(fudgemart2, temp_name.raw());
00172 FILE *fpjunk = _wfopen(fudgemart2, to_unicode_temp("rb"));
00173 if (!fpjunk)
00174 LOG(istring("failed to open the file for testing: ") + temp_name.raw() + "\n");
00175 if (fpjunk) fclose(fpjunk);
00176 #endif
00177
00178 }
00179 } while (FindNextFile(search_handle, &wfd));
00180 FindClose(search_handle);
00181 #endif
00182 #ifdef __UNIX__
00183 DIR *dir = opendir(_path->s());
00184
00185 if (!dir) return false;
00186 dirent *entry = readdir(dir);
00187 while (entry) {
00188 char *file = entry->d_name;
00189 bool add_it = true;
00190 if (!strcmp(file, cur_dir.s())) add_it = false;
00191 if (!strcmp(file, par_dir.s())) add_it = false;
00192
00193 if (add_it && !fnmatch(_pattern->s(), file, 0)) {
00194 filename temp_name(*_path, file);
00195
00196 if (temp_name.is_directory())
00197 _folders->concatenate(file);
00198 else
00199 _files->concatenate(file);
00200 }
00201 entry = readdir(dir);
00202 }
00203 closedir(dir);
00204 #endif
00205 shell_sort(_files->access(), _files->length());
00206 shell_sort(_folders->access(), _folders->length());
00207
00208 _scanned_okay = true;
00209 return true;
00210 }
00211
00212 bool directory::make_directory(const istring &path)
00213 {
00214 #ifdef __UNIX__
00215 int mk_ret = mkdir(path.s(), 0777);
00216 #endif
00217 #ifdef __WIN32__
00218 int mk_ret = mkdir(path.s());
00219 #endif
00220 return !mk_ret;
00221 }
00222
00223 bool directory::remove_directory(const istring &path)
00224 {
00225 #ifdef __UNIX__
00226 int rm_ret = rmdir(path.s());
00227 #endif
00228 #ifdef __WIN32__
00229 int rm_ret = rmdir(path.s());
00230 #endif
00231 return !rm_ret;
00232 }
00233
00234 bool directory::recursive_create(const istring &directory_name)
00235 {
00236 FUNCDEF("recursive_create");
00237 filename dir(directory_name);
00238 string_array pieces;
00239 dir.separate(pieces);
00240 for (int i = 0; i < pieces.length(); i++) {
00241
00242 string_array partial = pieces.subarray(0, i);
00243 filename curr;
00244 curr.join(partial);
00245
00246
00247 if (curr.raw()[curr.raw().end()] == ':')
00248 curr = curr.raw() + "/";
00249 if (curr.exists()) {
00250 if (curr.is_directory()) {
00251 continue;
00252 }
00253 return false;
00254 }
00255
00256 if (!directory::make_directory(curr.raw())) return false;
00257 }
00258 return true;
00259 }
00260
00261
00262 #endif //DIRECTORY_IMPLEMENTATION_FILE
00263