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"
16 std::vector<std::string> ListFilesInDir(const std::string &Dir) {
17 std::vector<std::string> V;
18 DIR *D = opendir(Dir.c_str());
20 while (auto E = readdir(D)) {
21 if (E->d_type == DT_REG || E->d_type == DT_LNK)
22 V.push_back(E->d_name);
28 Unit FileToVector(const std::string &Path) {
29 std::ifstream T(Path);
30 return Unit((std::istreambuf_iterator<char>(T)),
31 std::istreambuf_iterator<char>());
34 void WriteToFile(const Unit &U, const std::string &Path) {
35 std::ofstream OF(Path);
36 OF.write((const char*)U.data(), U.size());
39 void ReadDirToVectorOfUnits(const char *Path, std::vector<Unit> *V) {
40 for (auto &X : ListFilesInDir(Path))
41 V->push_back(FileToVector(std::string(Path) + "/" + X));