summaryrefslogtreecommitdiff
path: root/support/dktools/dk4dbia.ctr
blob: 056ff0bb8f0b48d6c4efac02aad578c7260ec49c (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2015-xxxx
license		=	bsd



%%	header

#ifndef DK4DBI_H_INCLUDED
#include "dk4dbi.h"
#endif

#ifndef DK4APP_H_INCLUDED
#include "dk4app.h"
#endif

/**	Open database.
	@param	fn	Database type and file name.
			The database type is one from "mem", "bdb" or "ndbm".
			Database type and file name are separated by two
			colons "::".
	@param	wa	Flag: Write access required.
	@param	tr	Flag: Truncate existing database (only in conjunction
			with wa).
	@param	erp	Error report, may be NULL.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Pointer to new database structure on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if fn is NULL or tp contains an invalid type number,
	- DK4_E_NOT_SUPPORTED<br>
	  if the database type is not supported,
	- DK4_E_MATH_OVERFLOW<br>
	  on numeric overflow when calculating size,
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  with mem.elsize and mem.nelem
	  set if there is not enough memory available,
	- DK4_E_SYNTAX<br>
	  if
	  - the specification contains an invalid backend type
	  - the database file name does not refer to a regular file
	  - or the file contents is not an in-memory database
	  - or key/value sizes in the file are in conflict with
	    size restrictions
	  - or an invalid database type was specified,
	- DK4_E_SEC_CHECK<br>
	  with failed check id in iDetails1 if access is denied by additional
	  security checks,
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  with mem.elsize and mem.nelem
	  set if there is not enough memory available,
	- DK4_E_OPEN_READ_FAILED<br>
	  if fopen() or fread() failed,
	- DK4_E_OPEN_WRITE_FAILED<br>
	  if fopen() or fwrite() failed,
	- DK4_E_CLOSE_FAILED<br>
	  with errno value in iDetails1 if fclose() failed.
*/
dk4_dbi_t *
dk4dbi_open_app(
  const dkChar	*fn,
  int		 wa,
  int		 tr,
  dk4_er_t	*erp,
  dk4_app_t	*app
);

/**	Open database.
	@param	fn	Database type and file name.
			The database type is one from "mem", "bdb" or "ndbm".
			Database type and file name are separated by two
			colons "::".
	@param	wa	Flag: Write access required.
	@param	tr	Flag: Truncate existing database (only in conjunction
			with wa).
	@param	km	Maximum key size.
	@param	vm	Maximum value size.
	@param	erp	Error report, may be NULL.
	@param	app	Application structure for diagnostics, may be NULL.
	@return	Pointer to new database structure on success, NULL on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if fn is NULL or tp contains an invalid type number,
	- DK4_E_NOT_SUPPORTED<br>
	  if the database type is not supported,
	- DK4_E_MATH_OVERFLOW<br>
	  on numeric overflow when calculating size,
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  with mem.elsize and mem.nelem
	  set if there is not enough memory available,
	- DK4_E_SYNTAX<br>
	  if
	  - the specification contains an invalid backend type
	  - the database file name does not refer to a regular file
	  - or the file contents is not an in-memory database
	  - or key/value sizes in the file are in conflict with
	    size restrictions
	  - or an invalid database type was specified,
	- DK4_E_SEC_CHECK<br>
	  with failed check id in iDetails1 if access is denied by additional
	  security checks,
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  with mem.elsize and mem.nelem
	  set if there is not enough memory available,
	- DK4_E_OPEN_READ_FAILED<br>
	  if fopen() or fread() failed,
	- DK4_E_OPEN_WRITE_FAILED<br>
	  if fopen() or fwrite() failed,
	- DK4_E_CLOSE_FAILED<br>
	  with errno value in iDetails1 if fclose() failed.
*/
dk4_dbi_t *
dk4dbi_open_sizes_app(
  const dkChar	*fn,
  int		 wa,
  int		 tr,
  size_t	 km,
  size_t	 vm,
  dk4_er_t	*erp,
  dk4_app_t	*app
);

%%	module



#include "dk4dbia.h"
#include "dk4const.h"


$!trace-include



/**	Preference names we are using here.
*/
static const dkChar * const	dk4dbi_pref_names[] = {
$!string-table	macro=dkT
db.key.max-size
db.value.max-size
$!end
};



/**	Constant texts used by the module, not localized.
*/
static const dkChar * const	dk4dbia_kwnl[] = {
$!string-table	macro=dkT
#
#	0	String table file name.
#
dk4dbia.str
$!end
};



/**	Constant texts used by the module, localized
	texts are used if available.
*/
static const dkChar * const	dk4dbia_kw_def[] = {
$!string-table	macro=dkT
#
#	0	Error: Failed to open database
#
Failed to open database!
#
#	1
#
\nInvalid arguments were provided to the DB opening function.
#
#	2
#
\nThe database type is not supported.
#
#	3
#
\nMathematical overflow in size calculation.
#
#	4
#
\nInsufficient memory available.
#
#	5
#
\nSyntax error (database name, file type or file contents).
#
#	6
#
\nInsufficient permissions.
#
#	7
#
\nFailed to open the file for read access.
#
#	8
#
\nFailed to open the file for write access.
#
#	9
#
\nFailed to close the file.
$!end
};



dk4_dbi_t *
dk4dbi_open_sizes_app(
  const dkChar	*fn,
  int		 wa,
  int		 tr,
  size_t	 km,
  size_t	 vm,
  dk4_er_t	*erp,
  dk4_app_t	*app
)
{
  dk4_er_t		 er;		/* Error report from dk4dbi_open */
  const dkChar *      	 parts[4];	/* Message parts array */
  const dkChar * const	*msg	= NULL;	/* Localized messages */
  dk4_dbi_t		*back	= NULL;	/* Result */
  size_t		 szpts	= 0;	/* Number of elements in parts */

  dk4error_init(&er);
  back = dk4dbi_open(fn, wa, tr, km, vm, &er);
  if (NULL == back) {
    dk4error_copy(erp, &er);
    if (NULL != app) {
      msg   = dk4app_string_table(app, dk4dbia_kwnl[0], dk4dbia_kw_def);
      parts[0] = msg[0];
      parts[1] = NULL;
      szpts = 1;
      switch (er.ec) {
        case DK4_E_INVALID_ARGUMENTS : {
	  parts[1] = msg[1];
	  parts[2] = NULL;
	  szpts = 2;
        } break;
        case DK4_E_NOT_SUPPORTED : {
	  parts[1] = msg[2];
	  parts[2] = NULL;
	  szpts = 2;
        } break;
        case DK4_E_MATH_OVERFLOW : {
	  parts[1] = msg[3];
	  parts[2] = NULL;
	  szpts = 2;
        } break;
        case DK4_E_MEMORY_ALLOCATION_FAILED : {
	  parts[1] = msg[4];
	  parts[2] = NULL;
	  szpts = 2;
        } break;
        case DK4_E_SYNTAX : {
	  parts[1] = msg[5];
	  parts[2] = NULL;
	  szpts = 2;
        } break;
        case DK4_E_SEC_CHECK : {
	  parts[1] = msg[6];
	  parts[2] = NULL;
	  szpts = 2;
        } break;
        case DK4_E_OPEN_READ_FAILED : {
	  parts[1] = msg[7];
	  parts[2] = NULL;
	  szpts = 2;
        } break;
        case DK4_E_OPEN_WRITE_FAILED : {
	  parts[1] = msg[8];
	  parts[2] = NULL;
	  szpts = 2;
        } break;
        case DK4_E_CLOSE_FAILED : {
	  parts[1] = msg[9];
	  parts[2] = NULL;
	  szpts = 2;
        } break;
      }
      if (0 < szpts) {
        dk4app_log_msg(app, DK4_LL_ERROR, parts, szpts);
      }
    }
  }
  return back;
}



dk4_dbi_t *
dk4dbi_open_app(
  const dkChar	*fn,
  int		 wa,
  int		 tr,
  dk4_er_t	*erp,
  dk4_app_t	*app
)
{
  size_t	 km	= 0;
  size_t	 vm	= 0;

  if (0 == dk4app_pref_get_size(&km, app, dk4dbi_pref_names[0], 0)) {
    km = 0;
  }
  if (0 == dk4app_pref_get_size(&vm, app, dk4dbi_pref_names[1], 0)) {
    vm = 0;
  }
  return (dk4dbi_open_sizes_app(fn, wa, tr, km, vm, erp, app));
}