1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-25 15:29:32 -05:00
denoland-deno/deno2/file_util.cc
2018-06-14 14:19:17 +02:00

98 lines
2.5 KiB
C++

// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
#include <stdio.h>
#include <fstream>
#include <iterator>
#include <string>
#include "file_util.h"
namespace deno {
bool ReadFileToString(const char* fn, std::string* contents) {
std::ifstream file(fn, std::ios::binary);
contents->assign(std::istreambuf_iterator<char>{file}, {});
return !file.bad();
}
class StartupDataCppWriter {
public:
StartupDataCppWriter(const char* name, const char* filename,
const std::string& data)
: name_(name),
filename_(filename),
data_(data),
file_(filename_, std::ios::binary) {}
bool Write() {
if (file_.bad()) {
return false;
}
WritePrefix();
WriteData();
WriteSuffix();
file_.close();
// printf("Wrote %s %d %s \n", name_, data_.size(), filename_);
return !file_.bad();
}
private:
void WritePrefix() {
file_ << "// Autogenerated snapshot file. Do not edit.\n\n";
file_ << "#include \"v8/include/v8.h\"\n\n";
}
void WriteSuffix() {
char buffer[500];
snprintf(buffer, sizeof(buffer),
"const v8::StartupData* StartupBlob_%s() {\n", name_);
file_ << buffer;
snprintf(buffer, sizeof(buffer), " return &%s_blob;\n", name_);
file_ << buffer;
file_ << "}\n\n";
}
void WriteBinaryContentsAsCArray() {
char buffer[5];
for (size_t i = 0; i < data_.size(); i++) {
if ((i & 0x1F) == 0x1F) file_ << "\n";
if (i > 0) file_ << ",";
snprintf(buffer, sizeof(buffer), "%u",
static_cast<unsigned char>(data_.at(i)));
file_ << buffer;
}
file_ << "\n";
}
void WriteData() {
char buffer[500];
snprintf(buffer, sizeof(buffer), "static const char %s_blob_data[] = {\n",
name_);
file_ << buffer;
WriteBinaryContentsAsCArray();
file_ << "};\n";
snprintf(buffer, sizeof(buffer), "static const int %s_blob_size = %ld;\n",
name_, data_.size());
file_ << buffer;
snprintf(buffer, sizeof(buffer), "static const v8::StartupData %s_blob =\n",
name_);
file_ << buffer;
snprintf(buffer, sizeof(buffer),
"{ (const char*) %s_blob_data, %s_blob_size };\n", name_, name_);
file_ << buffer;
}
const char* name_;
const char* filename_;
std::string data_;
std::ofstream file_;
};
bool WriteDataAsCpp(const char* name, const char* filename,
const std::string& data) {
StartupDataCppWriter writer(name, filename, data);
return writer.Write();
}
} // namespace deno