Home | History | Annotate | Download | only in quipper
      1 // Copyright 2016 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 #ifndef CHROMIUMOS_WIDE_PROFILING_FILE_UTILS_H_
      6 #define CHROMIUMOS_WIDE_PROFILING_FILE_UTILS_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/logging.h"
     11 #include "compat/string.h"
     12 
     13 namespace quipper {
     14 
     15 // Reads the contents of a file into |contents|. Returns true on success, false
     16 // if it fails.
     17 bool FileToBuffer(const string& filename, std::vector<char>* contents);
     18 
     19 // Writes |contents| to a file, overwriting the file if it exists. Returns true
     20 // on success, false if it fails.
     21 template <typename CharContainer>
     22 bool BufferToFile(const string& filename, const CharContainer& contents) {
     23   FILE* fp = fopen(filename.c_str(), "wb");
     24   if (!fp) return false;
     25   // Do not write anything if |contents| contains nothing.  fopen will create
     26   // an empty file.
     27   if (!contents.empty()) {
     28     CHECK_EQ(fwrite(contents.data(), sizeof(typename CharContainer::value_type),
     29                     contents.size(), fp),
     30              contents.size());
     31   }
     32   fclose(fp);
     33   return true;
     34 }
     35 
     36 // Returns true iff the file exists.
     37 bool FileExists(const string& filename);
     38 
     39 }  // namespace quipper
     40 
     41 #endif  // CHROMIUMOS_WIDE_PROFILING_FILE_UTILS_H_
     42