aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/file.h
blob: 0aa98e0deff564d6ec80f802b26ba84e38f2f9ec (plain) (blame)
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
#ifndef NVIM_FILE_H
#define NVIM_FILE_H

#include <stdbool.h>
#include <stddef.h>
#include <fcntl.h>

#include "nvim/func_attr.h"

/// Structure used to read from/write to file
typedef struct {
  int fd;  ///< File descriptor.
  bool eof;  ///< True if end of file was encountered.
} FileDescriptor;

/// file_open() flags
typedef enum {
  FILE_READ_ONLY = O_RDONLY,  ///< Open file read-only.
  FILE_CREATE = O_CREAT,  ///< Create file if it does not exist yet.
  FILE_WRITE_ONLY = O_WRONLY,  ///< Open file for writing only.
#ifdef O_NOFOLLOW
  FILE_NOSYMLINK = O_NOFOLLOW,  ///< Do not allow symbolic links.
#else
  FILE_NOSYMLINK = 0,
#endif
  FILE_CREATE_ONLY = O_CREAT|O_EXCL,  ///< Only create the file, failing
                                      ///< if it already exists.
  FILE_TRUNCATE = O_TRUNC,  ///< Truncate the file if it exists.
} FileOpenFlags;

/// Check whether end of file was encountered
///
/// @param[in]  fp  File to check.
///
/// @return true if it was, false if it was not or read operation was never
///         performed.
static inline bool file_eof(const FileDescriptor *const fp)
  FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
  FUNC_ATTR_ALWAYS_INLINE
{
  return fp->eof && rbuffer_size(fp->rv) == 0;
}

/// Return the file descriptor associated with the FileDescriptor structure
///
/// @param[in]  fp  File to check.
///
/// @return File descriptor.
static inline int file_fd(const FileDescriptor *const fp)
  FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
  FUNC_ATTR_ALWAYS_INLINE
{
  return fp->fd;
}

#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "file.h.generated.h"
#endif
#endif  // NVIM_FILE_H