genericopenlibs/openenvcore/libc/src/wfindfirst.cpp
changeset 18 47c74d1534e1
parent 0 e4d67989cc36
child 45 4b03adbd26ca
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
     1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
     1 // Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
    65 
    65 
    66 
    66 
    67 
    67 
    68 #include <stdlib.h>
    68 #include <stdlib.h>
    69 #include <wchar.h>
    69 #include <wchar.h>
    70 #include <bafindf.h>
       
    71 #include <errno.h> 
    70 #include <errno.h> 
    72 #include "sysif.h"
    71 #include "sysif.h"
    73 
    72 #include<e32base.h>
    74 #include<e32cmn.h>
    73 #include<e32cmn.h>
    75 #include<ltime.h>
    74 #include<ltime.h>
    76 #define	MAXPATHLEN	260	
    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     }
    77 
   145 
    78 extern "C" 
   146 extern "C" 
    79 {
   147 {
    80 	 //RFs FileServerSession ; 
   148      //RFs FileServerSession ; 
    81 EXPORT_C intptr_t wfindfirst(const wchar_t*  filespec, struct _wfinddata_t* fileinfo)
   149 EXPORT_C intptr_t wfindfirst(const wchar_t*  filespec, struct _wfinddata_t* fileinfo)
    82  {
   150  {
    83    
   151    
    84    if(!filespec||!fileinfo)
   152    if(!filespec||!fileinfo)
    85    return EINVAL;
   153    return EINVAL;
    86    
   154    
    87    long handle = -1;	
   155    long handle = -1;    
    88    wchar_t *dirf =(wchar_t*)malloc(MAXPATHLEN * sizeof(wchar_t)); // getting the cuurent directory
   156    wchar_t *dirf =(wchar_t*)malloc(MAXPATHLEN * sizeof(wchar_t)); // getting the cuurent directory
    89    wgetcwd(dirf,MAXPATHLEN);
   157    wgetcwd(dirf,MAXPATHLEN);
    90    TPtrC16 dird((const TUint16*)dirf); // converting it into descriptor
   158    TPtrC16 dird((const TUint16*)dirf); // converting it into descriptor
    91    TPtrC16 fsd((const TUint16*)filespec );
   159    TPtrC16 fsd((const TUint16*)filespec );
    92    TUidType uid(KNullUid, KNullUid, KNullUid);
   160  
    93    //FileServerSession.Connect() ;	
   161    CFindFileByPath *temp1= new CFindFileByPath(Backend()->FileSession());    
    94    
       
    95    CFindFileByType *temp1= new CFindFileByType(Backend()->FileSession()); // #include <bafindf.h> 	
       
    96    if(temp1== NULL)
   162    if(temp1== NULL)
    97    {
   163        {
    98   		return EINVAL;
   164        return EINVAL;
    99   	}
   165        }
   100    int k = temp1->FindFirst(fsd,dird,uid);
   166    int k = temp1->FindFirst(fsd,dird);
   101    if(k==KErrNone)
   167    if(k==KErrNone)
   102    {
   168        {        
   103    	    	
   169        UpdateFileInfo(fileinfo,*temp1);
   104    	wcscpy(fileinfo->name ,(wchar_t *)temp1->Entry().iName.Ptr());
   170        handle = reinterpret_cast<long>(temp1);
   105    	k = temp1->Entry().iName.Size()/sizeof(wchar_t);
   171        }
   106    	fileinfo->name[k] = L'\0';
       
   107    	
       
   108    	
       
   109    	fileinfo->size = temp1->Entry().iSize;
       
   110    	
       
   111    	fileinfo->attrib = temp1->Entry().iAtt; // we have to eport defined 
       
   112    	//also like KEntryAttNormal all this are availabel in f32file.h one 
       
   113    	//attribte _A_SUBDIR will not be suuported 
       
   114 
       
   115    	time_t time_modify = as_time_t(temp1->Entry().iModified); 
       
   116     fileinfo->time_write = time_modify;
       
   117     
       
   118    	fileinfo->time_create = -1L;
       
   119    	fileinfo->time_access = -1L;
       
   120    	
       
   121    	
       
   122    	handle = reinterpret_cast<long>(temp1);
       
   123    }
       
   124    else
   172    else
   125    {
   173        {
   126    	handle = -1;
   174         handle = -1;
   127   	errno = ENOENT ;
   175         delete temp1;
   128    }
   176         errno = ENOENT ;
   129    delete dirf; // delete directory pointer	
   177        }
   130    //FileServerSession.Close(); 
   178    delete dirf; // delete directory pointer 
   131  
       
   132    return handle;
   179    return handle;
   133  
   180  
   134  }
   181  }
   135 
   182 
   136 
   183 
   137 EXPORT_C intptr_t wfindnext(intptr_t handle, struct _wfinddata_t * fileinfo)
   184 EXPORT_C intptr_t wfindnext(intptr_t handle, struct _wfinddata_t * fileinfo)
   138  {
   185  {
   139  		if((handle<=0)||!fileinfo)
   186         if((handle<=0)||!fileinfo)
   140    	return EINVAL;	
   187     return EINVAL;  
   141  	
   188     
   142  	CFindFileByType *temp1 = reinterpret_cast<CFindFileByType *>(handle); 
   189     CFindFileByPath *temp1 = reinterpret_cast<CFindFileByPath *>(handle); 
   143  	int k = temp1->FindNext();
   190     int k = temp1->FindNext();
   144  	if(KErrNone==k)
   191     if(KErrNone==k)
   145  	{
   192     {
   146  		wcscpy(fileinfo->name , (wchar_t *)temp1->Entry().iName.Ptr());
   193     UpdateFileInfo(fileinfo,*temp1);
   147  		k = temp1->Entry().iName.Size()/sizeof(wchar_t);
   194     return 0;   
   148    	fileinfo->name[k] = L'\0';
   195     }
   149    	fileinfo->size = temp1->Entry().iSize;
   196     else 
   150    	return 0;	
   197     {
   151  	}
   198         errno = ENOENT ;
   152  	else 
   199         return -1;
   153  	{
   200     }
   154  	 	errno = ENOENT ;
       
   155 	 	return -1;
       
   156 	}
       
   157  }
   201  }
   158 	
   202     
   159  EXPORT_C int findclose( intptr_t handle)
   203  EXPORT_C int findclose( intptr_t handle)
   160  {
   204  {
   161  	if(handle <=0)
   205     if(handle <=0)
   162  	{
   206     {
   163 		errno = ENOENT ;
   207         errno = ENOENT ;
   164 	 	return -1;
   208         return -1;
   165  	}
   209     }
   166  	CFindFileByType *temp1 = reinterpret_cast<CFindFileByType *>(handle); 
   210     CFindFileByPath *temp1 = reinterpret_cast<CFindFileByPath *>(handle); 
   167  //FileServerSession.Close(); 
   211     delete temp1;
   168  	delete temp1;
   212     return 0;
   169  	return 0;
       
   170   }
   213   }
   171 } 
   214 } 
   172 
   215