1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "testing/util/read_file.h"
6 
7 #include <stdio.h>
8 
9 namespace openscreen {
10 
ReadEntireFileToString(absl::string_view filename)11 std::string ReadEntireFileToString(absl::string_view filename) {
12   FILE* file = fopen(filename.data(), "r");
13   if (file == nullptr) {
14     return {};
15   }
16   fseek(file, 0, SEEK_END);
17   long file_size = ftell(file);
18   fseek(file, 0, SEEK_SET);
19   std::string contents(file_size, 0);
20   int bytes_read = 0;
21   while (bytes_read < file_size) {
22     size_t ret = fread(&contents[bytes_read], 1, file_size - bytes_read, file);
23     if (ret == 0 && ferror(file)) {
24       return {};
25     } else {
26       bytes_read += ret;
27     }
28   }
29   fclose(file);
30 
31   return contents;
32 }
33 
34 }  // namespace openscreen
35