summaryrefslogtreecommitdiff
path: root/libc/src/stdio/printf_core/file_writer.h
blob: 0fd6d115ddd8b8dcafd558ec5a43cfcee0ccd996 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//===-- FILE Writer definition for printf -----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FILE_WRITER_H
#define LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FILE_WRITER_H

#include "src/__support/CPP/string_view.h"
#include "src/__support/File/file.h"
#include "src/__support/macros/attributes.h" // For LIBC_INLINE
#include "src/stdio/printf_core/core_structs.h"

#include <stddef.h>
#include <stdio.h>

namespace __llvm_libc {
namespace printf_core {

template <typename file_t> class FileWriter {
  file_t *file;

public:
  LIBC_INLINE FileWriter(file_t *init_file);

  LIBC_INLINE ~FileWriter();

  LIBC_INLINE int write(const char *__restrict to_write, size_t len);

  // These write functions take a FileWriter as a void* in raw_pointer, and
  // call the appropriate write function on it.
  static int write_str(void *raw_pointer, cpp::string_view new_string) {
    FileWriter *file_writer = reinterpret_cast<FileWriter *>(raw_pointer);
    return file_writer->write(new_string.data(), new_string.size());
  }
  static int write_chars(void *raw_pointer, char new_char, size_t len) {
    FileWriter *file_writer = reinterpret_cast<FileWriter *>(raw_pointer);
    constexpr size_t BUFF_SIZE = 8;
    char buff[BUFF_SIZE] = {new_char};
    int result;
    while (len > BUFF_SIZE) {
      result = file_writer->write(buff, BUFF_SIZE);
      if (result < 0)
        return result;
      len -= BUFF_SIZE;
    }
    return file_writer->write(buff, len);
  }
  static int write_char(void *raw_pointer, char new_char) {
    FileWriter *file_writer = reinterpret_cast<FileWriter *>(raw_pointer);
    return file_writer->write(&new_char, 1);
  }
};

// The interface for using our internal file implementation.
template <>
LIBC_INLINE
FileWriter<__llvm_libc::File>::FileWriter(__llvm_libc::File *init_file) {
  file = init_file;
  file->lock();
}
template <> LIBC_INLINE FileWriter<__llvm_libc::File>::~FileWriter() {
  file->unlock();
}
template <>
LIBC_INLINE int
FileWriter<__llvm_libc::File>::write(const char *__restrict to_write,
                                     size_t len) {
  auto result = file->write_unlocked(to_write, len);
  size_t written = result.value;
  if (written != len || result.has_error())
    written = FILE_WRITE_ERROR;
  if (file->error_unlocked())
    written = FILE_STATUS_ERROR;
  return written;
}

// The interface for using the system's file implementation.
template <> LIBC_INLINE FileWriter<::FILE>::FileWriter(::FILE *init_file) {
  file = init_file;
  ::flockfile(file);
}
template <> LIBC_INLINE FileWriter<::FILE>::~FileWriter() {
  ::funlockfile(file);
}
template <>
LIBC_INLINE int FileWriter<::FILE>::write(const char *__restrict to_write,
                                          size_t len) {
  size_t written = ::fwrite_unlocked(to_write, 1, len, file);
  if (written != len || ::ferror_unlocked(file))
    written = FILE_WRITE_ERROR;
  return written;
}

} // namespace printf_core
} // namespace __llvm_libc

#endif // LLVM_LIBC_SRC_STDIO_PRINTF_CORE_FILE_WRITER_H