home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 5151 / tbl.7z / xfpk.h < prev   
Encoding:
C/C++ Source or Header  |  2011-04-02  |  3.0 KB  |  109 lines

  1. #ifndef __MILETOS_XFPK_H__
  2. #define __MILETOS_XFPK_H__
  3.  
  4. //
  5. // Libmiletos
  6. //
  7. // Copyright (C) Lauris Kaplinski 2007-2009
  8. //
  9.  
  10. #include <string>
  11. #include <vector>
  12. #include <map>
  13.  
  14. #include <miletos/types.h>
  15. #include <miletos/filesystem.h>
  16. #include <miletos/uri.h>
  17.  
  18. namespace Miletos {
  19.  
  20. namespace Xfpk {
  21.  
  22. class FileData {
  23. private:
  24.     unsigned int valid;
  25.     std::string tblname;
  26.     std::string dataname;
  27.  
  28.     struct FileEntry {
  29.         u32 tag;
  30.         std::string name;
  31.         size_t start;
  32.         size_t length;
  33.     };
  34.     std::vector<FileEntry> entries;
  35.     std::map<std::string, int> map;
  36.  
  37.     // Helpers
  38.     void readTable (const unsigned char *tdata, size_t tsize, const unsigned char *cdata, size_t csize);
  39.     bool isCompressed (int idx);
  40. public:
  41.     // Constructor
  42.     FileData (const char *tblname, const char *dataname);
  43.     // FileData (const unsigned char *tdata, size_t tsize, const unsigned char *pxdata, size_t pxsize);
  44.  
  45.     // Access
  46.     bool isValid (void) const { return valid != 0; }
  47.     int getNumFiles (void) { return (int) entries.size (); }
  48.     const char *getFileName (int idx);
  49.     size_t getFileSize (int idx);
  50.     const unsigned char *getFileData (int idx, size_t *size);
  51.     Miletos::u32 getFileTag (int idx);
  52.     int lookupFileIdx (const char *name) { if (map.find (name) == map.end ()) return -1; return map[std::string(name)]; }
  53.  
  54.     // Static helpers
  55.     static bool isTable (const char *filename);
  56.     static u16 getU16LE (const unsigned char *c) { return (u16) c[0] | ((u16) c[1] << 8); }
  57.     static u32 getU32LE (const unsigned char *c) { return (u32) c[0] | ((u32) c[1] << 8) | ((u32) c[2] << 16) | ((u32) c[3] << 24); }
  58.     static unsigned char *unpack_a67f54cb (const unsigned char *cdata, size_t csize, size_t& dsize);
  59. };
  60.  
  61. class FileHandler : public FileSystem::HandlerFileList {
  62. private:
  63.     FileData data;
  64.  
  65.     // Handler implementation
  66.     // Test whether current handler has dir (relative to root)
  67.     virtual bool hasDir (const char *name);
  68.  
  69.     // Set current reading directory to given value (or unset if there is no such dir)
  70.     virtual bool loadDir (const char *name);
  71.     // Return the number of files in reading dir
  72.     virtual int getNumFiles (void);
  73.     // Return the name of file in reading dir
  74.     virtual const char *getFileName (int idx);
  75.     // Return the size of file in reading dir
  76.     virtual size_t getFileSize (int idx);
  77.     // Return the number of subdirectories in reading dir
  78.     virtual int getNumSubDirs (void);
  79.     // Return the name of subdirectory in reading dir
  80.     virtual const char *getSubDirName (int idx);
  81.  
  82.     // HandlerFileList implementation
  83.     virtual void addFileMapping (Entry *entry);
  84. public:
  85.     // Constructor
  86.     FileHandler (const char *tblname, const char *dataname);
  87. };
  88.  
  89. class URLHandler : public URI::URLHandler {
  90. private:
  91.     std::vector<FileSystem::Handler *> handlers;
  92.  
  93.     URLHandler (const char *location);
  94.     virtual ~URLHandler (void);
  95.  
  96.     // URLHandler implementation
  97.     virtual const unsigned char *mmapDataRelative (const char *name, size_t *size);
  98.     virtual void munmapData (const unsigned char *data);
  99. public:
  100.     // Static constructor
  101.     static URI::URLHandler *newURLHandler (const char *url);
  102. };
  103.  
  104. }; // Namespace Xfpk
  105.  
  106. }; // Namespace Miletos
  107.  
  108. #endif
  109.