⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileiter.cpp

📁 boost库提供标准的C++ API 配合dev c++使用,功能更加强大
💻 CPP
📖 第 1 页 / 共 2 页
字号:
}

file_iterator::file_iterator(const file_iterator& other)
{
   BOOST_RE_GUARD_STACK
   _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)
   std::strcpy(_root, other._root);
   std::strcpy(_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)
{
   BOOST_RE_GUARD_STACK
   std::strcpy(_root, other._root);
   std::strcpy(_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()
{
   BOOST_RE_GUARD_STACK
   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)
{
   BOOST_RE_GUARD_STACK
   file_iterator temp(*this);
   next();
   return temp;
}


void file_iterator::next()
{
   BOOST_RE_GUARD_STACK
   if(ref->hf != _fi_invalid_handle)
   {
      bool cont = true;
      while(cont)
      {
         cont = FindNextFileA(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
         std::strcpy(ptr, ref->_data.cFileName);
   }
}



directory_iterator::directory_iterator()
{
   BOOST_RE_GUARD_STACK
   _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)
{
   BOOST_RE_GUARD_STACK
   _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)
   std::strcpy(_root, wild);
   ptr = _root;
   while(*ptr)++ptr;
   while((ptr > _root) && (*ptr != *_fi_sep) && (*ptr != *_fi_sep_alt))--ptr;
   #if 0
   *ptr = 0;
   std::strcpy(_path, _root);
   if(*_path == 0)
      std::strcpy(_path, ".");
   std::strcat(_path, _fi_sep);
   ptr = _path + std::strlen(_path);
   #else
   if((ptr == _root) && ( (*ptr== *_fi_sep) || (*ptr==*_fi_sep_alt) ) )
   {
     _root[1]='\0';
     std::strcpy(_path, _root);
     ptr = _path + std::strlen(_path);
   }
   else
   {
     *ptr = 0;
     std::strcpy(_path, _root);
     if(*_path == 0)
       std::strcpy(_path, ".");
     std::strcat(_path, _fi_sep);
     ptr = _path + std::strlen(_path);
   }
   #endif
   ref = new file_iterator_ref();
   BOOST_REGEX_NOEH_ASSERT(ref)
   ref->count = 1;
   ref->hf = FindFirstFileA(wild, &(ref->_data));
   if(ref->hf == _fi_invalid_handle)
   {
      *_path = 0;
      ptr = _path;
   }
   else
   {
      std::strcpy(ptr, ref->_data.cFileName);
      if(((ref->_data.dwFileAttributes & _fi_dir) == 0) || (std::strcmp(ref->_data.cFileName, ".") == 0) || (std::strcmp(ref->_data.cFileName, "..") == 0))
         next();
   }
#ifndef BOOST_NO_EXCEPTIONS
   }
   catch(...)
   {
      delete[] _root;
      delete[] _path;
      delete ref;
      throw;
   }
#endif
}

directory_iterator::~directory_iterator()
{
   BOOST_RE_GUARD_STACK
   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)
{
   BOOST_RE_GUARD_STACK
   _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)
   std::strcpy(_root, other._root);
   std::strcpy(_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)
{
   BOOST_RE_GUARD_STACK
   std::strcpy(_root, other._root);
   std::strcpy(_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)
{
   BOOST_RE_GUARD_STACK
   directory_iterator temp(*this);
   next();
   return temp;
}

void directory_iterator::next()
{
   BOOST_RE_GUARD_STACK
   if(ref->hf != _fi_invalid_handle)
   {
      bool cont = true;
      while(cont)
      {
         cont = FindNextFileA(ref->hf, &(ref->_data));
         if(cont && (ref->_data.dwFileAttributes & _fi_dir))
         {
            if(std::strcmp(ref->_data.cFileName, ".") && std::strcmp(ref->_data.cFileName, ".."))
               break;
         }
      }
      if(!cont)
      {
         // end of sequence
         FindClose(ref->hf);
         ref->hf = _fi_invalid_handle;
         *_path = 0;
         ptr = _path;
      }
      else
         std::strcpy(ptr, ref->_data.cFileName);
   }
}


#ifdef BOOST_REGEX_FI_POSIX_DIR

struct _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)
{
   BOOST_RE_GUARD_STACK
   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)
{
   BOOST_RE_GUARD_STACK
   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)
{
   BOOST_RE_GUARD_STACK
   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)
{
   BOOST_RE_GUARD_STACK
   _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)
{
   BOOST_RE_GUARD_STACK
   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)
{
   BOOST_RE_GUARD_STACK
   closedir(dat->d);
   delete dat;
   return true;
}

#endif

} // namespace re_detail
} // namspace boost

#endif    // BOOST_REGEX_NO_FILEITER












⌨️ 快捷键说明

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