GSLAM  3.0.0
FileResource.h
1 // GSLAM - A general SLAM framework and benchmark
2 // Copyright 2018 PILAB Inc. All rights reserved.
3 // https://github.com/zdzhaoyong/GSLAM
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors may be
14 // used to endorse or promote products derived from this software without
15 // specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Author: zd5945@126.com (Yong Zhao)
30 //
31 // FileResource provides builtin resource file as buffer like Qt resources
32 
33 #ifndef GSLAM_FILERESOURCE_H
34 #define GSLAM_FILERESOURCE_H
35 
36 #include <fstream>
37 #include <istream>
38 #include <stdio.h>
39 #include <vector>
40 #include "Svar.h"
41 
42 #define GSLAM_REGISTER_RESOURCE(R) \
43  class ResourceRegister##R{\
44 public:\
45  ResourceRegister##R(){GSLAM::FileResource::Register(resource_names,resource_index,resource_data);}\
46 }ResourceRegister##R##inst;
47 
48 typedef unsigned char u_char;
49 
50 namespace GSLAM{
51 
53 public:
54  static std::string toHex(u_char* buf,int size){
55  std::string str;
56  str.resize(5*size,'0');
57  for(int i=0;i<size;i++){
58  u_char c=buf[i];
59  sprintf(&str.at(5*i),"0x%02x,",c);
60  }
61  return str;
62  }
63 
64  static std::string getBaseName(const std::string& path) {
65  std::string filename = getFileName(path);
66  auto idx = filename.find_last_of('.');
67  if (idx == std::string::npos)
68  return filename;
69  else
70  return filename.substr(0, idx);
71  }
72 
73  static std::string getFileName(const std::string& path) {
74  auto idx = std::string::npos;
75  if ((idx = path.find_last_of('/')) == std::string::npos)
76  idx = path.find_last_of('\\');
77  if (idx != std::string::npos)
78  return path.substr(idx + 1);
79  else
80  return path;
81  }
82 
83  static bool exportResourceFile(const std::map<std::string,std::string>& lut,const std::string& to){
84  std::vector<std::string> names;
85  std::vector<int> idxes;
86  std::vector<u_char> data;
87 
88  // load
89  for(std::pair<std::string,std::string> corr:lut){
90  FILE *in=fopen(corr.second.c_str(),"r");
91  if(!in) {
92  std::cerr<<"Failed to load file "<<corr.second<<std::endl;
93  continue;
94  }
95  while(true)
96  {
97  int ch=fgetc(in);
98  if(ch==EOF) break;
99  data.push_back(ch);
100  }
101  fclose(in);
102  int finishIdx=data.size();
103  names.push_back(corr.first);
104  idxes.push_back(finishIdx);
105  }
106  if(data.empty()) return false;
107 
108  // export
109  std::ofstream fileOut(to,std::ios::out);
110  if(!fileOut.is_open()) return false;
111  fileOut<<"#include \"GSLAM/core/FileResource.h\"\n\n\n\n";
112 
113  fileOut<<"static const std::string resource_names[]={\n";
114  for(size_t i=0;i<names.size();i++)
115  {
116  fileOut<<"\""<<names[i]<<"\",\n";
117  }
118  fileOut<<"\"\"};\n\n";
119 
120  fileOut<<"static const int resource_index[]={";
121  for(size_t i=0;i<idxes.size();i++){
122  fileOut<<idxes[i];
123  if(i+1!=names.size()) fileOut<<",\n";
124  else fileOut<<"};\n\n";
125  }
126 
127  fileOut<<"static const unsigned char resource_data[]= {\n";
128  for(size_t i=0;i<data.size();i+=16){
129  if(i+16<=data.size()) fileOut<<toHex(&data[i],16)<<"\n";
130  else fileOut<<toHex(&data[i],data.size()-i)<<"\n";
131  }
132  fileOut<<"};\n\n";
133  fileOut<<"GSLAM_REGISTER_RESOURCE("<<getBaseName(to)<<")";
134  fileOut.close();
135  return true;
136  }
137 
138  static void Register(const std::string resource_names[],
139  const int resource_index[],
140  const unsigned char resource_data[])
141  {
142  for(int i=0;;i++)
143  {
144  std::string name=resource_names[i];
145  if(name.empty()) break;
146  int start= i==0?0:resource_index[i-1];
147  int end =resource_index[i];
148  resources().set(name,SvarBuffer(&resource_data[start],end-start));
149  }
150  }
151 
152  static std::istream& getIStream(const std::string& resource){
153  static Svar files;
154  if(!files.exist(resource)) {
155  SvarBuffer buffer=getBuffer(resource);
156  files[resource]=std::shared_ptr<std::istream>(
157  new std::stringstream(std::string((const char*)buffer.ptr(),buffer.length())));
158  }
159 
160  return files[resource].as<std::istream>();
161  }
162 
163  static bool saveResource2File(const std::string& resource,
164  const std::string& file){
165  return getBuffer(resource).save(file);
166  }
167 
168  static SvarBuffer getBuffer(const std::string& resource){
169  return resources().get(resource,SvarBuffer(nullptr,0));
170  }
171 
172  static Svar& resources(){
173  static Svar r=Svar::object();
174  return r;
175  }
176 };
177 
178 }
179 
180 #endif
static Svar object(const std::map< std::string, Svar > &m={})
Create an Object instance.
Definition: Svar.h:632
const T & as() const
Should always check type with "is<T>()" first.
void set(const std::string &name, const T &def, bool parse_dot=false)
Set the child "name" to "create<T>(def)".
Definition: FileResource.h:52
Definition: Camera.h:45
bool exist(const Svar &id) const
For Object: dot compute and find the member For Array, Dict and Class, check if the item is not Undef...
The Svar class, A Tiny Modern C++ Header Brings Unified Interface for Different Languages.
Definition: Svar.h:561
T & get(const std::string &name, T def, bool parse_dot=false)
Force to return the children as type T, cast is performed, otherwise the old value will be droped and...