-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdic_changer.cpp
More file actions
76 lines (56 loc) · 1.66 KB
/
dic_changer.cpp
File metadata and controls
76 lines (56 loc) · 1.66 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
#include <cstdio>
#include <cstdlib>
#include <cstring>
size_t get_file_size(FILE* file)
{
size_t size = 0;
fseek(file, 0, SEEK_END);
size = ftell(file);
fseek(file, 0, SEEK_SET);
return size;
}
size_t get_eol_count(const char* buffer)
{
size_t eol_count = 0;
while(*buffer != '\0')
{
if (*buffer == '\n') eol_count++;
buffer++;
}
return eol_count;
}
int main()
{
FILE* file = fopen("src/dict_old.dic", "rb");
size_t file_size = get_file_size(file);
char *buffer = (char *)calloc(file_size + 1, sizeof(char));
fread(buffer, sizeof(char), file_size, file);
fclose(file);
char* new_buffer = (char *)calloc(file_size * 8 + 1, sizeof(char));
size_t eol_count = get_eol_count(buffer);
char* buf_ptr = buffer;
char* new_buf_ptr = new_buffer;
char* eq = strchr(buf_ptr, '=');
file = fopen("src/new_dict_.dic", "wb");
for (int i = 0; i < eol_count; i++)
{
memcpy(new_buf_ptr, buf_ptr, eq - buf_ptr);
new_buf_ptr += eq - buf_ptr;
for (int j = 0; j < (8 - (eq - buf_ptr) % 8) % 8; j++)
*(new_buf_ptr++) = 0;
*new_buf_ptr++ = '=';
buf_ptr = eq + 1;
eq = strchr(buf_ptr, '\n');
memcpy(new_buf_ptr, buf_ptr, eq - buf_ptr);
new_buf_ptr += eq - buf_ptr;
for (int j = 0; j < (8 - (eq - buf_ptr) % 8) % 8; j++)
*(new_buf_ptr++) = 0;
*new_buf_ptr++ = '\n';
buf_ptr = eq + 1;
eq = strchr(buf_ptr, '=');
}
fwrite(new_buffer, sizeof(char), new_buf_ptr - new_buffer, file);
fclose(file);
free(new_buffer);
free(buffer);
}