Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(364)

Side by Side Diff: helpers.cc

Issue 3530001: first cut: establishes base helper classes and main (Closed) Base URL: http://git.chromium.org/git/cros_boot_mode.git
Patch Set: truncation is bad, m'kay. . . Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « helpers.h ('k') | inherit-review-settings-ok » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
OLDNEW
« no previous file with comments | « helpers.h ('k') | inherit-review-settings-ok » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698