00001 // windirectory.cpp 00002 // 00003 // Copyright (C) 2002, Chris Laurel <claurel@shatters.net> 00004 // 00005 // This program is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU General Public License 00007 // as published by the Free Software Foundation; either version 2 00008 // of the License, or (at your option) any later version. 00009 00010 #include <iostream> 00011 #include <windows.h> 00012 #include "directory.h" 00013 00014 using namespace std; 00015 00016 00017 class WindowsDirectory : public Directory 00018 { 00019 public: 00020 WindowsDirectory(const std::string&); 00021 virtual ~WindowsDirectory(); 00022 00023 virtual bool nextFile(std::string&); 00024 00025 enum { 00026 DirGood = 0, 00027 DirBad = 1 00028 }; 00029 00030 private: 00031 string dirname; 00032 string searchName; 00033 int status; 00034 HANDLE searchHandle; 00035 }; 00036 00037 00038 WindowsDirectory::WindowsDirectory(const std::string& _dirname) : 00039 dirname(_dirname), 00040 status(DirGood), 00041 searchHandle(INVALID_HANDLE_VALUE) 00042 { 00043 searchName = dirname + string("\\*"); 00044 // Check to make sure that this file is a directory 00045 } 00046 00047 00048 WindowsDirectory::~WindowsDirectory() 00049 { 00050 if (searchHandle != INVALID_HANDLE_VALUE) 00051 FindClose(searchHandle); 00052 searchHandle = NULL; 00053 } 00054 00055 00056 bool WindowsDirectory::nextFile(std::string& filename) 00057 { 00058 WIN32_FIND_DATA findData; 00059 00060 if (status != DirGood) 00061 return false; 00062 00063 if (searchHandle == INVALID_HANDLE_VALUE) 00064 { 00065 searchHandle = FindFirstFile(const_cast<LPCTSTR>(searchName.c_str()), 00066 &findData); 00067 if (searchHandle == INVALID_HANDLE_VALUE) 00068 { 00069 status = DirBad; 00070 return false; 00071 } 00072 else 00073 { 00074 filename = findData.cFileName; 00075 return true; 00076 } 00077 } 00078 else 00079 { 00080 if (FindNextFile(searchHandle, &findData)) 00081 { 00082 filename = findData.cFileName; 00083 return true; 00084 } 00085 else 00086 { 00087 status = DirBad; 00088 return false; 00089 } 00090 } 00091 } 00092 00093 00094 Directory* OpenDirectory(const std::string& dirname) 00095 { 00096 return new WindowsDirectory(dirname); 00097 } 00098 00099 00100 bool IsDirectory(const std::string& filename) 00101 { 00102 DWORD attr = GetFileAttributes(const_cast<LPCTSTR>(filename.c_str())); 00103 if (attr == 0xffffffff) 00104 return false; 00105 else 00106 return ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0); 00107 } 00108 00109 std::string WordExp(const std::string& filename) { 00110 return filename; 00111 }
1.4.1