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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
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
22 // _wfindfirst, _wfindnext, _findclose
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.
41 // Closes the specified search handle and releases associated resources.
44 // If successful, returns 0. Otherwise, it returns –1 and sets errno to ENOENT
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.
48 // Invalid parameter: fileinfo was NULL. Or, the operating system returned an unexpected error.
50 // No more matching files could be found.
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.
56 // Invalid parameter: filespec or fileinfo was NULL. Or, the operating system returned an unexpected error.
58 // File specification that could not be matched.
60 // Not enough memory or the file name given was greater than MAX_PATH.
62 // Invalid file name specification.
75 #define MAXPATHLEN 260
76 #define ATTMASK (_A_ARCH|_A_HIDDEN|_A_NORMAL|_A_RDONLY|_A_SYSTEM|_A_SUBDIR )
78 class CFindFileByPath : public CBase
81 CFindFileByPath(RFs& aFs) : iFinder(aFs),iLastCount(-1)
91 return (*iDir)[iLastCount];
93 TInt FindFirst(const TDesC&,const TDesC&);
99 TBuf<MAXPATHLEN> iPath;
102 TInt CFindFileByPath::FindFirst(const TDesC &aPattern, const TDesC& aPath)
105 TInt ret = iFinder.FindWildByPath(aPattern,&iPath,iDir);
113 TInt CFindFileByPath::FindNext()
115 TInt count = iDir->Count();
116 // Results from the search either in pattern path or in the cwd.
117 if(++iLastCount<count)
124 void UpdateFileInfo(struct _wfinddata_t* aFileinfo, CFindFileByPath& aFinder)
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';
131 aFileinfo->size = aFinder.Entry().iSize;
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);
138 time_t time_modify = as_time_t(aFinder.Entry().iModified);
139 aFileinfo->time_write = time_modify;
141 aFileinfo->time_create = -1L;
142 aFileinfo->time_access = -1L;
148 //RFs FileServerSession ;
149 EXPORT_C intptr_t wfindfirst(const wchar_t* filespec, struct _wfinddata_t* fileinfo)
152 if(!filespec||!fileinfo)
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 );
161 CFindFileByPath *temp1= new CFindFileByPath(Backend()->FileSession());
166 int k = temp1->FindFirst(fsd,dird);
169 UpdateFileInfo(fileinfo,*temp1);
170 handle = reinterpret_cast<long>(temp1);
178 delete dirf; // delete directory pointer
184 EXPORT_C intptr_t wfindnext(intptr_t handle, struct _wfinddata_t * fileinfo)
186 if((handle<=0)||!fileinfo)
189 CFindFileByPath *temp1 = reinterpret_cast<CFindFileByPath *>(handle);
190 int k = temp1->FindNext();
193 UpdateFileInfo(fileinfo,*temp1);
203 EXPORT_C int findclose( intptr_t handle)
210 CFindFileByPath *temp1 = reinterpret_cast<CFindFileByPath *>(handle);