fileiter.cpp

来自「Boost provides free peer-reviewed portab」· C++ 代码 · 共 909 行 · 第 1/2 页

CPP
909
字号
     re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, _root));     if(*_path == 0)       re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, "."));     re_detail::overflow_error_if_not_zero(re_detail::strcat_s(_path, MAX_PATH, _fi_sep));   }   ptr = _path + std::strlen(_path);   ref = new file_iterator_ref();   BOOST_REGEX_NOEH_ASSERT(ref)   ref->hf = find_first_file(wild,  ref->_data);   ref->count = 1;   if(ref->hf == _fi_invalid_handle)   {      *_path = 0;      ptr = _path;   }   else   {      copy_find_file_result_with_overflow_check(ref->_data,  ptr, (MAX_PATH - (ptr - _path)));      if(ref->_data.dwFileAttributes & _fi_dir)         next();   }#ifndef BOOST_NO_EXCEPTIONS   }   catch(...)   {      delete[] _root;      delete[] _path;      delete ref;      throw;   }#endif}file_iterator::file_iterator(const file_iterator& other){   _root = _path = 0;   ref = 0;#ifndef BOOST_NO_EXCEPTIONS   try{#endif   _root = new char[MAX_PATH];   BOOST_REGEX_NOEH_ASSERT(_root)   _path = new char[MAX_PATH];   BOOST_REGEX_NOEH_ASSERT(_path)   re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_root, MAX_PATH, other._root));   re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, other._path));   ptr = _path + (other.ptr - other._path);   ref = other.ref;#ifndef BOOST_NO_EXCEPTIONS   }   catch(...)   {      delete[] _root;      delete[] _path;      throw;   }#endif   ++(ref->count);}file_iterator& file_iterator::operator=(const file_iterator& other){   re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_root, MAX_PATH, other._root));   re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, other._path));   ptr = _path + (other.ptr - other._path);   if(--(ref->count) == 0)   {      if(ref->hf != _fi_invalid_handle)         FindClose(ref->hf);      delete ref;   }   ref = other.ref;   ++(ref->count);   return *this;}file_iterator::~file_iterator(){   delete[] _root;   delete[] _path;   if(--(ref->count) == 0)   {      if(ref->hf != _fi_invalid_handle)         FindClose(ref->hf);      delete ref;   }}file_iterator file_iterator::operator++(int){   file_iterator temp(*this);   next();   return temp;}void file_iterator::next(){   if(ref->hf != _fi_invalid_handle)   {      bool cont = true;      while(cont)      {         cont = find_next_file(ref->hf, ref->_data);         if(cont && ((ref->_data.dwFileAttributes & _fi_dir) == 0))            break;      }      if(!cont)      {         // end of sequence         FindClose(ref->hf);         ref->hf = _fi_invalid_handle;         *_path = 0;         ptr = _path;      }      else         copy_find_file_result_with_overflow_check(ref->_data,  ptr, MAX_PATH - (ptr - _path));   }}directory_iterator::directory_iterator(){   _root = _path = 0;   ref = 0;#ifndef BOOST_NO_EXCEPTIONS   try{#endif   _root = new char[MAX_PATH];   BOOST_REGEX_NOEH_ASSERT(_root)   _path = new char[MAX_PATH];   BOOST_REGEX_NOEH_ASSERT(_path)   ptr = _path;   *_path = 0;   *_root = 0;   ref = new file_iterator_ref();   BOOST_REGEX_NOEH_ASSERT(ref)   ref->hf = _fi_invalid_handle;   ref->count = 1;#ifndef BOOST_NO_EXCEPTIONS   }   catch(...)   {      delete[] _root;      delete[] _path;      delete ref;      throw;   }#endif}directory_iterator::directory_iterator(const char* wild){   _root = _path = 0;   ref = 0;#ifndef BOOST_NO_EXCEPTIONS   try{#endif   _root = new char[MAX_PATH];   BOOST_REGEX_NOEH_ASSERT(_root)   _path = new char[MAX_PATH];   BOOST_REGEX_NOEH_ASSERT(_path)   re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_root, MAX_PATH, wild));   ptr = _root;   while(*ptr)++ptr;   while((ptr > _root) && (*ptr != *_fi_sep) && (*ptr != *_fi_sep_alt))--ptr;   if((ptr == _root) && ( (*ptr== *_fi_sep) || (*ptr==*_fi_sep_alt) ) )   {     _root[1]='\0';     re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, _root));   }   else   {     *ptr = 0;     re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, _root));     if(*_path == 0)       re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, "."));     re_detail::overflow_error_if_not_zero(re_detail::strcat_s(_path, MAX_PATH, _fi_sep));   }   ptr = _path + std::strlen(_path);   ref = new file_iterator_ref();   BOOST_REGEX_NOEH_ASSERT(ref)   ref->count = 1;   ref->hf = find_first_file(wild,  ref->_data);   if(ref->hf == _fi_invalid_handle)   {      *_path = 0;      ptr = _path;   }   else   {      copy_find_file_result_with_overflow_check(ref->_data,  ptr, MAX_PATH - (ptr - _path));      if(((ref->_data.dwFileAttributes & _fi_dir) == 0) || (std::strcmp(ptr, ".") == 0) || (std::strcmp(ptr, "..") == 0))         next();   }#ifndef BOOST_NO_EXCEPTIONS   }   catch(...)   {      delete[] _root;      delete[] _path;      delete ref;      throw;   }#endif}directory_iterator::~directory_iterator(){   delete[] _root;   delete[] _path;   if(--(ref->count) == 0)   {      if(ref->hf != _fi_invalid_handle)         FindClose(ref->hf);      delete ref;   }}directory_iterator::directory_iterator(const directory_iterator& other){   _root = _path = 0;   ref = 0;#ifndef BOOST_NO_EXCEPTIONS   try{#endif   _root = new char[MAX_PATH];   BOOST_REGEX_NOEH_ASSERT(_root)   _path = new char[MAX_PATH];   BOOST_REGEX_NOEH_ASSERT(_path)   re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_root, MAX_PATH, other._root));   re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, other._path));   ptr = _path + (other.ptr - other._path);   ref = other.ref;#ifndef BOOST_NO_EXCEPTIONS   }   catch(...)   {      delete[] _root;      delete[] _path;      throw;   }#endif   ++(ref->count);}directory_iterator& directory_iterator::operator=(const directory_iterator& other){   re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_root, MAX_PATH, other._root));   re_detail::overflow_error_if_not_zero(re_detail::strcpy_s(_path, MAX_PATH, other._path));   ptr = _path + (other.ptr - other._path);   if(--(ref->count) == 0)   {      if(ref->hf != _fi_invalid_handle)         FindClose(ref->hf);      delete ref;   }   ref = other.ref;   ++(ref->count);   return *this;}directory_iterator directory_iterator::operator++(int){   directory_iterator temp(*this);   next();   return temp;}void directory_iterator::next(){   if(ref->hf != _fi_invalid_handle)   {      bool cont = true;      while(cont)      {         cont = find_next_file(ref->hf, ref->_data);         if(cont && (ref->_data.dwFileAttributes & _fi_dir))         {            if(is_not_current_or_parent_path_string(ref->_data))               break;         }      }      if(!cont)      {         // end of sequence         FindClose(ref->hf);         ref->hf = _fi_invalid_handle;         *_path = 0;         ptr = _path;      }      else         copy_find_file_result_with_overflow_check(ref->_data,  ptr, MAX_PATH - (ptr - _path));   }}#ifdef BOOST_REGEX_FI_POSIX_DIRstruct _fi_priv_data{   char root[MAX_PATH];   char* mask;   DIR* d;   _fi_priv_data(const char* p);};_fi_priv_data::_fi_priv_data(const char* p){   std::strcpy(root, p);   mask = root;   while(*mask) ++mask;   while((mask > root) && (*mask != *_fi_sep) && (*mask != *_fi_sep_alt)) --mask;   if(mask == root && ((*mask== *_fi_sep) || (*mask == *_fi_sep_alt)) )   {      root[1] = '\0';      std::strcpy(root+2, p+1);      mask = root+2;   }   else if(mask == root)   {      root[0] = '.';      root[1] = '\0';      std::strcpy(root+2, p);      mask = root+2;   }   else   {      *mask = 0;      ++mask;   }}bool iswild(const char* mask, const char* name){   while(*mask && *name)   {      switch(*mask)      {      case '?':         ++name;         ++mask;         continue;      case '*':         ++mask;         if(*mask == 0)            return true;         while(*name)         {            if(iswild(mask, name))               return true;            ++name;         }         return false;      case '.':         if(0 == *name)         {            ++mask;            continue;         }         // fall through:      default:         if(BOOST_REGEX_FI_TRANSLATE(*mask) != BOOST_REGEX_FI_TRANSLATE(*name))            return false;         ++mask;         ++name;         continue;      }   }   if(*mask != *name)      return false;   return true;}unsigned _fi_attributes(const char* root, const char* name){   char buf[MAX_PATH];   if( ( (root[0] == *_fi_sep) || (root[0] == *_fi_sep_alt) ) && (root[1] == '\0') )      (std::sprintf)(buf, "%s%s", root, name);   else      (std::sprintf)(buf, "%s%s%s", root, _fi_sep, name);   DIR* d = opendir(buf);   if(d)   {      closedir(d);      return _fi_dir;   }   return 0;}_fi_find_handle _fi_FindFirstFile(const char* lpFileName, _fi_find_data* lpFindFileData){   _fi_find_handle dat = new _fi_priv_data(lpFileName);   DIR* h = opendir(dat->root);   dat->d = h;   if(h != 0)   {      if(_fi_FindNextFile(dat, lpFindFileData))         return dat;   }   delete dat;   return 0;}bool _fi_FindNextFile(_fi_find_handle dat, _fi_find_data* lpFindFileData){   dirent* d;   do   {      d = readdir(dat->d);   } while(d && !iswild(dat->mask, d->d_name));   if(d)   {      std::strcpy(lpFindFileData->cFileName, d->d_name);      lpFindFileData->dwFileAttributes = _fi_attributes(dat->root, d->d_name);      return true;   }   return false;}bool _fi_FindClose(_fi_find_handle dat){   closedir(dat->d);   delete dat;   return true;}#endif} // namespace re_detail} // namspace boost#endif    // BOOST_REGEX_NO_FILEITER

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?