1 //===- FuzzerIO.cpp - IO utils. -------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 //===----------------------------------------------------------------------===//
11 #include "FuzzerInternal.h"
18 static std::vector<std::string> ListFilesInDir(const std::string &Dir) {
19 std::vector<std::string> V;
20 DIR *D = opendir(Dir.c_str());
22 while (auto E = readdir(D)) {
23 if (E->d_type == DT_REG || E->d_type == DT_LNK)
24 V.push_back(E->d_name);
30 Unit FileToVector(const std::string &Path) {
31 std::ifstream T(Path);
32 return Unit((std::istreambuf_iterator<char>(T)),
33 std::istreambuf_iterator<char>());
36 void CopyFileToErr(const std::string &Path) {
37 std::ifstream T(Path);
38 std::copy(std::istreambuf_iterator<char>(T), std::istreambuf_iterator<char>(),
39 std::ostream_iterator<char>(std::cerr, ""));
42 void WriteToFile(const Unit &U, const std::string &Path) {
43 std::ofstream OF(Path);
44 OF.write((const char*)U.data(), U.size());
47 void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V) {
48 for (auto &X : ListFilesInDir(Path))
49 V->push_back(FileToVector(DirPlusFile(Path, X)));
52 std::string DirPlusFile(const std::string &DirPath,
53 const std::string &FileName) {
54 return DirPath + "/" + FileName;