Ruben Rodriguez Buchillon | 5cd4a9c | 2017-04-26 13:03:04 -0700 | [diff] [blame] | 1 | // Copyright 2017 The Chromium OS 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 "thd/source/file_source.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 9 | #include <base/check.h> |
Ruben Rodriguez Buchillon | 5cd4a9c | 2017-04-26 13:03:04 -0700 | [diff] [blame] | 10 | #include <base/files/file_util.h> |
| 11 | #include <base/logging.h> |
| 12 | #include <base/strings/string_number_conversions.h> |
| 13 | |
| 14 | namespace thd { |
| 15 | |
| 16 | FileSource::FileSource(const base::FilePath& file_path) |
| 17 | : file_path_(file_path) {} |
| 18 | |
| 19 | FileSource::~FileSource() {} |
| 20 | |
| 21 | bool FileSource::ReadValue(int64_t* value_out) { |
| 22 | DCHECK(value_out); |
| 23 | if (!base::PathExists(file_path_)) { |
| 24 | LOG(ERROR) << "Path " << file_path_.value() << " not found."; |
| 25 | return false; |
| 26 | } |
| 27 | std::string contents; |
| 28 | if (!base::ReadFileToString(file_path_, &contents)) { |
| 29 | PLOG(ERROR) << "Failed to read path " << file_path_.value() << "."; |
| 30 | return false; |
| 31 | } |
| 32 | return base::StringToInt64(contents, value_out); |
| 33 | } |
| 34 | |
| 35 | } // namespace thd |