OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 // Provides the implementation of the helper functions for PlatformReader |
| 6 // and derived classes. |
| 7 |
| 8 #include "helpers.h" |
| 9 |
| 10 #include <stdio.h> |
| 11 #include <sys/types.h> |
| 12 |
| 13 #include "platform_reader.h" |
| 14 |
| 15 namespace cros_boot_mode { |
| 16 namespace helpers { |
| 17 |
| 18 size_t read_file(const char *path, char *buf, size_t max_bytes) { |
| 19 if (!buf) |
| 20 return 0; |
| 21 ::FILE *fp = ::fopen(path, "r"); |
| 22 if (!fp || !max_bytes) |
| 23 return 0; |
| 24 size_t bytes_read = ::fread(buf, 1, max_bytes, fp); |
| 25 |
| 26 // If max_bytes doesn't consume the entire file, return 0 bytes read. |
| 27 // This is meant to ensure that unexpected platform changes appear as |
| 28 // kUnsupported rather than randomly based on truncation. |
| 29 if (::fgetc(fp) != EOF) { |
| 30 bytes_read = 0; |
| 31 } |
| 32 |
| 33 ::fclose(fp); |
| 34 return bytes_read; |
| 35 } |
| 36 |
| 37 int to_int(const char *file_contents, size_t length) { |
| 38 if (!length) |
| 39 return PlatformReader::kUnsupported; |
| 40 int enum_value = PlatformReader::kUnsupported; |
| 41 if (sscanf(file_contents, "%d", &enum_value) != 1 || enum_value < 0) |
| 42 return PlatformReader::kUnsupported; |
| 43 return enum_value; |
| 44 } |
| 45 |
| 46 } // namespace helpers |
| 47 } // namespace cros_boot_mode |
OLD | NEW |