main.cpp
Go to the documentation of this file.
00001 /*
00002 *  utilite is a cross-platform library with
00003 *  useful utilities for fast and small developing.
00004 *  Copyright (C) 2010  Mathieu Labbe
00005 *
00006 *  utilite is free library: you can redistribute it and/or modify
00007 *  it under the terms of the GNU Lesser General Public License as published by
00008 *  the Free Software Foundation, either version 3 of the License, or
00009 *  (at your option) any later version.
00010 *
00011 *  utilite is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *  GNU Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public License
00017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #include "rtabmap/utilite/UtiLite.h"
00021 
00022 #include <fstream>
00023 #include <iostream>
00024 #include <string.h>
00025 
00026 void showUsage()
00027 {
00028         printf("Usage:\n"
00029                         "uresourcegenerator.exe [option] \"file1\" \"file2\" ... \n"
00030                         "  Create a file named \"file\".h with string\n"
00031                         "  variable named \"file\" which contains the data of the file.\n"
00032                         "  Warning, it overwrites the target file\n"
00033                         "  Options:\n"
00034                         "     -n \"namespace\"      namespace used\n"
00035                         "     -p \"targetPath\"     target path where the file is created\n"
00036                         "     -v                    version of the UtiLite library\n");
00037         exit(1);
00038 }
00039 
00040 int main(int argc, char * argv[])
00041 {
00042         if(argc < 2)
00043         {
00044                 showUsage();
00045         }
00046         else if(argc == 2 && strcmp(argv[1], "-v") == 0)
00047         {
00048                 printf("%s\n", UTILITE_VERSION);
00049                 exit(0);
00050         }
00051 
00052         std::string targetDir = UDirectory::currentDir(); // By default, use the current directory
00053         std::string nspace; // namespace
00054 
00055         int k;
00056         for(k=1; k<(argc-1); ++k)
00057         {
00058                 if(strcmp(argv[k], "-n") == 0)
00059                 {
00060                         if(!(k+1<(argc-1)))
00061                         {
00062                                 showUsage();
00063                         }
00064                         nspace = argv[k+1];
00065                         printf(" Using namespace=%s\n", nspace.c_str());
00066                         ++k;
00067                 }
00068                 else if(strcmp(argv[k], "-p") == 0)
00069                 {
00070                         if(!(k+1<(argc-1)))
00071                         {
00072                                 showUsage();
00073                         }
00074                         targetDir = argv[k+1];
00075                         printf(" Using target directory=%s\n", targetDir.c_str());
00076                         ++k;
00077                 }
00078                 else
00079                 {
00080                         break;
00081                 }
00082         }
00083 
00084         while(k < argc)
00085         {
00086                 std::string filePath = argv[k];
00087                 std::string varName = UFile::getName(argv[k]);
00088                 // replace '_'
00089                 for(unsigned int i=0; i<varName.size(); ++i)
00090                 {
00091                         if(!((varName[i] >= '0' && varName[i] <= '9') ||
00092                                 (varName[i] >= 'A' && varName[i] <= 'Z') ||
00093                                 (varName[i] >= 'a' && varName[i] <= 'z')))
00094                         {
00095                                 varName[i] = '_';
00096                         }
00097                 }
00098                 std::string targetFileName = varName + ".h";
00099                 // upper case
00100                 for(unsigned int i=0; i<varName.size(); ++i)
00101                 {
00102                         if(varName[i] >= 'a' && varName[i] <= 'z')
00103                         {
00104                                 varName[i] -= 32; // upper case
00105                         }
00106                 }
00107 
00108                 std::fstream outFile;
00109                 std::fstream inFile;
00110                 outFile.open(((targetDir + "/") + targetFileName).c_str(), std::fstream::out);
00111                 inFile.open(filePath.c_str(), std::fstream::in | std::fstream::binary);
00112 
00113                 printf("Input file \"%s\" size = %ld bytes\n", filePath.c_str(), UFile::length(filePath));
00114                 if(outFile.is_open() && inFile.is_open())
00115                 {
00116                         outFile << "/*This is a generated file...*/\n\n";
00117                         outFile << "#ifndef " << varName << "_H\n";
00118                         outFile << "#define " << varName << "_H\n\n";
00119 
00120                         if(!nspace.empty())
00121                         {
00122                                 outFile << "namespace " << nspace.c_str() << "\n{\n\n";
00123                         }
00124 
00125                         outFile << "static const char * " << varName.c_str() << " = ";
00126 
00127                         if(!inFile.good())
00128                         {
00129                                 outFile << "\"\""; //empty string
00130                         }
00131                         else
00132                         {
00133                                 std::string startLine = "\n   \"";
00134                                 std::string endLine = "\"";
00135                                 std::vector<char> buffer(1024);
00136                                 while(inFile.good())
00137                                 {
00138                                         inFile.read(buffer.data(), 1024);
00139                                         std::streamsize count = inFile.gcount();
00140                                         if(count)
00141                                         {
00142                                                 outFile.write(startLine.c_str(), startLine.size());
00143 
00144                                                 std::string hex = uBytes2Hex(buffer.data(), count);
00145                                                 outFile.write(hex.c_str(), hex.size());
00146 
00147                                                 outFile.write(endLine.c_str(), endLine.size());
00148                                         }
00149                                 }
00150                         }
00151 
00152                         std::string endOfVar = ";\n\n";
00153                         outFile.write(endOfVar.c_str(), endOfVar.size());
00154 
00155                         if(!nspace.empty())
00156                         {
00157                                 outFile << "}\n\n";
00158                         }
00159 
00160                         outFile << "#endif //" << varName << "_H\n\n";
00161                 }
00162 
00163                 outFile.close();
00164                 inFile.close();
00165 
00166                 printf("Output file \"%s\" size = %ld bytes\n", ((targetDir + "/") + targetFileName).c_str(), UFile::length(((targetDir + "/") + targetFileName).c_str()));
00167                 ++k;
00168         }
00169 
00170 }


rtabmap
Author(s): Mathieu Labbe
autogenerated on Sat Jul 23 2016 11:44:16