FIX: stupid pb fixed (close to being medieval'ed by The Ken)
[cmake.git] / Source / cmDirectory.cxx
blobaf32322e1b7f406406c9c33c0d81d066ca34af39
1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmDirectory.cxx,v $
5 Language: C++
6 Date: $Date: 2002-03-13 15:25:07 $
7 Version: $Revision: 1.6 $
9 Copyright (c) 2002 Insight Consortium. All rights reserved.
10 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmDirectory.h"
21 // First microsoft compilers
23 #ifdef _MSC_VER
24 #include <windows.h>
25 #include <io.h>
26 #include <ctype.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
34 /**
37 bool
38 cmDirectory
39 ::Load(const char* name)
41 char* buf;
42 size_t n = strlen(name);
43 if ( name[n - 1] == '/' )
45 buf = new char[n + 1 + 1];
46 sprintf(buf, "%s*", name);
48 else
50 buf = new char[n + 2 + 1];
51 sprintf(buf, "%s/*", name);
53 struct _finddata_t data; // data of current file
55 // Now put them into the file array
56 size_t srchHandle = _findfirst(buf, &data);
57 delete [] buf;
59 if ( srchHandle == -1 )
61 return 0;
64 // Loop through names
65 do
67 m_Files.push_back(data.name);
69 while ( _findnext(srchHandle, &data) != -1 );
70 m_Path = name;
71 return _findclose(srchHandle) != -1;
74 #else
76 // Now the POSIX style directory access
78 #include <sys/types.h>
79 #include <dirent.h>
81 /**
84 bool
85 cmDirectory
86 ::Load(const char* name)
88 DIR* dir = opendir(name);
90 if (!dir)
92 return 0;
95 for (dirent* d = readdir(dir); d; d = readdir(dir) )
97 m_Files.push_back(d->d_name);
99 m_Path = name;
100 closedir(dir);
101 return 1;
104 #endif
110 const char*
111 cmDirectory
112 ::GetFile(size_t index)
114 if ( index >= m_Files.size() )
116 cmSystemTools::Error("Bad index for GetFile on cmDirectory\n", 0);
117 return 0;
119 return m_Files[index].c_str();