| 1 |
#include "FileTools.h"
|
| 2 |
#include <sys/stat.h>
|
| 3 |
#include "stdio.h"
|
| 4 |
#include <sys/types.h>
|
| 5 |
#include "../NetWork/MutexHandler.h"
|
| 6 |
|
| 7 |
|
| 8 |
//http://www.opengroup.org/onlinepubs/7908799/xsh/sysstat.h.html
|
| 9 |
|
| 10 |
#ifndef WIN32
|
| 11 |
#include <dirent.h>
|
| 12 |
#endif
|
| 13 |
|
| 14 |
#include "../System/SystemDefine.h"
|
| 15 |
|
| 16 |
|
| 17 |
FileTools::FileTools(){}
|
| 18 |
FileTools::~FileTools(){}
|
| 19 |
|
| 20 |
/*
|
| 21 |
int FileTools::readFile(char *fileName, int *fileBuffer, int *fileSize)
|
| 22 |
{
|
| 23 |
int fileStatus=0;
|
| 24 |
|
| 25 |
if (fileName != 0 )
|
| 26 |
{
|
| 27 |
FILE *f;
|
| 28 |
|
| 29 |
struct stat *Stat=0;
|
| 30 |
Stat=(struct stat*)malloc(sizeof(struct stat));
|
| 31 |
|
| 32 |
int statStatus=stat(fileName,Stat);
|
| 33 |
int stMode=Stat->st_mode;
|
| 34 |
|
| 35 |
if ( Stat != 0 )
|
| 36 |
{
|
| 37 |
free(Stat);
|
| 38 |
}
|
| 39 |
|
| 40 |
bool readStatus=true;
|
| 41 |
|
| 42 |
#ifdef WIN32
|
| 43 |
if ( (stMode & _S_IREAD) != _S_IREAD )
|
| 44 |
#endif
|
| 45 |
#ifdef linux
|
| 46 |
if ( (stMode & S_IRUSR) != S_IRUSR )
|
| 47 |
#endif
|
| 48 |
#ifdef sun
|
| 49 |
if ( (stMode & S_IRUSR) != S_IRUSR )
|
| 50 |
#endif
|
| 51 |
{
|
| 52 |
readStatus=false;
|
| 53 |
fileStatus=3;
|
| 54 |
}
|
| 55 |
|
| 56 |
if (f=fopen((char*)fileName, "rb"))
|
| 57 |
{
|
| 58 |
fseek(f,0,2);
|
| 59 |
fileSize[0]=(int)ftell(f);
|
| 60 |
fseek(f,0,0);
|
| 61 |
|
| 62 |
if (fileSize[0]!=0)
|
| 63 |
{
|
| 64 |
fileBuffer[0]=(int)malloc(fileSize[0]*sizeof(char));
|
| 65 |
|
| 66 |
if(fread((void *) fileBuffer[0], fileSize[0], 1, f))
|
| 67 |
{
|
| 68 |
fileStatus=1;
|
| 69 |
}
|
| 70 |
else
|
| 71 |
{
|
| 72 |
fileStatus=3;
|
| 73 |
}
|
| 74 |
}
|
| 75 |
else
|
| 76 |
{
|
| 77 |
fileStatus=3;
|
| 78 |
}
|
| 79 |
|
| 80 |
fclose(f);
|
| 81 |
}
|
| 82 |
else
|
| 83 |
{
|
| 84 |
if (!readStatus)
|
| 85 |
{
|
| 86 |
fileStatus=0;
|
| 87 |
}
|
| 88 |
}
|
| 89 |
}
|
| 90 |
else
|
| 91 |
{
|
| 92 |
fileStatus=2;
|
| 93 |
}
|
| 94 |
|
| 95 |
return fileStatus;
|
| 96 |
}
|
| 97 |
*/
|
| 98 |
|
| 99 |
int FileTools::readStringFile(char *fileName, int *fileBuffer, int *fileSize)
|
| 100 |
{
|
| 101 |
// pthread_mutex_t mut=MutexHandler::getMutex();
|
| 102 |
|
| 103 |
// pthread_mutex_lock(&mut);
|
| 104 |
|
| 105 |
int fileStatus=0;
|
| 106 |
|
| 107 |
if (fileName != 0 )
|
| 108 |
{
|
| 109 |
FILE *f;
|
| 110 |
|
| 111 |
bool readStatus=true;
|
| 112 |
|
| 113 |
|
| 114 |
struct stat *Stat=0;
|
| 115 |
Stat=(struct stat*)malloc(sizeof(struct stat));
|
| 116 |
|
| 117 |
int statStatus=stat(fileName,Stat);
|
| 118 |
int stMode=Stat->st_mode;
|
| 119 |
|
| 120 |
if ( Stat != 0 )
|
| 121 |
{
|
| 122 |
free(Stat);
|
| 123 |
|
| 124 |
}
|
| 125 |
|
| 126 |
#ifdef WIN32
|
| 127 |
if ( (stMode & _S_IREAD) != _S_IREAD )
|
| 128 |
#endif
|
| 129 |
#ifdef linux
|
| 130 |
if ( (stMode & S_IRUSR) != S_IRUSR )
|
| 131 |
#endif
|
| 132 |
#ifdef sun
|
| 133 |
if ( (stMode & S_IRUSR) != S_IRUSR )
|
| 134 |
#endif
|
| 135 |
{
|
| 136 |
readStatus=false;
|
| 137 |
fileStatus=3;
|
| 138 |
}
|
| 139 |
|
| 140 |
if (f=fopen((char*)fileName, "rb"))
|
| 141 |
{
|
| 142 |
fseek(f,0,2);
|
| 143 |
fileSize[0]=(int)ftell(f);
|
| 144 |
fseek(f,0,0);
|
| 145 |
|
| 146 |
if (fileSize[0]!=0)
|
| 147 |
{
|
| 148 |
// *sizeof(char)+sizeof(char)
|
| 149 |
fileBuffer[0]=(int)malloc(fileSize[0]+1);
|
| 150 |
|
| 151 |
char *tempBuffer=(char*)fileBuffer[0];
|
| 152 |
if(fread((void *) tempBuffer, fileSize[0], 1, f))
|
| 153 |
{
|
| 154 |
fileStatus=1;
|
| 155 |
tempBuffer[fileSize[0]]=0;
|
| 156 |
}
|
| 157 |
else
|
| 158 |
{
|
| 159 |
fileStatus=3;
|
| 160 |
}
|
| 161 |
}
|
| 162 |
else
|
| 163 |
{
|
| 164 |
fileStatus=3;
|
| 165 |
}
|
| 166 |
|
| 167 |
fclose(f);
|
| 168 |
}
|
| 169 |
else
|
| 170 |
{
|
| 171 |
if (!readStatus)
|
| 172 |
{
|
| 173 |
fileStatus=0;
|
| 174 |
}
|
| 175 |
}
|
| 176 |
}
|
| 177 |
else
|
| 178 |
{
|
| 179 |
fileStatus=2;
|
| 180 |
}
|
| 181 |
|
| 182 |
// pthread_mutex_unlock(&mut);
|
| 183 |
|
| 184 |
return fileStatus;
|
| 185 |
}
|
| 186 |
|
| 187 |
int FileTools::writeToFileEnd(char* fileName, char *fileBuffer, int fileSize)
|
| 188 |
{
|
| 189 |
return writeFile(fileName, fileBuffer, fileSize, "a+b");
|
| 190 |
}
|
| 191 |
|
| 192 |
|
| 193 |
int FileTools::writeFile(char* fileName, char *fileBuffer, int fileSize)
|
| 194 |
{
|
| 195 |
return writeFile(fileName, fileBuffer, fileSize, "w+b");
|
| 196 |
}
|
| 197 |
|
| 198 |
int FileTools::writeFile(char* fileName, char *fileBuffer, int fileSize, char *type)
|
| 199 |
{
|
| 200 |
int fileStatus=0;
|
| 201 |
|
| 202 |
if (fileName != 0 )
|
| 203 |
{
|
| 204 |
FILE *f;
|
| 205 |
|
| 206 |
struct stat *Stat=0;
|
| 207 |
Stat=(struct stat*)malloc(sizeof(struct stat));
|
| 208 |
|
| 209 |
int statStatus=stat(fileName,Stat);
|
| 210 |
int stMode=Stat->st_mode;
|
| 211 |
|
| 212 |
if ( Stat != 0 )
|
| 213 |
{
|
| 214 |
free(Stat);
|
| 215 |
}
|
| 216 |
|
| 217 |
bool writeStatus=true;
|
| 218 |
|
| 219 |
#ifdef WIN32
|
| 220 |
if ( (stMode & _S_IWRITE) != _S_IWRITE )
|
| 221 |
#endif
|
| 222 |
#ifdef linux
|
| 223 |
if ( (stMode & S_IWUSR) != S_IWUSR )
|
| 224 |
#endif
|
| 225 |
#ifdef sun
|
| 226 |
if ( (stMode & S_IWUSR) != S_IWUSR )
|
| 227 |
#endif
|
| 228 |
{
|
| 229 |
writeStatus=false;
|
| 230 |
fileStatus=4;
|
| 231 |
}
|
| 232 |
|
| 233 |
//w+b
|
| 234 |
//a+b
|
| 235 |
|
| 236 |
if (f=fopen((char*)fileName, type))
|
| 237 |
{
|
| 238 |
if(fwrite((void *) fileBuffer, fileSize, 1, f))
|
| 239 |
{
|
| 240 |
fileStatus=1;
|
| 241 |
}
|
| 242 |
else
|
| 243 |
{
|
| 244 |
fileStatus=4;
|
| 245 |
}
|
| 246 |
|
| 247 |
fclose(f);
|
| 248 |
}
|
| 249 |
else
|
| 250 |
{
|
| 251 |
if (!writeStatus)
|
| 252 |
{
|
| 253 |
fileStatus=0;
|
| 254 |
}
|
| 255 |
}
|
| 256 |
}
|
| 257 |
else
|
| 258 |
{
|
| 259 |
fileStatus=2;
|
| 260 |
}
|
| 261 |
|
| 262 |
return fileStatus;
|
| 263 |
}
|