filesystem.h 3.1 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2017 VisualDL Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

S
superjom 已提交
15 16
#ifndef VISUALDL_UTILS_FILESYSTEM_H
#define VISUALDL_UTILS_FILESYSTEM_H
S
superjom 已提交
17 18 19 20 21 22

#include <google/protobuf/text_format.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fstream>

23 24
#include "visualdl/utils/logging.h"

25 26 27 28 29 30
#ifdef _WIN32
#include <filesystem>
#else
#include <unistd.h>
#endif

S
superjom 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
namespace visualdl {

namespace fs {

template <typename T>
std::string Serialize(const T& proto, bool human_readable = false) {
  if (human_readable) {
    std::string buffer;
    google::protobuf::TextFormat::PrintToString(proto, &buffer);
    return buffer;
  }
  return proto.SerializeAsString();
}

template <typename T>
bool DeSerialize(T* proto, const std::string buf, bool human_readable = false) {
  // NOTE human_readable not valid
  if (human_readable) {
    return google::protobuf::TextFormat::ParseFromString(buf, proto);
  }
  return proto->ParseFromString(buf);
}

54 55 56 57 58 59 60 61 62
template <typename T>
bool SerializeToFile(const T& proto, const std::string& path) {
  std::ofstream file(path, std::ios::binary);
  return proto.SerializeToOstream(&file);
}

template <typename T>
bool DeSerializeFromFile(T* proto, const std::string& path) {
  std::ifstream file(path, std::ios::binary);
S
Superjom 已提交
63
  CHECK(file.is_open()) << "open " << path << " failed";
64 65 66
  return proto->ParseFromIstream(&file);
}

S
superjom 已提交
67
static void TryMkdir(const std::string& dir) {
S
superjom 已提交
68 69
  struct stat st = {0};
  if (stat(dir.c_str(), &st) == -1) {
70 71 72
#ifdef _WIN32
    std::experimental::filesystem::create_directory(dir);
#else
S
superjom 已提交
73
    ::mkdir(dir.c_str(), 0700);
74
#endif
S
superjom 已提交
75 76 77
  }
}

78
// Create a path by recursively create directries
S
superjom 已提交
79
static void TryRecurMkdir(const std::string& path) {
80 81 82 83 84 85 86 87 88 89 90
  // split path by '/'
  for (int i = 1; i < path.size() - 1; i++) {
    if (path[i] == '/') {
      auto dir = path.substr(0, i + 1);
      TryMkdir(dir);
    }
  }
  // the last level
  TryMkdir(path);
}

S
superjom 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
inline void Write(const std::string& path,
                  const std::string& buffer,
                  std::ios::openmode open_mode = std::ios::binary) {
  std::ofstream file(path, open_mode);
  CHECK(file.is_open()) << "failed to open " << path;
  file.write(buffer.c_str(), buffer.size());
  file.close();
}

inline std::string Read(const std::string& path,
                        std::ios::openmode open_mode = std::ios::binary) {
  std::string buffer;
  std::ifstream file(path, open_mode | std::ios::ate);
  CHECK(file.is_open()) << "failed to open " << path;
  size_t size = file.tellg();
  file.seekg(0);
  buffer.resize(size);
  file.read(&buffer[0], size);
  return buffer;
}

}  // namespace fs

}  // namespace visualdl

#endif  // VISUALDL_BACKEND_UTILS_FILESYSTEM_H