1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#include "Buffer.h"
8
#include "WalletConsole.h"
9
#include "Util.h"
10

11
#include "Data.h"
12

13
#include <iostream>
14
#include <vector>
15
#include <cassert>
16

17
namespace TW::WalletConsole {
18

19
using namespace std;
20

21 1
void Buffer::addResult(const string& val) {
22 1
    if (val.length() == 0) { return; }
23 1
    _last = SavedValue(val);
24 1
    _prev.push_back(SavedValue(val));
25
}
26

27 1
bool Buffer::prepareInput(const string& in, string& in_out) {
28 1
    string in2 = in;
29 1
    Util::trimLeft(in2);    
30 1
    if (in2.length() < 1) { in_out = in2; return true; }
31 1
    if (in2[0] != '#') {
32
        // no special handling, return as it is
33 1
        in_out = in2;
34 1
        return true;
35
    }
36 1
    if (in2 == "#") {
37
        // # represents last input
38 1
        in_out = _last.get();
39 1
        return true;
40
    }
41
    // assume #n
42
    try {
43 1
        int n = std::stoi(in2.substr(1));
44
        // of the form #n
45 1
        int idx = n - 1;
46 1
        if (idx < 0 || idx >= _prev.size()) {
47 1
            _out << "Requested " << in2 << ", but out of range of buffers (n=" << _prev.size() << ")." << endl;
48 1
            return false;
49
        }
50 1
        in_out = _prev[idx].get();
51 1
        return true;
52 1
    } catch (exception& ex) {
53 1
        _out << "Invalid input: " << in2 << endl;
54 1
        return false;
55
    }
56
}
57

58 1
void Buffer::buffer() const {
59 1
    _out << "Last value:  " << _last.get() << endl;
60 1
    _out << _prev.size() << " previous values:" << endl;
61 1
    for (int i = 0; i < _prev.size(); ++i) {
62 1
        _out << "  #" << i + 1 << "  " << _prev[i].get() << endl;
63
    }
64
}
65

66
} // namespace TW::WalletConsole

Read our documentation on viewing source code .

Loading