Pol  Revision:4b29d2b
dirlist.cpp
Go to the documentation of this file.
1 
7 #include "dirlist.h"
8 
9 #include <stddef.h>
10 
11 #ifdef __unix__
12 #include <unistd.h>
13 #endif
14 
15 #include "fileutil.h"
16 
17 #ifdef WINDOWS
18 
19 namespace Pol
20 {
21 namespace Clib
22 {
23 DirList::DirList( const char* dirname )
24 {
25  memset( &fd_, 0, sizeof( fd_ ) );
26  std::string srch = dirname;
27  srch += "*";
28 
29  hfd_ = FindFirstFile( srch.c_str(), &fd_ );
30 }
31 DirList::DirList() : hfd_( INVALID_HANDLE_VALUE )
32 {
33  memset( &fd_, 0, sizeof( fd_ ) );
34 }
35 
37 {
38  if ( hfd_ != INVALID_HANDLE_VALUE )
39  {
40  FindClose( hfd_ );
41  hfd_ = INVALID_HANDLE_VALUE;
42  }
43 }
44 
45 void DirList::open( const char* filespec )
46 {
47  if ( hfd_ != INVALID_HANDLE_VALUE )
48  FindClose( hfd_ );
49 
50  hfd_ = FindFirstFile( filespec, &fd_ );
51 }
52 
53 bool DirList::at_end() const
54 {
55  return ( hfd_ == INVALID_HANDLE_VALUE );
56 }
57 
58 std::string DirList::name() const
59 {
60  return fd_.cFileName;
61 }
62 
63 void DirList::next()
64 {
65  if ( hfd_ != INVALID_HANDLE_VALUE )
66  {
67  if ( !FindNextFile( hfd_, &fd_ ) )
68  {
69  FindClose( hfd_ );
70  hfd_ = INVALID_HANDLE_VALUE;
71  }
72  }
73 }
74 }
75 }
76 #else // non-brain dead places where we can use POSIX functions
77 
78 #include <dirent.h>
79 
80 namespace Pol
81 {
82 namespace Clib
83 {
84 DirList::DirList( const char* dirname )
85 {
86  dir_ = opendir( dirname );
87  next();
88 }
89 DirList::DirList() : dir_( NULL ) {}
91 {
92  if ( dir_ != NULL )
93  {
94  closedir( dir_ );
95  dir_ = NULL;
96  }
97 }
98 void DirList::open( const char* /*filespec*/ )
99 {
100  // TODO
101 }
102 bool DirList::at_end() const
103 {
104  return ( dir_ == NULL );
105 }
106 
107 std::string DirList::name() const
108 {
109  return cur_name_;
110 }
111 
113 {
114  if ( dir_ != NULL )
115  {
116  struct dirent* de = readdir( dir_ );
117  if ( de != NULL )
118  {
119  cur_name_ = de->d_name;
120  }
121  else
122  {
123  closedir( dir_ );
124  dir_ = NULL;
125  }
126  }
127 }
128 }
129 }
130 #endif
131 
132 #ifdef _WIN32
133 #include <direct.h>
134 #endif
135 namespace Pol
136 {
137 namespace Clib
138 {
139 PushDir::PushDir( const char* dir )
140 {
141  if ( getcwd( savedir_, sizeof savedir_ ) == NULL )
142  ok_ = ( chdir( dir ) == 0 );
143  else
144  ok_ = false;
145 }
146 
148 {
149  if ( ok_ )
150  {
151  ok_ = ( chdir( savedir_ ) == 0 );
152  }
153 }
154 
155 bool PushDir::ok() const
156 {
157  return ok_;
158 }
159 
160 std::string curdir()
161 {
162 #ifdef MAX_PATH
163  char cdir[_MAX_PATH];
164 #else
165  char cdir[256];
166 #endif
167  if ( getcwd( cdir, sizeof cdir ) == NULL )
168  return normalized_dir_form( cdir );
169  return "";
170 }
171 }
172 }
bool ok() const
Definition: dirlist.cpp:155
PushDir(const char *dir)
Definition: dirlist.cpp:139
std::string name() const
Definition: dirlist.cpp:107
std::string cur_name_
Definition: dirlist.h:43
bool at_end() const
Definition: dirlist.cpp:102
void open(const char *filespec)
Definition: dirlist.cpp:98
std::string curdir()
Definition: dirlist.cpp:160
std::string normalized_dir_form(const std::string &istr)
Definition: fileutil.cpp:25
Definition: berror.cpp:12