00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <string.h>
00036
00037 #ifdef WIN32
00038 #include <io.h>
00039 #endif
00040
00041 #ifndef WIN32
00042 #include <dirent.h>
00043 #include <sys/stat.h>
00044 #endif
00045
00046 #include <xmlrpc-c/abyss.h>
00047 #include "abyss_file.h"
00048
00049
00050
00051
00052
00053 abyss_bool FileOpen(TFile *f, const char *name,uint32_t attrib)
00054 {
00055 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00056 return ((*f=_open(name,attrib))!=(-1));
00057 #else
00058 return ((*f=open(name,attrib))!=(-1));
00059 #endif
00060 }
00061
00062 abyss_bool FileOpenCreate(TFile *f, const char *name, uint32_t attrib)
00063 {
00064 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00065 return ((*f=_open(name,attrib | O_CREAT,_S_IWRITE | _S_IREAD))!=(-1));
00066 #else
00067 return ((*f=open(name,attrib | O_CREAT,S_IWRITE | S_IREAD))!=(-1));
00068 #endif
00069 }
00070
00071 abyss_bool
00072 FileWrite(TFile * const f,
00073 const void * const buffer,
00074 uint32_t const len) {
00075 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00076 return (_write(*f,buffer,len)==(int32_t)len);
00077 #else
00078 return (write(*f,buffer,len)==(int32_t)len);
00079 #endif
00080 }
00081
00082 int32_t FileRead(TFile *f, void *buffer, uint32_t len)
00083 {
00084 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00085 return (_read(*f,buffer,len));
00086 #else
00087 return (read(*f,buffer,len));
00088 #endif
00089 }
00090
00091 abyss_bool FileSeek(TFile *f, uint64_t pos, uint32_t attrib)
00092 {
00093 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00094 return (_lseek(*f,pos,attrib)!=(-1));
00095 #else
00096 return (lseek(*f,pos,attrib)!=(-1));
00097 #endif
00098 }
00099
00100 uint64_t FileSize(TFile *f)
00101 {
00102 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00103 return (_filelength(*f));
00104 #else
00105 struct stat fs;
00106
00107 fstat(*f,&fs);
00108 return (fs.st_size);
00109 #endif
00110 }
00111
00112 abyss_bool FileClose(TFile *f)
00113 {
00114 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00115 return (_close(*f)!=(-1));
00116 #else
00117 return (close(*f)!=(-1));
00118 #endif
00119 }
00120
00121
00122
00123 abyss_bool
00124 FileStat(const char * const filename,
00125 TFileStat * const filestat) {
00126 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00127 return (_stati64(filename,filestat)!=(-1));
00128 #else
00129 return (stat(filename,filestat)!=(-1));
00130 #endif
00131 }
00132
00133
00134
00135 abyss_bool
00136 FileFindFirst(TFileFind * const filefind,
00137 const char * const path,
00138 TFileInfo * const fileinfo) {
00139 #ifdef WIN32
00140 abyss_bool ret;
00141 char *p=path+strlen(path);
00142
00143 *p='\\';
00144 *(p+1)='*';
00145 *(p+2)='\0';
00146 #ifndef __BORLANDC__
00147 ret=(((*filefind)=_findfirst(path,fileinfo))!=(-1));
00148 #else
00149 *filefind = FindFirstFile( path, &fileinfo->data );
00150 ret = *filefind != NULL;
00151 if( ret )
00152 {
00153 LARGE_INTEGER li;
00154 li.LowPart = fileinfo->data.nFileSizeLow;
00155 li.HighPart = fileinfo->data.nFileSizeHigh;
00156 strcpy( fileinfo->name, fileinfo->data.cFileName );
00157 fileinfo->attrib = fileinfo->data.dwFileAttributes;
00158 fileinfo->size = li.QuadPart;
00159 fileinfo->time_write = fileinfo->data.ftLastWriteTime.dwLowDateTime;
00160 }
00161 #endif
00162 *p='\0';
00163 return ret;
00164 #else
00165 strncpy(filefind->path,path,NAME_MAX);
00166 filefind->path[NAME_MAX]='\0';
00167 filefind->handle=opendir(path);
00168 if (filefind->handle)
00169 return FileFindNext(filefind,fileinfo);
00170
00171 return FALSE;
00172 #endif
00173 }
00174
00175
00176
00177 abyss_bool FileFindNext(TFileFind *filefind,TFileInfo *fileinfo)
00178 {
00179 #ifdef WIN32
00180
00181 #ifndef __BORLANDC__
00182 return (_findnext(*filefind,fileinfo)!=(-1));
00183 #else
00184 abyss_bool ret = FindNextFile( *filefind, &fileinfo->data );
00185 if( ret )
00186 {
00187 LARGE_INTEGER li;
00188 li.LowPart = fileinfo->data.nFileSizeLow;
00189 li.HighPart = fileinfo->data.nFileSizeHigh;
00190 strcpy( fileinfo->name, fileinfo->data.cFileName );
00191 fileinfo->attrib = fileinfo->data.dwFileAttributes;
00192 fileinfo->size = li.QuadPart;
00193 fileinfo->time_write = fileinfo->data.ftLastWriteTime.dwLowDateTime;
00194 }
00195 return ret;
00196 #endif
00197
00198 #else
00199 struct dirent *de;
00200
00201 char z[NAME_MAX+1];
00202
00203 de=readdir(filefind->handle);
00204 if (de)
00205 {
00206 struct stat fs;
00207
00208 strcpy(fileinfo->name,de->d_name);
00209 strcpy(z,filefind->path);
00210 strncat(z,"/",NAME_MAX);
00211 strncat(z,fileinfo->name,NAME_MAX);
00212 z[NAME_MAX]='\0';
00213
00214 stat(z,&fs);
00215
00216 if (fs.st_mode & S_IFDIR)
00217 fileinfo->attrib=A_SUBDIR;
00218 else
00219 fileinfo->attrib=0;
00220
00221 fileinfo->size=fs.st_size;
00222 fileinfo->time_write=fs.st_mtime;
00223
00224 return TRUE;
00225 };
00226
00227 return FALSE;
00228 #endif
00229 }
00230
00231 void FileFindClose(TFileFind *filefind)
00232 {
00233 #ifdef WIN32
00234
00235 #ifndef __BORLANDC__
00236 _findclose(*filefind);
00237 #else
00238 FindClose( *filefind );
00239 #endif
00240
00241 #else
00242 closedir(filefind->handle);
00243 #endif
00244 }