| 1 | /* | 
|---|
| 2 | * | 
|---|
| 3 | * Copyright (c) 1998-2002 | 
|---|
| 4 | * John Maddock | 
|---|
| 5 | * | 
|---|
| 6 | * Use, modification and distribution are subject to the | 
|---|
| 7 | * Boost Software License, Version 1.0. (See accompanying file | 
|---|
| 8 | * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 
|---|
| 9 | * | 
|---|
| 10 | */ | 
|---|
| 11 |  | 
|---|
| 12 | /* | 
|---|
| 13 | *   LOCATION:    see http://www.boost.org for most recent version. | 
|---|
| 14 | *   FILE:        fileiter.cpp | 
|---|
| 15 | *   VERSION:     see <boost/version.hpp> | 
|---|
| 16 | *   DESCRIPTION: Implements file io primitives + directory searching for class boost::RegEx. | 
|---|
| 17 | */ | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 | #define BOOST_REGEX_SOURCE | 
|---|
| 21 |  | 
|---|
| 22 | #include <climits> | 
|---|
| 23 | #include <stdexcept> | 
|---|
| 24 | #include <string> | 
|---|
| 25 | #include <boost/throw_exception.hpp> | 
|---|
| 26 | #include <boost/regex/v4/fileiter.hpp> | 
|---|
| 27 | #include <boost/regex/v4/regex_workaround.hpp> | 
|---|
| 28 | #include <boost/regex/pattern_except.hpp> | 
|---|
| 29 |  | 
|---|
| 30 | #include <cstdio> | 
|---|
| 31 | #if defined(BOOST_NO_STDC_NAMESPACE) | 
|---|
| 32 | namespace std{ | 
|---|
| 33 | using ::sprintf; | 
|---|
| 34 | using ::fseek; | 
|---|
| 35 | using ::fread; | 
|---|
| 36 | using ::ftell; | 
|---|
| 37 | using ::fopen; | 
|---|
| 38 | using ::fclose; | 
|---|
| 39 | using ::FILE; | 
|---|
| 40 | using ::strcpy; | 
|---|
| 41 | using ::strcpy; | 
|---|
| 42 | using ::strcat; | 
|---|
| 43 | using ::strcmp; | 
|---|
| 44 | using ::strlen; | 
|---|
| 45 | } | 
|---|
| 46 | #endif | 
|---|
| 47 |  | 
|---|
| 48 |  | 
|---|
| 49 | #ifndef BOOST_REGEX_NO_FILEITER | 
|---|
| 50 |  | 
|---|
| 51 | #if defined(__CYGWIN__) || defined(__CYGWIN32__) | 
|---|
| 52 | #include <sys/cygwin.h> | 
|---|
| 53 | #endif | 
|---|
| 54 |  | 
|---|
| 55 | #ifdef BOOST_MSVC | 
|---|
| 56 | #  pragma warning(disable: 4800) | 
|---|
| 57 | #endif | 
|---|
| 58 |  | 
|---|
| 59 | namespace boost{ | 
|---|
| 60 | namespace re_detail{ | 
|---|
| 61 | // start with the operating system specific stuff: | 
|---|
| 62 |  | 
|---|
| 63 | #if (defined(__BORLANDC__) || defined(BOOST_REGEX_FI_WIN32_DIR) || defined(BOOST_MSVC)) && !defined(BOOST_RE_NO_WIN32) | 
|---|
| 64 |  | 
|---|
| 65 | // platform is DOS or Windows | 
|---|
| 66 | // directories are separated with '\\' | 
|---|
| 67 | // and names are insensitive of case | 
|---|
| 68 |  | 
|---|
| 69 | BOOST_REGEX_DECL const char* _fi_sep = "\\"; | 
|---|
| 70 | const char* _fi_sep_alt = "/"; | 
|---|
| 71 | #define BOOST_REGEX_FI_TRANSLATE(c) std::tolower(c) | 
|---|
| 72 |  | 
|---|
| 73 | #else | 
|---|
| 74 |  | 
|---|
| 75 | // platform is not DOS or Windows | 
|---|
| 76 | // directories are separated with '/' | 
|---|
| 77 | // and names are sensitive of case | 
|---|
| 78 |  | 
|---|
| 79 | BOOST_REGEX_DECL const char* _fi_sep = "/"; | 
|---|
| 80 | const char* _fi_sep_alt = _fi_sep; | 
|---|
| 81 | #define BOOST_REGEX_FI_TRANSLATE(c) c | 
|---|
| 82 |  | 
|---|
| 83 | #endif | 
|---|
| 84 |  | 
|---|
| 85 | #ifdef BOOST_REGEX_FI_WIN32_MAP | 
|---|
| 86 |  | 
|---|
| 87 | void mapfile::open(const char* file) | 
|---|
| 88 | { | 
|---|
| 89 | #if defined(__CYGWIN__)||defined(__CYGWIN32__) | 
|---|
| 90 | char win32file[ MAX_PATH ]; | 
|---|
| 91 | cygwin_conv_to_win32_path( file, win32file ); | 
|---|
| 92 | hfile = CreateFileA(win32file, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | 
|---|
| 93 | #else | 
|---|
| 94 | hfile = CreateFileA(file, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | 
|---|
| 95 | #endif | 
|---|
| 96 | if(hfile != INVALID_HANDLE_VALUE) | 
|---|
| 97 | { | 
|---|
| 98 | hmap = CreateFileMapping(hfile, 0, PAGE_READONLY, 0, 0, 0); | 
|---|
| 99 | if((hmap == INVALID_HANDLE_VALUE) || (hmap == NULL)) | 
|---|
| 100 | { | 
|---|
| 101 | CloseHandle(hfile); | 
|---|
| 102 | hmap = 0; | 
|---|
| 103 | hfile = 0; | 
|---|
| 104 | std::runtime_error err("Unable to create file mapping."); | 
|---|
| 105 | boost::re_detail::raise_runtime_error(err); | 
|---|
| 106 | } | 
|---|
| 107 | _first = static_cast<const char*>(MapViewOfFile(hmap, FILE_MAP_READ, 0, 0, 0)); | 
|---|
| 108 | if(_first == 0) | 
|---|
| 109 | { | 
|---|
| 110 | CloseHandle(hmap); | 
|---|
| 111 | CloseHandle(hfile); | 
|---|
| 112 | hmap = 0; | 
|---|
| 113 | hfile = 0; | 
|---|
| 114 | std::runtime_error err("Unable to create file mapping."); | 
|---|
| 115 | } | 
|---|
| 116 | _last = _first + GetFileSize(hfile, 0); | 
|---|
| 117 | } | 
|---|
| 118 | else | 
|---|
| 119 | { | 
|---|
| 120 | hfile = 0; | 
|---|
| 121 | #ifndef BOOST_NO_EXCEPTIONS | 
|---|
| 122 | throw std::runtime_error("Unable to open file."); | 
|---|
| 123 | #else | 
|---|
| 124 | BOOST_REGEX_NOEH_ASSERT(hfile != INVALID_HANDLE_VALUE); | 
|---|
| 125 | #endif | 
|---|
| 126 | } | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | void mapfile::close() | 
|---|
| 130 | { | 
|---|
| 131 | if(hfile != INVALID_HANDLE_VALUE) | 
|---|
| 132 | { | 
|---|
| 133 | UnmapViewOfFile((void*)_first); | 
|---|
| 134 | CloseHandle(hmap); | 
|---|
| 135 | CloseHandle(hfile); | 
|---|
| 136 | hmap = hfile = 0; | 
|---|
| 137 | _first = _last = 0; | 
|---|
| 138 | } | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | #elif !defined(BOOST_RE_NO_STL) | 
|---|
| 142 |  | 
|---|
| 143 | mapfile_iterator& mapfile_iterator::operator = (const mapfile_iterator& i) | 
|---|
| 144 | { | 
|---|
| 145 | if(file && node) | 
|---|
| 146 | file->unlock(node); | 
|---|
| 147 | file = i.file; | 
|---|
| 148 | node = i.node; | 
|---|
| 149 | offset = i.offset; | 
|---|
| 150 | if(file) | 
|---|
| 151 | file->lock(node); | 
|---|
| 152 | return *this; | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | mapfile_iterator& mapfile_iterator::operator++ () | 
|---|
| 156 | { | 
|---|
| 157 | if((++offset == mapfile::buf_size) && file) | 
|---|
| 158 | { | 
|---|
| 159 | ++node; | 
|---|
| 160 | offset = 0; | 
|---|
| 161 | file->lock(node); | 
|---|
| 162 | file->unlock(node-1); | 
|---|
| 163 | } | 
|---|
| 164 | return *this; | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | mapfile_iterator mapfile_iterator::operator++ (int) | 
|---|
| 168 | { | 
|---|
| 169 | mapfile_iterator temp(*this); | 
|---|
| 170 | if((++offset == mapfile::buf_size) && file) | 
|---|
| 171 | { | 
|---|
| 172 | ++node; | 
|---|
| 173 | offset = 0; | 
|---|
| 174 | file->lock(node); | 
|---|
| 175 | file->unlock(node-1); | 
|---|
| 176 | } | 
|---|
| 177 | return temp; | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | mapfile_iterator& mapfile_iterator::operator-- () | 
|---|
| 181 | { | 
|---|
| 182 | if((offset == 0) && file) | 
|---|
| 183 | { | 
|---|
| 184 | --node; | 
|---|
| 185 | offset = mapfile::buf_size - 1; | 
|---|
| 186 | file->lock(node); | 
|---|
| 187 | file->unlock(node + 1); | 
|---|
| 188 | } | 
|---|
| 189 | else | 
|---|
| 190 | --offset; | 
|---|
| 191 | return *this; | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | mapfile_iterator mapfile_iterator::operator-- (int) | 
|---|
| 195 | { | 
|---|
| 196 | mapfile_iterator temp(*this); | 
|---|
| 197 | if((offset == 0) && file) | 
|---|
| 198 | { | 
|---|
| 199 | --node; | 
|---|
| 200 | offset = mapfile::buf_size - 1; | 
|---|
| 201 | file->lock(node); | 
|---|
| 202 | file->unlock(node + 1); | 
|---|
| 203 | } | 
|---|
| 204 | else | 
|---|
| 205 | --offset; | 
|---|
| 206 | return temp; | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | mapfile_iterator operator + (const mapfile_iterator& i, long off) | 
|---|
| 210 | { | 
|---|
| 211 | mapfile_iterator temp(i); | 
|---|
| 212 | temp += off; | 
|---|
| 213 | return temp; | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | mapfile_iterator operator - (const mapfile_iterator& i, long off) | 
|---|
| 217 | { | 
|---|
| 218 | mapfile_iterator temp(i); | 
|---|
| 219 | temp -= off; | 
|---|
| 220 | return temp; | 
|---|
| 221 | } | 
|---|
| 222 |  | 
|---|
| 223 | mapfile::iterator mapfile::begin()const | 
|---|
| 224 | { | 
|---|
| 225 | return mapfile_iterator(this, 0); | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | mapfile::iterator mapfile::end()const | 
|---|
| 229 | { | 
|---|
| 230 | return mapfile_iterator(this, _size); | 
|---|
| 231 | } | 
|---|
| 232 |  | 
|---|
| 233 | void mapfile::lock(pointer* node)const | 
|---|
| 234 | { | 
|---|
| 235 | BOOST_ASSERT(node >= _first); | 
|---|
| 236 | BOOST_ASSERT(node <= _last); | 
|---|
| 237 | if(node < _last) | 
|---|
| 238 | { | 
|---|
| 239 | if(*node == 0) | 
|---|
| 240 | { | 
|---|
| 241 | if(condemed.empty()) | 
|---|
| 242 | { | 
|---|
| 243 | *node = new char[sizeof(int) + buf_size]; | 
|---|
| 244 | *(reinterpret_cast<int*>(*node)) = 1; | 
|---|
| 245 | } | 
|---|
| 246 | else | 
|---|
| 247 | { | 
|---|
| 248 | pointer* p = condemed.front(); | 
|---|
| 249 | condemed.pop_front(); | 
|---|
| 250 | *node = *p; | 
|---|
| 251 | *p = 0; | 
|---|
| 252 | *(reinterpret_cast<int*>(*node)) = 1; | 
|---|
| 253 | } | 
|---|
| 254 | std::fseek(hfile, (node - _first) * buf_size, SEEK_SET); | 
|---|
| 255 | if(node == _last - 1) | 
|---|
| 256 | std::fread(*node + sizeof(int), _size % buf_size, 1, hfile); | 
|---|
| 257 | else | 
|---|
| 258 | std::fread(*node + sizeof(int), buf_size, 1, hfile); | 
|---|
| 259 | } | 
|---|
| 260 | else | 
|---|
| 261 | { | 
|---|
| 262 | if(*reinterpret_cast<int*>(*node) == 0) | 
|---|
| 263 | { | 
|---|
| 264 | *reinterpret_cast<int*>(*node) = 1; | 
|---|
| 265 | condemed.remove(node); | 
|---|
| 266 | } | 
|---|
| 267 | else | 
|---|
| 268 | ++(*reinterpret_cast<int*>(*node)); | 
|---|
| 269 | } | 
|---|
| 270 | } | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | void mapfile::unlock(pointer* node)const | 
|---|
| 274 | { | 
|---|
| 275 | BOOST_ASSERT(node >= _first); | 
|---|
| 276 | BOOST_ASSERT(node <= _last); | 
|---|
| 277 | if(node < _last) | 
|---|
| 278 | { | 
|---|
| 279 | if(--(*reinterpret_cast<int*>(*node)) == 0) | 
|---|
| 280 | { | 
|---|
| 281 | condemed.push_back(node); | 
|---|
| 282 | } | 
|---|
| 283 | } | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | long int get_file_length(std::FILE* hfile) | 
|---|
| 287 | { | 
|---|
| 288 | long int result; | 
|---|
| 289 | std::fseek(hfile, 0, SEEK_END); | 
|---|
| 290 | result = std::ftell(hfile); | 
|---|
| 291 | std::fseek(hfile, 0, SEEK_SET); | 
|---|
| 292 | return result; | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 |  | 
|---|
| 296 | void mapfile::open(const char* file) | 
|---|
| 297 | { | 
|---|
| 298 | hfile = std::fopen(file, "rb"); | 
|---|
| 299 | #ifndef BOOST_NO_EXCEPTIONS | 
|---|
| 300 | try{ | 
|---|
| 301 | #endif | 
|---|
| 302 | if(hfile != 0) | 
|---|
| 303 | { | 
|---|
| 304 | _size = get_file_length(hfile); | 
|---|
| 305 | long cnodes = (_size + buf_size - 1) / buf_size; | 
|---|
| 306 |  | 
|---|
| 307 | // check that number of nodes is not too high: | 
|---|
| 308 | if(cnodes > (long)((INT_MAX) / sizeof(pointer*))) | 
|---|
| 309 | { | 
|---|
| 310 | std::fclose(hfile); | 
|---|
| 311 | hfile = 0; | 
|---|
| 312 | _size = 0; | 
|---|
| 313 | return; | 
|---|
| 314 | } | 
|---|
| 315 |  | 
|---|
| 316 | _first = new pointer[(int)cnodes]; | 
|---|
| 317 | _last = _first + cnodes; | 
|---|
| 318 | std::memset(_first, 0, cnodes*sizeof(pointer)); | 
|---|
| 319 | } | 
|---|
| 320 | else | 
|---|
| 321 | { | 
|---|
| 322 | std::runtime_error err("Unable to open file."); | 
|---|
| 323 | } | 
|---|
| 324 | #ifndef BOOST_NO_EXCEPTIONS | 
|---|
| 325 | }catch(...) | 
|---|
| 326 | { close(); throw; } | 
|---|
| 327 | #endif | 
|---|
| 328 | } | 
|---|
| 329 |  | 
|---|
| 330 | void mapfile::close() | 
|---|
| 331 | { | 
|---|
| 332 | if(hfile != 0) | 
|---|
| 333 | { | 
|---|
| 334 | pointer* p = _first; | 
|---|
| 335 | while(p != _last) | 
|---|
| 336 | { | 
|---|
| 337 | if(*p) | 
|---|
| 338 | delete[] *p; | 
|---|
| 339 | ++p; | 
|---|
| 340 | } | 
|---|
| 341 | delete[] _first; | 
|---|
| 342 | _size = 0; | 
|---|
| 343 | _first = _last = 0; | 
|---|
| 344 | std::fclose(hfile); | 
|---|
| 345 | hfile = 0; | 
|---|
| 346 | condemed.erase(condemed.begin(), condemed.end()); | 
|---|
| 347 | } | 
|---|
| 348 | } | 
|---|
| 349 |  | 
|---|
| 350 |  | 
|---|
| 351 | #endif | 
|---|
| 352 |  | 
|---|
| 353 |  | 
|---|
| 354 | file_iterator::file_iterator() | 
|---|
| 355 | { | 
|---|
| 356 | _root = _path = 0; | 
|---|
| 357 | ref = 0; | 
|---|
| 358 | #ifndef BOOST_NO_EXCEPTIONS | 
|---|
| 359 | try{ | 
|---|
| 360 | #endif | 
|---|
| 361 | _root = new char[MAX_PATH]; | 
|---|
| 362 | BOOST_REGEX_NOEH_ASSERT(_root) | 
|---|
| 363 | _path = new char[MAX_PATH]; | 
|---|
| 364 | BOOST_REGEX_NOEH_ASSERT(_path) | 
|---|
| 365 | ptr = _path; | 
|---|
| 366 | *_path = 0; | 
|---|
| 367 | *_root = 0; | 
|---|
| 368 | ref = new file_iterator_ref(); | 
|---|
| 369 | BOOST_REGEX_NOEH_ASSERT(ref) | 
|---|
| 370 | ref->hf = _fi_invalid_handle; | 
|---|
| 371 | ref->count = 1; | 
|---|
| 372 | #ifndef BOOST_NO_EXCEPTIONS | 
|---|
| 373 | } | 
|---|
| 374 | catch(...) | 
|---|
| 375 | { | 
|---|
| 376 | delete[] _root; | 
|---|
| 377 | delete[] _path; | 
|---|
| 378 | delete ref; | 
|---|
| 379 | throw; | 
|---|
| 380 | } | 
|---|
| 381 | #endif | 
|---|
| 382 | } | 
|---|
| 383 |  | 
|---|
| 384 | file_iterator::file_iterator(const char* wild) | 
|---|
| 385 | { | 
|---|
| 386 | _root = _path = 0; | 
|---|
| 387 | ref = 0; | 
|---|
| 388 | #ifndef BOOST_NO_EXCEPTIONS | 
|---|
| 389 | try{ | 
|---|
| 390 | #endif | 
|---|
| 391 | _root = new char[MAX_PATH]; | 
|---|
| 392 | BOOST_REGEX_NOEH_ASSERT(_root) | 
|---|
| 393 | _path = new char[MAX_PATH]; | 
|---|
| 394 | BOOST_REGEX_NOEH_ASSERT(_path) | 
|---|
| 395 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_root, MAX_PATH, wild)); | 
|---|
| 396 | ptr = _root; | 
|---|
| 397 | while(*ptr)++ptr; | 
|---|
| 398 | while((ptr > _root) && (*ptr != *_fi_sep) && (*ptr != *_fi_sep_alt))--ptr; | 
|---|
| 399 | if((ptr == _root) && ( (*ptr== *_fi_sep) || (*ptr==*_fi_sep_alt) ) ) | 
|---|
| 400 | { | 
|---|
| 401 | _root[1]='\0'; | 
|---|
| 402 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, _root)); | 
|---|
| 403 | } | 
|---|
| 404 | else | 
|---|
| 405 | { | 
|---|
| 406 | *ptr = 0; | 
|---|
| 407 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, _root)); | 
|---|
| 408 | if(*_path == 0) | 
|---|
| 409 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, ".")); | 
|---|
| 410 | re_detail::overflow_error_if_not_zero(re_detail::strcat_s(_path, MAX_PATH, _fi_sep)); | 
|---|
| 411 | } | 
|---|
| 412 | ptr = _path + std::strlen(_path); | 
|---|
| 413 |  | 
|---|
| 414 | ref = new file_iterator_ref(); | 
|---|
| 415 | BOOST_REGEX_NOEH_ASSERT(ref) | 
|---|
| 416 | ref->hf = FindFirstFileA(wild, &(ref->_data)); | 
|---|
| 417 | ref->count = 1; | 
|---|
| 418 |  | 
|---|
| 419 | if(ref->hf == _fi_invalid_handle) | 
|---|
| 420 | { | 
|---|
| 421 | *_path = 0; | 
|---|
| 422 | ptr = _path; | 
|---|
| 423 | } | 
|---|
| 424 | else | 
|---|
| 425 | { | 
|---|
| 426 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(ptr, (MAX_PATH - (ptr - _path)), ref->_data.cFileName)); | 
|---|
| 427 | if(ref->_data.dwFileAttributes & _fi_dir) | 
|---|
| 428 | next(); | 
|---|
| 429 | } | 
|---|
| 430 | #ifndef BOOST_NO_EXCEPTIONS | 
|---|
| 431 | } | 
|---|
| 432 | catch(...) | 
|---|
| 433 | { | 
|---|
| 434 | delete[] _root; | 
|---|
| 435 | delete[] _path; | 
|---|
| 436 | delete ref; | 
|---|
| 437 | throw; | 
|---|
| 438 | } | 
|---|
| 439 | #endif | 
|---|
| 440 | } | 
|---|
| 441 |  | 
|---|
| 442 | file_iterator::file_iterator(const file_iterator& other) | 
|---|
| 443 | { | 
|---|
| 444 | _root = _path = 0; | 
|---|
| 445 | ref = 0; | 
|---|
| 446 | #ifndef BOOST_NO_EXCEPTIONS | 
|---|
| 447 | try{ | 
|---|
| 448 | #endif | 
|---|
| 449 | _root = new char[MAX_PATH]; | 
|---|
| 450 | BOOST_REGEX_NOEH_ASSERT(_root) | 
|---|
| 451 | _path = new char[MAX_PATH]; | 
|---|
| 452 | BOOST_REGEX_NOEH_ASSERT(_path) | 
|---|
| 453 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_root, MAX_PATH, other._root)); | 
|---|
| 454 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, other._path)); | 
|---|
| 455 | ptr = _path + (other.ptr - other._path); | 
|---|
| 456 | ref = other.ref; | 
|---|
| 457 | #ifndef BOOST_NO_EXCEPTIONS | 
|---|
| 458 | } | 
|---|
| 459 | catch(...) | 
|---|
| 460 | { | 
|---|
| 461 | delete[] _root; | 
|---|
| 462 | delete[] _path; | 
|---|
| 463 | throw; | 
|---|
| 464 | } | 
|---|
| 465 | #endif | 
|---|
| 466 | ++(ref->count); | 
|---|
| 467 | } | 
|---|
| 468 |  | 
|---|
| 469 | file_iterator& file_iterator::operator=(const file_iterator& other) | 
|---|
| 470 | { | 
|---|
| 471 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_root, MAX_PATH, other._root)); | 
|---|
| 472 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, other._path)); | 
|---|
| 473 | ptr = _path + (other.ptr - other._path); | 
|---|
| 474 | if(--(ref->count) == 0) | 
|---|
| 475 | { | 
|---|
| 476 | if(ref->hf != _fi_invalid_handle) | 
|---|
| 477 | FindClose(ref->hf); | 
|---|
| 478 | delete ref; | 
|---|
| 479 | } | 
|---|
| 480 | ref = other.ref; | 
|---|
| 481 | ++(ref->count); | 
|---|
| 482 | return *this; | 
|---|
| 483 | } | 
|---|
| 484 |  | 
|---|
| 485 |  | 
|---|
| 486 | file_iterator::~file_iterator() | 
|---|
| 487 | { | 
|---|
| 488 | delete[] _root; | 
|---|
| 489 | delete[] _path; | 
|---|
| 490 | if(--(ref->count) == 0) | 
|---|
| 491 | { | 
|---|
| 492 | if(ref->hf != _fi_invalid_handle) | 
|---|
| 493 | FindClose(ref->hf); | 
|---|
| 494 | delete ref; | 
|---|
| 495 | } | 
|---|
| 496 | } | 
|---|
| 497 |  | 
|---|
| 498 | file_iterator file_iterator::operator++(int) | 
|---|
| 499 | { | 
|---|
| 500 | file_iterator temp(*this); | 
|---|
| 501 | next(); | 
|---|
| 502 | return temp; | 
|---|
| 503 | } | 
|---|
| 504 |  | 
|---|
| 505 |  | 
|---|
| 506 | void file_iterator::next() | 
|---|
| 507 | { | 
|---|
| 508 | if(ref->hf != _fi_invalid_handle) | 
|---|
| 509 | { | 
|---|
| 510 | bool cont = true; | 
|---|
| 511 | while(cont) | 
|---|
| 512 | { | 
|---|
| 513 | cont = FindNextFileA(ref->hf, &(ref->_data)); | 
|---|
| 514 | if(cont && ((ref->_data.dwFileAttributes & _fi_dir) == 0)) | 
|---|
| 515 | break; | 
|---|
| 516 | } | 
|---|
| 517 | if(!cont) | 
|---|
| 518 | { | 
|---|
| 519 | // end of sequence | 
|---|
| 520 | FindClose(ref->hf); | 
|---|
| 521 | ref->hf = _fi_invalid_handle; | 
|---|
| 522 | *_path = 0; | 
|---|
| 523 | ptr = _path; | 
|---|
| 524 | } | 
|---|
| 525 | else | 
|---|
| 526 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(ptr, MAX_PATH - (ptr - _path), ref->_data.cFileName)); | 
|---|
| 527 | } | 
|---|
| 528 | } | 
|---|
| 529 |  | 
|---|
| 530 |  | 
|---|
| 531 |  | 
|---|
| 532 | directory_iterator::directory_iterator() | 
|---|
| 533 | { | 
|---|
| 534 | _root = _path = 0; | 
|---|
| 535 | ref = 0; | 
|---|
| 536 | #ifndef BOOST_NO_EXCEPTIONS | 
|---|
| 537 | try{ | 
|---|
| 538 | #endif | 
|---|
| 539 | _root = new char[MAX_PATH]; | 
|---|
| 540 | BOOST_REGEX_NOEH_ASSERT(_root) | 
|---|
| 541 | _path = new char[MAX_PATH]; | 
|---|
| 542 | BOOST_REGEX_NOEH_ASSERT(_path) | 
|---|
| 543 | ptr = _path; | 
|---|
| 544 | *_path = 0; | 
|---|
| 545 | *_root = 0; | 
|---|
| 546 | ref = new file_iterator_ref(); | 
|---|
| 547 | BOOST_REGEX_NOEH_ASSERT(ref) | 
|---|
| 548 | ref->hf = _fi_invalid_handle; | 
|---|
| 549 | ref->count = 1; | 
|---|
| 550 | #ifndef BOOST_NO_EXCEPTIONS | 
|---|
| 551 | } | 
|---|
| 552 | catch(...) | 
|---|
| 553 | { | 
|---|
| 554 | delete[] _root; | 
|---|
| 555 | delete[] _path; | 
|---|
| 556 | delete ref; | 
|---|
| 557 | throw; | 
|---|
| 558 | } | 
|---|
| 559 | #endif | 
|---|
| 560 | } | 
|---|
| 561 |  | 
|---|
| 562 | directory_iterator::directory_iterator(const char* wild) | 
|---|
| 563 | { | 
|---|
| 564 | _root = _path = 0; | 
|---|
| 565 | ref = 0; | 
|---|
| 566 | #ifndef BOOST_NO_EXCEPTIONS | 
|---|
| 567 | try{ | 
|---|
| 568 | #endif | 
|---|
| 569 | _root = new char[MAX_PATH]; | 
|---|
| 570 | BOOST_REGEX_NOEH_ASSERT(_root) | 
|---|
| 571 | _path = new char[MAX_PATH]; | 
|---|
| 572 | BOOST_REGEX_NOEH_ASSERT(_path) | 
|---|
| 573 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_root, MAX_PATH, wild)); | 
|---|
| 574 | ptr = _root; | 
|---|
| 575 | while(*ptr)++ptr; | 
|---|
| 576 | while((ptr > _root) && (*ptr != *_fi_sep) && (*ptr != *_fi_sep_alt))--ptr; | 
|---|
| 577 |  | 
|---|
| 578 | if((ptr == _root) && ( (*ptr== *_fi_sep) || (*ptr==*_fi_sep_alt) ) ) | 
|---|
| 579 | { | 
|---|
| 580 | _root[1]='\0'; | 
|---|
| 581 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, _root)); | 
|---|
| 582 | } | 
|---|
| 583 | else | 
|---|
| 584 | { | 
|---|
| 585 | *ptr = 0; | 
|---|
| 586 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, _root)); | 
|---|
| 587 | if(*_path == 0) | 
|---|
| 588 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, ".")); | 
|---|
| 589 | re_detail::overflow_error_if_not_zero(re_detail::strcat_s(_path, MAX_PATH, _fi_sep)); | 
|---|
| 590 | } | 
|---|
| 591 | ptr = _path + std::strlen(_path); | 
|---|
| 592 |  | 
|---|
| 593 | ref = new file_iterator_ref(); | 
|---|
| 594 | BOOST_REGEX_NOEH_ASSERT(ref) | 
|---|
| 595 | ref->count = 1; | 
|---|
| 596 | ref->hf = FindFirstFileA(wild, &(ref->_data)); | 
|---|
| 597 | if(ref->hf == _fi_invalid_handle) | 
|---|
| 598 | { | 
|---|
| 599 | *_path = 0; | 
|---|
| 600 | ptr = _path; | 
|---|
| 601 | } | 
|---|
| 602 | else | 
|---|
| 603 | { | 
|---|
| 604 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(ptr, MAX_PATH - (ptr - _path), ref->_data.cFileName)); | 
|---|
| 605 | if(((ref->_data.dwFileAttributes & _fi_dir) == 0) || (std::strcmp(ref->_data.cFileName, ".") == 0) || (std::strcmp(ref->_data.cFileName, "..") == 0)) | 
|---|
| 606 | next(); | 
|---|
| 607 | } | 
|---|
| 608 | #ifndef BOOST_NO_EXCEPTIONS | 
|---|
| 609 | } | 
|---|
| 610 | catch(...) | 
|---|
| 611 | { | 
|---|
| 612 | delete[] _root; | 
|---|
| 613 | delete[] _path; | 
|---|
| 614 | delete ref; | 
|---|
| 615 | throw; | 
|---|
| 616 | } | 
|---|
| 617 | #endif | 
|---|
| 618 | } | 
|---|
| 619 |  | 
|---|
| 620 | directory_iterator::~directory_iterator() | 
|---|
| 621 | { | 
|---|
| 622 | delete[] _root; | 
|---|
| 623 | delete[] _path; | 
|---|
| 624 | if(--(ref->count) == 0) | 
|---|
| 625 | { | 
|---|
| 626 | if(ref->hf != _fi_invalid_handle) | 
|---|
| 627 | FindClose(ref->hf); | 
|---|
| 628 | delete ref; | 
|---|
| 629 | } | 
|---|
| 630 | } | 
|---|
| 631 |  | 
|---|
| 632 | directory_iterator::directory_iterator(const directory_iterator& other) | 
|---|
| 633 | { | 
|---|
| 634 | _root = _path = 0; | 
|---|
| 635 | ref = 0; | 
|---|
| 636 | #ifndef BOOST_NO_EXCEPTIONS | 
|---|
| 637 | try{ | 
|---|
| 638 | #endif | 
|---|
| 639 | _root = new char[MAX_PATH]; | 
|---|
| 640 | BOOST_REGEX_NOEH_ASSERT(_root) | 
|---|
| 641 | _path = new char[MAX_PATH]; | 
|---|
| 642 | BOOST_REGEX_NOEH_ASSERT(_path) | 
|---|
| 643 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_root, MAX_PATH, other._root)); | 
|---|
| 644 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, other._path)); | 
|---|
| 645 | ptr = _path + (other.ptr - other._path); | 
|---|
| 646 | ref = other.ref; | 
|---|
| 647 | #ifndef BOOST_NO_EXCEPTIONS | 
|---|
| 648 | } | 
|---|
| 649 | catch(...) | 
|---|
| 650 | { | 
|---|
| 651 | delete[] _root; | 
|---|
| 652 | delete[] _path; | 
|---|
| 653 | throw; | 
|---|
| 654 | } | 
|---|
| 655 | #endif | 
|---|
| 656 | ++(ref->count); | 
|---|
| 657 | } | 
|---|
| 658 |  | 
|---|
| 659 | directory_iterator& directory_iterator::operator=(const directory_iterator& other) | 
|---|
| 660 | { | 
|---|
| 661 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_root, MAX_PATH, other._root)); | 
|---|
| 662 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, other._path)); | 
|---|
| 663 | ptr = _path + (other.ptr - other._path); | 
|---|
| 664 | if(--(ref->count) == 0) | 
|---|
| 665 | { | 
|---|
| 666 | if(ref->hf != _fi_invalid_handle) | 
|---|
| 667 | FindClose(ref->hf); | 
|---|
| 668 | delete ref; | 
|---|
| 669 | } | 
|---|
| 670 | ref = other.ref; | 
|---|
| 671 | ++(ref->count); | 
|---|
| 672 | return *this; | 
|---|
| 673 | } | 
|---|
| 674 |  | 
|---|
| 675 | directory_iterator directory_iterator::operator++(int) | 
|---|
| 676 | { | 
|---|
| 677 | directory_iterator temp(*this); | 
|---|
| 678 | next(); | 
|---|
| 679 | return temp; | 
|---|
| 680 | } | 
|---|
| 681 |  | 
|---|
| 682 | void directory_iterator::next() | 
|---|
| 683 | { | 
|---|
| 684 | if(ref->hf != _fi_invalid_handle) | 
|---|
| 685 | { | 
|---|
| 686 | bool cont = true; | 
|---|
| 687 | while(cont) | 
|---|
| 688 | { | 
|---|
| 689 | cont = FindNextFileA(ref->hf, &(ref->_data)); | 
|---|
| 690 | if(cont && (ref->_data.dwFileAttributes & _fi_dir)) | 
|---|
| 691 | { | 
|---|
| 692 | if(std::strcmp(ref->_data.cFileName, ".") && std::strcmp(ref->_data.cFileName, "..")) | 
|---|
| 693 | break; | 
|---|
| 694 | } | 
|---|
| 695 | } | 
|---|
| 696 | if(!cont) | 
|---|
| 697 | { | 
|---|
| 698 | // end of sequence | 
|---|
| 699 | FindClose(ref->hf); | 
|---|
| 700 | ref->hf = _fi_invalid_handle; | 
|---|
| 701 | *_path = 0; | 
|---|
| 702 | ptr = _path; | 
|---|
| 703 | } | 
|---|
| 704 | else | 
|---|
| 705 | re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(ptr, MAX_PATH - (ptr - _path), ref->_data.cFileName)); | 
|---|
| 706 | } | 
|---|
| 707 | } | 
|---|
| 708 |  | 
|---|
| 709 |  | 
|---|
| 710 | #ifdef BOOST_REGEX_FI_POSIX_DIR | 
|---|
| 711 |  | 
|---|
| 712 | struct _fi_priv_data | 
|---|
| 713 | { | 
|---|
| 714 | char root[MAX_PATH]; | 
|---|
| 715 | char* mask; | 
|---|
| 716 | DIR* d; | 
|---|
| 717 | _fi_priv_data(const char* p); | 
|---|
| 718 | }; | 
|---|
| 719 |  | 
|---|
| 720 | _fi_priv_data::_fi_priv_data(const char* p) | 
|---|
| 721 | { | 
|---|
| 722 | std::strcpy(root, p); | 
|---|
| 723 | mask = root; | 
|---|
| 724 | while(*mask) ++mask; | 
|---|
| 725 | while((mask > root) && (*mask != *_fi_sep) && (*mask != *_fi_sep_alt)) --mask; | 
|---|
| 726 | if(mask == root && ((*mask== *_fi_sep) || (*mask == *_fi_sep_alt)) ) | 
|---|
| 727 | { | 
|---|
| 728 | root[1] = '\0'; | 
|---|
| 729 | std::strcpy(root+2, p+1); | 
|---|
| 730 | mask = root+2; | 
|---|
| 731 | } | 
|---|
| 732 | else if(mask == root) | 
|---|
| 733 | { | 
|---|
| 734 | root[0] = '.'; | 
|---|
| 735 | root[1] = '\0'; | 
|---|
| 736 | std::strcpy(root+2, p); | 
|---|
| 737 | mask = root+2; | 
|---|
| 738 | } | 
|---|
| 739 | else | 
|---|
| 740 | { | 
|---|
| 741 | *mask = 0; | 
|---|
| 742 | ++mask; | 
|---|
| 743 | } | 
|---|
| 744 | } | 
|---|
| 745 |  | 
|---|
| 746 | bool iswild(const char* mask, const char* name) | 
|---|
| 747 | { | 
|---|
| 748 | while(*mask && *name) | 
|---|
| 749 | { | 
|---|
| 750 | switch(*mask) | 
|---|
| 751 | { | 
|---|
| 752 | case '?': | 
|---|
| 753 | ++name; | 
|---|
| 754 | ++mask; | 
|---|
| 755 | continue; | 
|---|
| 756 | case '*': | 
|---|
| 757 | ++mask; | 
|---|
| 758 | if(*mask == 0) | 
|---|
| 759 | return true; | 
|---|
| 760 | while(*name) | 
|---|
| 761 | { | 
|---|
| 762 | if(iswild(mask, name)) | 
|---|
| 763 | return true; | 
|---|
| 764 | ++name; | 
|---|
| 765 | } | 
|---|
| 766 | return false; | 
|---|
| 767 | case '.': | 
|---|
| 768 | if(0 == *name) | 
|---|
| 769 | { | 
|---|
| 770 | ++mask; | 
|---|
| 771 | continue; | 
|---|
| 772 | } | 
|---|
| 773 | // fall through: | 
|---|
| 774 | default: | 
|---|
| 775 | if(BOOST_REGEX_FI_TRANSLATE(*mask) != BOOST_REGEX_FI_TRANSLATE(*name)) | 
|---|
| 776 | return false; | 
|---|
| 777 | ++mask; | 
|---|
| 778 | ++name; | 
|---|
| 779 | continue; | 
|---|
| 780 | } | 
|---|
| 781 | } | 
|---|
| 782 | if(*mask != *name) | 
|---|
| 783 | return false; | 
|---|
| 784 | return true; | 
|---|
| 785 | } | 
|---|
| 786 |  | 
|---|
| 787 | unsigned _fi_attributes(const char* root, const char* name) | 
|---|
| 788 | { | 
|---|
| 789 | char buf[MAX_PATH]; | 
|---|
| 790 | if( ( (root[0] == *_fi_sep) || (root[0] == *_fi_sep_alt) ) && (root[1] == '\0') ) | 
|---|
| 791 | (std::sprintf)(buf, "%s%s", root, name); | 
|---|
| 792 | else | 
|---|
| 793 | (std::sprintf)(buf, "%s%s%s", root, _fi_sep, name); | 
|---|
| 794 | DIR* d = opendir(buf); | 
|---|
| 795 | if(d) | 
|---|
| 796 | { | 
|---|
| 797 | closedir(d); | 
|---|
| 798 | return _fi_dir; | 
|---|
| 799 | } | 
|---|
| 800 | return 0; | 
|---|
| 801 | } | 
|---|
| 802 |  | 
|---|
| 803 | _fi_find_handle _fi_FindFirstFile(const char* lpFileName, _fi_find_data* lpFindFileData) | 
|---|
| 804 | { | 
|---|
| 805 | _fi_find_handle dat = new _fi_priv_data(lpFileName); | 
|---|
| 806 |  | 
|---|
| 807 | DIR* h = opendir(dat->root); | 
|---|
| 808 | dat->d = h; | 
|---|
| 809 | if(h != 0) | 
|---|
| 810 | { | 
|---|
| 811 | if(_fi_FindNextFile(dat, lpFindFileData)) | 
|---|
| 812 | return dat; | 
|---|
| 813 | } | 
|---|
| 814 | delete dat; | 
|---|
| 815 | return 0; | 
|---|
| 816 | } | 
|---|
| 817 |  | 
|---|
| 818 | bool _fi_FindNextFile(_fi_find_handle dat, _fi_find_data* lpFindFileData) | 
|---|
| 819 | { | 
|---|
| 820 | dirent* d; | 
|---|
| 821 | do | 
|---|
| 822 | { | 
|---|
| 823 | d = readdir(dat->d); | 
|---|
| 824 | } while(d && !iswild(dat->mask, d->d_name)); | 
|---|
| 825 |  | 
|---|
| 826 | if(d) | 
|---|
| 827 | { | 
|---|
| 828 | std::strcpy(lpFindFileData->cFileName, d->d_name); | 
|---|
| 829 | lpFindFileData->dwFileAttributes = _fi_attributes(dat->root, d->d_name); | 
|---|
| 830 | return true; | 
|---|
| 831 | } | 
|---|
| 832 | return false; | 
|---|
| 833 | } | 
|---|
| 834 |  | 
|---|
| 835 | bool _fi_FindClose(_fi_find_handle dat) | 
|---|
| 836 | { | 
|---|
| 837 | closedir(dat->d); | 
|---|
| 838 | delete dat; | 
|---|
| 839 | return true; | 
|---|
| 840 | } | 
|---|
| 841 |  | 
|---|
| 842 | #endif | 
|---|
| 843 |  | 
|---|
| 844 | } // namespace re_detail | 
|---|
| 845 | } // namspace boost | 
|---|
| 846 |  | 
|---|
| 847 | #endif    // BOOST_REGEX_NO_FILEITER | 
|---|
| 848 |  | 
|---|
| 849 |  | 
|---|
| 850 |  | 
|---|
| 851 |  | 
|---|
| 852 |  | 
|---|
| 853 |  | 
|---|
| 854 |  | 
|---|
| 855 |  | 
|---|
| 856 |  | 
|---|
| 857 |  | 
|---|
| 858 |  | 
|---|
| 859 |  | 
|---|