Home | History | Annotate | Download | only in wx
      1 /*
      2  * Copyright (C) 2007 Kevin Ollivier
      3  * Copyright (C) 2008 Collabora, Ltd.
      4  * Copyright (C) 2009 Peter Laufenberg @ Inhance Digital Corp
      5  *
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     25  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 #include "config.h"
     31 #include "FileSystem.h"
     32 
     33 #include "CString.h"
     34 #include "NotImplemented.h"
     35 #include "PlatformString.h"
     36 
     37 #include <wx/wx.h>
     38 #include <wx/datetime.h>
     39 #include <wx/dir.h>
     40 #include <wx/dynlib.h>
     41 #include <wx/file.h>
     42 #include <wx/filefn.h>
     43 #include <wx/filename.h>
     44 
     45 #if OS(DARWIN)
     46 #include <CoreFoundation/CoreFoundation.h>
     47 #endif
     48 
     49 namespace WebCore {
     50 
     51 bool fileExists(const String& path)
     52 {
     53     // NOTE: This is called for directory paths too so we need to check both.
     54     return wxFileName::FileExists(path) || wxFileName::DirExists(path);
     55 }
     56 
     57 bool deleteFile(const String& path)
     58 {
     59     return wxRemoveFile(path);
     60 }
     61 
     62 bool deleteEmptyDirectory(const String& path)
     63 {
     64     return wxFileName::Rmdir(path);
     65 }
     66 
     67 bool getFileSize(const String& path, long long& resultSize)
     68 {
     69     wxULongLong size = wxFileName::GetSize(path);
     70     if (wxInvalidSize != size) {
     71         // TODO: why is FileSystem::getFileSize signed?
     72         resultSize = (long long)size.GetValue();
     73         return true;
     74     }
     75 
     76     return false;
     77 }
     78 
     79 bool getFileModificationTime(const String& path, time_t& t)
     80 {
     81     t = wxFileName(path).GetModificationTime().GetTicks();
     82     return true;
     83 }
     84 
     85 bool makeAllDirectories(const String& path)
     86 {
     87     return wxFileName::Mkdir(path, 0777, wxPATH_MKDIR_FULL);
     88 }
     89 
     90 String pathByAppendingComponent(const String& path, const String& component)
     91 {
     92     return wxFileName(path, component).GetFullPath();
     93 }
     94 
     95 String homeDirectoryPath()
     96 {
     97     return wxFileName::GetHomeDir();
     98 }
     99 
    100 String pathGetFileName(const String& path)
    101 {
    102     return wxFileName(path).GetFullName();
    103 }
    104 
    105 String directoryName(const String& path)
    106 {
    107     return wxFileName(path).GetPath();
    108 }
    109 
    110 CString openTemporaryFile(const char* prefix, PlatformFileHandle& handle)
    111 {
    112     notImplemented();
    113     handle = invalidPlatformFileHandle;
    114     return CString();
    115 }
    116 
    117 void closeFile(PlatformFileHandle&)
    118 {
    119     notImplemented();
    120 }
    121 
    122 int writeToFile(PlatformFileHandle, const char* data, int length)
    123 {
    124     notImplemented();
    125     return 0;
    126 }
    127 
    128 bool unloadModule(PlatformModule mod)
    129 {
    130 #if OS(WINDOWS)
    131     return ::FreeLibrary(mod);
    132 #elif OS(DARWIN)
    133     CFRelease(mod);
    134     return true;
    135 #else
    136     wxASSERT(mod);
    137     delete mod;
    138     return true;
    139 #endif
    140 }
    141 
    142 
    143 class wxDirTraverserNonRecursive : public wxDirTraverser {
    144 public:
    145     wxDirTraverserNonRecursive(wxString basePath, wxArrayString& files) : m_basePath(basePath), m_files(files) { }
    146 
    147     virtual wxDirTraverseResult OnFile(const wxString& filename)
    148     {
    149         wxFileName afile(filename);
    150         afile.MakeRelativeTo(m_basePath);
    151         if (afile.GetFullPath().Find(afile.GetPathSeparator()) == wxNOT_FOUND)
    152             m_files.push_back(filename);
    153 
    154         return wxDIR_CONTINUE;
    155     }
    156 
    157     virtual wxDirTraverseResult OnDir(const wxString& dirname)
    158     {
    159         wxFileName dirfile(dirname);
    160         dirfile.MakeRelativeTo(m_basePath);
    161         if (dirfile.GetFullPath().Find(dirfile.GetPathSeparator()) == wxNOT_FOUND)
    162             m_files.push_back(dirname);
    163 
    164         return wxDIR_CONTINUE;
    165     }
    166 
    167 private:
    168     wxString m_basePath;
    169     wxArrayString& m_files;
    170 
    171     DECLARE_NO_COPY_CLASS(wxDirTraverserNonRecursive)
    172 };
    173 
    174 Vector<String> listDirectory(const String& path, const String& filter)
    175 {
    176     wxArrayString   file_paths;
    177     // wxDir::GetAllFiles recurses and for platforms like Mac where
    178     // a .plugin or .bundle can be a dir wx will recurse into the bundle
    179     // and list the files rather than just returning the plugin name, so
    180     // we write a special traverser that works around that issue.
    181     wxDirTraverserNonRecursive traverser(path, file_paths);
    182 
    183     wxDir dir(path);
    184     dir.Traverse(traverser, _T(""), wxDIR_FILES | wxDIR_DIRS);
    185 
    186     Vector<String> entries;
    187 
    188     for (int i = 0; i < file_paths.GetCount(); i++)
    189     {
    190         entries.append(file_paths[i]);
    191     }
    192 
    193     return entries;
    194 }
    195 
    196 }
    197