os/ossrv/genericopenlibs/openenvcore/libc/src/wfindfirst.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // Name        : wfindfirst.cpp
    15 // Part of     : libc library
    16 // The _wfindfirst function provides information about the first 
    17 // instance of a file name that matches the file specified in the 
    18 // filespec argument. Any wildcard combination supported by the host 
    19 // operating system can be used in filespec. 
    20 // File information is returned in a _wfinddata_t structure, defined in wchar.h
    21 // FUNCTIONS
    22 // _wfindfirst, _wfindnext, _findclose
    23 // INDEX
    24 // _wfindfirst
    25 // _wfindnext
    26 // _findclose
    27 // ANSI_SYNOPSIS
    28 // #include <wchar.h>
    29 // int _wfindnext(intptr_t, struct _wfinddata_t *); 
    30 // intptr_t wfindfirst(const wchar_t* , struct _wfinddata_t* );
    31 // int findclose( intptr_t handle);
    32 // The _wfindfirst function provides information about the first 
    33 // instance of a file name that matches the file specified in the 
    34 // filespec argument. Any wildcard combination supported by the host 
    35 // operating system can be used in filespec. 
    36 // File information is returned in a _wfinddata_t structure, defined in wchar.h
    37 // wfindnext find the next name, if any, that matches the filespec argument 
    38 // in a previous call to _findfirst, and then alter the fileinfo structure 
    39 // contents accordingly.
    40 // _findclose  
    41 // Closes the specified search handle and releases associated resources.
    42 // RETURNS
    43 // _findclose
    44 // If successful, returns 0. Otherwise, it returns –1 and sets errno to ENOENT
    45 // _wfindnext
    46 // If successful, returns 0. Otherwise, returns –1 and sets errno to a value indicating the nature of the failure. Possible error codes are shown below. 
    47 // EINVAL
    48 // Invalid parameter: fileinfo was NULL. Or, the operating system returned an unexpected error.
    49 // ENOENT
    50 // No more matching files could be found.
    51 // _wfindfirst
    52 // If successful, _findfirst returns a unique search handle identifying the file or group of files
    53 // matching the filespec specification, which can be used in a subsequent call to _findnext or to 
    54 // _findclose. Otherwise, _findfirst returns –1 and sets errno to one of the following values.
    55 // EINVAL
    56 // Invalid parameter: filespec or fileinfo was NULL. Or, the operating system returned an unexpected error.
    57 // ENOENT
    58 // File specification that could not be matched.
    59 // ENOMEM
    60 // Not enough memory or the file name given was greater than MAX_PATH.
    61 // EINVAL
    62 // Invalid file name specification.
    63 //
    64 
    65 
    66 
    67 
    68 #include <stdlib.h>
    69 #include <wchar.h>
    70 #include <errno.h> 
    71 #include "sysif.h"
    72 #include<e32base.h>
    73 #include<e32cmn.h>
    74 #include<ltime.h>
    75 #define MAXPATHLEN  260 
    76 #define ATTMASK (_A_ARCH|_A_HIDDEN|_A_NORMAL|_A_RDONLY|_A_SYSTEM|_A_SUBDIR )
    77 
    78 class CFindFileByPath : public CBase
    79     {
    80     public:
    81         CFindFileByPath(RFs& aFs) : iFinder(aFs),iLastCount(-1)
    82             {  
    83             }
    84         ~CFindFileByPath()
    85             {
    86             delete iDir;
    87             iDir = NULL;
    88             }
    89         const TEntry& Entry()
    90             {
    91             return (*iDir)[iLastCount];
    92             }
    93         TInt FindFirst(const TDesC&,const TDesC&);
    94         TInt FindNext();
    95     private:
    96         TFindFile iFinder;
    97         CDir* iDir;
    98         TInt iLastCount;
    99         TBuf<MAXPATHLEN> iPath;
   100     };
   101 
   102 TInt CFindFileByPath::FindFirst(const TDesC &aPattern, const TDesC& aPath)
   103     {
   104     iPath = aPath;
   105     TInt ret = iFinder.FindWildByPath(aPattern,&iPath,iDir);
   106     if(ret != KErrNone)
   107         {
   108         return ret;
   109         }
   110     return FindNext();
   111     }
   112 
   113 TInt CFindFileByPath::FindNext()
   114     {
   115     TInt count = iDir->Count();
   116 //    Results from the search either in pattern path or in the cwd.
   117     if(++iLastCount<count)
   118         {
   119         return KErrNone;
   120         }
   121     return KErrNotFound;
   122     }
   123 
   124 void UpdateFileInfo(struct _wfinddata_t* aFileinfo, CFindFileByPath& aFinder)
   125     {
   126     wcscpy(aFileinfo->name ,(wchar_t *)aFinder.Entry().iName.Ptr());
   127     TInt k = aFinder.Entry().iName.Size()/sizeof(wchar_t);
   128     aFileinfo->name[k] = L'\0';
   129 
   130 
   131     aFileinfo->size = aFinder.Entry().iSize;
   132 
   133     // Unmask unnecessary attributes in iAtt. 
   134     // All constant definitions are in sync with what is expected in finddata structure.
   135     aFileinfo->attrib = aFinder.Entry().iAtt;    
   136     aFileinfo->attrib &= (ATTMASK);
   137 
   138     time_t time_modify = as_time_t(aFinder.Entry().iModified); 
   139     aFileinfo->time_write = time_modify;
   140 
   141     aFileinfo->time_create = -1L;
   142     aFileinfo->time_access = -1L;
   143 
   144     }
   145 
   146 extern "C" 
   147 {
   148      //RFs FileServerSession ; 
   149 EXPORT_C intptr_t wfindfirst(const wchar_t*  filespec, struct _wfinddata_t* fileinfo)
   150  {
   151    
   152    if(!filespec||!fileinfo)
   153    return EINVAL;
   154    
   155    long handle = -1;    
   156    wchar_t *dirf =(wchar_t*)malloc(MAXPATHLEN * sizeof(wchar_t)); // getting the cuurent directory
   157    wgetcwd(dirf,MAXPATHLEN);
   158    TPtrC16 dird((const TUint16*)dirf); // converting it into descriptor
   159    TPtrC16 fsd((const TUint16*)filespec );
   160  
   161    CFindFileByPath *temp1= new CFindFileByPath(Backend()->FileSession());    
   162    if(temp1== NULL)
   163        {
   164        return EINVAL;
   165        }
   166    int k = temp1->FindFirst(fsd,dird);
   167    if(k==KErrNone)
   168        {        
   169        UpdateFileInfo(fileinfo,*temp1);
   170        handle = reinterpret_cast<long>(temp1);
   171        }
   172    else
   173        {
   174         handle = -1;
   175         delete temp1;
   176         errno = ENOENT ;
   177        }
   178    delete dirf; // delete directory pointer 
   179    return handle;
   180  
   181  }
   182 
   183 
   184 EXPORT_C intptr_t wfindnext(intptr_t handle, struct _wfinddata_t * fileinfo)
   185  {
   186         if((handle<=0)||!fileinfo)
   187     return EINVAL;  
   188     
   189     CFindFileByPath *temp1 = reinterpret_cast<CFindFileByPath *>(handle); 
   190     int k = temp1->FindNext();
   191     if(KErrNone==k)
   192     {
   193     UpdateFileInfo(fileinfo,*temp1);
   194     return 0;   
   195     }
   196     else 
   197     {
   198         errno = ENOENT ;
   199         return -1;
   200     }
   201  }
   202     
   203  EXPORT_C int findclose( intptr_t handle)
   204  {
   205     if(handle <=0)
   206     {
   207         errno = ENOENT ;
   208         return -1;
   209     }
   210     CFindFileByPath *temp1 = reinterpret_cast<CFindFileByPath *>(handle); 
   211     delete temp1;
   212     return 0;
   213   }
   214 } 
   215