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