-
Notifications
You must be signed in to change notification settings - Fork 0
/
yio.h
executable file
·88 lines (73 loc) · 3.21 KB
/
yio.h
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* yio.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ihhrabar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/10 12:54:35 by ihhrabar #+# #+# */
/* Updated: 2023/03/10 12:54:36 by ihhrabar ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef YIO_H
# define YIO_H
# ifndef GNL_BSIZE
# define GNL_BSIZE 20
# endif
# ifndef GNL_CSTEP
# define GNL_CSTEP 30
# endif
# include "ytypes.h"
// Used to store file contents while reading from it
typedef struct s_file_cache_node
{
char content[GNL_BSIZE];
int fd;
unsigned int read_index;
} t_fcnode;
// Represents a list of file cache nodes
typedef struct s_file_cache
{
unsigned int size;
unsigned int capacity;
t_fcnode *node_array;
} t_file_cache;
// Private function!
// Creates a new cache node in the cache and returns a pointer to it
t_fcnode *_new_cache_node(t_file_cache *c, unsigned int fd, char *ct);
// Private function!
// Retrieves cache node from the file descriptor
// Returns NULL if node doesnt exist
t_fcnode *_get_cache_node(t_file_cache *cache, int fd);
// Private function!
// Stores cache node under a file descriptor fd. Creates fd node if
// it doesn't exist yet. If content is NULL or empty string, removes fd
// node
// Returns 1 if node is not exhausted, otherwise returns 0 and removes node
// from the cache
unsigned int _update_cache_node(t_file_cache *cache, t_fcnode *node);
// Return the next line from specified file descriptor. Returns NULL/YNULL if
// file descriptor is exhausted
char *get_next_line(int fd);
// Write cstring to file descriptor
void write_cstr_fd(int fd, const char *s);
// Try to open file at path str, with specified flags. Returns TRUE upon
// success and sets value of fd to the opened file descriptor. Otherwise
// returns FALSE
t_bool try_open_file(const t_string *str, int flags, int *fd);
// Same as try_open_file, except the path is specified with cstring instead
// of string
t_bool try_open_file_cstr(const char *cstr, int flags, int *fd);
// Read file from fd in its entirety into a list of strings. Returns
// NULL/YNULL if file descriptor is invalid
t_list *read_fd_to_lines(int fd);
// Read file at path str with specified flags into a list of strings.
// Returns NULL/YNULL if file descriptor is invalid
t_list *read_file_to_lines(const t_string *str, int flags);
// Same as read_file_to_lines, except the path is specified with cstring
// instead of string
t_list *read_file_to_lines_cstr(const char *str, int flags);
// Read file from fd in its entirety into a string. Returns NULL/YNULL if
// file descriptor is invalid
t_string *read_file_to_string(int fd);
#endif