sl@0: // Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // Name : wfindfirst.cpp sl@0: // Part of : libc library sl@0: // The _wfindfirst function provides information about the first sl@0: // instance of a file name that matches the file specified in the sl@0: // filespec argument. Any wildcard combination supported by the host sl@0: // operating system can be used in filespec. sl@0: // File information is returned in a _wfinddata_t structure, defined in wchar.h sl@0: // FUNCTIONS sl@0: // _wfindfirst, _wfindnext, _findclose sl@0: // INDEX sl@0: // _wfindfirst sl@0: // _wfindnext sl@0: // _findclose sl@0: // ANSI_SYNOPSIS sl@0: // #include sl@0: // int _wfindnext(intptr_t, struct _wfinddata_t *); sl@0: // intptr_t wfindfirst(const wchar_t* , struct _wfinddata_t* ); sl@0: // int findclose( intptr_t handle); sl@0: // The _wfindfirst function provides information about the first sl@0: // instance of a file name that matches the file specified in the sl@0: // filespec argument. Any wildcard combination supported by the host sl@0: // operating system can be used in filespec. sl@0: // File information is returned in a _wfinddata_t structure, defined in wchar.h sl@0: // wfindnext find the next name, if any, that matches the filespec argument sl@0: // in a previous call to _findfirst, and then alter the fileinfo structure sl@0: // contents accordingly. sl@0: // _findclose sl@0: // Closes the specified search handle and releases associated resources. sl@0: // RETURNS sl@0: // _findclose sl@0: // If successful, returns 0. Otherwise, it returns –1 and sets errno to ENOENT sl@0: // _wfindnext sl@0: // 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. sl@0: // EINVAL sl@0: // Invalid parameter: fileinfo was NULL. Or, the operating system returned an unexpected error. sl@0: // ENOENT sl@0: // No more matching files could be found. sl@0: // _wfindfirst sl@0: // If successful, _findfirst returns a unique search handle identifying the file or group of files sl@0: // matching the filespec specification, which can be used in a subsequent call to _findnext or to sl@0: // _findclose. Otherwise, _findfirst returns –1 and sets errno to one of the following values. sl@0: // EINVAL sl@0: // Invalid parameter: filespec or fileinfo was NULL. Or, the operating system returned an unexpected error. sl@0: // ENOENT sl@0: // File specification that could not be matched. sl@0: // ENOMEM sl@0: // Not enough memory or the file name given was greater than MAX_PATH. sl@0: // EINVAL sl@0: // Invalid file name specification. sl@0: // sl@0: sl@0: sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include "sysif.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #define MAXPATHLEN 260 sl@0: #define ATTMASK (_A_ARCH|_A_HIDDEN|_A_NORMAL|_A_RDONLY|_A_SYSTEM|_A_SUBDIR ) sl@0: sl@0: class CFindFileByPath : public CBase sl@0: { sl@0: public: sl@0: CFindFileByPath(RFs& aFs) : iFinder(aFs),iLastCount(-1) sl@0: { sl@0: } sl@0: ~CFindFileByPath() sl@0: { sl@0: delete iDir; sl@0: iDir = NULL; sl@0: } sl@0: const TEntry& Entry() sl@0: { sl@0: return (*iDir)[iLastCount]; sl@0: } sl@0: TInt FindFirst(const TDesC&,const TDesC&); sl@0: TInt FindNext(); sl@0: private: sl@0: TFindFile iFinder; sl@0: CDir* iDir; sl@0: TInt iLastCount; sl@0: TBuf iPath; sl@0: }; sl@0: sl@0: TInt CFindFileByPath::FindFirst(const TDesC &aPattern, const TDesC& aPath) sl@0: { sl@0: iPath = aPath; sl@0: TInt ret = iFinder.FindWildByPath(aPattern,&iPath,iDir); sl@0: if(ret != KErrNone) sl@0: { sl@0: return ret; sl@0: } sl@0: return FindNext(); sl@0: } sl@0: sl@0: TInt CFindFileByPath::FindNext() sl@0: { sl@0: TInt count = iDir->Count(); sl@0: // Results from the search either in pattern path or in the cwd. sl@0: if(++iLastCountname ,(wchar_t *)aFinder.Entry().iName.Ptr()); sl@0: TInt k = aFinder.Entry().iName.Size()/sizeof(wchar_t); sl@0: aFileinfo->name[k] = L'\0'; sl@0: sl@0: sl@0: aFileinfo->size = aFinder.Entry().iSize; sl@0: sl@0: // Unmask unnecessary attributes in iAtt. sl@0: // All constant definitions are in sync with what is expected in finddata structure. sl@0: aFileinfo->attrib = aFinder.Entry().iAtt; sl@0: aFileinfo->attrib &= (ATTMASK); sl@0: sl@0: time_t time_modify = as_time_t(aFinder.Entry().iModified); sl@0: aFileinfo->time_write = time_modify; sl@0: sl@0: aFileinfo->time_create = -1L; sl@0: aFileinfo->time_access = -1L; sl@0: sl@0: } sl@0: sl@0: extern "C" sl@0: { sl@0: //RFs FileServerSession ; sl@0: EXPORT_C intptr_t wfindfirst(const wchar_t* filespec, struct _wfinddata_t* fileinfo) sl@0: { sl@0: sl@0: if(!filespec||!fileinfo) sl@0: return EINVAL; sl@0: sl@0: long handle = -1; sl@0: wchar_t *dirf =(wchar_t*)malloc(MAXPATHLEN * sizeof(wchar_t)); // getting the cuurent directory sl@0: wgetcwd(dirf,MAXPATHLEN); sl@0: TPtrC16 dird((const TUint16*)dirf); // converting it into descriptor sl@0: TPtrC16 fsd((const TUint16*)filespec ); sl@0: sl@0: CFindFileByPath *temp1= new CFindFileByPath(Backend()->FileSession()); sl@0: if(temp1== NULL) sl@0: { sl@0: return EINVAL; sl@0: } sl@0: int k = temp1->FindFirst(fsd,dird); sl@0: if(k==KErrNone) sl@0: { sl@0: UpdateFileInfo(fileinfo,*temp1); sl@0: handle = reinterpret_cast(temp1); sl@0: } sl@0: else sl@0: { sl@0: handle = -1; sl@0: delete temp1; sl@0: errno = ENOENT ; sl@0: } sl@0: delete dirf; // delete directory pointer sl@0: return handle; sl@0: sl@0: } sl@0: sl@0: sl@0: EXPORT_C intptr_t wfindnext(intptr_t handle, struct _wfinddata_t * fileinfo) sl@0: { sl@0: if((handle<=0)||!fileinfo) sl@0: return EINVAL; sl@0: sl@0: CFindFileByPath *temp1 = reinterpret_cast(handle); sl@0: int k = temp1->FindNext(); sl@0: if(KErrNone==k) sl@0: { sl@0: UpdateFileInfo(fileinfo,*temp1); sl@0: return 0; sl@0: } sl@0: else sl@0: { sl@0: errno = ENOENT ; sl@0: return -1; sl@0: } sl@0: } sl@0: sl@0: EXPORT_C int findclose( intptr_t handle) sl@0: { sl@0: if(handle <=0) sl@0: { sl@0: errno = ENOENT ; sl@0: return -1; sl@0: } sl@0: CFindFileByPath *temp1 = reinterpret_cast(handle); sl@0: delete temp1; sl@0: return 0; sl@0: } sl@0: } sl@0: