2018-06-14 05:50:35 -04:00
|
|
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
|
|
|
// All rights reserved. MIT License.
|
2018-07-01 12:21:03 -04:00
|
|
|
#include <inttypes.h>
|
2018-06-14 05:50:35 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "file_util.h"
|
|
|
|
|
|
|
|
namespace deno {
|
|
|
|
|
2018-07-13 01:21:28 -04:00
|
|
|
std::string BinaryContentAsC(const char* name, const std::string& data) {
|
|
|
|
char b[512];
|
|
|
|
std::string output;
|
|
|
|
// Write prefix.
|
|
|
|
snprintf(b, sizeof(b), "static const char %s_data[] = {\n", name);
|
|
|
|
output.append(b);
|
|
|
|
// Write actual data.
|
|
|
|
for (size_t i = 0; i < data.size(); ++i) {
|
|
|
|
if ((i & 0x1F) == 0x1F) output.append("\n");
|
|
|
|
if (i > 0) output.append(",");
|
|
|
|
snprintf(b, sizeof(b), "%hhu", static_cast<unsigned char>(data.at(i)));
|
|
|
|
output.append(b);
|
|
|
|
}
|
|
|
|
output.append("\n");
|
|
|
|
// Write suffix.
|
|
|
|
output.append("};\n");
|
|
|
|
snprintf(b, sizeof(b), "static const int %s_size = %" PRId64 ";\n", name,
|
|
|
|
static_cast<uint64_t>(data.size()));
|
|
|
|
output.append(b);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2018-06-14 05:50:35 -04:00
|
|
|
bool ReadFileToString(const char* fn, std::string* contents) {
|
|
|
|
std::ifstream file(fn, std::ios::binary);
|
2018-06-15 09:45:45 -04:00
|
|
|
if (file.fail()) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-14 05:50:35 -04:00
|
|
|
contents->assign(std::istreambuf_iterator<char>{file}, {});
|
2018-06-15 09:45:45 -04:00
|
|
|
return !file.fail();
|
2018-06-14 05:50:35 -04:00
|
|
|
}
|
|
|
|
|
2018-07-13 01:21:28 -04:00
|
|
|
std::string Basename(std::string const& filename) {
|
|
|
|
for (auto it = filename.rbegin(); it != filename.rend(); ++it) {
|
|
|
|
char ch = *it;
|
|
|
|
if (ch == '\\' || ch == '/') {
|
|
|
|
return std::string(it.base(), filename.end());
|
2018-06-14 05:50:35 -04:00
|
|
|
}
|
|
|
|
}
|
2018-07-13 01:21:28 -04:00
|
|
|
return filename;
|
2018-06-14 05:50:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace deno
|