1 /**
2  * @file
3  * Shared store code
4  *
5  * @authors
6  * Copyright (C) 2020 Richard Russon <rich@flatcap.org>
7  *
8  * @copyright
9  * This program is free software: you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License as published by the Free Software
11  * Foundation, either version 2 of the License, or (at your option) any later
12  * version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 /**
24  * @page store_store Shared store code
25  *
26  * Shared store code
27  */
28 
29 #include "config.h"
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include "mutt/lib.h"
34 #include "lib.h"
35 
36 #define STORE_BACKEND(name) extern const struct StoreOps store_##name##_ops;
37 STORE_BACKEND(bdb)
38 STORE_BACKEND(gdbm)
39 STORE_BACKEND(kyotocabinet)
40 STORE_BACKEND(lmdb)
41 STORE_BACKEND(qdbm)
42 STORE_BACKEND(rocksdb)
43 STORE_BACKEND(tdb)
44 STORE_BACKEND(tokyocabinet)
45 #undef STORE_BACKEND
46 
47 /**
48  * StoreOps - Backend implementations
49  */
50 static const struct StoreOps *StoreOps[] = {
51 #ifdef HAVE_TC
52   &store_tokyocabinet_ops,
53 #endif
54 #ifdef HAVE_KC
55   &store_kyotocabinet_ops,
56 #endif
57 #ifdef HAVE_QDBM
58   &store_qdbm_ops,
59 #endif
60 #ifdef HAVE_ROCKSDB
61   &store_rocksdb_ops,
62 #endif
63 #ifdef HAVE_GDBM
64   &store_gdbm_ops,
65 #endif
66 #ifdef HAVE_BDB
67   &store_bdb_ops,
68 #endif
69 #ifdef HAVE_TDB
70   &store_tdb_ops,
71 #endif
72 #ifdef HAVE_LMDB
73   &store_lmdb_ops,
74 #endif
75   NULL,
76 };
77 
78 /**
79  * store_backend_list - Get a list of backend names
80  * @retval ptr Comma-space-separated list of names
81  *
82  * The caller should free the string.
83  */
store_backend_list(void)84 const char *store_backend_list(void)
85 {
86   char tmp[256] = { 0 };
87   const struct StoreOps **ops = StoreOps;
88   size_t len = 0;
89 
90   for (; *ops; ops++)
91   {
92     if (len != 0)
93     {
94       len += snprintf(tmp + len, sizeof(tmp) - len, ", ");
95     }
96     len += snprintf(tmp + len, sizeof(tmp) - len, "%s", (*ops)->name);
97   }
98 
99   return mutt_str_dup(tmp);
100 }
101 
102 /**
103  * store_get_backend_ops - Get the API functions for an store backend
104  * @param str Name of the Store
105  * @retval ptr Set of function pointers
106  */
store_get_backend_ops(const char * str)107 const struct StoreOps *store_get_backend_ops(const char *str)
108 {
109   const struct StoreOps **ops = StoreOps;
110 
111   if (!str || (*str == '\0'))
112   {
113     return *ops;
114   }
115 
116   for (; *ops; ops++)
117     if (strcmp(str, (*ops)->name) == 0)
118       break;
119 
120   return *ops;
121 }
122 
123 /**
124  * store_is_valid_backend - Is the string a valid Store backend
125  * @param str Store name
126  * @retval true  s is recognized as a valid backend
127  * @retval false otherwise
128  */
store_is_valid_backend(const char * str)129 bool store_is_valid_backend(const char *str)
130 {
131   return store_get_backend_ops(str);
132 }
133