?? file.h
字號:
// Copyright (C) 1999-2005 Open Source Telecom Corporation.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//// As a special exception, you may use this file as part of a free software// library without restriction. Specifically, if other files instantiate// templates or use macros or inline functions from this file, or you compile// this file and link it with other files to produce an executable, this// file does not by itself cause the resulting executable to be covered by// the GNU General Public License. This exception does not however // invalidate any other reasons why the executable file might be covered by// the GNU General Public License. //// This exception applies only to the code released under the name GNU// Common C++. If you copy code from other releases into a copy of GNU// Common C++, as the General Public License permits, the exception does// not apply to the code that you add in this way. To avoid misleading// anyone as to the status of such modified files, you must delete// this exception notice from them.//// If you write modifications of your own for GNU Common C++, it is your choice// whether to permit this exception to apply to your modifications.// If you do not wish that, delete this exception notice.///** * @file file.h * @short Files and dynamic loader services. **/#ifndef CCXX_FILE_H_#define CCXX_FILE_H_#ifndef CCXX_CONFIG_H_#include <cc++/config.h>#endif#ifndef CCXX_MISSING_H_#include <cc++/missing.h>#endif#ifndef CCXX_THREAD_H_#include <cc++/thread.h>#endif#ifndef CCXX_EXCEPTION_H_#include <cc++/exception.h>#endif#ifndef WIN32# ifdef __BORLANDC__# include <stdio.h># include <sys/types.h># else# include <cstdio># endif# include <dirent.h># include <sys/stat.h># include <sys/mman.h>#else# if __BORLANDC__ >= 0x0560# include <dirent.h># include <sys/stat.h># else# include <direct.h># endif#endif#ifdef HAVE_SHL_LOAD#include <dl.h>#endif#ifdef HAVE_MACH_DYLD#include <mach-o/dyld.h>#endif#ifdef CCXX_NAMESPACESnamespace ost {#endiftypedef unsigned long pos_t;#ifndef WIN32// use a define so that if the sys/types.h header already defines caddr_t// as it may on BSD systems, we do not break it by redefining again.#undef caddr_t#define caddr_t char *typedef size_t ccxx_size_t;#else#if !defined(__BORLANDC__) || __BORLANDC__ >= 0x0560typedef LONG off_t;#endiftypedef void* caddr_t;typedef DWORD ccxx_size_t;#endif#ifndef PATH_MAX#define PATH_MAX 256#endif#ifndef NAME_MAX#define NAME_MAX 64#endifclass __EXPORT File{public: enum Error { errSuccess = 0, errNotOpened, errMapFailed, errInitFailed, errOpenDenied, errOpenFailed, errOpenInUse, errReadInterrupted, errReadIncomplete, errReadFailure, errWriteInterrupted, errWriteIncomplete, errWriteFailure, errLockFailure, errExtended }; typedef enum Error Error; enum Access {#ifndef WIN32 accessReadOnly = O_RDONLY, accessWriteOnly= O_WRONLY, accessReadWrite = O_RDWR#else accessReadOnly = GENERIC_READ, accessWriteOnly = GENERIC_WRITE, accessReadWrite = GENERIC_READ | GENERIC_WRITE#endif }; typedef enum Access Access;protected: typedef struct _fcb { struct _fcb *next; caddr_t address; ccxx_size_t len; off_t pos; bool locked; } fcb_t;public:#ifdef WIN32 enum Open { openReadOnly, // = FILE_OPEN_READONLY, openWriteOnly, // = FILE_OPEN_WRITEONLY, openReadWrite, // = FILE_OPEN_READWRITE, openAppend, // = FILE_OPEN_APPEND, openTruncate // = FILE_OPEN_TRUNCATE }; #else enum Open { openReadOnly = O_RDONLY, openWriteOnly = O_WRONLY, openReadWrite = O_RDWR, openAppend = O_WRONLY | O_APPEND,#ifdef O_SYNC openSync = O_RDWR | O_SYNC,#else openSync = O_RDWR,#endif openTruncate = O_RDWR | O_TRUNC }; typedef enum Open Open;/* to be used in future */#ifndef S_IRUSR#define S_IRUSR 0400#define S_IWUSR 0200#define S_IRGRP 0040#define S_IWGRP 0020#define S_IROTH 0004#define S_IWOTH 0002#endif#endif // !WIN32#ifndef WIN32 enum Attr { attrInvalid = 0, attrPrivate = S_IRUSR | S_IWUSR, attrGroup = attrPrivate | S_IRGRP | S_IWGRP, attrPublic = attrGroup | S_IROTH | S_IWOTH }; #else // defined WIN32 enum Attr { attrInvalid=0, attrPrivate, attrGroup, attrPublic };#endif // !WIN32 typedef enum Attr Attr;#ifdef WIN32 enum Complete { completionImmediate, // = FILE_COMPLETION_IMMEDIATE, completionDelayed, // = FILE_COMPLETION_DELAYED, completionDeferred // = FILE_COMPLETION_DEFERRED }; enum Mapping { mappedRead, mappedWrite, mappedReadWrite };#else enum Mapping { mappedRead = accessReadOnly, mappedWrite = accessWriteOnly, mappedReadWrite = accessReadWrite }; enum Complete { completionImmediate, completionDelayed, completionDeferred };#endif typedef enum Complete Complete; typedef enum Mapping Mapping;public: static const char *getExtension(const char *path); static const char *getFilename(const char *path); static char *getFilename(const char *path, char *buffer, size_t size = NAME_MAX); static char *getDirname(const char *path, char *buffer, size_t size = PATH_MAX); static char *getRealpath(const char *path, char *buffer, size_t size = PATH_MAX);};/** * A low level portable directory class. Used to support ccstd Directory * container. This provides a basic mechanism for allocating and * accessing file entries. * * @author David Sugar <dyfet@ostel.com> * @short low level directory access class. */class __EXPORT Dir : public File{private:#ifndef WIN32 DIR *dir;#ifdef HAVE_READDIR_R struct dirent *save; char save_space[sizeof(struct dirent) + PATH_MAX + 1];#endif struct dirent *entry;#else HANDLE hDir; WIN32_FIND_DATA data, fdata; char *name;#endifpublic: Dir(const char *name = NULL); static bool create(const char *path, Attr attr = attrGroup); static bool remove(const char *path); static bool setPrefix(const char *path); static bool getPrefix(char *path, size_t size = PATH_MAX); void open(const char *name); void close(void); virtual ~Dir(); const char *getName(void); const char *operator++() {return getName();}; const char *operator++(int) {return getName();}; const char *operator*(); bool rewind(void); bool operator!()#ifndef WIN32 {return !dir;};#else {return hDir != INVALID_HANDLE_VALUE;};#endif bool isValid(void);};/** * A generic class to walk a hierarchical directory structure. * * @author David Sugar <dyfet@ostel.com> * @short Directory tree walking. */class __EXPORT DirTree {private: char path[PATH_MAX + 1]; Dir *dir; unsigned max, current, prefixpos;protected: /** * Virtual method to filter results. Virtual override methods * should call baseclass method to assure . and .. names are * stripped out. * * @return true if current filename is accepted. * @param file path to examine * @param ino info of type, date, etc. */ virtual bool filter(const char *file, struct stat *ino);public: /** * Construct a directory tree walk starting at the specified * prefix. A maximum subdirectory depth is also specified. * * @param prefix to start walk. * @param maxdepth subdirectory depth to examine. */ DirTree(const char *prefix, unsigned maxdepth); /** * Construct an un-opened directory tree of a known maximum depth * * @param maxdepth subdirectory subdirectory depth. */ DirTree(unsigned maxdepth); virtual ~DirTree(); /** * Open a directory tree path. * * @param prefix directory path to open. */ void open(const char *prefix); /** * Close the directory path. */ void close(void); /** * Extract the next full pathname from the directory walk. * When returning directories, a '/' is appended. The * returned string is a buffer of MAX_PATH size. * * @return path of next subdirectory entry or NULL. */ char *getPath(void); /** * This is used to step through the filter virtual for an * entire subtree, and is used for cases where a derived * DirTree class performs it's primary operations through * filter rather than externally by calling getPath(). * * @return number of files and directories examined. * @param prefix directory path to examine. */ unsigned perform(const char *prefix);};/** * The purpose of this class is to define a base class for low level * random file access that is portable between Win32 and Posix systems. * This class is a foundation both for optimized thread shared and * traditional locked file access that is commonly used to build * database services, rather than the standard C++ streaming file classes. * * @author David Sugar <dyfet@ostel.com> * @short Portable random disk file access. */class __EXPORT RandomFile : protected Mutex, public File{private: Error errid; char *errstr;protected:#ifndef WIN32 int fd; // FIXME: WIN32 as no access member Access access;#else HANDLE fd;#endif char *pathname; struct { unsigned count : 16; bool thrown : 1; bool initial : 1;#ifndef WIN32 bool immediate : 1;#endif bool temp : 1; } flags; /** * Create an unopened random access file. */ RandomFile(const char *name = NULL); /** * Default copy constructor. */ RandomFile(const RandomFile &rf); /** * Post an error event. * * @return error code. * @param errid error code. * @param errstr error message string. */ Error error(Error errid, char *errstr = NULL); /** * Post an extended string error message. * * @return errExtended. * @param err error string. */ inline Error error(char *err) {return error(errExtended, err);}; /** * Used to enable or disable throwing of exceptions on * errors. * * @param enable true if errors will be thrown. */ inline void setError(bool enable) {flags.thrown = !enable;};#ifndef WIN32 /** * Used to set file completion modes. * * @return errSuccess if okay. * @param mode completion mode. * @todo implement in win32 */ Error setCompletion(Complete mode);#endif /** * Used to set the temporary attribute for the file. Temporary * files are automatically deleted when closed. * * @param enable true for marking as temporary. */ inline void setTemporary(bool enable) {flags.temp = enable;}; /** * This method is used to initialize a newly created file as * indicated by the "initial" flag. This method also returns * the file access permissions that should be associated with * the file. This method should never be called directly, but * is instead used to impliment the "Initial" method. Typically * one would use this to build an empty database shell when a * previously empty database file is created. * * @return access, or attrInvalid if should be removed. */ virtual Attr initialize(void); /** * Close the file. */ void final(void);public: /** * Destroy a random access file or it's derived class. */ virtual ~RandomFile(); /** * This method should be called right after a RandomFile derived * object has been created. This method will invoke initialize * if the object is newly created, and set file access permissions * appropriately. * * @return true if file had to be initialized. */ bool initial(void); /**
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -