coan  6.0.1
A C/C++ Configuration Analyzer
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
directory_win.h
Go to the documentation of this file.
1 #ifndef DIRECTORY_WIN_H
2 #define DIRECTORY_WIN_H
3 #pragma once
4 /***************************************************************************
5  * Copyright (C) 2007-2013 Mike Kinghan, imk@burroingroingjoing.com *
6  * All rights reserved. *
7  * *
8  * Contributed originally by Mike Kinghan, imk@burroingroingjoing.com *
9  * *
10  * Redistribution and use in source and binary forms, with or without *
11  * modification, are permitted provided that the following conditions *
12  * are met: *
13  * *
14  * Redistributions of source code must retain the above copyright *
15  * notice, this list of conditions and the following disclaimer. *
16  * *
17  * Redistributions in binary form must reproduce the above copyright *
18  * notice, this list of conditions and the following disclaimer in the *
19  * documentation and/or other materials provided with the distribution. *
20  * *
21  * Neither the name of Mike Kinghan nor the names of its contributors *
22  * may be used to endorse or promote products derived from this software *
23  * without specific prior written permission. *
24  * *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
28  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
29  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
30  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
31  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
32  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
33  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,*
34  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF *
35  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
36  * DAMAGE. *
37  * *
38  **************************************************************************/
39 
40 #include "prohibit.h"
41 #include "directory_common.h"
42 #include <windows.h>
43 
44 
49 namespace win
51 {
52 
56 struct directory : common::directory, private no_copy {
60  explicit directory(std::string const & path)
61  : common::directory(path),_handle(INVALID_HANDLE_VALUE){}
62 
65  close();
66  }
67 
69  std::string cur_memb() const {
70  return _obj_info.cFileName;
71  }
72 
76  bool open() {
77  return true;
78  }
79 
83  bool close() {
84  return (_handle != INVALID_HANDLE_VALUE && !FindClose(_handle)) ?
85  !(_last_error = GetLastError()) : true;
86  }
87 
92  std::string next() {
93  bool found = true;
94  int errnum = 0;
95  _obj_info.cFileName[0] = '\0';
96  if (_handle != INVALID_HANDLE_VALUE) {
97  found = FindNextFile(_handle,&_obj_info);
98  if (!found) {
99  errnum = GetLastError();
100  if (errnum == ERROR_NO_MORE_FILES) {
101  errnum = 0;
102  }
103  }
104  } else {
105  path_t wildcard(_abs_path);
106  wildcard.push_back("*");
107  _handle = FindFirstFile(wildcard.str().c_str(),&_obj_info);
108  if (_handle == INVALID_HANDLE_VALUE) {
109  int errnum = GetLastError();
110  if (errnum == ERROR_FILE_NOT_FOUND) {
111  errnum = 0;
112  }
113  }
114  }
115  if (errnum) {
116  _last_error = errnum;
117  } else if (found && is_dot_name(_obj_info.cFileName)) {
118  return next();
119  }
120  return _obj_info.cFileName;
121  }
122 
123 private:
124 
126  HANDLE _handle;
128  WIN32_FIND_DATA _obj_info;
129 };
130 
131 } // namespace
132 
133 #endif
static bool is_dot_name(char const *leafname)
Say whether a file leafname consists of 1 or 2 dots.
bool open()
Open the directory.
Definition: directory_win.h:76
struct common::directory encapsulates OS-neutral directory functionality.
std::string cur_memb() const
Get the leafname of the current member of the directory.
Definition: directory_win.h:69
std::string const & str() const
Get the path as a string.
Definition: path.h:106
A utility class to prevent copying of containing class.
Definition: prohibit.h:68
~directory()
Destructor.
Definition: directory_win.h:64
WIN32_FIND_DATA _obj_info
Search data updated by FindFirstFile or FindFile()
std::string _abs_path
The absolute pathname of the directory.
void push_back(std::string const &str)
Append a string to the path.
Definition: path.h:178
Encapsulates a filesystem path.
Definition: path.h:59
bool close()
Close the directory.
Definition: directory_win.h:83
std::string next()
Move to the next entry in the directory.
Definition: directory_win.h:92
unsigned _last_error
The last error system code returned by a directory operation.
HANDLE _handle
Handle returned by FindFirstFile()
directory(std::string const &path)
Explicitly construct given a path.
Definition: directory_win.h:60
struct win::directory encapsulates a MS-Windows specific directory functionality. ...
Definition: directory_win.h:56