os/ossrv/genericopenlibs/openenvcore/libc/src/wfindfirst.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/openenvcore/libc/src/wfindfirst.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,215 @@
     1.4 +// Copyright (c) 2005-2010 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// Name        : wfindfirst.cpp
    1.18 +// Part of     : libc library
    1.19 +// The _wfindfirst function provides information about the first 
    1.20 +// instance of a file name that matches the file specified in the 
    1.21 +// filespec argument. Any wildcard combination supported by the host 
    1.22 +// operating system can be used in filespec. 
    1.23 +// File information is returned in a _wfinddata_t structure, defined in wchar.h
    1.24 +// FUNCTIONS
    1.25 +// _wfindfirst, _wfindnext, _findclose
    1.26 +// INDEX
    1.27 +// _wfindfirst
    1.28 +// _wfindnext
    1.29 +// _findclose
    1.30 +// ANSI_SYNOPSIS
    1.31 +// #include <wchar.h>
    1.32 +// int _wfindnext(intptr_t, struct _wfinddata_t *); 
    1.33 +// intptr_t wfindfirst(const wchar_t* , struct _wfinddata_t* );
    1.34 +// int findclose( intptr_t handle);
    1.35 +// The _wfindfirst function provides information about the first 
    1.36 +// instance of a file name that matches the file specified in the 
    1.37 +// filespec argument. Any wildcard combination supported by the host 
    1.38 +// operating system can be used in filespec. 
    1.39 +// File information is returned in a _wfinddata_t structure, defined in wchar.h
    1.40 +// wfindnext find the next name, if any, that matches the filespec argument 
    1.41 +// in a previous call to _findfirst, and then alter the fileinfo structure 
    1.42 +// contents accordingly.
    1.43 +// _findclose  
    1.44 +// Closes the specified search handle and releases associated resources.
    1.45 +// RETURNS
    1.46 +// _findclose
    1.47 +// If successful, returns 0. Otherwise, it returns –1 and sets errno to ENOENT
    1.48 +// _wfindnext
    1.49 +// 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. 
    1.50 +// EINVAL
    1.51 +// Invalid parameter: fileinfo was NULL. Or, the operating system returned an unexpected error.
    1.52 +// ENOENT
    1.53 +// No more matching files could be found.
    1.54 +// _wfindfirst
    1.55 +// If successful, _findfirst returns a unique search handle identifying the file or group of files
    1.56 +// matching the filespec specification, which can be used in a subsequent call to _findnext or to 
    1.57 +// _findclose. Otherwise, _findfirst returns –1 and sets errno to one of the following values.
    1.58 +// EINVAL
    1.59 +// Invalid parameter: filespec or fileinfo was NULL. Or, the operating system returned an unexpected error.
    1.60 +// ENOENT
    1.61 +// File specification that could not be matched.
    1.62 +// ENOMEM
    1.63 +// Not enough memory or the file name given was greater than MAX_PATH.
    1.64 +// EINVAL
    1.65 +// Invalid file name specification.
    1.66 +//
    1.67 +
    1.68 +
    1.69 +
    1.70 +
    1.71 +#include <stdlib.h>
    1.72 +#include <wchar.h>
    1.73 +#include <errno.h> 
    1.74 +#include "sysif.h"
    1.75 +#include<e32base.h>
    1.76 +#include<e32cmn.h>
    1.77 +#include<ltime.h>
    1.78 +#define MAXPATHLEN  260 
    1.79 +#define ATTMASK (_A_ARCH|_A_HIDDEN|_A_NORMAL|_A_RDONLY|_A_SYSTEM|_A_SUBDIR )
    1.80 +
    1.81 +class CFindFileByPath : public CBase
    1.82 +    {
    1.83 +    public:
    1.84 +        CFindFileByPath(RFs& aFs) : iFinder(aFs),iLastCount(-1)
    1.85 +            {  
    1.86 +            }
    1.87 +        ~CFindFileByPath()
    1.88 +            {
    1.89 +            delete iDir;
    1.90 +            iDir = NULL;
    1.91 +            }
    1.92 +        const TEntry& Entry()
    1.93 +            {
    1.94 +            return (*iDir)[iLastCount];
    1.95 +            }
    1.96 +        TInt FindFirst(const TDesC&,const TDesC&);
    1.97 +        TInt FindNext();
    1.98 +    private:
    1.99 +        TFindFile iFinder;
   1.100 +        CDir* iDir;
   1.101 +        TInt iLastCount;
   1.102 +        TBuf<MAXPATHLEN> iPath;
   1.103 +    };
   1.104 +
   1.105 +TInt CFindFileByPath::FindFirst(const TDesC &aPattern, const TDesC& aPath)
   1.106 +    {
   1.107 +    iPath = aPath;
   1.108 +    TInt ret = iFinder.FindWildByPath(aPattern,&iPath,iDir);
   1.109 +    if(ret != KErrNone)
   1.110 +        {
   1.111 +        return ret;
   1.112 +        }
   1.113 +    return FindNext();
   1.114 +    }
   1.115 +
   1.116 +TInt CFindFileByPath::FindNext()
   1.117 +    {
   1.118 +    TInt count = iDir->Count();
   1.119 +//    Results from the search either in pattern path or in the cwd.
   1.120 +    if(++iLastCount<count)
   1.121 +        {
   1.122 +        return KErrNone;
   1.123 +        }
   1.124 +    return KErrNotFound;
   1.125 +    }
   1.126 +
   1.127 +void UpdateFileInfo(struct _wfinddata_t* aFileinfo, CFindFileByPath& aFinder)
   1.128 +    {
   1.129 +    wcscpy(aFileinfo->name ,(wchar_t *)aFinder.Entry().iName.Ptr());
   1.130 +    TInt k = aFinder.Entry().iName.Size()/sizeof(wchar_t);
   1.131 +    aFileinfo->name[k] = L'\0';
   1.132 +
   1.133 +
   1.134 +    aFileinfo->size = aFinder.Entry().iSize;
   1.135 +
   1.136 +    // Unmask unnecessary attributes in iAtt. 
   1.137 +    // All constant definitions are in sync with what is expected in finddata structure.
   1.138 +    aFileinfo->attrib = aFinder.Entry().iAtt;    
   1.139 +    aFileinfo->attrib &= (ATTMASK);
   1.140 +
   1.141 +    time_t time_modify = as_time_t(aFinder.Entry().iModified); 
   1.142 +    aFileinfo->time_write = time_modify;
   1.143 +
   1.144 +    aFileinfo->time_create = -1L;
   1.145 +    aFileinfo->time_access = -1L;
   1.146 +
   1.147 +    }
   1.148 +
   1.149 +extern "C" 
   1.150 +{
   1.151 +     //RFs FileServerSession ; 
   1.152 +EXPORT_C intptr_t wfindfirst(const wchar_t*  filespec, struct _wfinddata_t* fileinfo)
   1.153 + {
   1.154 +   
   1.155 +   if(!filespec||!fileinfo)
   1.156 +   return EINVAL;
   1.157 +   
   1.158 +   long handle = -1;    
   1.159 +   wchar_t *dirf =(wchar_t*)malloc(MAXPATHLEN * sizeof(wchar_t)); // getting the cuurent directory
   1.160 +   wgetcwd(dirf,MAXPATHLEN);
   1.161 +   TPtrC16 dird((const TUint16*)dirf); // converting it into descriptor
   1.162 +   TPtrC16 fsd((const TUint16*)filespec );
   1.163 + 
   1.164 +   CFindFileByPath *temp1= new CFindFileByPath(Backend()->FileSession());    
   1.165 +   if(temp1== NULL)
   1.166 +       {
   1.167 +       return EINVAL;
   1.168 +       }
   1.169 +   int k = temp1->FindFirst(fsd,dird);
   1.170 +   if(k==KErrNone)
   1.171 +       {        
   1.172 +       UpdateFileInfo(fileinfo,*temp1);
   1.173 +       handle = reinterpret_cast<long>(temp1);
   1.174 +       }
   1.175 +   else
   1.176 +       {
   1.177 +        handle = -1;
   1.178 +        delete temp1;
   1.179 +        errno = ENOENT ;
   1.180 +       }
   1.181 +   delete dirf; // delete directory pointer 
   1.182 +   return handle;
   1.183 + 
   1.184 + }
   1.185 +
   1.186 +
   1.187 +EXPORT_C intptr_t wfindnext(intptr_t handle, struct _wfinddata_t * fileinfo)
   1.188 + {
   1.189 +        if((handle<=0)||!fileinfo)
   1.190 +    return EINVAL;  
   1.191 +    
   1.192 +    CFindFileByPath *temp1 = reinterpret_cast<CFindFileByPath *>(handle); 
   1.193 +    int k = temp1->FindNext();
   1.194 +    if(KErrNone==k)
   1.195 +    {
   1.196 +    UpdateFileInfo(fileinfo,*temp1);
   1.197 +    return 0;   
   1.198 +    }
   1.199 +    else 
   1.200 +    {
   1.201 +        errno = ENOENT ;
   1.202 +        return -1;
   1.203 +    }
   1.204 + }
   1.205 +    
   1.206 + EXPORT_C int findclose( intptr_t handle)
   1.207 + {
   1.208 +    if(handle <=0)
   1.209 +    {
   1.210 +        errno = ENOENT ;
   1.211 +        return -1;
   1.212 +    }
   1.213 +    CFindFileByPath *temp1 = reinterpret_cast<CFindFileByPath *>(handle); 
   1.214 +    delete temp1;
   1.215 +    return 0;
   1.216 +  }
   1.217 +} 
   1.218 +