Make respack less dumb

This commit is contained in:
Thomas Goyne 2014-01-17 06:33:18 -08:00
parent b1ffffaa4c
commit 934431a29e

View file

@ -11,49 +11,26 @@
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// $Id$
/// @file common-respack.cpp
/// @brief Load any file into a byte array.
/// @ingroup util
#include <iterator> #include <iterator>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <string> #include <string>
/// Clean a filename for use as an identity. /// Clean a filename for use as an identity.
/// @param str[in] String containing filename. /// @param str[in] String containing filename.
inline void clean(std::string &str) { void clean(std::string &str) {
// Remove path. // Chop extension
std::string::size_type pos = str.rfind('/'); auto pos = str.rfind('.');
if (pos != std::string::npos) { if (pos != std::string::npos)
str = str.substr(pos+1, str.size()); str.erase(pos, str.size() - pos);
}
// Chop extension. // Remove path
pos = str.rfind('.'); pos = str.rfind('/');
if (pos != std::string::npos) { if (pos != std::string::npos)
str = str.substr(0, pos); str.erase(0, pos + 1);
}
for (unsigned int i = 0; i != str.size(); i++) {
int c = (int)str[i];
if (((c >= 65) && (c <= 90)) || /* A-Z */
((c >= 97) && (c <= 122)) || /* a-z */
((c >= 48) && (c <= 57)) || /* 0-9 */
(c == 95)) { /* _ */
continue;
} else {
str.erase(i, 1);
}
}
} }
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {
// Needs 3 arguments // Needs 3 arguments
if (argc != 4) { if (argc != 4) {
@ -90,46 +67,32 @@ int main(int argc, const char *argv[]) {
path_base = manifest.substr(0, pos+1); path_base = manifest.substr(0, pos+1);
} }
file_cpp << "#include \"libresrc.h\"\n";
file_cpp << "#include \"libresrc.h\"" << std::endl; std::string file;
while (std::getline(file_manifest, file)) {
std::string file; // File for array.
while (file_manifest) {
std::getline(file_manifest, file);
if (file.empty()) continue; if (file.empty()) continue;
std::ifstream ifp((path_base + file).c_str(), std::ios_base::in|std::ios_base::binary); std::ifstream ifp((path_base + file).c_str(), std::ios_base::binary);
if (!ifp.is_open()) { if (!ifp.is_open()) {
std::cout << "Error opening file: " << file << std::endl; std::cout << "Error opening file: " << file << std::endl;
return 1; return 1;
} }
// Identity used in C/Header files. clean(file);
std::string ident(file); file_cpp << "const unsigned char " << file << "[] = {";
clean(ident);
file_cpp << "const unsigned char " << ident << "[] = {"; size_t length = 0;
for (std::istreambuf_iterator<char> it(ifp), end; it != end; ++it) {
/// Create byte-array.
std::istreambuf_iterator<char> ifp_i(ifp);
std::istreambuf_iterator<char> eof;
int length = 0;
while (ifp_i != eof) {
if (length > 0) file_cpp << ","; if (length > 0) file_cpp << ",";
file_cpp << (unsigned int)(unsigned char)*ifp_i; file_cpp << (unsigned int)(unsigned char)*it;
++length;
++ifp_i;
length++;
} }
// Finish file_cpp << "};\n";
file_cpp << "};" << std::endl;
// Prototype.
file_h << "extern const unsigned char " << ident << "[" << length << "];" << std::endl;
file_h << "extern const unsigned char " << file << "[" << length << "];\n";
} }
return 0; return 0;