filesystem.h 2.1 KB
Newer Older
S
superjom 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#ifndef VISUALDL_BACKEND_UTILS_FILESYSTEM_H
#define VISUALDL_BACKEND_UTILS_FILESYSTEM_H

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

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);
}

33 34 35 36 37 38 39 40 41 42 43 44
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);
  return proto->ParseFromIstream(&file);
}

S
superjom 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
void TryMkdir(const std::string& dir) {
  VLOG(1) << "try to mkdir " << dir;
  struct stat st = {0};
  if (stat(dir.c_str(), &st) == -1) {
    ::mkdir(dir.c_str(), 0700);
  }
}

inline void Write(const std::string& path,
                  const std::string& buffer,
                  std::ios::openmode open_mode = std::ios::binary) {
  VLOG(1) << "write to path " << path;
  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) {
  VLOG(1) << "read from path " << path;
  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