| 1 |
#ifndef _PROTO_FILE |
| 2 |
#define _PROTO_FILE |
| 3 |
|
| 4 |
// This module defines some simple classes for manipulating files. |
| 5 |
// Unix and Win32 platforms are supported. Routines for iterating |
| 6 |
// over directories are also provided. And a file/directory list |
| 7 |
// class is provided to manage a list of files. |
| 8 |
|
| 9 |
#ifdef WIN32 |
| 10 |
//#include <io.h> |
| 11 |
#else |
| 12 |
#include <unistd.h> |
| 13 |
#include <dirent.h> |
| 14 |
#endif // if/else WIN32 |
| 15 |
|
| 16 |
#ifdef _WIN32_WCE |
| 17 |
#include <stdio.h> |
| 18 |
#else |
| 19 |
#include <fcntl.h> |
| 20 |
#include <sys/types.h> |
| 21 |
#include <sys/stat.h> |
| 22 |
#endif // if/else _WIN32_WCE |
| 23 |
|
| 24 |
#include <ctype.h> // for "isprint()" |
| 25 |
// required protolib files |
| 26 |
#include "protoChannel.h" |
| 27 |
#include "protoBitmask.h" |
| 28 |
#include "protoCap.h" |
| 29 |
#include "protoDefs.h" |
| 30 |
#include "protoDebug.h" |
| 31 |
#include "protoQueue.h" |
| 32 |
|
| 33 |
#ifndef MIN |
| 34 |
#define MIN(X,Y) ((X<Y)?X:Y) |
| 35 |
#define MAX(X,Y) ((X>Y)?X:Y) |
| 36 |
#endif // !MIN |
| 37 |
// (TBD) Rewrite this implementation to use |
| 38 |
// native WIN32 APIs on that platform !!! |
| 39 |
|
| 40 |
#ifdef _WIN32_WCE |
| 41 |
// Here we enumerate some stuff to |
| 42 |
// make the ProtoFile work on WinCE |
| 43 |
enum |
| 44 |
{ |
| 45 |
O_CREAT = 0x01, |
| 46 |
O_TRUNC = 0x02, |
| 47 |
O_RDONLY = 0x04, |
| 48 |
O_WRONLY = 0x08, |
| 49 |
O_RDWR = 0x10, |
| 50 |
O_BINARY = 0x20 |
| 51 |
}; |
| 52 |
#endif // _WIN32_WCE |
| 53 |
|
| 54 |
class ProtoFile : public ProtoChannel |
| 55 |
{ |
| 56 |
public: |
| 57 |
#ifdef WIN32 |
| 58 |
typedef __int64 Offset; |
| 59 |
#else |
| 60 |
typedef off_t Offset; |
| 61 |
#endif // if/else WIN32/UNIX |
| 62 |
|
| 63 |
enum Type {INVALID, NORMAL, DIRECTORY}; |
| 64 |
ProtoFile(); |
| 65 |
~ProtoFile(); |
| 66 |
bool Open(const char* path, int theFlags = 0); |
| 67 |
bool Lock(); |
| 68 |
void Unlock(); |
| 69 |
bool Rename(const char* oldName, const char* newName); |
| 70 |
static bool Unlink(const char *path); |
| 71 |
void Close(); |
| 72 |
bool IsOpen() const |
| 73 |
{ |
| 74 |
#ifdef _WIN32_WCE |
| 75 |
return ((NULL != file_ptr) && ProtoChannel::IsOpen()); |
| 76 |
#else |
| 77 |
return ((descriptor >= 0) && ProtoChannel::IsOpen()); |
| 78 |
#endif // _WIN32_WCE |
| 79 |
} |
| 80 |
bool Read(char* buffer, unsigned int& numBytes);//numBytes going is is requested amount comming out is amount read. Note that a return value of true with numBytes =0 means nothing was read. |
| 81 |
bool Readline(char* buffer, unsigned int& bufferSize);//uses bufferedRead to read in a line quickly will return false if full line is not available |
| 82 |
size_t Write(const char* buffer, size_t len); |
| 83 |
bool Seek(Offset theOffset); |
| 84 |
ProtoFile::Offset GetOffset() const {return (offset);} |
| 85 |
ProtoFile::Offset GetSize() const; |
| 86 |
bool Pad(Offset theOffset); // if file size is less than theOffset, writes a byte to force filesize |
| 87 |
|
| 88 |
// static helper methods |
| 89 |
static ProtoFile::Type GetType(const char *path); |
| 90 |
static ProtoFile::Offset GetSize(const char* path); |
| 91 |
static time_t GetUpdateTime(const char* path); |
| 92 |
static bool IsLocked(const char *path); |
| 93 |
|
| 94 |
static bool Exists(const char* path) |
| 95 |
{ |
| 96 |
#ifdef WIN32 |
| 97 |
#ifdef _UNICODE |
| 98 |
wchar_t wideBuffer[MAX_PATH]; |
| 99 |
size_t count = mbstowcs(wideBuffer, path, strlen(path)+1); |
| 100 |
return (0xFFFFFFFF != GetFileAttributes(wideBuffer)); |
| 101 |
#else |
| 102 |
return (0xFFFFFFFF != GetFileAttributes(path)); |
| 103 |
#endif // if/else _UNICODE |
| 104 |
#else |
| 105 |
return (0 == access(path, F_OK)); |
| 106 |
#endif // if/else WIN32 |
| 107 |
} |
| 108 |
|
| 109 |
static bool IsWritable(const char* path) |
| 110 |
{ |
| 111 |
#ifdef WIN32 |
| 112 |
#ifdef _UNICODE |
| 113 |
wchar_t wideBuffer[MAX_PATH]; |
| 114 |
size_t count = mbstowcs(wideBuffer, path, strlen(path)+1); |
| 115 |
DWORD attr = GetFileAttributes(wideBuffer); |
| 116 |
#else |
| 117 |
DWORD attr = GetFileAttributes(path); |
| 118 |
#endif // if/else _UNICODE |
| 119 |
return ((0xFFFFFFFF == attr) ? |
| 120 |
false : (0 == (attr & FILE_ATTRIBUTE_READONLY))); |
| 121 |
#else |
| 122 |
return (0 == access(path, W_OK)); |
| 123 |
#endif // if/else WIN32 |
| 124 |
} |
| 125 |
|
| 126 |
class Path : public ProtoQueue::Item |
| 127 |
{ |
| 128 |
|
| 129 |
// wrapper around file path string |
| 130 |
// with helper methods like GetBaseName(), GetDirName(), |
| 131 |
// IsDirectory(), etc |
| 132 |
public: |
| 133 |
Path(const char* path) |
| 134 |
{SetPath(path);} |
| 135 |
~Path() {} |
| 136 |
const char* GetPath() const |
| 137 |
{return path_name;} |
| 138 |
void SetPath(const char* path) |
| 139 |
{ |
| 140 |
strncpy(path_name, path, PATH_MAX); |
| 141 |
path_name[PATH_MAX] = '\0'; |
| 142 |
} |
| 143 |
Type GetType() const |
| 144 |
{return ProtoFile::GetType(path_name);} |
| 145 |
bool IsDirectory() const |
| 146 |
{return (DIRECTORY == GetType());} |
| 147 |
|
| 148 |
private: |
| 149 |
char path_name[PATH_MAX + 1]; |
| 150 |
}; // end class ProtoFile::Path |
| 151 |
|
| 152 |
private: |
| 153 |
class Directory; // forward declaration for the DirectoryIterator |
| 154 |
|
| 155 |
public: |
| 156 |
class DirectoryIterator |
| 157 |
{ |
| 158 |
public: |
| 159 |
DirectoryIterator(); |
| 160 |
~DirectoryIterator(); |
| 161 |
bool Open(const char* thePath); |
| 162 |
void Close(); |
| 163 |
bool GetPath(char* pathBuffer) const; |
| 164 |
// "path" here _MUST_ be at least PATH_MAX long! (should use Path object instead) |
| 165 |
bool GetNextPath(char* path, bool includeFiles=true, bool includeDirs=true); |
| 166 |
bool GetNextFile(char* path) |
| 167 |
{return GetNextPath(path, true, false);} |
| 168 |
bool GetNextDirectory(char* path) |
| 169 |
{return GetNextPath(path, false, true);} |
| 170 |
private: |
| 171 |
Directory* current; |
| 172 |
int path_len; |
| 173 |
}; // end class ProtoFile::DirectoryIterator |
| 174 |
|
| 175 |
// linked list of Path items, with added directory tree iteration capability |
| 176 |
class PathList : public ProtoSimpleQueueTemplate<Path> |
| 177 |
{ |
| 178 |
public: |
| 179 |
bool AppendPath(const char* thePath); |
| 180 |
class PathIterator |
| 181 |
{ |
| 182 |
// This class iterates over the path list and walks any directory |
| 183 |
// trees found. (Alternatively, use the PathList::Iterator to |
| 184 |
// simpy iterate over the linked list of Path items if that is |
| 185 |
// needed instead). |
| 186 |
public: |
| 187 |
// Use current, non-zero initTime so only files modified _after_ Init() are returned |
| 188 |
// Otherwise, the first pass iteration will include _all_ files |
| 189 |
PathIterator(PathList& pathList, bool updatesOnly = false, time_t initTime = 0); |
| 190 |
~PathIterator(); |
| 191 |
void Init(bool updatesOnly = false, time_t initTime = 0) |
| 192 |
{ |
| 193 |
updates_only = updatesOnly; |
| 194 |
big_time = initTime; |
| 195 |
Reset(); |
| 196 |
} |
| 197 |
void Reset() |
| 198 |
{ |
| 199 |
last_time = big_time; |
| 200 |
list_iterator.Reset(); |
| 201 |
} |
| 202 |
bool AppendPath(const char* thePath); |
| 203 |
// TBD - replace all the "char*" args with Path reference args instead |
| 204 |
// (and the "time_t initTime" should be a ProtoTime instead ...) |
| 205 |
bool GetNextPath(char* path, bool includeFiles=true, bool includeDirs=true); |
| 206 |
bool GetNextFile(char* path) |
| 207 |
{return GetNextPath(path, true, false);} |
| 208 |
bool GetNextDirectory(char* path) |
| 209 |
{return GetNextPath(path, false, true);} |
| 210 |
|
| 211 |
const Path* GetCurrentPathItem() const |
| 212 |
{return list_iterator.PeekPrevItem();} |
| 213 |
|
| 214 |
private: |
| 215 |
Iterator list_iterator; |
| 216 |
DirectoryIterator dir_iterator; |
| 217 |
time_t big_time; |
| 218 |
time_t last_time; |
| 219 |
bool updates_only; |
| 220 |
|
| 221 |
}; // end class ProtoFile::PathList::PathIterator |
| 222 |
|
| 223 |
}; // end class ProtoFile::PathList |
| 224 |
|
| 225 |
class PathTable : public ProtoIndexedQueueTemplate<Path> |
| 226 |
{ |
| 227 |
// Table of Path items, indexed by path name for quick lookup |
| 228 |
private: |
| 229 |
const char* GetKey(const Item& item) const |
| 230 |
{return static_cast<const Path&>(item).GetPath();} |
| 231 |
unsigned int GetKeysize(const Item& item) const |
| 232 |
{return ((unsigned int)strlen(static_cast<const Path&>(item).GetPath()) << 3);} |
| 233 |
}; // end class ProtoFile::PathTable |
| 234 |
|
| 235 |
|
| 236 |
private: |
| 237 |
bool ReadPrivate(char* buffer, unsigned int& numBytes); // helper for Readline() |
| 238 |
|
| 239 |
// This is used by the ProtoFile::DirectoryIterator |
| 240 |
class Directory |
| 241 |
{ |
| 242 |
public: |
| 243 |
char path[PATH_MAX+1]; |
| 244 |
Directory* parent; |
| 245 |
#ifdef WIN32 |
| 246 |
HANDLE hSearch; |
| 247 |
#else // UNIX |
| 248 |
DIR* dptr; |
| 249 |
#endif // if/else WIN32/UNIX |
| 250 |
Directory(const char *thePath, Directory* theParent = NULL); |
| 251 |
~Directory(); |
| 252 |
void GetFullName(char* namePtr); |
| 253 |
bool Open(); |
| 254 |
void Close(); |
| 255 |
|
| 256 |
const char* Path() const {return path;} |
| 257 |
void RecursiveCatName(char* ptr); |
| 258 |
}; // end class ProtoFile::Directory |
| 259 |
|
| 260 |
#ifdef WIN32 |
| 261 |
#ifdef _WIN32_WCE |
| 262 |
FILE* file_ptr; |
| 263 |
#else |
| 264 |
int descriptor; |
| 265 |
#endif // if/else _WIN32_WCE |
| 266 |
__int64 offset; |
| 267 |
#else // UNIX |
| 268 |
off_t offset; |
| 269 |
#endif // if/else WIN32/UNIX |
| 270 |
int flags; |
| 271 |
// This is used for buffered reading that enables |
| 272 |
// better performance for ProtoFile::Readline() |
| 273 |
// (TBD - allocate this only when needed or split to separate "FastReader" class?) |
| 274 |
enum {BUFFER_SIZE = 2048}; |
| 275 |
char read_buffer[BUFFER_SIZE]; |
| 276 |
char* read_ptr; |
| 277 |
unsigned int read_count; |
| 278 |
}; // end class ProtoFile |
| 279 |
|
| 280 |
#endif // _PROTO_FILE |