libs: introduce lmo - Lua Machine Objects, an implementation of binary hash tables
[project/luci.git] / libs / lmo / src / lmo_lookup.c
1 /*
2  * lmo - Lua Machine Objects - Lookup utility
3  *
4  *   Copyright (C) 2009 Jo-Philipp Wich <xm@subsignal.org>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18
19 #include "lmo.h"
20
21 extern char _lmo_error[1024];
22
23 static void die(const char *msg)
24 {
25         printf("Error: %s\n", msg);
26         exit(1);
27 }
28
29 static void usage(const char *name)
30 {
31         printf("Usage: %s input.lmo key\n", name);
32         exit(1);
33 }
34
35 int main(int argc, char *argv[])
36 {
37         char val[4096];
38         lmo_archive_t *ar = NULL;
39
40         if( argc != 3 )
41                 usage(argv[0]);
42
43         if( (ar = (lmo_archive_t *) lmo_open(argv[1])) != NULL )
44         {
45                 if( lmo_lookup(ar, argv[2], val, sizeof(val)) > -1 )
46                 {
47                         printf("%s\n", val);
48                 }
49
50                 lmo_close(ar);
51         }
52         else
53         {
54                 die(lmo_error());
55         }
56
57         return 0;
58 }