Changeset 13644

Show
Ignore:
Timestamp:
01/31/08 16:39:46 (4 months ago)
Author:
rouault
Message:

Apply more efficient directory listing for Windows and MEM virtual file systems (bug #2158)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.5/gdal/port/cpl_vsi_mem.cpp

    r13333 r13644  
    533533 
    534534    std::map<CPLString,VSIMemFile*>::const_iterator iter; 
    535     char **papszResult = NULL; 
     535    char **papszDir = NULL; 
    536536    int nPathLen = strlen(pszPath); 
    537537 
    538538    if( pszPath[nPathLen-1] == '/' ) 
    539539        nPathLen--; 
     540 
     541    /* In case of really big number of files in the directory, CSLAddString */ 
     542    /* can be slow (see #2158). We then directly build the list. */ 
     543    int nItems=0; 
     544    int nAllocatedItems=0; 
    540545 
    541546    for( iter = oFileList.begin(); iter != oFileList.end(); iter++ ) 
     
    546551            && strstr(pszFilePath+nPathLen+1,"/") == NULL ) 
    547552        { 
    548             papszResult = CSLAddString( papszResult,  
    549                                         pszFilePath+nPathLen+1 ); 
     553            if (nItems == 0) 
     554            { 
     555                papszDir = (char**) CPLCalloc(2,sizeof(char*)); 
     556                nAllocatedItems = 1; 
     557            } 
     558            else if (nItems >= nAllocatedItems) 
     559            { 
     560                nAllocatedItems = nAllocatedItems * 2; 
     561                papszDir = (char**)CPLRealloc(papszDir,  
     562                                              (nAllocatedItems+2)*sizeof(char*)); 
     563            } 
     564 
     565            papszDir[nItems] = CPLStrdup(pszFilePath+nPathLen+1); 
     566            papszDir[nItems+1] = NULL; 
     567 
     568            nItems++; 
    550569        } 
    551570    } 
    552571 
    553     return papszResult
     572    return papszDir
    554573} 
    555574 
  • branches/1.5/gdal/port/cpl_vsil_win32.cpp

    r11304 r13644  
    461461    if ( (hFile = _findfirst( pszFileSpec, &c_file )) != -1L ) 
    462462    { 
     463        /* In case of really big number of files in the directory, CSLAddString */ 
     464        /* can be slow (see #2158). We then directly build the list. */ 
     465        int nItems=0; 
     466        int nAllocatedItems=0; 
    463467        do 
    464468        { 
    465             papszDir = CSLAddString(papszDir, c_file.name); 
     469            if (nItems == 0) 
     470            { 
     471                papszDir = (char**) CPLCalloc(2,sizeof(char*)); 
     472                nAllocatedItems = 1; 
     473            } 
     474            else if (nItems >= nAllocatedItems) 
     475            { 
     476                nAllocatedItems = nAllocatedItems * 2; 
     477                papszDir = (char**)CPLRealloc(papszDir,  
     478                                              (nAllocatedItems+2)*sizeof(char*)); 
     479            } 
     480 
     481            papszDir[nItems] = CPLStrdup(c_file.name); 
     482            papszDir[nItems+1] = NULL; 
     483 
     484            nItems++; 
    466485        } while( _findnext( hFile, &c_file ) == 0 ); 
    467486