Esempio n. 1
0
bool wxDirData::Read(wxString *filename)
{
    bool first = false;

    WIN32_FIND_DATA finddata;
    #define PTR_TO_FINDDATA (&finddata)

    if ( !IsFindDataOk(m_finddata) )
    {
        // open first
        wxString filespec = m_dirname;
        if ( !wxEndsWithPathSeparator(filespec) )
        {
            filespec += wxT('\\');
        }
        if ( !m_filespec )
            filespec += wxT("*.*");
        else
            filespec += m_filespec;

        m_finddata = FindFirst(filespec, PTR_TO_FINDDATA);

        first = true;
    }

    if ( !IsFindDataOk(m_finddata) )
    {
#ifdef __WIN32__
        DWORD err = ::GetLastError();

        if ( err != ERROR_FILE_NOT_FOUND && err != ERROR_NO_MORE_FILES )
        {
            wxLogSysError(err, _("Can not enumerate files in directory '%s'"),
                          m_dirname.c_str());
        }
#endif // __WIN32__
        //else: not an error, just no (such) files

        return false;
    }

    const wxChar *name;
    FIND_ATTR attr;

    for ( ;; )
    {
        if ( first )
        {
            first = false;
        }
        else
        {
            if ( !FindNext(m_finddata, PTR_TO_FINDDATA) )
            {
#ifdef __WIN32__
                DWORD err = ::GetLastError();

                if ( err != ERROR_NO_MORE_FILES )
                {
                    wxLogLastError(wxT("FindNext"));
                }
#endif // __WIN32__
                //else: not an error, just no more (such) files

                return false;
            }
        }

        name = GetNameFromFindData(PTR_TO_FINDDATA);
        attr = GetAttrFromFindData(PTR_TO_FINDDATA);

        // don't return "." and ".." unless asked for
        if ( name[0] == wxT('.') &&
             ((name[1] == wxT('.') && name[2] == wxT('\0')) ||
              (name[1] == wxT('\0'))) )
        {
            if ( !(m_flags & wxDIR_DOTDOT) )
                continue;
        }

        // check the type now
        if ( !(m_flags & wxDIR_FILES) && !IsDir(attr) )
        {
            // it's a file, but we don't want them
            continue;
        }
        else if ( !(m_flags & wxDIR_DIRS) && IsDir(attr) )
        {
            // it's a dir, and we don't want it
            continue;
        }

        // finally, check whether it's a hidden file
        if ( !(m_flags & wxDIR_HIDDEN) )
        {
            if ( IsHidden(attr) )
            {
                // it's a hidden file, skip it
                continue;
            }
        }

        *filename = name;

        break;
    }

    return true;
}
Esempio n. 2
0
bool wxDirData::Read(
  wxString*                         psFilename
)
{
    bool                            bFirst = false;

    FILEFINDBUF3                    vFinddata;
    #define PTR_TO_FINDDATA (&vFinddata)

    if (!IsFindDataOk(m_vFinddata))
    {
        //
        // Open first
        //
        wxString                    sFilespec = m_sDirname;

        if ( !wxEndsWithPathSeparator(sFilespec) )
        {
            sFilespec += wxT('\\');
        }
        sFilespec += (!m_sFilespec ? wxT("*.*") : m_sFilespec.c_str());

        m_vFinddata = FindFirst( sFilespec
                                ,PTR_TO_FINDDATA
                               );
        bFirst = true;
    }

    if ( !IsFindDataOk(m_vFinddata) )
    {
        return false;
    }

    const wxChar*                   zName;
    FIND_ATTR                       vAttr;

    for ( ;; )
    {
        if (bFirst)
        {
            bFirst = false;
        }
        else
        {
            if (!FindNext( m_vFinddata
                          ,PTR_TO_FINDDATA
                         ))
            {
                return false;
            }
        }

        zName = GetNameFromFindData(PTR_TO_FINDDATA);
        vAttr = GetAttrFromFindData(PTR_TO_FINDDATA);

        //
        // Don't return "." and ".." unless asked for
        //
        if ( zName[0] == wxT('.') &&
             ((zName[1] == wxT('.') && zName[2] == wxT('\0')) ||
              (zName[1] == wxT('\0'))) )
        {
            if (!(m_nFlags & wxDIR_DOTDOT))
                continue;
        }

        //
        // Check the type now
        //
        if (!(m_nFlags & wxDIR_FILES) && !IsDir(vAttr))
        {
            //
            // It's a file, but we don't want them
            //
            continue;
        }
        else if (!(m_nFlags & wxDIR_DIRS) && IsDir(vAttr) )
        {
            //
            // It's a dir, and we don't want it
            //
            continue;
        }

        //
        // Finally, check whether it's a hidden file
        //
        if (!(m_nFlags & wxDIR_HIDDEN))
        {
            if (IsHidden(vAttr))
            {
                //
                // It's a hidden file, skip it
                //
                continue;
            }
        }
        *psFilename = zName;
        break;
    }
    return true;
} // end of wxDirData::Read