00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "stdmisc.h"
00026 #include "nel/misc/file.h"
00027 #include "nel/misc/path.h"
00028 #include "nel/misc/async_file_manager.h"
00029
00030
00031 using namespace std;
00032
00033 namespace NLMISC
00034 {
00035
00036 CAsyncFileManager *CAsyncFileManager::_Singleton = NULL;
00037
00038
00039
00040 CAsyncFileManager::CAsyncFileManager()
00041 {
00042 }
00043
00044
00045
00046 CAsyncFileManager &CAsyncFileManager::getInstance()
00047 {
00048 if (_Singleton == NULL)
00049 {
00050 _Singleton = new CAsyncFileManager();
00051 }
00052 return *_Singleton;
00053 }
00054
00055
00056
00057 void CAsyncFileManager::terminate ()
00058 {
00059 if (_Singleton != NULL)
00060 {
00061 delete &getInstance();
00062 _Singleton = NULL;
00063 }
00064 }
00065
00066
00067 void CAsyncFileManager::addLoadTask(IRunnable *ploadTask)
00068 {
00069 addTask(ploadTask);
00070 }
00071
00072 bool CAsyncFileManager::cancelLoadTask(const CAsyncFileManager::ICancelCallback &callback)
00073 {
00074 CSynchronized<list<IRunnable *> >::CAccessor acces(&_TaskQueue);
00075 list<IRunnable*> &rTaskQueue = acces.value ();
00076 list<IRunnable*>::iterator it = rTaskQueue.begin();
00077
00078 while (it != rTaskQueue.end())
00079 {
00080 IRunnable *pR = *it;
00081
00082
00083 if (callback.callback(pR))
00084 {
00085
00086 delete pR;
00087 rTaskQueue.erase (it);
00088 return true;
00089 }
00090 ++it;
00091 }
00092
00093
00094 waitCurrentTaskToComplete ();
00095
00096 return false;
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 void CAsyncFileManager::loadFile (const std::string& sFileName, uint8 **ppFile)
00150 {
00151 addTask (new CFileLoad (sFileName, ppFile));
00152 }
00153
00154
00155
00156 void CAsyncFileManager::loadFiles (const std::vector<std::string> &vFileNames, const std::vector<uint8**> &vPtrs)
00157 {
00158 addTask (new CMultipleFileLoad (vFileNames, vPtrs));
00159 }
00160
00161
00162
00163 void CAsyncFileManager::signal (bool *pSgn)
00164 {
00165 addTask (new CSignal (pSgn));
00166 }
00167
00168
00169
00170 void CAsyncFileManager::cancelSignal (bool *pSgn)
00171 {
00172 CSynchronized<list<IRunnable *> >::CAccessor acces(&_TaskQueue);
00173 list<IRunnable*> &rTaskQueue = acces.value ();
00174 list<IRunnable*>::iterator it = rTaskQueue.begin();
00175
00176 while (it != rTaskQueue.end())
00177 {
00178 IRunnable *pR = *it;
00179 CSignal *pS = dynamic_cast<CSignal*>(pR);
00180 if (pS != NULL)
00181 {
00182 if (pS->Sgn == pSgn)
00183 {
00184
00185 delete pS;
00186 rTaskQueue.erase (it);
00187 return;
00188 }
00189 }
00190 ++it;
00191 }
00192 }
00193
00194
00195
00196
00197
00198
00199 CAsyncFileManager::CFileLoad::CFileLoad (const std::string& sFileName, uint8 **ppFile)
00200 {
00201 _FileName = sFileName;
00202 _ppFile = ppFile;
00203 }
00204
00205
00206 void CAsyncFileManager::CFileLoad::run (void)
00207 {
00208 FILE *f = fopen (_FileName.c_str(), "rb");
00209 if (f != NULL)
00210 {
00211 uint8 *ptr;
00212 fseek (f, 0, SEEK_END);
00213 long filesize = ftell (f);
00214 nlSleep(5);
00215 fseek (f, 0, SEEK_SET);
00216 ptr = new uint8[filesize];
00217 fread (ptr, filesize, 1, f);
00218 fclose (f);
00219
00220 *_ppFile = ptr;
00221 }
00222 else
00223 {
00224 nlwarning ("Couldn't load '%s'", _FileName.c_str());
00225 *_ppFile = (uint8*)-1;
00226 }
00227 }
00228
00229
00230
00231
00232
00233
00234 CAsyncFileManager::CMultipleFileLoad::CMultipleFileLoad (const std::vector<std::string> &vFileNames,
00235 const std::vector<uint8**> &vPtrs)
00236 {
00237 _FileNames = vFileNames;
00238 _Ptrs = vPtrs;
00239 }
00240
00241
00242 void CAsyncFileManager::CMultipleFileLoad::run (void)
00243 {
00244 for (uint32 i = 0; i < _FileNames.size(); ++i)
00245 {
00246 FILE *f = fopen (_FileNames[i].c_str(), "rb");
00247 if (f != NULL)
00248 {
00249 uint8 *ptr;
00250 fseek (f, 0, SEEK_END);
00251 long filesize = ftell (f);
00252 nlSleep(5);
00253 fseek (f, 0, SEEK_SET);
00254 ptr = new uint8[filesize];
00255 fread (ptr, filesize, 1, f);
00256 fclose (f);
00257
00258 *_Ptrs[i] = ptr;
00259 }
00260 else
00261 {
00262 nlwarning ("Couldn't load '%s'", _FileNames[i].c_str());
00263 *_Ptrs[i] = (uint8*)-1;
00264 }
00265 }
00266
00267 }
00268
00269
00270
00271
00272
00273
00274 CAsyncFileManager::CSignal::CSignal (bool *pSgn)
00275 {
00276 Sgn = pSgn;
00277 *Sgn = false;
00278 }
00279
00280
00281 void CAsyncFileManager::CSignal::run (void)
00282 {
00283 *Sgn = true;
00284 }
00285
00286
00287 }
00288