directory.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : directory                                                         *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 2001-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include "directory.h"
00016 #include "filename.h"
00017 
00018 #include <algorithms/shell_sort.h>
00019 #include <application/windoze_helper.h>
00020 #include <basis/astring.h>
00021 #include <basis/contracts.h>
00022 #include <basis/functions.h>
00023 #include <basis/utf_conversion.h>
00024 #include <loggers/program_wide_logger.h>
00025 #include <structures/string_array.h>
00026 
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <sys/stat.h>
00030 #ifdef __UNIX__
00031   #include <dirent.h>
00032   #include <fnmatch.h>
00033   #include <string.h>
00034   #include <unistd.h>
00035 #endif
00036 #ifdef __WIN32__
00037   #include <direct.h>
00038 #endif
00039 
00040 /*
00041 #ifdef __WIN32__
00042   const int MAX_ABS_PATH = 2048;
00043 #elif defined(__APPLE__)
00044   const int MAX_ABS_PATH = 2048;
00045 #else
00046   const int MAX_ABS_PATH = MAX_ABS_PATH;
00047 #endif
00048 */
00049 
00050 #undef LOG
00051 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
00052 
00053 using namespace algorithms;
00054 using namespace basis;
00055 using namespace loggers;
00056 using namespace structures;
00057 
00058 namespace filesystem {
00059 
00060 directory::directory(const astring &path, const char *pattern)
00061 : _scanned_okay(false),
00062   _path(new astring),
00063   _files(new string_array),
00064   _folders(new string_array),
00065   _pattern(new astring(pattern))
00066 { reset(path, pattern); }
00067 
00068 directory::directory(const directory &to_copy)
00069 : _scanned_okay(false),
00070   _path(new astring),
00071   _files(new string_array),
00072   _folders(new string_array),
00073   _pattern(new astring)
00074 { reset(*to_copy._path, to_copy._pattern->observe()); }
00075 
00076 directory::~directory()
00077 {
00078   _scanned_okay = false;
00079   WHACK(_path);
00080   WHACK(_files);
00081   WHACK(_folders);
00082   WHACK(_pattern);
00083 }
00084 
00085 const astring &directory::path() const { return *_path; }
00086 
00087 const astring &directory::pattern() const { return *_pattern; }
00088 
00089 directory &directory::operator =(const directory &to_copy)
00090 {
00091   if (this == &to_copy) return *this;  // oops.
00092   _scanned_okay = false;
00093   reset(*to_copy._path, to_copy._pattern->observe());
00094   return *this;
00095 }
00096 
00097 astring directory::absolute_path(const astring &rel_path)
00098 {
00099   char abs_path[MAX_ABS_PATH + 1];
00100   abs_path[0] = '\0';
00101 #ifdef __WIN32__
00102   if (!_fullpath(abs_path, rel_path.s(), MAX_ABS_PATH)) return "";
00103   return abs_path;
00104 #else
00105   if (!realpath(rel_path.s(), abs_path)) return "";
00106   return abs_path;
00107 #endif
00108 }
00109 
00110 astring directory::current()
00111 {
00112   astring to_return(".");  // failure result.
00113 #ifdef __WIN32__
00114   flexichar buffer[MAX_ABS_PATH + 1] = { '\0' };
00115   GetCurrentDirectory(MAX_ABS_PATH, buffer);
00116   to_return = from_unicode_temp(buffer);
00117 #else
00118   char buffer[MAX_ABS_PATH + 1] = { '\0' };
00119   if (realpath(".", buffer)) to_return = buffer;
00120 #endif
00121   return to_return;
00122 }
00123 
00124 bool directory::reset(const astring &path, const char *pattern)
00125 { *_path = path; *_pattern = pattern; return rescan(); }
00126 
00127 bool directory::move_up(const char *pattern)
00128 {
00129   astring currdir = current();
00130   return reset(currdir + "/..", pattern);
00131 }
00132 
00133 bool directory::move_down(const astring &subdir, const char *pattern)
00134 {
00135   astring currdir = current();
00136   return reset(currdir + "/" + subdir, pattern);
00137 }
00138 
00139 const string_array &directory::files() const { return *_files; }
00140 
00141 const string_array &directory::directories() const { return *_folders; }
00142 
00143 bool directory::rescan()
00144 {
00145   FUNCDEF("rescan");
00146   _scanned_okay = false;
00147   _files->reset();
00148   _folders->reset();
00149   astring cur_dir = ".";
00150   astring par_dir = "..";
00151 #ifdef __WIN32__
00152   // start reading the directory.
00153   WIN32_FIND_DATA wfd;
00154   astring real_path_spec = *_path + "/" + *_pattern;
00155   HANDLE search_handle = FindFirstFile(to_unicode_temp(real_path_spec), &wfd);
00156   if (search_handle == INVALID_HANDLE_VALUE) return false;  // bad path.
00157   do {
00158     // ignore the two standard directory entries.
00159     astring filename_transcoded(from_unicode_temp(wfd.cFileName));
00160     if (!strcmp(filename_transcoded.s(), cur_dir.s())) continue;
00161     if (!strcmp(filename_transcoded.s(), par_dir.s())) continue;
00162 
00163 #ifdef UNICODE
00164 //temp
00165     to_unicode_persist(fudgemart, filename_transcoded);
00166     if (memcmp((wchar_t*)fudgemart, wfd.cFileName, wcslen(wfd.cFileName)*2))
00167       printf("failed to compare the string before and after transcoding\n");
00168 #endif
00169 
00170 //wprintf(to_unicode_temp("file is %ls\n"), (wchar_t*)to_unicode_temp(filename_transcoded));
00171     
00172     filename temp_name(*_path, filename_transcoded.s());
00173 
00174     // add this to the appropriate list.
00175     if (temp_name.is_directory()) {
00176       _folders->concatenate(filename_transcoded);
00177     } else {
00178       _files->concatenate(filename_transcoded);
00179 
00180 #ifdef UNICODE
00181       to_unicode_persist(fudgemart2, temp_name.raw());
00182       FILE *fpjunk = _wfopen(fudgemart2, to_unicode_temp("rb"));
00183       if (!fpjunk)
00184         LOG(astring("failed to open the file for testing: ") + temp_name.raw() + "\n");
00185       if (fpjunk) fclose(fpjunk);
00186 #endif
00187 
00188   }
00189   } while (FindNextFile(search_handle, &wfd));
00190   FindClose(search_handle);
00191 #endif
00192 #ifdef __UNIX__
00193   DIR *dir = opendir(_path->s());
00194 //hmmm: could check errno to determine what caused the problem.
00195   if (!dir) return false;
00196   dirent *entry = readdir(dir);
00197   while (entry) {
00198     char *file = entry->d_name;
00199     bool add_it = true;
00200     if (!strcmp(file, cur_dir.s())) add_it = false;
00201     if (!strcmp(file, par_dir.s())) add_it = false;
00202     // make sure that the filename matches the pattern also.
00203     if (add_it && !fnmatch(_pattern->s(), file, 0)) {
00204       filename temp_name(*_path, file);
00205       // add this to the appropriate list.
00206       if (temp_name.is_directory())
00207         _folders->concatenate(file);
00208       else 
00209         _files->concatenate(file);
00210     }
00211     entry = readdir(dir);
00212   }
00213   closedir(dir);
00214 #endif
00215   shell_sort(_files->access(), _files->length());
00216   shell_sort(_folders->access(), _folders->length());
00217 
00218   _scanned_okay = true;
00219   return true;
00220 }
00221 
00222 bool directory::make_directory(const astring &path)
00223 {
00224 #ifdef __UNIX__
00225   int mk_ret = mkdir(path.s(), 0777);
00226 #endif
00227 #ifdef __WIN32__
00228   int mk_ret = mkdir(path.s());
00229 #endif
00230   return !mk_ret;
00231 }
00232 
00233 bool directory::remove_directory(const astring &path)
00234 {
00235 #ifdef __UNIX__
00236   int rm_ret = rmdir(path.s());
00237 #endif
00238 #ifdef __WIN32__
00239   int rm_ret = rmdir(path.s());
00240 #endif
00241   return !rm_ret;
00242 }
00243 
00244 bool directory::recursive_create(const astring &directory_name)
00245 {
00246 //  FUNCDEF("recursive_create");
00247   filename dir(directory_name);
00248   string_array pieces;
00249   dir.separate(pieces);
00250   for (int i = 0; i < pieces.length(); i++) {
00251     // check each location along the way.
00252     string_array partial = pieces.subarray(0, i);
00253     filename curr;
00254     curr.join(partial);  // this is our current location.
00255     // make sure, if we see a drive letter component, that we call it
00256     // a proper directory name.
00257     if (curr.raw()[curr.raw().end()] == ':')
00258       curr = curr.raw() + "/";
00259     if (curr.exists()) {
00260       if (curr.is_directory()) {
00261         continue;  // that's good.
00262       }
00263       return false;  // if it's an existing file, we're hosed.
00264     }
00265     // the directory at this place doesn't exist yet.  let's create it.
00266     if (!directory::make_directory(curr.raw())) return false;
00267   }
00268   return true;
00269 }
00270 
00271 } // namespace.
Generated on Sat Jan 28 04:22:18 2012 for hoople2 project by  doxygen 1.6.3