This repository has been archived by the owner on Aug 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.cxx
204 lines (183 loc) · 7.03 KB
/
entry.cxx
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
/*******************************************************************************
* program: zsdatab-entry
* package: zsdatab
* SPDX-License-Identifier: LGPL-2.1-or-later
*******************************************************************************
* Copyright (C) 2021 zseri
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along
* with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*******************************************************************************/
#include "zsdatable.hpp"
#include <ctype.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <deque>
using namespace std;
static string my_tolower(string instr) {
transform(instr.begin(), instr.end(), instr.begin(), ::tolower);
return instr;
}
static unsigned int xsel_gmatcht(const string &mat) {
if(mat == "whole" || mat == "=") return 1;
if(mat == "part" || mat == "LIKE") return 2;
return 0;
}
int main(int argc, char *argv[]) {
if(argc < 2) {
cerr << "USAGE: zsdatab-entry [-z] TABLE [CMD ARGS... ]...\n"
"\n"
"Options:\n"
" -z TABLE is gzipped and packed instead of plain\n"
"\n"
"Commands:\n"
" select FIELD VALUE select all entries that match VALUE (deprecated)\n"
" xsel whole|part|LIKE|= FIELD VALUE select all entries that match VALUE (whole field or partial)\n"
" neg negate buffer\n"
" get FIELD get field FIELD and exit\n"
"\n"
" ch FIELD NEWVALUE change FIELD to NEWVALUE\n"
" rmpart FIELD SUBSTRING remove FIELD-part SUBSTRING\n"
" appart FIELD SUBSTRING append FIELD-part SUBSTRING\n"
"\n"
" new COLUMNS... create new entry\n"
" rm remove selected entries (= negate push)\n"
" rmexcept remove everything except selected entries (= push)\n"
"\n"
"Other Commands:\n"
" quit quit without printing buffer\n"
" push update table file\n"
"\n"
"Commands can be joined\n\n"
"zsdatab v0.3.1 by zseri <[email protected]>\n"
"released under LGPL-2.1-or-later\n";
return 1;
} else if(argc == 2 && string(argv[1]) != "-z") {
string tmp;
ifstream in(argv[1]);
if(!in) {
cerr << "zsdatab-entry: ERROR: " << argv[1] << ": file not found\n";
return 1;
}
while(getline(in, tmp)) cout << tmp << "\n";
return 0;
}
const bool is_gzipped = (string(argv[1]) == "-z");
zsdatab::table my_table = is_gzipped ? zsdatab::make_gzipped_table(argv[2]) : zsdatab::table(argv[1]);
if(!my_table.good()) {
cerr << "zsdatab-entry: ERROR: " << argv[1] << ": file not found / read failed\n";
return 1;
}
const size_t colcnt = my_table.get_metadata().get_field_count();
zsdatab::context my_ctx(my_table);
deque<string> commands(argv + 2 + (is_gzipped ? 1 : 0), argv + argc);
string cmd, field;
// parse commands
try {
while(!commands.empty()) {
cmd = my_tolower(commands.front());
commands.pop_front();
string selector;
// check args
{
bool args_ok = true;
if(cmd == "ch" || cmd == "select" || cmd == "appart" || cmd == "rmpart") {
if(commands.size() < 2 || commands.front().empty()) args_ok = false;
else field = commands[0];
commands.pop_front();
} else if(cmd == "xsel") {
if(commands.size() < 3 || commands[0].empty() || commands[1].empty()) args_ok = false;
else if(!xsel_gmatcht(commands[0])) args_ok = false;
else field = commands[1];
selector = commands[0];
commands.pop_front();
commands.pop_front();
} else if(cmd == "new") {
if(commands.size() < colcnt) args_ok = false;
} else if(cmd == "get") {
if(commands.empty() || commands.front().empty()) args_ok = false;
else field = commands[0];
commands.pop_front();
} else if(cmd == "rm" || cmd == "rmexcept" || cmd == "neg" || cmd == "quit" || cmd == "push") {
// do nothing
} else {
cerr << "zsdatab-entry: ERROR: unknown command '" << cmd << "'\n";
return 1;
}
if(!args_ok) {
cerr << "zsdatab-entry: ERROR: command " << cmd << ": invalid args\n";
return 1;
}
}
// command execution
if(cmd == "ch") {
zsdatab::context tmp_ctx = my_ctx;
tmp_ctx.negate();
my_ctx.set_field(field, commands[0]);
commands.pop_front();
tmp_ctx += my_ctx;
tmp_ctx.push();
} else if(cmd == "appart") {
zsdatab::context tmp_ctx = my_ctx;
tmp_ctx.negate();
my_ctx.append_part(field, commands[0]);
commands.pop_front();
tmp_ctx += my_ctx;
tmp_ctx.push();
} else if(cmd == "rmpart") {
zsdatab::context tmp_ctx = my_ctx;
tmp_ctx.negate();
my_ctx.remove_part(field, commands[0]);
commands.pop_front();
tmp_ctx += my_ctx;
tmp_ctx.push();
} else if(cmd == "select") {
my_ctx.filter(field, commands[0]);
commands.pop_front();
} else if(cmd == "xsel") {
my_ctx.filter(field, commands[0], xsel_gmatcht(selector) == 1);
commands.pop_front();
} else if(cmd == "new") {
const auto cbi = commands.begin();
const auto cei = cbi + colcnt;
const vector<string> line(cbi, cei);
commands.erase(cbi, cei);
my_ctx.pull();
my_ctx += line;
my_ctx.push();
} else if(cmd == "get") {
for(auto &&l : my_ctx.get_column_data(field))
cout << l << '\n';
return 0;
} else if(cmd == "rm") {
my_ctx.negate();
my_ctx.push();
} else if(cmd == "rmexcept")
my_ctx.push();
else if(cmd == "neg")
my_ctx.negate();
else if(cmd == "push")
my_ctx.push();
else if(cmd == "quit")
return 0;
}
} catch(const out_of_range&e) {
cerr << "zsdatab-entry: ERROR: command " << cmd << ": unknown fieldname '" << field << "'\n";
return 1;
}
// print buffer
cout << my_ctx;
return 0;
}