-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.cpp
62 lines (52 loc) · 1.2 KB
/
query.cpp
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
//@ {"targets":[{"name":"temple_query","type":"application"}]}
#include "itemtree.hpp"
#include <cstdio>
#include <clocale>
struct Reader
{
FILE* handle;
};
bool read(Reader& reader,char& ch)
{
auto ch_in=getc(reader.handle);
if(ch_in==-1)
{return 0;}
ch=ch_in;
return 1;
}
const char* name(Reader& reader) noexcept
{return "stdin";}
int main(int argc,char** argv)
{
setlocale(LC_ALL,"");
try
{
if(argc<2)
{throw Temple::Error("Path expression missing.");}
auto eh=[](const Temple::Error& err) [[noreturn]]
{throw err;};
Temple::ItemTree<> tree(Reader{stdin},eh);
if(tree.empty())
{throw Temple::Error("Item tree is empty.");}
auto& item=Temple::find(eh,tree.root(),argv + 1);
if(arrayUnset(item.type())==Temple::Type::COMPOUND)
{
Temple::temple_store(item,stdout);
}
else
{
Temple::for_type<decltype(tree)::StorageModel,Temple::Type::I8,1,Temple::Type::STRING_ARRAY>
(item.type(),[&item,&tree](auto tag)
{
using type=typename decltype(tag)::type;
Temple::write<decltype(tree)::StorageModel>(item.value<type>(),stdout);
});
}
}
catch(const Temple::Error& error)
{
fprintf(stderr,"%s\n",error.message());
return -1;
}
return 0;
}