file_io.h 2.59 KB
Newer Older
1
2
3
4
/*!
 * Copyright (c) 2018 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
5
6
7
#ifndef LIGHTGBM_UTILS_FILE_IO_H_
#define LIGHTGBM_UTILS_FILE_IO_H_

8
#include <string>
Guolin Ke's avatar
Guolin Ke committed
9
10
11
#include <cstdio>
#include <cstdlib>
#include <cstring>
12
13
#include <iostream>
#include <memory>
14
#include <vector>
15

16
namespace LightGBM {
17
18
19
20
21

/*!
 * \brief An interface for writing files from buffers
 */
struct VirtualFileWriter {
22
  virtual ~VirtualFileWriter() {}
23
24
25
26
27
28
29
30
31
32
33
34
  /*!
   * \brief Initialize the writer
   * \return True when the file is available for writes
   */
  virtual bool Init() = 0;
  /*!
   * \brief Append buffer to file
   * \param data Buffer to write from
   * \param bytes Number of bytes to write from buffer
   * \return Number of bytes written
   */
  virtual size_t Write(const void* data, size_t bytes) const = 0;
35
36
37
38
39
40
41
42
43
44

  size_t AlignedWrite(const void* data, size_t bytes, size_t alignment = 8) const {
    auto ret = Write(data, bytes);
    if (bytes % alignment != 0) {
      size_t padding = AlignedSize(bytes, alignment) - bytes;
      std::vector<char> tmp(padding, 0);
      ret += Write(tmp.data(), padding);
    }
    return ret;
  }
45
46
47
48
49
50
51
52
53
54
55
56
  /*!
   * \brief Create appropriate writer for filename
   * \param filename Filename of the data
   * \return File writer instance
   */
  static std::unique_ptr<VirtualFileWriter> Make(const std::string& filename);
  /*!
   * \brief Check filename existence
   * \param filename Filename of the data
   * \return True when the file exists
   */
  static bool Exists(const std::string& filename);
57
58
59
60
61
62
63
64

  static size_t AlignedSize(size_t bytes, size_t alignment = 8) {
    if (bytes % alignment == 0) {
      return bytes;
    } else {
      return bytes / alignment * alignment + alignment;
    }
  }
65
66
67
68
69
70
71
72
73
74
};

/**
 * \brief An interface for reading files into buffers
 */
struct VirtualFileReader {
  /*!
   * \brief Constructor
   * \param filename Filename of the data
   */
75
  virtual ~VirtualFileReader() {}
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  /*!
   * \brief Initialize the reader
   * \return True when the file is available for read
   */
  virtual bool Init() = 0;
  /*!
   * \brief Read data into buffer
   * \param buffer Buffer to read data into
   * \param bytes Number of bytes to read
   * \return Number of bytes read
   */
  virtual size_t Read(void* buffer, size_t bytes) const = 0;
  /*!
   * \brief Create appropriate reader for filename
   * \param filename Filename of the data
   * \return File reader instance
   */
  static std::unique_ptr<VirtualFileReader> Make(const std::string& filename);
};

}  // namespace LightGBM

#endif   // LightGBM_UTILS_FILE_IO_H_