-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmmap_parser.c
More file actions
73 lines (55 loc) · 2.03 KB
/
mmap_parser.c
File metadata and controls
73 lines (55 loc) · 2.03 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
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#define MAX_LINE_LENGTH
#define DEFAULT_CHUNK_SIZE 200 * 1024 * 1024
size_t get_file_size(FILE *file) {
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
fseek(file, 0, SEEK_SET);
return size;
}
int main() {
FILE *file = fopen("measurements.txt", "r");
if (file == NULL) {
perror("Error opening file");
return EXIT_FAILURE;
}
size_t file_size = get_file_size(file);
printf("File size: %llu bytes\n", (u_int64_t)file_size);
for (size_t offset = 0; offset < file_size; offset += DEFAULT_CHUNK_SIZE) {
size_t bytes_to_map = DEFAULT_CHUNK_SIZE;
if (offset + bytes_to_map > file_size) {
bytes_to_map = file_size - offset;
}
void *file_memory = mmap(NULL, bytes_to_map, PROT_READ, MAP_PRIVATE,
fileno(file), offset);
if (file_memory == MAP_FAILED) {
perror("mmap failed");
fclose(file);
return EXIT_FAILURE;
}
char remainer_buffer[1000];
int remainer_index = 0;
for (int scan_char = bytes_to_map - 1; scan_char >= 0; scan_char--) {
if (((char *)file_memory)[scan_char] == '\n') {
remainer_buffer[remainer_index] = '\0';
break;
}
remainer_buffer[remainer_index] = ((char *)file_memory)[scan_char];
remainer_index++;
}
char *send_buffer =
malloc(sizeof(char) * (bytes_to_map - remainer_index + 1));
memcpy(send_buffer, file_memory, bytes_to_map - remainer_index);
memcpy(remainer_buffer, &file_memory[bytes_to_map - remainer_index],
remainer_index);
size_t print_size = (bytes_to_map < 100) ? bytes_to_map : 100;
fwrite(file_memory, 1, print_size, stdout);
printf("\n--- End of chunk ---\n");
printf("%s", (char *)remainer_buffer);
munmap(file_memory, bytes_to_map);
}
}