directory.cpp

Go to the documentation of this file.
00001 #ifndef DIRECTORY_IMPLEMENTATION_FILE
00002 #define DIRECTORY_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : directory                                                         *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 2001-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
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;  // oops.
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];  // fairly long path allowed.
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(".");  // failure result.
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   // start reading the directory.
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;  // bad path.
00147   do {
00148     // ignore the two standard directory entries.
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 //temp
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 //wprintf(to_unicode_temp("file is %ls\n"), (wchar_t*)to_unicode_temp(filename_transcoded));
00161     
00162     filename temp_name(*_path, filename_transcoded.s());
00163 
00164     // add this to the appropriate list.
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 //hmmm: could check errno to determine what caused the problem.
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     // make sure that the filename matches the pattern also.
00193     if (add_it && !fnmatch(_pattern->s(), file, 0)) {
00194       filename temp_name(*_path, file);
00195       // add this to the appropriate list.
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     // check each location along the way.
00242     string_array partial = pieces.subarray(0, i);
00243     filename curr;
00244     curr.join(partial);  // this is our current location.
00245     // make sure, if we see a drive letter component, that we call it
00246     // a proper directory name.
00247     if (curr.raw()[curr.raw().end()] == ':')
00248       curr = curr.raw() + "/";
00249     if (curr.exists()) {
00250       if (curr.is_directory()) {
00251         continue;  // that's good.
00252       }
00253       return false;  // if it's an existing file, we're hosed.
00254     }
00255     // the directory at this place doesn't exist yet.  let's create it.
00256     if (!directory::make_directory(curr.raw())) return false;
00257   }
00258   return true;
00259 }
00260 
00261 
00262 #endif //DIRECTORY_IMPLEMENTATION_FILE
00263 

Generated on Sat Oct 11 04:28:48 2008 for HOOPLE Libraries by  doxygen 1.5.1