Go to the documentation of this file.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 "restream.h"
00036
00037 #include "common/wave_ex.h"
00038 #include "wave-crypto/wave-crypto.h"
00039 #include "perf/perf.h"
00040 #include "resources/resources.h"
00041 #include "util/file.h"
00042
00043
00044 namespace nstream {
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 static smart_ptr<Entry> getEntryForName(IN const char * name);
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 class ResStream : public Stream {
00066 public:
00067
00068 ~ResStream(void) throw() { }
00069
00070
00071 std::istream& getStream(void) { return m_stream; }
00072 smart_ptr<File> getFile(void);
00073
00074
00075 static smart_ptr<Stream> create(IN const char * nameSpace,
00076 IN const char * name);
00077
00078 private:
00079
00080 ResStream(void) throw() { }
00081
00082
00083 std::istringstream m_stream;
00084 std::string m_name;
00085 std::string m_nameSpace;
00086 };
00087
00088
00089
00090 smart_ptr<File>
00091 ResStream::getFile
00092 (
00093 void
00094 )
00095 {
00096 std::string fullPath = m_nameSpace;
00097 fullPath += "/";
00098 fullPath += m_name;
00099
00100
00101 return getEntryForName(fullPath.c_str());
00102 }
00103
00104
00105
00106 smart_ptr<Stream>
00107 ResStream::create
00108 (
00109 IN const char * nameSpace,
00110 IN const char * name
00111 )
00112 {
00113 ASSERT(nameSpace, "null");
00114 ASSERT(name, "null");
00115
00116
00117
00118
00119 smart_ptr<ResStream> local = new ResStream;
00120 ASSERT(local, "out of memory");
00121
00122 local->m_nameSpace = nameSpace;
00123 local->m_name = name;
00124
00125 int bytes = getResourceSize(nameSpace, name);
00126 ASSERT_THROW(bytes > 0,
00127 "No such resource: namespace='" << nameSpace << "', name='" <<
00128 name);
00129
00130 const byte_t * value = getResourceData(nameSpace, name);
00131 ASSERT_THROW(value > 0,
00132 "No such resource: namespace='" << nameSpace << "', name='" <<
00133 name);
00134
00135
00136 std::string source;
00137 source.assign((const char *) value, bytes);
00138
00139
00140 local->m_stream.str(source);
00141 ASSERT_THROW(local->m_stream.good(),
00142 "Failed to open resouce for reading: namespace='" << nameSpace <<
00143 "', name='" << name << "'");
00144
00145
00146 return local;
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158 class ResFile : public File {
00159 public:
00160
00161 ~ResFile(void) throw() { }
00162
00163
00164 eType getType(void) const throw() { return eType_File; }
00165 const char * getName(void) const throw() { return m_name.c_str(); }
00166 smart_ptr<Manager> getManager(void) {
00167 return getResourceStreamManager();
00168 }
00169
00170
00171 smart_ptr<Stream> openStream(void);
00172
00173
00174 static smart_ptr<File> create(IN const char * nameSpace,
00175 IN const char * name);
00176
00177 private:
00178
00179 ResFile(void) throw() { }
00180
00181
00182 std::string m_name;
00183 std::string m_nameSpace;
00184 };
00185
00186
00187
00188 smart_ptr<Stream>
00189 ResFile::openStream
00190 (
00191 void
00192 )
00193 {
00194 return ResStream::create(m_nameSpace.c_str(), m_name.c_str());
00195 }
00196
00197
00198
00199 smart_ptr<File>
00200 ResFile::create
00201 (
00202 IN const char * nameSpace,
00203 IN const char * name
00204 )
00205 {
00206 ASSERT(nameSpace, "null");
00207 ASSERT(name, "null");
00208
00209 smart_ptr<ResFile> local = new ResFile;
00210 ASSERT(local, "out of memory");
00211
00212 local->m_nameSpace = nameSpace;
00213 local->m_name = name;
00214
00215 return local;
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 class ResFolder : public Folder {
00232 public:
00233
00234 ~ResFolder(void) throw();
00235
00236
00237 eType getType(void) const throw() { return eType_Folder; }
00238 const char * getName(void) const throw() { return m_name.c_str(); }
00239 smart_ptr<Manager> getManager(void) {
00240 return getResourceStreamManager();
00241 }
00242
00243
00244 smart_ptr<Entry> getChildByName(IN const char * name);
00245 void resetIteration(void);
00246 smart_ptr<Entry> getNextChild(void);
00247
00248
00249 static smart_ptr<Folder> create(IN const char * name);
00250
00251 private:
00252
00253 ResFolder(void) throw();
00254
00255
00256 std::string m_name;
00257 int m_index;
00258 };
00259
00260
00261
00262 ResFolder::ResFolder(void)
00263 throw()
00264 {
00265 m_index = 0;
00266 }
00267
00268
00269
00270 ResFolder::~ResFolder(void)
00271 throw()
00272 {
00273 }
00274
00275
00276
00277 smart_ptr<Entry>
00278 ResFolder::getChildByName
00279 (
00280 IN const char * name
00281 )
00282 {
00283 ASSERT(name, "null");
00284
00285 if ("" == m_name) {
00286 return ResFolder::create(name);
00287 } else {
00288 return ResFile::create(m_name.c_str(), name);
00289 }
00290 }
00291
00292
00293
00294 void
00295 ResFolder::resetIteration
00296 (
00297 void
00298 )
00299 {
00300 m_index = 0;
00301 }
00302
00303
00304
00305 smart_ptr<Entry>
00306 ResFolder::getNextChild
00307 (
00308 void
00309 )
00310 {
00311 if ("" == m_name) {
00312
00313 int nMax = getResourceNamespaceCount();
00314 if (m_index >= nMax) {
00315
00316 return NULL;
00317 }
00318
00319
00320 return ResFolder::create(getResourceNamespaceName(m_index++));
00321 }
00322
00323
00324 const char * name = getResourceName(m_name.c_str(), m_index);
00325 if (!name) {
00326 return NULL;
00327 }
00328
00329
00330 ++m_index;
00331
00332
00333 return this->getChildByName(name);
00334 }
00335
00336
00337
00338 smart_ptr<Folder>
00339 ResFolder::create
00340 (
00341 IN const char * name
00342 )
00343 {
00344 ASSERT(name, "null");
00345
00346
00347
00348 smart_ptr<ResFolder> local = new ResFolder();
00349 ASSERT(local, "out of memory");
00350
00351 local->m_name = name;
00352
00353 return local;
00354 }
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365 class ResMgr : public Manager {
00366 public:
00367
00368 ~ResMgr(void) throw() { }
00369
00370
00371 void initialize(void);
00372
00373
00374 smart_ptr<Entry> getEntry(IN const char * name);
00375 smart_ptr<Folder> getRoot(void);
00376 std::string getFullName(IN const char * name);
00377
00378 private:
00379
00380 };
00381
00382
00383
00384 void
00385 ResMgr::initialize
00386 (
00387 void
00388 )
00389 {
00390 }
00391
00392
00393
00394 smart_ptr<Entry>
00395 ResMgr::getEntry
00396 (
00397 IN const char * name
00398 )
00399 {
00400 ASSERT(name, "null");
00401
00402 return getEntryForName(name);
00403 }
00404
00405
00406
00407 smart_ptr<Folder>
00408 ResMgr::getRoot
00409 (
00410 void
00411 )
00412 {
00413 return ResFolder::create("");
00414 }
00415
00416
00417
00418 std::string
00419 ResMgr::getFullName
00420 (
00421 IN const char * name
00422 )
00423 {
00424 ASSERT(name, "null");
00425 return name;
00426 }
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436 static smart_ptr<Entry>
00437 getEntryForName
00438 (
00439 IN const char * name
00440 )
00441 {
00442 ASSERT(name, "null");
00443
00444 DPRINTF("Creating entry for name: %s", name);
00445
00446
00447 while (*name && '/' == *name) {
00448 ++name;
00449 }
00450
00451
00452 std::string parentDir;
00453 GetParentDirectory(name, parentDir);
00454 const char * resname = GetFilename(name);
00455
00456 if ("" == parentDir || !*resname) {
00457 DPRINTF(" ...namespace!");
00458
00459 const char * nameSpace = NULL;
00460 if (!*resname) {
00461
00462 nameSpace = parentDir.c_str();
00463 } else {
00464
00465 nameSpace = resname;
00466 }
00467
00468
00469 return ResFolder::create(nameSpace);
00470 }
00471
00472
00473 DPRINTF(" ...namespace + resource name");
00474 DPRINTF(" namespace: '%s'", parentDir.c_str());
00475 DPRINTF(" resource: '%s'", resname);
00476
00477
00478 return ResFile::create(parentDir.c_str(), resname);
00479 }
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489 smart_ptr<Manager>
00490 getResourceStreamManager
00491 (
00492 void
00493 )
00494 {
00495 smart_ptr<ResMgr> local = new ResMgr;
00496 local->initialize();
00497 return local;
00498 }
00499
00500
00501
00502 };
00503