00001 #ifndef cppdir_h
00002 #define cppdir_h
00003
00004 #include <string>
00005 #include <list>
00006
00007 #include <dirent.h>
00008 #include <sys/types.h>
00009 #include <iostream>
00010
00016 namespace CppDir
00017 {
00018 class File
00019 {
00020 public:
00021 typedef enum Type
00022 {
00023 UNKNOWN = 0,
00024 FIFO = 1,
00025 CHAR = 2,
00026 DIR = 4,
00027 BLOCK = 6,
00028 REGULAR = 8
00029 };
00030
00031 typedef unsigned long size_type;
00032
00033 private:
00034 std::string name;
00035 Type type;
00036
00037 long inode_number;
00038 bool valid;
00039
00040 std::string path;
00041 size_type file_size;
00042 size_type date;
00043 size_type access_date;
00044 bool err;
00045
00046 public:
00048 File( struct dirent d, std::string path );
00049 File( std::string path, std::string name );
00050 File( std::string path );
00051
00052 bool is_valid() const { return valid; }
00053 bool operator!() const { return !valid; }
00054
00055 std::string get_name() const { return name; }
00056 Type get_type() const { return type; }
00057 long get_inode() const { return inode_number; }
00058
00059 std::string get_path() const { return path; }
00060 size_type get_size() const { return file_size; }
00061 size_type get_ksize() const { return file_size / 1024; }
00062 size_type get_msize() const { return get_ksize() / 1024; }
00063 size_type get_gsize() const { return get_msize() / 1024; }
00064 size_type get_date() const { return date; }
00065 size_type get_access_date() const { return access_date; }
00066
00067 private:
00068 Type get_type( const std::string& name );
00069 };
00070
00071 inline bool operator == ( const File& f1, const File& f2 )
00072 { return f1.get_name() == f2.get_name(); }
00073
00074
00075
00076 class Directory
00077 {
00078 public:
00079 typedef std::list<File> file_list;
00080 typedef file_list::iterator file_list_it;
00081
00082 private:
00083 std::string name;
00084 bool valid;
00085 file_list files;
00086 bool is_open;
00087
00088
00089 DIR* dir;
00090
00091 public:
00092 Directory( std::string pname );
00093 Directory( File file );
00094
00095 bool is_valid() const { return valid; }
00096 bool operator!() const { return !valid; }
00097
00099 file_list get_files() { return files; }
00100
00101 private:
00102 bool open( File file );
00103 bool open( std::string fname );
00104 void close();
00105 };
00106
00107 std::ostream& operator << ( std::ostream& out , File::Type type );
00108
00110 std::string concat_dir( std::string path, std::string name );
00111
00113 std::string pwd();
00114
00116 static std::string exec_path = pwd();
00117 }
00118
00119 #endif