reSIProcate/rutil  9694
FileSystem.cxx
Go to the documentation of this file.
00001 #include "rutil/FileSystem.hxx"
00002 #include "rutil/Logger.hxx"
00003 
00004 #include <cassert>
00005 using namespace resip;
00006 
00007 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
00008 
00009 
00010 static FileSystem::Directory::iterator staticEnd;
00011 
00012 
00013 FileSystem::Directory::Directory(const Data& path)
00014    : mPath(path)
00015 {
00016 }
00017 
00018 
00019 #ifndef WIN32
00020 // !jf! added this constructor since it was missing - don't know if it is correct
00021 FileSystem::Directory::iterator::iterator() : mNixDir(0), mDirent(0)
00022 {
00023 }
00024 
00025 
00026 FileSystem::Directory::iterator::iterator(const Directory& dir)
00027 {
00028    assert(!dir.getPath().empty());   
00029    //InfoLog(<< "FileSystem::Directory::iterator::iterator: " << dir.getPath());   
00030    if ((mNixDir = opendir( dir.getPath().c_str() )))
00031    {
00032       mDirent = readdir(mNixDir);
00033       if (mDirent)
00034       {
00035          //InfoLog(<< "FileSystem::Directory::iterator::iterator, first file " << mFile);   
00036          mFile = mDirent->d_name;
00037       }
00038    }
00039    else
00040    {
00041       mDirent = 0;
00042    }
00043 }
00044 
00045 
00046 FileSystem::Directory::iterator::~iterator()
00047 {
00048    if (mNixDir)
00049    {
00050       closedir(mNixDir);
00051    }
00052 }
00053 
00054 
00055 FileSystem::Directory::iterator& 
00056 FileSystem::Directory::iterator::operator++()
00057 {
00058    if (mDirent)
00059    {
00060       mDirent = readdir(mNixDir);
00061       if (mDirent)
00062       {
00063          mFile = mDirent->d_name;
00064          //InfoLog(<< "FileSystem::Directory::iterator::iterator, next file " << mFile);   
00065       }
00066    }
00067    return *this;
00068 }
00069 
00070 
00071 bool 
00072 FileSystem::Directory::iterator::operator!=(const iterator& rhs) const
00073 {
00074    return !(*this == rhs);
00075 }
00076 
00077 
00078 bool 
00079 FileSystem::Directory::iterator::operator==(const iterator& rhs) const
00080 {
00081    if (mDirent && rhs.mDirent)
00082    {
00083       return **this == *rhs;
00084    }
00085 
00086    return mDirent == rhs.mDirent;
00087 }
00088 
00089 
00090 const Data& 
00091 FileSystem::Directory::iterator::operator*() const
00092 {
00093    return mFile;
00094 }
00095 
00096 
00097 const Data*
00098 FileSystem::Directory::iterator::operator->() const
00099 {
00100    return &mFile;
00101 }
00102 #else
00103 
00104 
00105 FileSystem::Directory::iterator::iterator() :
00106    mWinSearch(0)
00107 {
00108 }
00109 
00110 
00111 FileSystem::Directory::iterator::iterator(const Directory& dir)
00112 {
00113    Data searchPath;
00114    if (dir.getPath().postfix("/") || dir.getPath().postfix("\\"))
00115    {
00116       searchPath = dir.getPath() + Data("*");
00117    }
00118    else
00119    {
00120       searchPath = dir.getPath() + Data("/*");
00121    }
00122    WIN32_FIND_DATAA fileData;
00123    mWinSearch = FindFirstFileA( searchPath.c_str(), &fileData);
00124    
00125    if (mWinSearch == INVALID_HANDLE_VALUE)
00126    {
00127       mWinSearch = 0;
00128    }
00129    else
00130    {
00131       mFile = fileData.cFileName;
00132    }
00133 }
00134 
00135 
00136 FileSystem::Directory::iterator::~iterator()
00137 {
00138    if (mWinSearch)
00139    {
00140       FindClose(mWinSearch);
00141    }
00142 }
00143 
00144 
00145 FileSystem::Directory::iterator&
00146 FileSystem::Directory::iterator::operator++()
00147 {
00148    WIN32_FIND_DATAA fileData;
00149 
00150    if (!FindNextFileA(mWinSearch, &fileData))
00151    {
00152       if (GetLastError() == ERROR_NO_MORE_FILES)
00153       {
00154          FindClose(mWinSearch);
00155          mWinSearch = 0;
00156       }
00157    }
00158    else
00159    {
00160       mFile = fileData.cFileName;
00161    }  
00162    return *this;
00163 }
00164 
00165 
00166 bool 
00167 FileSystem::Directory::iterator::operator!=(const iterator& rhs) const
00168 {
00169    return !(*this == rhs);
00170 }
00171 
00172 
00173 bool 
00174 FileSystem::Directory::iterator::operator==(const iterator& rhs) const
00175 {
00176    if (mWinSearch && rhs.mWinSearch)
00177    {
00178       return **this == *rhs;
00179    }
00180    else
00181    {
00182       return mWinSearch == rhs.mWinSearch;
00183    }
00184 }
00185 
00186 
00187 const Data& 
00188 FileSystem::Directory::iterator::operator*() const
00189 {
00190    return mFile;
00191 }
00192 
00193 
00194 const Data* 
00195 FileSystem::Directory::iterator::operator->() const
00196 {
00197    return &mFile;
00198 }
00199 #endif
00200 
00201 
00202 FileSystem::Directory::iterator FileSystem::Directory::begin() const
00203 {
00204    return iterator(*this);   
00205 }
00206 
00207 
00208 FileSystem::Directory::iterator FileSystem::Directory::end() const
00209 {
00210    return staticEnd;   
00211 }
00212 
00213 /* ====================================================================
00214  * The Vovida Software License, Version 1.0 
00215  * 
00216  * Copyright (c) 2000-2005 Vovida Networks, Inc.  All rights reserved.
00217  * 
00218  * Redistribution and use in source and binary forms, with or without
00219  * modification, are permitted provided that the following conditions
00220  * are met:
00221  * 
00222  * 1. Redistributions of source code must retain the above copyright
00223  *    notice, this list of conditions and the following disclaimer.
00224  * 
00225  * 2. Redistributions in binary form must reproduce the above copyright
00226  *    notice, this list of conditions and the following disclaimer in
00227  *    the documentation and/or other materials provided with the
00228  *    distribution.
00229  * 
00230  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00231  *    and "Vovida Open Communication Application Library (VOCAL)" must
00232  *    not be used to endorse or promote products derived from this
00233  *    software without prior written permission. For written
00234  *    permission, please contact vocal@vovida.org.
00235  *
00236  * 4. Products derived from this software may not be called "VOCAL", nor
00237  *    may "VOCAL" appear in their name, without prior written
00238  *    permission of Vovida Networks, Inc.
00239  * 
00240  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00241  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00242  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00243  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00244  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00245  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00246  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00247  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00248  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00249  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00250  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00251  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00252  * DAMAGE.
00253  * 
00254  * ====================================================================
00255  * 
00256  * This software consists of voluntary contributions made by Vovida
00257  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00258  * Inc.  For more information on Vovida Networks, Inc., please see
00259  * <http://www.vovida.org/>.
00260  *
00261  */