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 "Util.h"
8

9
#include "Base64.h"
10
#include "HexCoding.h"
11
#include "Data.h"
12

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

18
namespace TW::WalletConsole {
19

20
using namespace std;
21
using namespace TW;
22

23 1
bool Util::hex(const string& p, string& res) {
24 1
    res = TW::hex(data(p));
25 1
    return true;
26
}
27

28 1
bool Util::base64Encode(const string& p, string& res) {
29 1
    _out << p << endl;
30 1
    Data data = parse_hex(p);
31 1
    if (data.size() == 0) {
32 1
        _out << "Error decoding input as hex" << endl;
33 1
        return false;
34
    }
35 1
    _out << TW::hex(data) << endl;
36 1
    res = Base64::encode(data);
37 1
    return true;
38
}
39

40 1
bool Util::base64Decode(const string& p, string& res) {
41
    try {
42 1
        auto dec = Base64::decode(p);
43 1
        res = TW::hex(dec);
44 1
        return true;
45 1
    } catch (exception& ex) {
46 1
        _out << "Error while Base64 decode" << endl;
47 1
        return false;
48
    }
49
}
50

51 1
bool Util::fileW(const string& fileName, const string& data, string& res) {
52 1
    if (fileExists(fileName)) {
53 1
        _out << "Warning: File '" << fileName << "' already exists, not overwriting to be safe." << endl;
54 1
        return false;
55
    }
56
    try {
57 1
        ofstream outfile(fileName,  std::ios::out | std::ios::binary);
58 1
        Data bindata = parse_hex(data);
59 1
        outfile.write((const char*)bindata.data(), bindata.size());
60 1
        outfile.close();
61 1
        _out << "Written to file '" << fileName << "', " << bindata.size() << " bytes." << endl;
62 1
    } catch (exception& ex) {
63 0
        _out << "Error writing to file '" << fileName << "': " << ex.what() << endl;
64
    }
65 1
    return false;
66
}
67

68 1
bool Util::fileR(const string& fileName, string& res) {
69 1
    if (!fileExists(fileName)) {
70 1
        _out << "Error: File not found '" << fileName << "'" << endl;
71 1
        return false;
72
    }
73
    try {
74 1
        ifstream infile(fileName,  std::ios::in | std::ios::binary);
75
        // get length of file:
76 1
        infile.seekg (0, infile.end);
77 1
        auto length = infile.tellg();
78 1
        infile.seekg (0, infile.beg);
79 1
        char* buffer = new char[length];
80 1
        if (!infile.read(buffer, length)) {
81 0
            _out << "Could not read file '" << fileName << "'" << endl;
82 0
            delete[] buffer;
83 0
            return false;
84
        }
85 1
        auto red = infile.gcount();
86 1
        infile.close();
87 1
        res = string(TW::hex(data((const byte*)buffer, red)));
88 1
        delete[] buffer;
89 1
        _out << "Read " << red << " bytes from file '" << fileName << "'." << endl;
90 1
        return true;
91 1
    } catch (exception& ex) {
92 0
        _out << "Error reading from file '" << fileName << "': " << ex.what() << endl;
93 0
        return false;
94
    }
95
}
96

97 1
vector<string> Util::tokenize(const string& line) {
98 1
    vector<string> tok;
99 1
    size_t n = line.length();
100 1
    size_t idx = 0;
101 1
    while (true) {
102 1
        auto spaceIdx = line.find(' ', idx);
103 1
        if (spaceIdx == string::npos || spaceIdx >= n) {
104
            // no more space
105 1
            if (idx < n) {
106 1
                tok.push_back(line.substr(idx, n - idx));
107
            }
108 1
            return tok;
109
        }
110
        // there is a space, take current word up to it
111 1
        if (idx < spaceIdx) {
112 1
            tok.push_back(line.substr(idx, spaceIdx - idx));
113
        }
114 1
        idx = spaceIdx + 1;
115
    }
116
}
117

118 1
void Util::trimLeft(std::string& s) {
119 1
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
120 1
        return !std::isspace(ch);
121
    }));
122
}
123

124 1
void Util::toLower(std::string& s) {
125 1
    std::transform(s.begin(), s.end(), s.begin(),
126 1
        [](unsigned char c){ return std::tolower(c); });
127
}
128

129 1
bool Util::fileExists(const std::string& fileName) {
130
    //return filesystem::exists(filesystem::path(fileName)); // not used due to compiler issues (on iOS)
131 1
    ifstream f(fileName.c_str());
132 1
    return f.good();
133
}
134

135
} // namespace TW::WalletConsole

Read our documentation on viewing source code .

Loading