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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
#include <algorithm>
#include <memory>
#include <fstream>
#include <cstring>
#ifndef _WIN32
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#endif /* _WIN32 */
#include "replxx.hxx"
#include "history.hxx"
using namespace std;
namespace replxx {
namespace {
void delete_ReplxxHistoryScanImpl( Replxx::HistoryScanImpl* impl_ ) {
delete impl_;
}
}
static int const REPLXX_DEFAULT_HISTORY_MAX_LEN( 1000 );
Replxx::HistoryScan::HistoryScan( impl_t impl_ )
: _impl( std::move( impl_ ) ) {
}
bool Replxx::HistoryScan::next( void ) {
return ( _impl->next() );
}
Replxx::HistoryScanImpl::HistoryScanImpl( History::entries_t const& entries_ )
: _entries( entries_ )
, _it( _entries.end() )
, _utf8Cache()
, _entryCache( std::string(), std::string() )
, _cacheValid( false ) {
}
Replxx::HistoryEntry const& Replxx::HistoryScan::get( void ) const {
return ( _impl->get() );
}
bool Replxx::HistoryScanImpl::next( void ) {
if ( _it == _entries.end() ) {
_it = _entries.begin();
} else {
++ _it;
}
_cacheValid = false;
return ( _it != _entries.end() );
}
Replxx::HistoryEntry const& Replxx::HistoryScanImpl::get( void ) const {
if ( _cacheValid ) {
return ( _entryCache );
}
_utf8Cache.assign( _it->text() );
_entryCache = Replxx::HistoryEntry( _it->timestamp(), _utf8Cache.get() );
_cacheValid = true;
return ( _entryCache );
}
Replxx::HistoryScan::impl_t History::scan( void ) const {
return ( Replxx::HistoryScan::impl_t( new Replxx::HistoryScanImpl( _entries ), delete_ReplxxHistoryScanImpl ) );
}
History::History( void )
: _entries()
, _maxSize( REPLXX_DEFAULT_HISTORY_MAX_LEN )
, _current( _entries.begin() )
, _yankPos( _entries.end() )
, _previous( _entries.begin() )
, _recallMostRecent( false )
, _unique( true ) {
}
void History::add( UnicodeString const& line, std::string const& when ) {
if ( _maxSize <= 0 ) {
return;
}
if ( ! _entries.empty() && ( line == _entries.back().text() ) ) {
_entries.back() = Entry( now_ms_str(), line );
return;
}
remove_duplicate( line );
trim_to_max_size();
_entries.emplace_back( when, line );
_locations.insert( make_pair( line, last() ) );
if ( _current == _entries.end() ) {
_current = last();
}
_yankPos = _entries.end();
}
#ifndef _WIN32
class FileLock {
std::string _path;
int _lockFd;
public:
FileLock( std::string const& name_ )
: _path( name_ + ".lock" )
, _lockFd( ::open( _path.c_str(), O_CREAT | O_RDWR, 0600 ) ) {
static_cast<void>( ::lockf( _lockFd, F_LOCK, 0 ) == 0 );
}
~FileLock( void ) {
static_cast<void>( ::lockf( _lockFd, F_ULOCK, 0 ) == 0 );
::close( _lockFd );
::unlink( _path.c_str() );
return;
}
};
#endif
bool History::save( std::string const& filename, bool sync_ ) {
#ifndef _WIN32
mode_t old_umask = umask( S_IXUSR | S_IRWXG | S_IRWXO );
FileLock fileLock( filename );
#endif
entries_t entries;
locations_t locations;
if ( ! sync_ ) {
entries.swap( _entries );
locations.swap( _locations );
_entries = entries;
reset_iters();
}
do_load( filename );
sort();
remove_duplicates();
trim_to_max_size();
ofstream histFile( filename );
if ( ! histFile ) {
return ( false );
}
#ifndef _WIN32
umask( old_umask );
chmod( filename.c_str(), S_IRUSR | S_IWUSR );
#endif
Utf8String utf8;
for ( Entry const& h : _entries ) {
if ( ! h.text().is_empty() ) {
utf8.assign( h.text() );
histFile << "### " << h.timestamp() << "\n" << utf8.get() << endl;
}
}
if ( ! sync_ ) {
_entries = std::move( entries );
_locations = std::move( locations );
}
reset_iters();
return ( true );
}
namespace {
bool is_timestamp( std::string const& s ) {
static char const TIMESTAMP_PATTERN[] = "### dddd-dd-dd dd:dd:dd.ddd";
static int const TIMESTAMP_LENGTH( sizeof ( TIMESTAMP_PATTERN ) - 1 );
if ( s.length() != TIMESTAMP_LENGTH ) {
return ( false );
}
for ( int i( 0 ); i < TIMESTAMP_LENGTH; ++ i ) {
if ( TIMESTAMP_PATTERN[i] == 'd' ) {
if ( ! isdigit( s[i] ) ) {
return ( false );
}
} else if ( s[i] != TIMESTAMP_PATTERN[i] ) {
return ( false );
}
}
return ( true );
}
}
bool History::do_load( std::string const& filename ) {
ifstream histFile( filename );
if ( ! histFile ) {
return ( false );
}
string line;
string when( "0000-00-00 00:00:00.000" );
while ( getline( histFile, line ).good() ) {
string::size_type eol( line.find_first_of( "\r\n" ) );
if ( eol != string::npos ) {
line.erase( eol );
}
if ( is_timestamp( line ) ) {
when.assign( line, 4, std::string::npos );
continue;
}
if ( ! line.empty() ) {
_entries.emplace_back( when, UnicodeString( line ) );
}
}
return ( true );
}
bool History::load( std::string const& filename ) {
clear();
bool success( do_load( filename ) );
sort();
remove_duplicates();
trim_to_max_size();
_previous = _current = last();
_yankPos = _entries.end();
return ( success );
}
void History::sort( void ) {
typedef std::vector<Entry> sortable_entries_t;
_locations.clear();
sortable_entries_t sortableEntries( _entries.begin(), _entries.end() );
std::stable_sort( sortableEntries.begin(), sortableEntries.end() );
_entries.clear();
_entries.insert( _entries.begin(), sortableEntries.begin(), sortableEntries.end() );
}
void History::clear( void ) {
_locations.clear();
_entries.clear();
_current = _entries.begin();
_recallMostRecent = false;
}
void History::set_max_size( int size_ ) {
if ( size_ >= 0 ) {
_maxSize = size_;
trim_to_max_size();
}
}
void History::reset_yank_iterator( void ) {
_yankPos = _entries.end();
}
bool History::next_yank_position( void ) {
bool resetYankSize( false );
if ( _yankPos == _entries.end() ) {
resetYankSize = true;
}
if ( ( _yankPos != _entries.begin() ) && ( _yankPos != _entries.end() ) ) {
-- _yankPos;
} else {
_yankPos = moved( _entries.end(), -2 );
}
return ( resetYankSize );
}
bool History::move( bool up_ ) {
bool doRecall( _recallMostRecent && ! up_ );
if ( doRecall ) {
_current = _previous; // emulate Windows down-arrow
}
_recallMostRecent = false;
return ( doRecall || move( _current, up_ ? -1 : 1 ) );
}
void History::jump( bool start_, bool reset_ ) {
if ( start_ ) {
_current = _entries.begin();
} else {
_current = last();
}
if ( reset_ ) {
_recallMostRecent = false;
}
}
void History::save_pos( void ) {
_previous = _current;
}
void History::restore_pos( void ) {
_current = _previous;
}
bool History::common_prefix_search( UnicodeString const& prefix_, int prefixSize_, bool back_ ) {
int step( back_ ? -1 : 1 );
entries_t::const_iterator it( moved( _current, step, true ) );
while ( it != _current ) {
if ( it->text().starts_with( prefix_.begin(), prefix_.begin() + prefixSize_ ) ) {
_current = it;
commit_index();
return ( true );
}
move( it, step, true );
}
return ( false );
}
bool History::move( entries_t::const_iterator& it_, int by_, bool wrapped_ ) const {
if ( by_ > 0 ) {
for ( int i( 0 ); i < by_; ++ i ) {
++ it_;
if ( it_ != _entries.end() ) {
} else if ( wrapped_ ) {
it_ = _entries.begin();
} else {
-- it_;
return ( false );
}
}
} else {
for ( int i( 0 ); i > by_; -- i ) {
if ( it_ != _entries.begin() ) {
-- it_;
} else if ( wrapped_ ) {
it_ = last();
} else {
return ( false );
}
}
}
return ( true );
}
History::entries_t::const_iterator History::moved( entries_t::const_iterator it_, int by_, bool wrapped_ ) const {
move( it_, by_, wrapped_ );
return ( it_ );
}
void History::erase( entries_t::const_iterator it_ ) {
bool invalidated( it_ == _current );
_locations.erase( it_->text() );
it_ = _entries.erase( it_ );
if ( invalidated ) {
_current = it_;
}
if ( ( _current == _entries.end() ) && ! _entries.empty() ) {
-- _current;
}
_yankPos = _entries.end();
_previous = _current;
}
void History::trim_to_max_size( void ) {
while ( size() > _maxSize ) {
erase( _entries.begin() );
}
}
void History::remove_duplicate( UnicodeString const& line_ ) {
if ( ! _unique ) {
return;
}
locations_t::iterator it( _locations.find( line_ ) );
if ( it == _locations.end() ) {
return;
}
erase( it->second );
}
void History::remove_duplicates( void ) {
if ( ! _unique ) {
return;
}
_locations.clear();
typedef std::pair<locations_t::iterator, bool> locations_insertion_result_t;
for ( entries_t::iterator it( _entries.begin() ), end( _entries.end() ); it != end; ++ it ) {
locations_insertion_result_t locationsInsertionResult( _locations.insert( make_pair( it->text(), it ) ) );
if ( ! locationsInsertionResult.second ) {
_entries.erase( locationsInsertionResult.first->second );
locationsInsertionResult.first->second = it;
}
}
}
void History::update_last( UnicodeString const& line_ ) {
if ( _unique ) {
_locations.erase( _entries.back().text() );
remove_duplicate( line_ );
_locations.insert( make_pair( line_, last() ) );
}
_entries.back() = Entry( now_ms_str(), line_ );
}
void History::drop_last( void ) {
erase( last() );
}
bool History::is_last( void ) const {
return ( _current == last() );
}
History::entries_t::const_iterator History::last( void ) const {
return ( moved( _entries.end(), -1 ) );
}
void History::reset_iters( void ) {
_previous = _current = last();
_yankPos = _entries.end();
}
}
|