~ubuntu-branches/ubuntu/intrepid/cmake/intrepid-backports

« back to all changes in this revision

Viewing changes to Source/cmDirectory.cxx

  • Committer: Bazaar Package Importer
  • Author(s): Maitland Bottoms
  • Date: 2002-02-14 18:36:25 UTC
  • Revision ID: james.westby@ubuntu.com-20020214183625-8m44isdas2k4l0f7
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*=========================================================================
 
2
 
 
3
  Program:   Insight Segmentation & Registration Toolkit
 
4
  Module:    $RCSfile: cmDirectory.cxx,v $
 
5
  Language:  C++
 
6
  Date:      $Date: 2001/09/07 14:08:03 $
 
7
  Version:   $Revision: 1.4 $
 
8
 
 
9
Copyright (c) 2001 Insight Consortium
 
10
All rights reserved.
 
11
 
 
12
Redistribution and use in source and binary forms, with or without
 
13
modification, are permitted provided that the following conditions are met:
 
14
 
 
15
 * Redistributions of source code must retain the above copyright notice,
 
16
   this list of conditions and the following disclaimer.
 
17
 
 
18
 * Redistributions in binary form must reproduce the above copyright notice,
 
19
   this list of conditions and the following disclaimer in the documentation
 
20
   and/or other materials provided with the distribution.
 
21
 
 
22
 * The name of the Insight Consortium, nor the names of any consortium members,
 
23
   nor of any contributors, may be used to endorse or promote products derived
 
24
   from this software without specific prior written permission.
 
25
 
 
26
  * Modified source versions must be plainly marked as such, and must not be
 
27
    misrepresented as being the original software.
 
28
 
 
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
 
30
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
31
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
32
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
 
33
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
34
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 
35
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
36
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 
37
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
38
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
39
 
 
40
=========================================================================*/
 
41
#include "cmDirectory.h"
 
42
 
 
43
 
 
44
 
 
45
// First microsoft compilers
 
46
 
 
47
#ifdef _MSC_VER
 
48
#include <windows.h>
 
49
#include <io.h>
 
50
#include <ctype.h>
 
51
#include <fcntl.h>
 
52
#include <stdio.h>
 
53
#include <stdlib.h>
 
54
#include <string.h>
 
55
#include <sys/stat.h>
 
56
#include <sys/types.h>
 
57
  
 
58
/**
 
59
 *
 
60
 */
 
61
bool 
 
62
cmDirectory
 
63
::Load(const char* name)
 
64
{
 
65
  char* buf;
 
66
  int n = strlen(name);
 
67
  if ( name[n - 1] == '/' ) 
 
68
    {
 
69
    buf = new char[n + 1 + 1];
 
70
    sprintf(buf, "%s*", name);
 
71
    } 
 
72
  else
 
73
    {
 
74
    buf = new char[n + 2 + 1];
 
75
    sprintf(buf, "%s/*", name);
 
76
    }
 
77
  struct _finddata_t data;      // data of current file
 
78
  
 
79
  // Now put them into the file array
 
80
  long srchHandle = _findfirst(buf, &data);
 
81
  delete [] buf;
 
82
  
 
83
  if ( srchHandle == -1 )
 
84
    {
 
85
    return 0;
 
86
    }
 
87
  
 
88
  // Loop through names
 
89
  do 
 
90
    {
 
91
    m_Files.push_back(data.name);
 
92
    } 
 
93
  while ( _findnext(srchHandle, &data) != -1 );
 
94
  m_Path = name;
 
95
  return _findclose(srchHandle) != -1;
 
96
}
 
97
 
 
98
#else
 
99
 
 
100
// Now the POSIX style directory access
 
101
 
 
102
#include <sys/types.h>
 
103
#include <dirent.h>
 
104
  
 
105
/**
 
106
 *
 
107
 */
 
108
bool 
 
109
cmDirectory
 
110
::Load(const char* name)
 
111
{
 
112
  DIR* dir = opendir(name);
 
113
 
 
114
  if (!dir)
 
115
    {
 
116
    return 0;
 
117
    }
 
118
 
 
119
  for (dirent* d = readdir(dir); d; d = readdir(dir) )
 
120
    {
 
121
    m_Files.push_back(d->d_name);
 
122
    }
 
123
  m_Path = name;
 
124
  closedir(dir);
 
125
  return 1;
 
126
}
 
127
 
 
128
#endif
 
129
 
 
130
  
 
131
/**
 
132
 *
 
133
 */
 
134
const char* 
 
135
cmDirectory
 
136
::GetFile(unsigned int index)
 
137
{
 
138
  if ( index >= m_Files.size() )
 
139
    {
 
140
    cmSystemTools::Error("Bad index for GetFile on cmDirectory\n", 0);
 
141
    return 0;
 
142
    }
 
143
  return m_Files[index].c_str();
 
144
}
 
145