-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_output.cpp
More file actions
107 lines (82 loc) · 2.14 KB
/
Copy pathinput_output.cpp
File metadata and controls
107 lines (82 loc) · 2.14 KB
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
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <assert.h>
#include "headers/input_output.h"
#include "../../err_codes.h"
#include "headers/DSL.h"
static err fill_buffer (FILE* read, char** buf);
int GetFileSize(FILE* fp)
{
int startPos = ftell(fp);
fseek(fp, 0L, SEEK_END);
int fsize = ftell(fp);
fseek(fp, startPos, SEEK_SET);
return fsize;
}
err fill_buffer (FILE* read, char** buf)
{
CHECK_PTR(read);
CHECK_PTR(buf);
int fsize = GetFileSize(read);
CALLOC(*buf, char, (fsize + 1));
fread(*buf, sizeof(char), fsize, read);
return SUCCESS;
}
void clear_tabs(struct line* data, int nLines)
{
for(int i = 0; i < nLines; i++)
while(data[i].str[0] == ' ')
{
data[i].str[0] = '\0';
data[i].str++;
}
}
Data input_data (FILE* input)
{
int fsize = GetFileSize(input);
char* buffer = NULL;
fill_buffer(input, &buffer);
char* buffer_cp = buffer;
// printf ("Buffer: \n%s\n end of buffer\n", buffer);
Data data = {};
for (int i = 0; i < fsize + 1; i++)
if (buffer[i] == '\r' && buffer[i - 1] == '\n')
data.quant -= 1;
for (int i = 0; i < fsize + 1; i++)
{
if (buffer[i] == '\n' && buffer[i - 1] == '\r')
{
data.quant += 1;
buffer[i] = '\0';
buffer[i - 1] = '\0';
}
}
data.lines = (line*)calloc(data.quant, sizeof(line));
if (buffer[fsize - 1] != '\n')
data.quant += 1;
for (int i = 0; i < data.quant; i++)
{
// printf("%d - %s\n", i, buffer);
data.lines[i].str = strdup(buffer);
data.lines[i].len = strlen(buffer);
buffer += strlen(buffer);
while (*buffer == '\0')
buffer++;
}
free(buffer_cp);
return data;
}
void clear_data (Data* data)
{
for (int i = 0; i < data->quant; i++)
free(data->lines[i].str);
free(data->lines);
free(data);
}
void dump_data (Data* data)
{
printf ("qant - %d \n", data->quant);
for (int i = 0; i < data->quant; i++)
printf("%s: len = %d \n", data->lines[i].str, data->lines[i].len);
}