Ticket #2158: test_fast_read_dir.c

File test_fast_read_dir.c, 1.7 kB (added by rouault, 3 months ago)
Line 
1 /* gcc -g -Wall test_fast_read_dir.c -o test_fast_read_dir -lgdal -L. -Igcore -Iport */
2
3 #include "cpl_string.h"
4 #include "gdal.h"
5
6 typedef struct
7 {
8     char* pszDir;
9     char** papszDirFileList;
10 } sCacheDirEntry;
11
12 char** myReadDirFun(const char* pszDir)
13 {
14     static int             nCachedDirEntries = 0;
15     static sCacheDirEntry* psCacheDirEntries = NULL;
16
17     int i;
18     for(i=0;i<nCachedDirEntries;i++)
19     {
20         if (strcmp(pszDir, psCacheDirEntries[i].pszDir) == 0)
21             return CSLDuplicate(psCacheDirEntries[i].papszDirFileList);
22     }
23
24     psCacheDirEntries = (sCacheDirEntry*)CPLRealloc(psCacheDirEntries, (nCachedDirEntries + 1) * sizeof(sCacheDirEntry));
25     psCacheDirEntries[nCachedDirEntries].pszDir = CPLStrdup(pszDir);
26     psCacheDirEntries[nCachedDirEntries].papszDirFileList = VSIReadDirWithoutCache(pszDir);
27     nCachedDirEntries++;
28
29     return CSLDuplicate(psCacheDirEntries[nCachedDirEntries-1].papszDirFileList);
30 }
31
32 int main(int argc, char* argv[])
33 {
34     GDALAllRegister();
35
36     VSIInstallCustomReadDirCallback(myReadDirFun);
37
38     CPLSetErrorHandler(CPLQuietErrorHandler);
39
40     const char* pszPath = (argc == 1) ? "." : argv[1];
41     char** papszDirFileList = VSIReadDir(pszPath);
42     char** papszIter = papszDirFileList;
43     while(*papszIter)
44     {
45         char* pszFileName = CPLStrdup(CPLFormFilename(pszPath, *papszIter, NULL));
46         GDALDatasetH hDS = GDALOpen(pszFileName, GA_ReadOnly);
47         if (hDS)
48         {
49             printf("%s : %d x %d\n", *papszIter, GDALGetRasterXSize(hDS), GDALGetRasterYSize(hDS));
50             GDALClose(hDS);
51         }
52         CPLFree(pszFileName);
53
54         papszIter ++;
55     }
56     CSLDestroy(papszDirFileList);
57
58     return 0;
59 }