/
ft_get_grid.c
128 lines (117 loc) · 3.22 KB
/
ft_get_grid.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_get_grid.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdos-san <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/07/28 09:31:37 by mdos-san #+# #+# */
/* Updated: 2015/07/30 23:07:11 by mdos-san ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include "ft.h"
#include "ft_get_grid.h"
#include "ft_error.h"
int ft_get_length(int *fd, char *file)
{
int length;
int ret;
char *buffer;
length = 0;
buffer = (char*)malloc(BUFFER_SIZE);
if (buffer == 0)
return (0);
if (fd == 0)
while ((ret = read(0, buffer, BUFFER_SIZE)) > 0)
length = length + ret;
if (fd != 0)
while ((ret = read(*fd, buffer, BUFFER_SIZE)) > 0)
length = length + ret;
(ret == -1) ? ft_print_error() : 0;
if (fd > 0)
{
(close(*fd) == -1) ? ft_print_error() : 0;
*fd = open(file, O_RDONLY);
(*fd == -1) ? ft_print_error() : 0;
}
free(buffer);
return (length);
}
char *ft_allocate_str(int length)
{
char *str;
str = (char*)malloc(sizeof(char) * (length + 1));
if (str == 0)
return (0);
str[length] = '\0';
return (str);
}
int ft_assign_str(char **str, int length, int fd)
{
int i;
char buffer;
buffer = 0;
i = 0;
buffer = 0;
while (i < length)
{
if (read(fd, &buffer, 1) == -1)
ft_print_error();
(*str)[i] = buffer;
i++;
}
return (1);
}
int ft_get_info(char **str, int *line, char *ref)
{
int i;
int i2;
int check;
i = 0;
i2 = 3;
check = 0;
while ((*str)[i] != '\n' && (*str)[i] != '\0')
i++;
ref[0] = (*str)[i - 3];
ref[1] = (*str)[i - 2];
ref[2] = (*str)[i - 1];
ref[3] = '\0';
if (ref[0] == ref[1] || ref[0] == ref[2] || ref[1] == ref[2])
ft_print_error();
(*str)[i - 3] = ' ';
*line = ft_atoi(*str);
(*str)[i - 3] = ref[0];
return (1);
}
void ft_get_stdin(int *length)
{
char *buffer;
char ref[4];
int line;
int fd;
int ret;
line = 0;
buffer = (char*)malloc(BUFFER_SIZE);
fd = open("menu_maxi_best_of_+.txt", O_CREAT | O_RDWR | O_TRUNC, 0644);
(fd == -1) ? ft_print_error() : 0;
ret = read(0, buffer, BUFFER_SIZE);
(ret == -1) ? ft_print_error() : write(fd, buffer, ret);
(*length) = (*length) + ret;
(close(fd) == -1) ? ft_print_error() : 0;
fd = open("menu_maxi_best_of_+.txt", O_RDWR | O_APPEND, 0644);
(fd == -1) ? ft_print_error() : ft_get_info(&buffer, &line, ref);
while ((ret = read(0, buffer, BUFFER_SIZE)) > 0)
{
(ft_char_is_ref(buffer[0], ref) == 0 && buffer[0] != '\n') ?\
ft_print_error() : write(fd, buffer, ret);
(*length) = (*length) + ret;
}
(ret == -1) ? ft_print_error() : 0;
free(buffer);
(close(fd) == -1) ? ft_print_error() : 0;
}