52938
|
1 |
/*
|
|
2 |
* Copyright (c) 2002-2018, the original author or authors.
|
|
3 |
*
|
|
4 |
* This software is distributable under the BSD license. See the terms of the
|
|
5 |
* BSD license in the documentation provided with this software.
|
|
6 |
*
|
58903
|
7 |
* https://opensource.org/licenses/BSD-3-Clause
|
52938
|
8 |
*/
|
|
9 |
package jdk.internal.org.jline.reader.impl.history;
|
|
10 |
|
|
11 |
import java.io.*;
|
|
12 |
import java.nio.file.*;
|
58903
|
13 |
import java.time.DateTimeException;
|
52938
|
14 |
import java.time.Instant;
|
|
15 |
import java.util.*;
|
|
16 |
|
|
17 |
import jdk.internal.org.jline.reader.History;
|
|
18 |
import jdk.internal.org.jline.reader.LineReader;
|
|
19 |
import jdk.internal.org.jline.utils.Log;
|
|
20 |
|
|
21 |
import static jdk.internal.org.jline.reader.LineReader.HISTORY_IGNORE;
|
|
22 |
import static jdk.internal.org.jline.reader.impl.ReaderUtils.*;
|
|
23 |
|
|
24 |
/**
|
|
25 |
* {@link History} using a file for persistent backing.
|
|
26 |
* <p>
|
|
27 |
* Implementers should install shutdown hook to call {@link DefaultHistory#save}
|
|
28 |
* to save history to disk.
|
|
29 |
* </p>
|
|
30 |
*/
|
|
31 |
public class DefaultHistory implements History {
|
|
32 |
|
|
33 |
public static final int DEFAULT_HISTORY_SIZE = 500;
|
|
34 |
public static final int DEFAULT_HISTORY_FILE_SIZE = 10000;
|
|
35 |
|
|
36 |
private final LinkedList<Entry> items = new LinkedList<>();
|
|
37 |
|
|
38 |
private LineReader reader;
|
|
39 |
|
58903
|
40 |
private Map<String, HistoryFileData> historyFiles = new HashMap<>();
|
52938
|
41 |
private int offset = 0;
|
|
42 |
private int index = 0;
|
|
43 |
|
|
44 |
public DefaultHistory() {
|
|
45 |
}
|
|
46 |
|
|
47 |
public DefaultHistory(LineReader reader) {
|
|
48 |
attach(reader);
|
|
49 |
}
|
|
50 |
|
|
51 |
private Path getPath() {
|
|
52 |
Object obj = reader != null ? reader.getVariables().get(LineReader.HISTORY_FILE) : null;
|
|
53 |
if (obj instanceof Path) {
|
|
54 |
return (Path) obj;
|
|
55 |
} else if (obj instanceof File) {
|
|
56 |
return ((File) obj).toPath();
|
|
57 |
} else if (obj != null) {
|
|
58 |
return Paths.get(obj.toString());
|
|
59 |
} else {
|
|
60 |
return null;
|
|
61 |
}
|
|
62 |
}
|
|
63 |
|
|
64 |
@Override
|
|
65 |
public void attach(LineReader reader) {
|
|
66 |
if (this.reader != reader) {
|
|
67 |
this.reader = reader;
|
|
68 |
try {
|
|
69 |
load();
|
|
70 |
}
|
58903
|
71 |
catch (IllegalArgumentException | IOException e) {
|
52938
|
72 |
Log.warn("Failed to load history", e);
|
|
73 |
}
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
@Override
|
|
78 |
public void load() throws IOException {
|
|
79 |
Path path = getPath();
|
|
80 |
if (path != null) {
|
|
81 |
try {
|
|
82 |
if (Files.exists(path)) {
|
|
83 |
Log.trace("Loading history from: ", path);
|
|
84 |
try (BufferedReader reader = Files.newBufferedReader(path)) {
|
|
85 |
internalClear();
|
|
86 |
reader.lines().forEach(line -> addHistoryLine(path, line));
|
58903
|
87 |
setHistoryFileData(path, new HistoryFileData(items.size(), items.size()));
|
52938
|
88 |
maybeResize();
|
|
89 |
}
|
|
90 |
}
|
58903
|
91 |
} catch (IllegalArgumentException | IOException e) {
|
52938
|
92 |
Log.debug("Failed to load history; clearing", e);
|
|
93 |
internalClear();
|
|
94 |
throw e;
|
|
95 |
}
|
|
96 |
}
|
|
97 |
}
|
|
98 |
|
58903
|
99 |
@Override
|
|
100 |
public void read(Path file, boolean incremental) throws IOException {
|
|
101 |
Path path = file != null ? file : getPath();
|
|
102 |
if (path != null) {
|
|
103 |
try {
|
|
104 |
if (Files.exists(path)) {
|
|
105 |
Log.trace("Reading history from: ", path);
|
|
106 |
try (BufferedReader reader = Files.newBufferedReader(path)) {
|
|
107 |
reader.lines().forEach(line -> addHistoryLine(path, line, incremental));
|
|
108 |
setHistoryFileData(path, new HistoryFileData(items.size(), items.size()));
|
|
109 |
maybeResize();
|
|
110 |
}
|
|
111 |
}
|
|
112 |
} catch (IllegalArgumentException | IOException e) {
|
|
113 |
Log.debug("Failed to read history; clearing", e);
|
|
114 |
internalClear();
|
|
115 |
throw e;
|
|
116 |
}
|
|
117 |
}
|
|
118 |
}
|
|
119 |
|
|
120 |
private String doHistoryFileDataKey (Path path){
|
|
121 |
return path != null ? path.toAbsolutePath().toString() : null;
|
|
122 |
}
|
|
123 |
|
|
124 |
private HistoryFileData getHistoryFileData(Path path) {
|
|
125 |
String key = doHistoryFileDataKey(path);
|
|
126 |
if (!historyFiles.containsKey(key)){
|
|
127 |
historyFiles.put(key, new HistoryFileData());
|
|
128 |
}
|
|
129 |
return historyFiles.get(key);
|
|
130 |
}
|
|
131 |
|
|
132 |
private void setHistoryFileData(Path path, HistoryFileData historyFileData) {
|
|
133 |
historyFiles.put(doHistoryFileDataKey(path), historyFileData);
|
|
134 |
}
|
|
135 |
|
|
136 |
private boolean isLineReaderHistory (Path path) throws IOException {
|
|
137 |
Path lrp = getPath();
|
|
138 |
if (lrp == null) {
|
|
139 |
if (path != null) {
|
|
140 |
return false;
|
|
141 |
} else {
|
|
142 |
return true;
|
|
143 |
}
|
|
144 |
}
|
|
145 |
return Files.isSameFile(lrp, path);
|
|
146 |
}
|
|
147 |
|
|
148 |
private void setLastLoaded(Path path, int lastloaded){
|
|
149 |
getHistoryFileData(path).setLastLoaded(lastloaded);
|
|
150 |
}
|
|
151 |
|
|
152 |
private void setEntriesInFile(Path path, int entriesInFile){
|
|
153 |
getHistoryFileData(path).setEntriesInFile(entriesInFile);
|
|
154 |
}
|
|
155 |
|
|
156 |
private void incEntriesInFile(Path path, int amount){
|
|
157 |
getHistoryFileData(path).incEntriesInFile(amount);
|
|
158 |
}
|
|
159 |
|
|
160 |
private int getLastLoaded(Path path){
|
|
161 |
return getHistoryFileData(path).getLastLoaded();
|
|
162 |
}
|
|
163 |
|
|
164 |
private int getEntriesInFile(Path path){
|
|
165 |
return getHistoryFileData(path).getEntriesInFile();
|
|
166 |
}
|
|
167 |
|
52938
|
168 |
protected void addHistoryLine(Path path, String line) {
|
58903
|
169 |
addHistoryLine(path, line, false);
|
|
170 |
}
|
|
171 |
|
|
172 |
protected void addHistoryLine(Path path, String line, boolean checkDuplicates) {
|
52938
|
173 |
if (reader.isSet(LineReader.Option.HISTORY_TIMESTAMPED)) {
|
|
174 |
int idx = line.indexOf(':');
|
58903
|
175 |
final String badHistoryFileSyntax = "Bad history file syntax! " +
|
|
176 |
"The history file `" + path + "` may be an older history: " +
|
|
177 |
"please remove it or use a different history file.";
|
52938
|
178 |
if (idx < 0) {
|
58903
|
179 |
throw new IllegalArgumentException(badHistoryFileSyntax);
|
52938
|
180 |
}
|
58903
|
181 |
Instant time;
|
|
182 |
try {
|
|
183 |
time = Instant.ofEpochMilli(Long.parseLong(line.substring(0, idx)));
|
|
184 |
} catch (DateTimeException | NumberFormatException e) {
|
|
185 |
throw new IllegalArgumentException(badHistoryFileSyntax);
|
|
186 |
}
|
|
187 |
|
52938
|
188 |
String unescaped = unescape(line.substring(idx + 1));
|
58903
|
189 |
internalAdd(time, unescaped, checkDuplicates);
|
52938
|
190 |
}
|
|
191 |
else {
|
58903
|
192 |
internalAdd(Instant.now(), unescape(line), checkDuplicates);
|
52938
|
193 |
}
|
|
194 |
}
|
|
195 |
|
|
196 |
@Override
|
|
197 |
public void purge() throws IOException {
|
|
198 |
internalClear();
|
|
199 |
Path path = getPath();
|
|
200 |
if (path != null) {
|
|
201 |
Log.trace("Purging history from: ", path);
|
|
202 |
Files.deleteIfExists(path);
|
|
203 |
}
|
|
204 |
}
|
|
205 |
|
|
206 |
@Override
|
58903
|
207 |
public void write(Path file, boolean incremental) throws IOException {
|
|
208 |
Path path = file != null ? file : getPath();
|
|
209 |
if (path != null && Files.exists(path)) {
|
|
210 |
path.toFile().delete();
|
|
211 |
}
|
|
212 |
internalWrite(path, incremental ? getLastLoaded(path) : 0);
|
|
213 |
}
|
|
214 |
|
|
215 |
@Override
|
|
216 |
public void append(Path file, boolean incremental) throws IOException {
|
|
217 |
internalWrite(file != null ? file : getPath(),
|
|
218 |
incremental ? getLastLoaded(file) : 0);
|
|
219 |
}
|
|
220 |
|
|
221 |
@Override
|
52938
|
222 |
public void save() throws IOException {
|
58903
|
223 |
internalWrite(getPath(), getLastLoaded(getPath()));
|
|
224 |
}
|
|
225 |
|
|
226 |
private void internalWrite(Path path, int from) throws IOException {
|
52938
|
227 |
if (path != null) {
|
|
228 |
Log.trace("Saving history to: ", path);
|
|
229 |
Files.createDirectories(path.toAbsolutePath().getParent());
|
|
230 |
// Append new items to the history file
|
|
231 |
try (BufferedWriter writer = Files.newBufferedWriter(path.toAbsolutePath(),
|
|
232 |
StandardOpenOption.WRITE, StandardOpenOption.APPEND, StandardOpenOption.CREATE)) {
|
58903
|
233 |
for (Entry entry : items.subList(from, items.size())) {
|
52938
|
234 |
if (isPersistable(entry)) {
|
|
235 |
writer.append(format(entry));
|
|
236 |
}
|
|
237 |
}
|
|
238 |
}
|
58903
|
239 |
incEntriesInFile(path, items.size() - from);
|
52938
|
240 |
int max = getInt(reader, LineReader.HISTORY_FILE_SIZE, DEFAULT_HISTORY_FILE_SIZE);
|
58903
|
241 |
if (getEntriesInFile(path) > max + max / 4) {
|
52938
|
242 |
trimHistory(path, max);
|
|
243 |
}
|
|
244 |
}
|
58903
|
245 |
setLastLoaded(path, items.size());
|
52938
|
246 |
}
|
|
247 |
|
|
248 |
protected void trimHistory(Path path, int max) throws IOException {
|
|
249 |
Log.trace("Trimming history path: ", path);
|
|
250 |
// Load all history entries
|
|
251 |
LinkedList<Entry> allItems = new LinkedList<>();
|
|
252 |
try (BufferedReader reader = Files.newBufferedReader(path)) {
|
|
253 |
reader.lines().forEach(l -> {
|
|
254 |
int idx = l.indexOf(':');
|
|
255 |
Instant time = Instant.ofEpochMilli(Long.parseLong(l.substring(0, idx)));
|
|
256 |
String line = unescape(l.substring(idx + 1));
|
|
257 |
allItems.add(createEntry(allItems.size(), time, line));
|
|
258 |
});
|
|
259 |
}
|
|
260 |
// Remove duplicates
|
|
261 |
doTrimHistory(allItems, max);
|
|
262 |
// Write history
|
|
263 |
Path temp = Files.createTempFile(path.toAbsolutePath().getParent(), path.getFileName().toString(), ".tmp");
|
|
264 |
try (BufferedWriter writer = Files.newBufferedWriter(temp, StandardOpenOption.WRITE)) {
|
|
265 |
for (Entry entry : allItems) {
|
|
266 |
writer.append(format(entry));
|
|
267 |
}
|
|
268 |
}
|
|
269 |
Files.move(temp, path, StandardCopyOption.REPLACE_EXISTING);
|
|
270 |
// Keep items in memory
|
58903
|
271 |
if (isLineReaderHistory(path)) {
|
|
272 |
internalClear();
|
|
273 |
offset = allItems.get(0).index();
|
|
274 |
items.addAll(allItems);
|
|
275 |
setHistoryFileData(path, new HistoryFileData(items.size(), items.size()));
|
|
276 |
} else {
|
|
277 |
setEntriesInFile(path, allItems.size());
|
|
278 |
}
|
52938
|
279 |
maybeResize();
|
|
280 |
}
|
|
281 |
|
|
282 |
/**
|
|
283 |
* Create a history entry. Subclasses may override to use their own entry implementations.
|
|
284 |
* @param index index of history entry
|
|
285 |
* @param time entry creation time
|
|
286 |
* @param line the entry text
|
|
287 |
* @return entry object
|
|
288 |
*/
|
|
289 |
protected EntryImpl createEntry(int index, Instant time, String line) {
|
|
290 |
return new EntryImpl(index, time, line);
|
|
291 |
}
|
|
292 |
|
|
293 |
private void internalClear() {
|
|
294 |
offset = 0;
|
|
295 |
index = 0;
|
58903
|
296 |
historyFiles = new HashMap<>();
|
52938
|
297 |
items.clear();
|
|
298 |
}
|
|
299 |
|
|
300 |
static void doTrimHistory(List<Entry> allItems, int max) {
|
|
301 |
int idx = 0;
|
|
302 |
while (idx < allItems.size()) {
|
|
303 |
int ridx = allItems.size() - idx - 1;
|
|
304 |
String line = allItems.get(ridx).line().trim();
|
|
305 |
ListIterator<Entry> iterator = allItems.listIterator(ridx);
|
|
306 |
while (iterator.hasPrevious()) {
|
|
307 |
String l = iterator.previous().line();
|
|
308 |
if (line.equals(l.trim())) {
|
|
309 |
iterator.remove();
|
|
310 |
}
|
|
311 |
}
|
|
312 |
idx++;
|
|
313 |
}
|
|
314 |
while (allItems.size() > max) {
|
|
315 |
allItems.remove(0);
|
|
316 |
}
|
|
317 |
}
|
|
318 |
|
|
319 |
public int size() {
|
|
320 |
return items.size();
|
|
321 |
}
|
|
322 |
|
|
323 |
public boolean isEmpty() {
|
|
324 |
return items.isEmpty();
|
|
325 |
}
|
|
326 |
|
|
327 |
public int index() {
|
|
328 |
return offset + index;
|
|
329 |
}
|
|
330 |
|
|
331 |
public int first() {
|
|
332 |
return offset;
|
|
333 |
}
|
|
334 |
|
|
335 |
public int last() {
|
|
336 |
return offset + items.size() - 1;
|
|
337 |
}
|
|
338 |
|
|
339 |
private String format(Entry entry) {
|
|
340 |
if (reader.isSet(LineReader.Option.HISTORY_TIMESTAMPED)) {
|
|
341 |
return Long.toString(entry.time().toEpochMilli()) + ":" + escape(entry.line()) + "\n";
|
|
342 |
}
|
|
343 |
return escape(entry.line()) + "\n";
|
|
344 |
}
|
|
345 |
|
|
346 |
public String get(final int index) {
|
|
347 |
return items.get(index - offset).line();
|
|
348 |
}
|
|
349 |
|
|
350 |
@Override
|
|
351 |
public void add(Instant time, String line) {
|
|
352 |
Objects.requireNonNull(time);
|
|
353 |
Objects.requireNonNull(line);
|
|
354 |
|
|
355 |
if (getBoolean(reader, LineReader.DISABLE_HISTORY, false)) {
|
|
356 |
return;
|
|
357 |
}
|
|
358 |
if (isSet(reader, LineReader.Option.HISTORY_IGNORE_SPACE) && line.startsWith(" ")) {
|
|
359 |
return;
|
|
360 |
}
|
|
361 |
if (isSet(reader, LineReader.Option.HISTORY_REDUCE_BLANKS)) {
|
|
362 |
line = line.trim();
|
|
363 |
}
|
|
364 |
if (isSet(reader, LineReader.Option.HISTORY_IGNORE_DUPS)) {
|
|
365 |
if (!items.isEmpty() && line.equals(items.getLast().line())) {
|
|
366 |
return;
|
|
367 |
}
|
|
368 |
}
|
|
369 |
if (matchPatterns(getString(reader, HISTORY_IGNORE, ""), line)) {
|
|
370 |
return;
|
|
371 |
}
|
|
372 |
internalAdd(time, line);
|
|
373 |
if (isSet(reader, LineReader.Option.HISTORY_INCREMENTAL)) {
|
|
374 |
try {
|
|
375 |
save();
|
|
376 |
}
|
|
377 |
catch (IOException e) {
|
|
378 |
Log.warn("Failed to save history", e);
|
|
379 |
}
|
|
380 |
}
|
|
381 |
}
|
|
382 |
|
|
383 |
protected boolean matchPatterns(String patterns, String line) {
|
|
384 |
if (patterns == null || patterns.isEmpty()) {
|
|
385 |
return false;
|
|
386 |
}
|
|
387 |
StringBuilder sb = new StringBuilder();
|
|
388 |
for (int i = 0; i < patterns.length(); i++) {
|
|
389 |
char ch = patterns.charAt(i);
|
|
390 |
if (ch == '\\') {
|
|
391 |
ch = patterns.charAt(++i);
|
|
392 |
sb.append(ch);
|
|
393 |
} else if (ch == ':') {
|
|
394 |
sb.append('|');
|
|
395 |
} else if (ch == '*') {
|
|
396 |
sb.append('.').append('*');
|
|
397 |
}
|
|
398 |
}
|
|
399 |
return line.matches(sb.toString());
|
|
400 |
}
|
|
401 |
|
|
402 |
protected void internalAdd(Instant time, String line) {
|
58903
|
403 |
internalAdd(time, line, false);
|
|
404 |
}
|
|
405 |
|
|
406 |
protected void internalAdd(Instant time, String line, boolean checkDuplicates) {
|
52938
|
407 |
Entry entry = new EntryImpl(offset + items.size(), time, line);
|
58903
|
408 |
if (checkDuplicates) {
|
|
409 |
for (Entry e: items) {
|
|
410 |
if (e.line().trim().equals(line.trim())) {
|
|
411 |
return;
|
|
412 |
}
|
|
413 |
}
|
|
414 |
}
|
52938
|
415 |
items.add(entry);
|
|
416 |
maybeResize();
|
|
417 |
}
|
|
418 |
|
|
419 |
private void maybeResize() {
|
|
420 |
while (size() > getInt(reader, LineReader.HISTORY_SIZE, DEFAULT_HISTORY_SIZE)) {
|
|
421 |
items.removeFirst();
|
58903
|
422 |
for (HistoryFileData hfd: historyFiles.values()) {
|
|
423 |
hfd.decLastLoaded();
|
|
424 |
}
|
52938
|
425 |
offset++;
|
|
426 |
}
|
|
427 |
index = size();
|
|
428 |
}
|
|
429 |
|
|
430 |
public ListIterator<Entry> iterator(int index) {
|
|
431 |
return items.listIterator(index - offset);
|
|
432 |
}
|
|
433 |
|
|
434 |
@Override
|
|
435 |
public Spliterator<Entry> spliterator() {
|
|
436 |
return items.spliterator();
|
|
437 |
}
|
|
438 |
|
|
439 |
protected static class EntryImpl implements Entry {
|
|
440 |
|
|
441 |
private final int index;
|
|
442 |
private final Instant time;
|
|
443 |
private final String line;
|
|
444 |
|
|
445 |
public EntryImpl(int index, Instant time, String line) {
|
|
446 |
this.index = index;
|
|
447 |
this.time = time;
|
|
448 |
this.line = line;
|
|
449 |
}
|
|
450 |
|
|
451 |
public int index() {
|
|
452 |
return index;
|
|
453 |
}
|
|
454 |
|
|
455 |
public Instant time() {
|
|
456 |
return time;
|
|
457 |
}
|
|
458 |
|
|
459 |
public String line() {
|
|
460 |
return line;
|
|
461 |
}
|
|
462 |
|
|
463 |
@Override
|
|
464 |
public String toString() {
|
|
465 |
return String.format("%d: %s", index, line);
|
|
466 |
}
|
|
467 |
}
|
|
468 |
|
|
469 |
//
|
|
470 |
// Navigation
|
|
471 |
//
|
|
472 |
|
|
473 |
/**
|
|
474 |
* This moves the history to the last entry. This entry is one position
|
|
475 |
* before the moveToEnd() position.
|
|
476 |
*
|
|
477 |
* @return Returns false if there were no history iterator or the history
|
|
478 |
* index was already at the last entry.
|
|
479 |
*/
|
|
480 |
public boolean moveToLast() {
|
|
481 |
int lastEntry = size() - 1;
|
|
482 |
if (lastEntry >= 0 && lastEntry != index) {
|
|
483 |
index = size() - 1;
|
|
484 |
return true;
|
|
485 |
}
|
|
486 |
|
|
487 |
return false;
|
|
488 |
}
|
|
489 |
|
|
490 |
/**
|
|
491 |
* Move to the specified index in the history
|
|
492 |
*/
|
|
493 |
public boolean moveTo(int index) {
|
|
494 |
index -= offset;
|
|
495 |
if (index >= 0 && index < size()) {
|
|
496 |
this.index = index;
|
|
497 |
return true;
|
|
498 |
}
|
|
499 |
return false;
|
|
500 |
}
|
|
501 |
|
|
502 |
/**
|
|
503 |
* Moves the history index to the first entry.
|
|
504 |
*
|
|
505 |
* @return Return false if there are no iterator in the history or if the
|
|
506 |
* history is already at the beginning.
|
|
507 |
*/
|
|
508 |
public boolean moveToFirst() {
|
|
509 |
if (size() > 0 && index != 0) {
|
|
510 |
index = 0;
|
|
511 |
return true;
|
|
512 |
}
|
|
513 |
return false;
|
|
514 |
}
|
|
515 |
|
|
516 |
/**
|
|
517 |
* Move to the end of the history buffer. This will be a blank entry, after
|
|
518 |
* all of the other iterator.
|
|
519 |
*/
|
|
520 |
public void moveToEnd() {
|
|
521 |
index = size();
|
|
522 |
}
|
|
523 |
|
|
524 |
/**
|
|
525 |
* Return the content of the current buffer.
|
|
526 |
*/
|
|
527 |
public String current() {
|
|
528 |
if (index >= size()) {
|
|
529 |
return "";
|
|
530 |
}
|
|
531 |
return items.get(index).line();
|
|
532 |
}
|
|
533 |
|
|
534 |
/**
|
|
535 |
* Move the pointer to the previous element in the buffer.
|
|
536 |
*
|
|
537 |
* @return true if we successfully went to the previous element
|
|
538 |
*/
|
|
539 |
public boolean previous() {
|
|
540 |
if (index <= 0) {
|
|
541 |
return false;
|
|
542 |
}
|
|
543 |
index--;
|
|
544 |
return true;
|
|
545 |
}
|
|
546 |
|
|
547 |
/**
|
|
548 |
* Move the pointer to the next element in the buffer.
|
|
549 |
*
|
|
550 |
* @return true if we successfully went to the next element
|
|
551 |
*/
|
|
552 |
public boolean next() {
|
|
553 |
if (index >= size()) {
|
|
554 |
return false;
|
|
555 |
}
|
|
556 |
index++;
|
|
557 |
return true;
|
|
558 |
}
|
|
559 |
|
|
560 |
@Override
|
|
561 |
public String toString() {
|
|
562 |
StringBuilder sb = new StringBuilder();
|
|
563 |
for (Entry e : this) {
|
|
564 |
sb.append(e.toString()).append("\n");
|
|
565 |
}
|
|
566 |
return sb.toString();
|
|
567 |
}
|
|
568 |
|
|
569 |
private static String escape(String s) {
|
|
570 |
StringBuilder sb = new StringBuilder();
|
|
571 |
for (int i = 0; i < s.length(); i++) {
|
|
572 |
char ch = s.charAt(i);
|
|
573 |
switch (ch) {
|
|
574 |
case '\n':
|
|
575 |
sb.append('\\');
|
|
576 |
sb.append('n');
|
|
577 |
break;
|
|
578 |
case '\r':
|
|
579 |
sb.append('\\');
|
|
580 |
sb.append('r');
|
|
581 |
break;
|
|
582 |
case '\\':
|
|
583 |
sb.append('\\');
|
|
584 |
sb.append('\\');
|
|
585 |
break;
|
|
586 |
default:
|
|
587 |
sb.append(ch);
|
|
588 |
break;
|
|
589 |
}
|
|
590 |
}
|
|
591 |
return sb.toString();
|
|
592 |
}
|
|
593 |
|
|
594 |
static String unescape(String s) {
|
|
595 |
StringBuilder sb = new StringBuilder();
|
|
596 |
for (int i = 0; i < s.length(); i++) {
|
|
597 |
char ch = s.charAt(i);
|
|
598 |
switch (ch) {
|
|
599 |
case '\\':
|
|
600 |
ch = s.charAt(++i);
|
|
601 |
if (ch == 'n') {
|
|
602 |
sb.append('\n');
|
|
603 |
} else if (ch == 'r') {
|
|
604 |
sb.append('\r');
|
|
605 |
} else {
|
|
606 |
sb.append(ch);
|
|
607 |
}
|
|
608 |
break;
|
|
609 |
default:
|
|
610 |
sb.append(ch);
|
|
611 |
break;
|
|
612 |
}
|
|
613 |
}
|
|
614 |
return sb.toString();
|
|
615 |
}
|
|
616 |
|
58903
|
617 |
private class HistoryFileData {
|
|
618 |
private int lastLoaded = 0;
|
|
619 |
private int entriesInFile = 0;
|
|
620 |
|
|
621 |
public HistoryFileData() {
|
|
622 |
}
|
|
623 |
|
|
624 |
public HistoryFileData(int lastLoaded, int entriesInFile) {
|
|
625 |
this.lastLoaded = lastLoaded;
|
|
626 |
this.entriesInFile = entriesInFile;
|
|
627 |
}
|
|
628 |
|
|
629 |
public int getLastLoaded() {
|
|
630 |
return lastLoaded;
|
|
631 |
}
|
|
632 |
|
|
633 |
public void setLastLoaded(int lastLoaded) {
|
|
634 |
this.lastLoaded = lastLoaded;
|
|
635 |
}
|
|
636 |
|
|
637 |
public void decLastLoaded() {
|
|
638 |
lastLoaded = lastLoaded - 1;
|
|
639 |
if (lastLoaded < 0) {
|
|
640 |
lastLoaded = 0;
|
|
641 |
}
|
|
642 |
}
|
|
643 |
|
|
644 |
public int getEntriesInFile() {
|
|
645 |
return entriesInFile;
|
|
646 |
}
|
|
647 |
|
|
648 |
public void setEntriesInFile(int entriesInFile) {
|
|
649 |
this.entriesInFile = entriesInFile;
|
|
650 |
}
|
|
651 |
|
|
652 |
public void incEntriesInFile(int amount) {
|
|
653 |
entriesInFile = entriesInFile + amount;
|
|
654 |
}
|
|
655 |
|
|
656 |
}
|
|
657 |
|
52938
|
658 |
}
|
|
659 |
|