Beispiel #1
0
Status readFile(const boost::filesystem::path& path, std::string& content) {
  auto path_exists = pathExists(path);
  if (!path_exists.ok()) {
    return path_exists;
  }

  int statusCode = 0;
  std::string statusMessage = "OK";
  std::stringstream buffer;

  fs::ifstream file_h(path);
  if (file_h.is_open()) {
    buffer << file_h.rdbuf();

    if (file_h.bad()) {
      statusCode = 1;
      statusMessage = "Could not read file";
    } else 
      content.assign(std::move(buffer.str()));
      
  } else {
    statusCode = 1;
    statusMessage = "Could not open file for reading";
  }
  return Status(statusCode, statusMessage);
}
void CPU::loadRom(const std::string& h, const std::string& g, const std::string& f, const std::string& e)
{
    QFile file_h(QString::fromStdString(h));
    if(!file_h.open(QIODevice::ReadOnly))
    {
        error("Cannot open rom 1 : " + file_h.errorString().toStdString() + " : " +
              std::to_string(file_h.error()));
    }
    QFile file_g(QString::fromStdString(g));
    if(!file_g.open(QIODevice::ReadOnly))
    {
        error("Cannot open rom 2 : " + file_g.errorString().toStdString() + " : " +
              std::to_string(file_h.error()));
    }
    QFile file_f(QString::fromStdString(f));
    if(!file_f.open(QIODevice::ReadOnly))
    {
        error("Cannot open rom 3 : " + file_f.errorString().toStdString() + " : " +
              std::to_string(file_h.error()));
    }
    QFile file_e(QString::fromStdString(e));
    if(!file_e.open(QIODevice::ReadOnly))
    {
        error("Cannot open rom 4 : " + file_e.errorString().toStdString() + " : " +
              std::to_string(file_h.error()));
    }
    QByteArray hContents = file_h.readAll();
    std::copy(hContents.constBegin(), hContents.constEnd(),
              m_state.mem.begin());
    QByteArray gContents = file_g.readAll();
    std::copy(gContents.constBegin(), gContents.constEnd(),
              m_state.mem.begin() + 0x800);
    QByteArray fContents = file_f.readAll();
    std::copy(fContents.constBegin(), fContents.constEnd(),
              m_state.mem.begin() + 0x1000);
    QByteArray eContents = file_e.readAll();
    std::copy(eContents.constBegin(), eContents.constEnd(),
              m_state.mem.begin() + 0x1800);
}
Beispiel #3
0
int main(int argc, const char *argv[]) {
	// Needs 3 arguments
	if (argc != 4) {
		std::cout << "Usage: <manifest>[in] <c++ file>[out] <header>[out]" << std::endl;
		return 1;
	}

	std::cout << "Manifest: " << argv[1] << "  CPP: " << argv[2] << "  Header: " << argv[3] << std::endl;

	std::ifstream file_manifest(argv[1]);
	std::ofstream file_cpp(argv[2]);
	std::ofstream file_h(argv[3]);

	if (!file_manifest.good()) {
		std::cout << "Failed to open manifest" << std::endl;
		return 1;
	}

	if (!file_cpp.good()) {
		std::cout << "Failed to open output CPP file" << std::endl;
		return 1;
	}

	if (!file_h.good()) {
		std::cout << "Failed to open output H file" << std::endl;
		return 1;
	}

	// If the manifest has a path use that as the base for finding files.
	std::string manifest(argv[1]);
	std::string path_base;
	std::string::size_type pos = manifest.rfind('/');
	if (pos != std::string::npos) {
		path_base = manifest.substr(0, pos+1);
	}

	file_cpp << "#include \"libresrc.h\"\n";

	std::string file;
	while (std::getline(file_manifest, file)) {
		if (file.empty()) continue;

		std::ifstream ifp((path_base + file).c_str(), std::ios_base::binary);

		if (!ifp.is_open()) {
			std::cout << "Error opening file: " << file << std::endl;
			return 1;
		}

		clean(file);
		file_cpp << "const unsigned char " << file << "[] = {";

		size_t length = 0;
		for (std::istreambuf_iterator<char> it(ifp), end; it != end; ++it) {
			if (length > 0) file_cpp << ",";
			file_cpp << (unsigned int)(unsigned char)*it;
			++length;
		}

		file_cpp << "};\n";

		file_h << "extern const unsigned char " << file << "[" << length << "];\n";
	}

	return 0;
}