aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/apps/vespa-fileheader-inspect/vespa-fileheader-inspect.cpp
blob: eb5b4cba342a4b550cef313d21141f58c20926a9 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/util/signalhandler.h>
#include <vespa/fastos/file.h>
#include <iomanip>
#include <iostream>
#include <vespa/vespalib/data/fileheader.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <unistd.h>
#include <vespa/log/log.h>
LOG_SETUP("vespa-fileheader-inspect");

using namespace vespalib;

class Application {
private:
    vespalib::string _fileName;
    char        _delimiter;
    bool        _quiet;

    int parseOpts(int argc, char **argv);
    void usage(const char *self);
    void printQuiet(FileHeader &header);
    void printVerbose(FileHeader &header);
    vespalib::string escape(const vespalib::string &str, char quote = '\0');
    vespalib::string getTypeString(const FileHeader::Tag &tag);
    vespalib::string getValueString(const FileHeader::Tag &tag);

public:
    Application();
    ~Application();
    int main(int argc, char **argv);
};

Application::Application() :
    _fileName(""),
    _delimiter(';'),
    _quiet(false)
{ }

Application::~Application() {}


void
Application::usage(const char *self)
{
    printf("Tool for inspecting the headers of files used by Vespa.\n");
    printf("Usage: %s [options] filename\n", self);
    printf("\n");
    printf("The options are:\n");
    printf("-d delimiter   The delimiter to use to separate values in quiet output.\n");
    printf("-f file        The name of the file to inspect.\n");
    printf("-q             Enables machine readable output.\n");
    printf("-h             Shows this help page.\n");
}


int
Application::parseOpts(int argc, char **argv)
{
    int c = '?';
    while ((c = getopt(argc, argv, "d:f:qh")) != -1) {
        switch (c) {
        case 'd':
            _delimiter = optarg[0];
            break;
        case 'f':
            _fileName = optarg;
            break;
        case 'q':
            _quiet = true;
            break;
        case 'h':
            usage(argv[0]);
            return EXIT_SUCCESS;
        default:
            usage(argv[0]);
            return EXIT_FAILURE;
        }
    }
    if (argc == optind + 1) {
        _fileName = argv[optind];
    }
    if (_fileName.empty()) {
        std::cerr << "No filename given." << std::endl;
        return EXIT_FAILURE;
    }
    return ~(EXIT_SUCCESS | EXIT_FAILURE);
}

int
Application::main(int argc, char **argv)
{
    int ret = parseOpts(argc, argv);
    if (ret == EXIT_FAILURE || ret == EXIT_SUCCESS) {
        return ret;
    }

    FastOS_File file;
    if (!file.OpenReadOnly(_fileName.c_str())) {
        std::cerr << "Failed to open file '" << _fileName << "'." << std::endl;
        return EXIT_FAILURE;
    }

    FileHeader header;
    try {
        header.readFile(file);
    } catch (IllegalHeaderException &e) {
        std::cerr << e.getMessage() << std::endl;
        return EXIT_FAILURE;
    }

    if (_quiet) {
        printQuiet(header);
    } else {
        printVerbose(header);
    }
    return EXIT_SUCCESS;
}

void
Application::printQuiet(FileHeader &header)
{
    for (uint32_t i = 0, len = header.getNumTags(); i < len; ++i) {
        const FileHeader::Tag &tag = header.getTag(i);
        std::cout << escape(tag.getName(), _delimiter) << _delimiter
                  << escape(getTypeString(tag), _delimiter) << _delimiter
                  << escape(getValueString(tag), _delimiter) << std::endl;
    }
}

void
Application::printVerbose(FileHeader &header)
{
    uint32_t nameWidth = 3, typeWidth = 4, valueWidth = 5;
    for (uint32_t i = 0, len = header.getNumTags(); i < len; ++i) {
        const FileHeader::Tag &tag = header.getTag(i);
        nameWidth = std::max(nameWidth, (uint32_t)tag.getName().size());
        typeWidth = std::max(typeWidth, (uint32_t)getTypeString(tag).size());
        valueWidth = std::max(valueWidth, (uint32_t)getValueString(tag).size());
    }

    vespalib::asciistream line;
    line << "+" << std::string(nameWidth + 2, '-')
         << "+" << std::string(typeWidth + 2, '-')
         << "+" << std::string(valueWidth + 2, '-')
         << "+";

    std::cout << std::left << line.str() << std::endl;
    std::cout << "| " << std::setw(nameWidth) << "Tag" << " "
              << "| " << std::setw(typeWidth) << "Type" << " "
              << "| " << std::setw(valueWidth)<< "Value" << " "
              << "| " << std::endl;
    std::cout << line.str() << std::endl;
    for (uint32_t i = 0, len = header.getNumTags(); i < len; ++i) {
        const FileHeader::Tag &tag = header.getTag(i);
        std::cout << "| " << std::setw(nameWidth) << escape(tag.getName()) << " "
                  << "| " << std::setw(typeWidth) << getTypeString(tag) << " "
                  << "| " << std::setw(valueWidth) << escape(getValueString(tag)) << " "
                  << "| " << std::endl;
    }
    std::cout << line.str() << std::endl;
}

vespalib::string
Application::escape(const vespalib::string &str, char quote)
{
    vespalib::string ret = "";
    for (uint32_t i = 0, len = str.size(); i < len; ++i) {
        char c = str[i];
        switch (c) {
        case '\f':
            ret.append("\\f");
            break;
        case '\n':
            ret.append("\\n");
            break;
        case '\r':
            ret.append("\\r");
            break;
        case '\t':
            ret.append("\\t");
            break;
        default:
            if (c != '\0' && c == quote) {
                ret.append("\\");
            }
            ret.push_back(c);
        }
    }
    return ret;
}

vespalib::string
Application::getTypeString(const FileHeader::Tag &tag)
{
    switch (tag.getType()) {
    case FileHeader::Tag::TYPE_FLOAT:
        return "float";
    case FileHeader::Tag::TYPE_INTEGER:
        return "integer";
    case FileHeader::Tag::TYPE_STRING:
        return "string";
    default:
        LOG_ASSERT(tag.getType() == FileHeader::Tag::TYPE_INTEGER);
        LOG_ABORT("should not be reached");
    }
}

vespalib::string
Application::getValueString(const FileHeader::Tag &tag)
{
    vespalib::asciistream out;
    out << tag;
    return out.str();
}

int
main(int argc, char** argv)
{
    vespalib::SignalHandler::PIPE.ignore();
    Application app;
    return app.main(argc, argv);
}