-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage_builder.cpp
More file actions
147 lines (115 loc) · 4.5 KB
/
Copy pathlanguage_builder.cpp
File metadata and controls
147 lines (115 loc) · 4.5 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if _WIN32
#include <windows.h>
#else
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
#endif
#include "../cOMS/utils/StringUtils.h"
#include "../cOMS/system/FileUtils.cpp"
#include "../cOMS/memory/RingMemory.h"
#include "../cOMS/localization/Language.cpp"
Language* languages;
int lang_index = 0;
#if _WIN32
void iter_directories_recursive(RingMemory* const ring, const char *dir_path) {
WIN32_FIND_DATAA findFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
char path[PATH_MAX_LENGTH];
char searchPath[PATH_MAX_LENGTH];
snprintf(searchPath, sizeof(searchPath), "%s\\*", dir_path);
hFind = FindFirstFileA(searchPath, &findFileData);
if (hFind == INVALID_HANDLE_VALUE) {
fprintf(stderr, "FindFirstFile failed (%d)\n", GetLastError());
return;
}
do {
if (findFileData.cFileName[0] == '.') {
continue;
}
snprintf(path, sizeof(path), "%s\\%s", dir_path, findFileData.cFileName);
if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
iter_directories_recursive(ring, path);
} else if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) {
const char* const ext = strrchr(findFileData.cFileName, '.');
if (ext && strcmp(ext, ".langtxt") == 0) {
char abs_path[PATH_MAX_LENGTH];
if (GetFullPathNameA(path, sizeof(abs_path), abs_path, NULL)) {
printf("Found .langtxt file: %s\n", abs_path);
languages[lang_index].data = (byte *) calloc(1, file_size(abs_path) + 1 * MEGABYTE);
language_from_file_txt(languages + lang_index, abs_path, ring);
FileBody output = {0};
output.content = (byte *) calloc(10, MEGABYTE);
output.size = language_to_data(languages + lang_index, output.content);
char new_path[PATH_MAX_LENGTH];
str_replace(abs_path, ".langtxt", ".langbin", new_path);
file_write(new_path, &output);
free(languages[lang_index].data);
free(output.content);
++lang_index;
}
}
}
} while (FindNextFileA(hFind, &findFileData) != 0);
FindClose(hFind);
}
#else
void iter_directories_recursive(RingMemory* const ring, const char *dir_path) {
struct dirent *entry;
DIR *dir = opendir(dir_path);
if (!dir) {
fprintf(stderr, "Could not open directory: %s\n", dir_path);
return;
}
while ((entry = readdir(dir)) != NULL) {
char path[PATH_MAX_LENGTH];
struct stat statbuf;
if (entry->d_name == '.') {
continue;
}
snprintf(path, sizeof(path), "%s/%s", dir_path, entry->d_name);
if (stat(path, &statbuf) != 0) {
continue;
}
if (S_ISDIR(statbuf.st_mode)) {
iter_directories_recursive(ring, path);
} else if (S_ISREG(statbuf.st_mode)) {
const char *ext = strrchr(entry->d_name, '.');
if (ext && strcmp(ext, ".langtxt") == 0) {
char abs_path[PATH_MAX_LENGTH];
if (realpath(path, abs_path)) {
printf("Found .langtxt file: %s\n", abs_path);
FileBody file = {0};
file_read(abs_path, &file, ring);
languages[lang_index].data = (byte *) calloc(1, file.size + 1 * MEGABYTE);
language_from_file_txt(languages + lang_index, file.content);
char new_path[PATH_MAX_LENGTH];
str_replace(abs_path, ".langtxt", ".langbin", new_path);
language_to_file(ring, new_path, languages + lang_index);
free(languages[lang_index].data);
++lang_index;
}
}
}
}
closedir(dir);
}
#endif
int main(int argc, char *argv[])
{
if (argc < 2) {
fprintf(stderr, "Usage: %s <directory>\n", argv[0]);
return EXIT_FAILURE;
}
RingMemory memory_volatile = {0};
memory_volatile.memory = (byte *) malloc(sizeof(byte) * GIGABYTE * 1);
memory_volatile.size = sizeof(byte) * GIGABYTE * 1;
int lang_count = 1000;
languages = (Language *) malloc(sizeof(Language) * lang_count);
iter_directories_recursive(&memory_volatile, argv[1]);
printf("Language Files %d\n", lang_index);
}