utilite/resource_generator/main.cpp
Go to the documentation of this file.
1 /*
2 * utilite is a cross-platform library with
3 * useful utilities for fast and small developing.
4 * Copyright (C) 2010 Mathieu Labbe
5 *
6 * utilite is free library: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * utilite is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
21 
22 #include <fstream>
23 #include <iostream>
24 #include <string.h>
25 
26 void showUsage()
27 {
28  printf("Usage:\n"
29  "uresourcegenerator.exe [option] \"file1\" \"file2\" ... \n"
30  " Create a file named \"file\".h with string\n"
31  " variable named \"file\" which contains the data of the file.\n"
32  " Warning, it overwrites the target file\n"
33  " Options:\n"
34  " -n \"namespace\" namespace used\n"
35  " -p \"targetPath\" target path where the file is created\n"
36  " -v version of the UtiLite library\n");
37  exit(1);
38 }
39 
40 int main(int argc, char * argv[])
41 {
42  if(argc < 2)
43  {
44  showUsage();
45  }
46  else if(argc == 2 && strcmp(argv[1], "-v") == 0)
47  {
48  printf("%s\n", UTILITE_VERSION);
49  exit(0);
50  }
51 
52  std::string targetDir = UDirectory::currentDir(); // By default, use the current directory
53  std::string nspace; // namespace
54 
55  int k;
56  for(k=1; k<(argc-1); ++k)
57  {
58  if(strcmp(argv[k], "-n") == 0)
59  {
60  if(!(k+1<(argc-1)))
61  {
62  showUsage();
63  }
64  nspace = argv[k+1];
65  printf(" Using namespace=%s\n", nspace.c_str());
66  ++k;
67  }
68  else if(strcmp(argv[k], "-p") == 0)
69  {
70  if(!(k+1<(argc-1)))
71  {
72  showUsage();
73  }
74  targetDir = argv[k+1];
75  printf(" Using target directory=%s\n", targetDir.c_str());
76  ++k;
77  }
78  else
79  {
80  break;
81  }
82  }
83 
84  while(k < argc)
85  {
86  std::string filePath = argv[k];
87  std::string varName = UFile::getName(argv[k]);
88  // replace '_'
89  for(unsigned int i=0; i<varName.size(); ++i)
90  {
91  if(!((varName[i] >= '0' && varName[i] <= '9') ||
92  (varName[i] >= 'A' && varName[i] <= 'Z') ||
93  (varName[i] >= 'a' && varName[i] <= 'z')))
94  {
95  varName[i] = '_';
96  }
97  }
98  std::string targetFileName = varName + ".h";
99  // upper case
100  for(unsigned int i=0; i<varName.size(); ++i)
101  {
102  if(varName[i] >= 'a' && varName[i] <= 'z')
103  {
104  varName[i] -= 32; // upper case
105  }
106  }
107 
108  std::fstream outFile;
109  std::fstream inFile;
110  outFile.open(((targetDir + "/") + targetFileName).c_str(), std::fstream::out);
111  inFile.open(filePath.c_str(), std::fstream::in | std::fstream::binary);
112 
113  printf("Input file \"%s\" size = %ld bytes\n", filePath.c_str(), UFile::length(filePath));
114  if(outFile.is_open() && inFile.is_open())
115  {
116  outFile << "/*This is a generated file...*/\n\n";
117  outFile << "#ifndef " << varName << "_H\n";
118  outFile << "#define " << varName << "_H\n\n";
119 
120  if(!nspace.empty())
121  {
122  outFile << "namespace " << nspace.c_str() << "\n{\n\n";
123  }
124 
125  outFile << "static const char * " << varName.c_str() << " = ";
126 
127  if(!inFile.good())
128  {
129  outFile << "\"\""; //empty string
130  }
131  else
132  {
133  std::string startLine = "\n \"";
134  std::string endLine = "\"";
135  std::vector<char> buffer(1024);
136  while(inFile.good())
137  {
138  inFile.read(buffer.data(), 1024);
139  std::streamsize count = inFile.gcount();
140  if(count)
141  {
142  outFile.write(startLine.c_str(), startLine.size());
143 
144  std::string hex = uBytes2Hex(buffer.data(), count);
145  outFile.write(hex.c_str(), hex.size());
146 
147  outFile.write(endLine.c_str(), endLine.size());
148  }
149  }
150  }
151 
152  std::string endOfVar = ";\n\n";
153  outFile.write(endOfVar.c_str(), endOfVar.size());
154 
155  if(!nspace.empty())
156  {
157  outFile << "}\n\n";
158  }
159 
160  outFile << "#endif //" << varName << "_H\n\n";
161  }
162 
163  outFile.close();
164  inFile.close();
165 
166  printf("Output file \"%s\" size = %ld bytes\n", ((targetDir + "/") + targetFileName).c_str(), UFile::length(((targetDir + "/") + targetFileName).c_str()));
167  ++k;
168  }
169 
170 }
UtiLite.h
UFile::getName
std::string getName()
Definition: UFile.h:135
UFile::length
long length()
Definition: UFile.h:110
count
Index count
buffer
main
int main(int argc, char *argv[])
Definition: utilite/resource_generator/main.cpp:40
UDirectory::currentDir
static std::string currentDir(bool trailingSeparator=false)
Definition: UDirectory.cpp:309
rtabmap_netvlad.argv
argv
Definition: rtabmap_netvlad.py:15
uBytes2Hex
std::string UTILITE_EXPORT uBytes2Hex(const char *bytes, unsigned int bytesLen)
Definition: UConversion.cpp:203
c_str
const char * c_str(Args &&...args)
showUsage
void showUsage()
Definition: utilite/resource_generator/main.cpp:26
i
int i


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:12