-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.cpp
More file actions
191 lines (149 loc) · 5.14 KB
/
Copy pathengine.cpp
File metadata and controls
191 lines (149 loc) · 5.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "engine.h"
#include <cstring>
#include <fstream>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
std::vector<std::string> session_words;
size_t current_word_index = 0;
static void* mapped_data = nullptr;
static size_t mapped_size = 0;
static std::vector<const char*> mmap_words;
static bool is_mmap_mode = false;
static SRHeader* current_header = nullptr;
void cleanup_session() {
if ( mapped_data ) {
munmap( mapped_data, mapped_size );
mapped_data = nullptr;
}
mmap_words.clear();
session_words.clear();
current_word_index = 0;
current_header = nullptr;
is_mmap_mode = false;
}
extern "C" {
bool compile_sr( const char* output_path ) {
if ( session_words.empty() ) return false;
std::ofstream out( output_path, std::ios::binary );
if ( !out ) return false;
SRHeader header = {};
header.magic[0] = 'S'; header.magic[1] = 'R'; header.magic[2] = '0'; header.magic[3] = '1';
header.saved_index = 0;
header.total_words = session_words.size();
header.chapter_count = 0;
out.write( reinterpret_cast<const char*>( &header ), sizeof( SRHeader ) );
for ( const auto& w : session_words ) {
out.write( w.c_str(), w.length() );
char null_term = '\0';
out.write( &null_term, 1 );
}
return true;
}
bool load_sr_session( const char* file_path ) {
cleanup_session();
is_mmap_mode = true;
int fd = open( file_path, O_RDWR );
if ( fd < 0 ) return false;
struct stat sb;
if ( fstat( fd, &sb ) == -1 ) { close( fd ); return false; }
mapped_size = sb.st_size;
mapped_data = mmap( nullptr, mapped_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
close( fd );
if ( mapped_data == MAP_FAILED ) {
mapped_data = nullptr;
return false;
}
current_header = reinterpret_cast<SRHeader*>( mapped_data );
if ( strncmp( current_header->magic, "SR01", 4 ) != 0 ) return false;
char* payload = reinterpret_cast<char*>( mapped_data ) + sizeof( SRHeader );
size_t payload_size = mapped_size - sizeof( SRHeader );
char* ptr = payload;
char* end = payload + payload_size;
while ( ptr < end ) {
mmap_words.push_back( ptr );
while ( ptr < end && *ptr != '\0' ) ptr++;
ptr++;
}
current_word_index = 0;
return true;
}
void sync_sr_progress( int word_index ) {
if ( is_mmap_mode && current_header ) {
current_header->saved_index = word_index;
msync( mapped_data, sizeof( SRHeader ), MS_ASYNC );
}
}
bool add_sr_chapter( const char* title, int word_index ) {
if ( !is_mmap_mode || !current_header ) {
return false;
}
if ( current_header->chapter_count >= 50 ) {
return false;
}
int idx = current_header->chapter_count;
current_header->chapters[ idx ].word_index = word_index;
strncpy( current_header->chapters[ idx ].title, title, 63 );
current_header->chapters[ idx ].title[ 63 ] = '\0';
current_header->chapter_count++;
msync( mapped_data, sizeof( SRHeader ), MS_ASYNC );
return true;
}
int get_sr_saved_index() {
if ( is_mmap_mode && current_header ) return current_header->saved_index;
return 0;
}
int get_sr_chapter_word( int chapter_num ) {
if ( !is_mmap_mode || !current_header ) {
return -1;
}
if ( chapter_num >= 1 && chapter_num <= (int)current_header->chapter_count ) {
return current_header->chapters[ chapter_num - 1 ].word_index;
}
return -1;
}
int get_sr_chapter_count() {
if ( is_mmap_mode && current_header ) return current_header->chapter_count;
return 0;
}
const char* get_sr_chapter_title( int index ) {
if ( is_mmap_mode && current_header && index >= 0 && index < (int)current_header->chapter_count ) {
return current_header->chapters[ index ].title;
}
return "";
}
int get_sr_chapter_word_by_index( int index ) {
if ( is_mmap_mode && current_header && index >= 0 && index < (int)current_header->chapter_count ) {
return current_header->chapters[ index ].word_index;
}
return -1;
}
int get_total_words() {
return is_mmap_mode ? mmap_words.size() : session_words.size();
}
bool get_next_word( char* buffer, int max_len, int* orp_index, float* delay_multiplier ) {
size_t total = is_mmap_mode ? mmap_words.size() : session_words.size();
if ( current_word_index >= total ) return false;
std::string word;
if ( is_mmap_mode ) {
word = mmap_words[ current_word_index ];
} else {
word = session_words[ current_word_index ];
}
current_word_index++;
int len = word.length();
if ( len == 1 ) *orp_index = 0;
else if ( len <= 3 ) *orp_index = 1;
else if ( len <= 5 ) *orp_index = 2;
else if ( len <= 9 ) *orp_index = 3;
else *orp_index = 4;
*delay_multiplier = 1.0f;
char last_char = word.back();
if ( last_char == ',' ) *delay_multiplier = 1.5f;
else if ( last_char == '.' || last_char == '?' || last_char == '!' || last_char == ';' ) *delay_multiplier = 2.0f;
strncpy( buffer, word.c_str(), max_len - 1 );
buffer[ max_len - 1 ] = '\0';
return true;
}
} // extern "C"