libs/EDoc/DirectoryArchive.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003    Copyright (C) 2007 by Brendon Costa
00004 
00005    This library is free software; you can redistribute it and/or modify 
00006    it under the terms of the "LGPL Like" License defined in the file COPYING 
00007    that should have been distributed along with this source.
00008 
00009    This library is distributed in the hope that it will be useful, 
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of 
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013    You should have received a copy of the "LGPL Like" License 
00014    along with this library; see the file COPYING. if not, it can be 
00015    obtained from the EDoc++ website: 
00016    http://edoc.sourceforge.net/license.html
00017 
00018 *******************************************************************************/
00019 #include "config.h"
00020 
00021 #include "EDoc/DirectoryArchive.h"
00022 #include "EDoc/exceptions.h"
00023 #include "EDoc/utils.h"
00024 
00025 #ifdef HAVE_STRING_H
00026    #include <string.h>
00027 #endif
00028 
00029 #ifdef HAVE_SYS_STAT_H
00030    #include <sys/stat.h>
00031 #endif
00032 
00033 #ifdef HAVE_DIRENT_H
00034    #include <dirent.h>
00035 #endif
00036 
00037 #ifdef HAVE_ERRNO_H
00038    #include <errno.h>
00039 #endif
00040 
00041 
00042 namespace EDoc
00043 {
00044    //===========================================================================
00045    static bool IsDirectory(std::string dirname)
00046    {
00047       struct stat buffer;
00048       int result = stat(dirname.c_str(), &buffer);
00049       if (result == -1)
00050       {
00051          return false;
00052       }
00053       return buffer.st_mode & S_IFDIR;
00054    }
00055    //===========================================================================
00056    static std::list<ManagedFile> GetAllFiles(const ManagedFile& arch,
00057       std::string subdir)
00058    {
00059       DIR* dir;
00060       std::list<ManagedFile> ret;
00061 
00062       // Open the directory.
00063       std::string full_dir = arch.GetWorkFileName() + subdir;
00064       dir = opendir(full_dir.c_str());
00065       if (!dir)
00066       {
00067          std::ostringstream error_stream;
00068          error_stream   << "Failed to open the directory."
00069             << " Name: " << full_dir;
00070          EDOC_THROW_EXCEPTION(EDoc::FileIOException, strerror(errno), 
00071             error_stream.str());
00072       }
00073 
00074       for (struct dirent* entry = readdir(dir); entry; entry = readdir(dir))
00075       {
00076          // We need to do a stat of the current_entry->d_name
00077          // field, as the Linux man page says that using any fields other than
00078          // d_ino and d_name are non-portable as they are not defined by posix,
00079          // so we are unable to use d_type
00080          if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
00081          {
00082             continue;
00083          }
00084 
00085          std::string filename = full_dir + "/" + entry->d_name;
00086          if (IsDirectory(filename))
00087          {
00088             Append(ret, GetAllFiles(arch, subdir + "/" + entry->d_name));
00089          }
00090          else
00091          {
00092             // Add the subdir + name as an identifier within the base directory 
00093             // stored in arch.
00094             // This means that the complete path to the file: filename
00095             // should be stored asa seperate "work file".
00096             std::list<std::string> id = arch.GetIdentifier();
00097             id.push_back(subdir + "/" + entry->d_name);
00098             ret.push_back(ManagedFile(id, filename, NULL));
00099          }
00100       }
00101       
00102       closedir(dir);
00103 
00104       return ret;
00105    }
00106    //===========================================================================
00107    bool DirectoryArchive::HandlesType(const ManagedFile& dirname)
00108    {
00109       return IsDirectory(dirname.GetWorkFileName());
00110    }
00111    //===========================================================================
00112    std::list<ManagedFile> DirectoryArchive::ReadExtractAll(
00113       const ManagedFile& dirname)
00114    {
00115       return GetAllFiles(dirname, "");
00116    }
00117    //===========================================================================
00118    ManagedFile DirectoryArchive::WriteOpen(const ManagedFile& arch, 
00119       std::string item_filename)
00120    {
00121       std::list<std::string> id = arch.GetIdentifier();
00122       id.push_back(item_filename);
00123       
00124       return ManagedFile(id, arch.GetWorkFileName() + "/" + item_filename, NULL);
00125    }
00126    //===========================================================================
00127 }
00128 

Generated on Tue Jan 20 18:26:07 2009 for EDoc-0.2.1 by  doxygen 1.5.1