00001
00002
00003 #ifndef _FILECLS_H
00004 #define _FILECLS_H
00005
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include "general.h"
00009
00010 #ifndef MPLIB_SINGLE
00011 class LFile : public LFDStream
00012 #else
00013 class LFile : public LStream
00014 #endif
00015 {
00016 public:
00017
00018 enum {
00019 SeekBegin=0,
00020 SeekRel=1,
00021 SeekEnd=2
00022 };
00023 enum {
00024 O_Read=O_RDONLY,
00025 O_Write=O_WRONLY,
00026 O_AccMode=O_ACCMODE,
00027 O_ReadWrite=O_RDWR,
00028 O_Create=O_CREAT,
00029 O_Exclusive=O_EXCL,
00030 O_Truncate=O_TRUNC,
00031 O_Append=O_APPEND,
00032
00033 O_WriteNew=O_WRONLY|O_TRUNC|O_CREAT
00034 };
00035 enum {
00036 FILE_UNKNOWN,
00037 FILE_GZ,
00038 FILE_BZ2
00039 };
00040 protected:
00041
00042 struct stat m_stat;
00043
00044 LFile(int h) :handle(h)
00045 {
00046 if(fstat(handle, &m_stat) < 0) {
00047 SYSERROR("Can't stat file !");
00048 LSys::Abort();
00049 }
00050 }
00051 public:
00052 int handle;
00053 LFile(const char *name, unsigned attr, mode_t mode=00644)
00054 {
00055 handle=open(name, attr, mode);
00056 if(handle < 0) {
00057 SYSERROR("File open error : "<< name );
00058 LSys::Abort();
00059 }
00060 Stat();
00061 }
00062 virtual ~LFile()
00063 {
00064 close(handle);
00065 }
00066 virtual int Read(char *buffer, int size)
00067 {
00068 ASSERT(buffer!=NULL || size==0);
00069 ASSERT(handle > 0);
00070 return read(handle, buffer, size);
00071 }
00072 virtual int Write(const char *data, int size)
00073 {
00074 ASSERT(data!=NULL || size==0);
00075 ASSERT(handle > 0);
00076 return write(handle, data, size);
00077 }
00078
00079 virtual int Seek(int gap, int mode)
00080 { return lseek(handle, gap, mode); }
00081
00082 int SeekFromBegin(int gap)
00083 { return Seek(gap, SeekBegin); }
00084
00085 int SeekFromEnd(int gap)
00086 { return Seek(gap, SeekEnd); }
00087
00088 int SeekRelatively(int gap)
00089 { return Seek(gap, SeekRel); }
00090 void Stat()
00091 {
00092 if(fstat(handle, &m_stat) < 0) {
00093 SYSERROR("LFile::Stat(): Can't stat file");
00094 LSys::Abort();
00095 }
00096 }
00097
00098 unsigned long Length() { return (unsigned long)m_stat.st_size; }
00099
00100 static LFile *MakeTemp(char *tpl)
00101 {
00102 int handle;
00103 handle=mkstemp(tpl);
00104 if(handle > 0) return new LFile(handle);
00105 return NULL;
00106 }
00107 static LFile *Open(const char *name, unsigned attr, mode_t mode=00644)
00108 {
00109 int handle;
00110 handle=open(name, attr, mode);
00111 if(handle > 0) return new LFile(handle);
00112 return NULL;
00113 }
00114 static void Close(LFile *&f)
00115 {
00116 if(f!=NULL) {
00117 ASSERT(f->handle>0);
00118 delete f;
00119 f=NULL;
00120 }
00121 }
00122 static int Unlink(const char * name)
00123 {
00124 int ret=unlink(name);
00125 return ret==-1?-errno:ret;
00126 }
00127 static int Rename(const char *oldname, const char *newname)
00128 {
00129 int ret=rename(oldname, newname);
00130 return ret==-1?-errno:ret;
00131 }
00132 static int Symlink(const char* oldpath, const char *newpath)
00133 {
00134 int ret=symlink(oldpath, newpath);
00135 return ret==-1?-errno:ret;
00136 }
00137 static int Stat(const char *name, struct stat *buf=0)
00138 {
00139 static struct stat buff;
00140 if(buf==0) buf=&buff;
00141 int ret=stat(name, buf);
00142 return ret==-1?-errno:ret;
00143 }
00144
00145
00146
00147 static int LoadToString(const char *fname, char *&buffer, int size);
00148 static void BZipFile(const char *fname, bool bg=false);
00149 static void GZipFile(const char *fname, bool bg=false);
00150
00151 static void SubHomeDir(const char *fname, char *extname);
00152
00153 int FileType();
00154 };
00155
00156 #endif //_FILECLS_H