1 /*****************************************************************************\
4 * Author : Chris Koeritz *
6 *******************************************************************************
7 * Copyright (c) 2001-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
15 #include "directory.h"
18 #include <algorithms/shell_sort.h>
19 #include <application/windoze_helper.h>
20 #include <basis/astring.h>
21 #include <basis/contracts.h>
22 #include <basis/functions.h>
23 #include <basis/utf_conversion.h>
24 #include <loggers/program_wide_logger.h>
25 #include <structures/string_array.h>
42 const int MAX_ABS_PATH = 2048;
43 #elif defined(__APPLE__)
44 const int MAX_ABS_PATH = 2048;
46 const int MAX_ABS_PATH = MAX_ABS_PATH;
51 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
53 using namespace algorithms;
54 using namespace basis;
55 using namespace loggers;
56 using namespace structures;
58 namespace filesystem {
60 directory::directory(const astring &path, const char *pattern)
61 : _scanned_okay(false),
63 _files(new string_array),
64 _folders(new string_array),
65 _pattern(new astring(pattern))
66 { reset(path, pattern); }
68 directory::directory(const directory &to_copy)
69 : _scanned_okay(false),
71 _files(new string_array),
72 _folders(new string_array),
74 { reset(*to_copy._path, to_copy._pattern->observe()); }
76 directory::~directory()
78 _scanned_okay = false;
85 const astring &directory::path() const { return *_path; }
87 const astring &directory::pattern() const { return *_pattern; }
89 directory &directory::operator =(const directory &to_copy)
91 if (this == &to_copy) return *this; // oops.
92 _scanned_okay = false;
93 reset(*to_copy._path, to_copy._pattern->observe());
97 astring directory::absolute_path(const astring &rel_path)
99 char abs_path[MAX_ABS_PATH + 1];
102 if (!_fullpath(abs_path, rel_path.s(), MAX_ABS_PATH)) return "";
105 if (!realpath(rel_path.s(), abs_path)) return "";
110 astring directory::current()
112 astring to_return("."); // failure result.
114 flexichar buffer[MAX_ABS_PATH + 1] = { '\0' };
115 GetCurrentDirectory(MAX_ABS_PATH, buffer);
116 to_return = from_unicode_temp(buffer);
118 char buffer[MAX_ABS_PATH + 1] = { '\0' };
119 if (realpath(".", buffer)) to_return = buffer;
124 bool directory::reset(const astring &path, const char *pattern)
125 { *_path = path; *_pattern = pattern; return rescan(); }
127 bool directory::move_up(const char *pattern)
129 astring currdir = current();
130 return reset(currdir + "/..", pattern);
133 bool directory::move_down(const astring &subdir, const char *pattern)
135 astring currdir = current();
136 return reset(currdir + "/" + subdir, pattern);
139 const string_array &directory::files() const { return *_files; }
141 const string_array &directory::directories() const { return *_folders; }
143 bool directory::rescan()
146 _scanned_okay = false;
149 astring cur_dir = ".";
150 astring par_dir = "..";
152 // start reading the directory.
154 astring real_path_spec = *_path + "/" + *_pattern;
155 HANDLE search_handle = FindFirstFile(to_unicode_temp(real_path_spec), &wfd);
156 if (search_handle == INVALID_HANDLE_VALUE) return false; // bad path.
158 // ignore the two standard directory entries.
159 astring filename_transcoded(from_unicode_temp(wfd.cFileName));
160 if (!strcmp(filename_transcoded.s(), cur_dir.s())) continue;
161 if (!strcmp(filename_transcoded.s(), par_dir.s())) continue;
165 to_unicode_persist(fudgemart, filename_transcoded);
166 if (memcmp((wchar_t*)fudgemart, wfd.cFileName, wcslen(wfd.cFileName)*2))
167 printf("failed to compare the string before and after transcoding\n");
170 //wprintf(to_unicode_temp("file is %ls\n"), (wchar_t*)to_unicode_temp(filename_transcoded));
172 filename temp_name(*_path, filename_transcoded.s());
174 // add this to the appropriate list.
175 if (temp_name.is_directory()) {
176 _folders->concatenate(filename_transcoded);
178 _files->concatenate(filename_transcoded);
181 to_unicode_persist(fudgemart2, temp_name.raw());
182 FILE *fpjunk = _wfopen(fudgemart2, to_unicode_temp("rb"));
184 LOG(astring("failed to open the file for testing: ") + temp_name.raw() + "\n");
185 if (fpjunk) fclose(fpjunk);
189 } while (FindNextFile(search_handle, &wfd));
190 FindClose(search_handle);
193 DIR *dir = opendir(_path->s());
194 //hmmm: could check errno to determine what caused the problem.
195 if (!dir) return false;
196 dirent *entry = readdir(dir);
198 char *file = entry->d_name;
200 if (!strcmp(file, cur_dir.s())) add_it = false;
201 if (!strcmp(file, par_dir.s())) add_it = false;
202 // make sure that the filename matches the pattern also.
203 if (add_it && !fnmatch(_pattern->s(), file, 0)) {
204 filename temp_name(*_path, file);
205 // add this to the appropriate list.
206 if (temp_name.is_directory())
207 _folders->concatenate(file);
209 _files->concatenate(file);
211 entry = readdir(dir);
215 shell_sort(_files->access(), _files->length());
216 shell_sort(_folders->access(), _folders->length());
218 _scanned_okay = true;
222 bool directory::make_directory(const astring &path)
225 int mk_ret = mkdir(path.s(), 0777);
228 int mk_ret = mkdir(path.s());
233 bool directory::remove_directory(const astring &path)
236 int rm_ret = rmdir(path.s());
239 int rm_ret = rmdir(path.s());
244 bool directory::recursive_create(const astring &directory_name)
246 // FUNCDEF("recursive_create");
247 filename dir(directory_name);
249 dir.separate(pieces);
250 for (int i = 0; i < pieces.length(); i++) {
251 // check each location along the way.
252 string_array partial = pieces.subarray(0, i);
254 curr.join(partial); // this is our current location.
255 // make sure, if we see a drive letter component, that we call it
256 // a proper directory name.
257 if (curr.raw()[curr.raw().end()] == ':')
258 curr = curr.raw() + "/";
260 if (curr.is_directory()) {
261 continue; // that's good.
263 return false; // if it's an existing file, we're hosed.
265 // the directory at this place doesn't exist yet. let's create it.
266 if (!directory::make_directory(curr.raw())) return false;