33 #ifndef GSLAM_FILERESOURCE_H 34 #define GSLAM_FILERESOURCE_H 42 #define GSLAM_REGISTER_RESOURCE(R) \ 43 class ResourceRegister##R{\ 45 ResourceRegister##R(){GSLAM::FileResource::Register(resource_names,resource_index,resource_data);}\ 46 }ResourceRegister##R##inst; 48 typedef unsigned char u_char;
54 static std::string toHex(u_char* buf,
int size){
56 str.resize(5*size,
'0');
57 for(
int i=0;i<size;i++){
59 sprintf(&str.at(5*i),
"0x%02x,",c);
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)
70 return filename.substr(0, idx);
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);
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;
89 for(std::pair<std::string,std::string> corr:lut){
90 FILE *in=fopen(corr.second.c_str(),
"r");
92 std::cerr<<
"Failed to load file "<<corr.second<<std::endl;
102 int finishIdx=data.size();
103 names.push_back(corr.first);
104 idxes.push_back(finishIdx);
106 if(data.empty())
return false;
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";
113 fileOut<<
"static const std::string resource_names[]={\n";
114 for(
size_t i=0;i<names.size();i++)
116 fileOut<<
"\""<<names[i]<<
"\",\n";
118 fileOut<<
"\"\"};\n\n";
120 fileOut<<
"static const int resource_index[]={";
121 for(
size_t i=0;i<idxes.size();i++){
123 if(i+1!=names.size()) fileOut<<
",\n";
124 else fileOut<<
"};\n\n";
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";
133 fileOut<<
"GSLAM_REGISTER_RESOURCE("<<getBaseName(to)<<
")";
138 static void Register(
const std::string resource_names[],
139 const int resource_index[],
140 const unsigned char resource_data[])
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));
152 static std::istream& getIStream(
const std::string& resource){
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())));
160 return files[resource].
as<std::istream>();
163 static bool saveResource2File(
const std::string& resource,
164 const std::string& file){
165 return getBuffer(resource).save(file);
168 static SvarBuffer getBuffer(
const std::string& resource){
169 return resources().
get(resource,SvarBuffer(
nullptr,0));
172 static Svar& resources(){
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
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...