ruby_odeum 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/COPYING +504 -0
- data/LICENSE +504 -0
- data/README +50 -0
- data/bin/odeum_mgr +106 -0
- data/doc/rdoc/classes/Odeum.html +235 -0
- data/doc/rdoc/classes/Odeum.src/M000010.html +25 -0
- data/doc/rdoc/classes/Odeum.src/M000011.html +22 -0
- data/doc/rdoc/classes/Odeum.src/M000012.html +27 -0
- data/doc/rdoc/classes/Odeum.src/M000013.html +27 -0
- data/doc/rdoc/classes/Odeum.src/M000014.html +28 -0
- data/doc/rdoc/classes/Odeum/Document.html +382 -0
- data/doc/rdoc/classes/Odeum/Document.src/M000040.html +25 -0
- data/doc/rdoc/classes/Odeum/Document.src/M000041.html +22 -0
- data/doc/rdoc/classes/Odeum/Document.src/M000042.html +23 -0
- data/doc/rdoc/classes/Odeum/Document.src/M000043.html +23 -0
- data/doc/rdoc/classes/Odeum/Document.src/M000044.html +24 -0
- data/doc/rdoc/classes/Odeum/Document.src/M000045.html +32 -0
- data/doc/rdoc/classes/Odeum/Document.src/M000046.html +22 -0
- data/doc/rdoc/classes/Odeum/Document.src/M000047.html +22 -0
- data/doc/rdoc/classes/Odeum/Document.src/M000048.html +22 -0
- data/doc/rdoc/classes/Odeum/Document.src/M000049.html +22 -0
- data/doc/rdoc/classes/Odeum/Document.src/M000050.html +24 -0
- data/doc/rdoc/classes/Odeum/Document.src/M000051.html +27 -0
- data/doc/rdoc/classes/Odeum/Index.html +662 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000015.html +46 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000016.html +33 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000017.html +35 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000018.html +23 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000019.html +22 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000020.html +22 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000021.html +22 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000022.html +22 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000023.html +22 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000024.html +29 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000025.html +23 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000026.html +24 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000027.html +23 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000028.html +26 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000029.html +24 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000030.html +20 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000031.html +22 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000032.html +22 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000033.html +22 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000034.html +22 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000035.html +20 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000036.html +20 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000037.html +22 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000038.html +22 -0
- data/doc/rdoc/classes/Odeum/Index.src/M000039.html +22 -0
- data/doc/rdoc/classes/OdeumTest.html +257 -0
- data/doc/rdoc/classes/OdeumTest.src/M000001.html +18 -0
- data/doc/rdoc/classes/OdeumTest.src/M000002.html +19 -0
- data/doc/rdoc/classes/OdeumTest.src/M000003.html +27 -0
- data/doc/rdoc/classes/OdeumTest.src/M000004.html +25 -0
- data/doc/rdoc/classes/OdeumTest.src/M000005.html +44 -0
- data/doc/rdoc/classes/OdeumTest.src/M000006.html +20 -0
- data/doc/rdoc/classes/OdeumTest.src/M000007.html +39 -0
- data/doc/rdoc/classes/OdeumTest.src/M000008.html +59 -0
- data/doc/rdoc/classes/OdeumTest.src/M000009.html +41 -0
- data/doc/rdoc/created.rid +1 -0
- data/doc/rdoc/files/COPYING.html +756 -0
- data/doc/rdoc/files/LICENSE.html +756 -0
- data/doc/rdoc/files/README.html +175 -0
- data/doc/rdoc/files/ext/odeum_index/odeum_index_c.html +101 -0
- data/doc/rdoc/files/test/test_odeum_rb.html +109 -0
- data/doc/rdoc/fr_class_index.html +30 -0
- data/doc/rdoc/fr_file_index.html +31 -0
- data/doc/rdoc/fr_method_index.html +77 -0
- data/doc/rdoc/index.html +24 -0
- data/doc/rdoc/rdoc-style.css +208 -0
- data/ext/odeum_index/cabin.c +2735 -0
- data/ext/odeum_index/cabin.h +1040 -0
- data/ext/odeum_index/curia.c +1114 -0
- data/ext/odeum_index/curia.h +430 -0
- data/ext/odeum_index/depot.c +1910 -0
- data/ext/odeum_index/depot.h +439 -0
- data/ext/odeum_index/extconf.rb +10 -0
- data/ext/odeum_index/myconf.c +668 -0
- data/ext/odeum_index/myconf.h +523 -0
- data/ext/odeum_index/odeum.c +1743 -0
- data/ext/odeum_index/odeum.h +541 -0
- data/ext/odeum_index/odeum_index.c +991 -0
- data/ext/odeum_index/villa.c +1923 -0
- data/ext/odeum_index/villa.h +470 -0
- data/ext/odeum_index/vista.c +159 -0
- data/ext/odeum_index/vista.h +111 -0
- data/test/test_odeum.rb +174 -0
- metadata +138 -0
@@ -0,0 +1,439 @@
|
|
1
|
+
/*************************************************************************************************
|
2
|
+
* The basic API of QDBM
|
3
|
+
* Copyright (C) 2000-2005 Mikio Hirabayashi
|
4
|
+
* This file is part of QDBM, Quick Database Manager.
|
5
|
+
* QDBM is free software; you can redistribute it and/or modify it under the terms of the GNU
|
6
|
+
* Lesser General Public License as published by the Free Software Foundation; either version
|
7
|
+
* 2.1 of the License or any later version. QDBM is distributed in the hope that it will be
|
8
|
+
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
9
|
+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
10
|
+
* details.
|
11
|
+
* You should have received a copy of the GNU Lesser General Public License along with QDBM; if
|
12
|
+
* not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
13
|
+
* 02111-1307 USA.
|
14
|
+
*************************************************************************************************/
|
15
|
+
|
16
|
+
|
17
|
+
#ifndef _DEPOT_H /* duplication check */
|
18
|
+
#define _DEPOT_H
|
19
|
+
|
20
|
+
#if defined(__cplusplus) /* export for C++ */
|
21
|
+
extern "C" {
|
22
|
+
#endif
|
23
|
+
|
24
|
+
|
25
|
+
#include <stdlib.h>
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
/*************************************************************************************************
|
30
|
+
* API
|
31
|
+
*************************************************************************************************/
|
32
|
+
|
33
|
+
|
34
|
+
typedef struct { /* type of structure for a database handle */
|
35
|
+
char *name; /* name of the database file */
|
36
|
+
int wmode; /* whether writable or not */
|
37
|
+
int inode; /* inode of the database file */
|
38
|
+
int mtime; /* last modified time of the database */
|
39
|
+
int fd; /* file descriptor of the database file */
|
40
|
+
int fsiz; /* size of the database file */
|
41
|
+
char *map; /* pointer to the mapped memory */
|
42
|
+
int msiz; /* size of the mapped memory */
|
43
|
+
int *buckets; /* pointer to the bucket array */
|
44
|
+
int bnum; /* number of the bucket array */
|
45
|
+
int rnum; /* number of records */
|
46
|
+
int fatal; /* whether a fatal error occured or not */
|
47
|
+
int ioff; /* offset of the iterator */
|
48
|
+
int mroff; /* offset of the last moved region */
|
49
|
+
int mrsiz; /* size of the last moved region */
|
50
|
+
int align; /* basic size of alignment */
|
51
|
+
} DEPOT;
|
52
|
+
|
53
|
+
enum { /* enumeration for error codes */
|
54
|
+
DP_ENOERR, /* no error */
|
55
|
+
DP_EFATAL, /* with fatal error */
|
56
|
+
DP_EMODE, /* invalid mode */
|
57
|
+
DP_EBROKEN, /* broken database file */
|
58
|
+
DP_EKEEP, /* existing record */
|
59
|
+
DP_ENOITEM, /* no item found */
|
60
|
+
DP_EALLOC, /* memory allocation error */
|
61
|
+
DP_EMAP, /* memory mapping error */
|
62
|
+
DP_EOPEN, /* open error */
|
63
|
+
DP_ECLOSE, /* close error */
|
64
|
+
DP_ETRUNC, /* trunc error */
|
65
|
+
DP_ESYNC, /* sync error */
|
66
|
+
DP_ESTAT, /* stat error */
|
67
|
+
DP_ESEEK, /* seek error */
|
68
|
+
DP_EREAD, /* read error */
|
69
|
+
DP_EWRITE, /* write error */
|
70
|
+
DP_ELOCK, /* lock error */
|
71
|
+
DP_EUNLINK, /* unlink error */
|
72
|
+
DP_EMKDIR, /* mkdir error */
|
73
|
+
DP_ERMDIR, /* rmdir error */
|
74
|
+
DP_EMISC /* miscellaneous error */
|
75
|
+
};
|
76
|
+
|
77
|
+
enum { /* enumeration for open modes */
|
78
|
+
DP_OREADER = 1 << 0, /* open as a reader */
|
79
|
+
DP_OWRITER = 1 << 1, /* open as a writer */
|
80
|
+
DP_OCREAT = 1 << 2, /* a writer creating */
|
81
|
+
DP_OTRUNC = 1 << 3, /* a writer truncating */
|
82
|
+
DP_ONOLCK = 1 << 4, /* open without locking */
|
83
|
+
DP_OSPARSE = 1 << 5 /* create as a sparse file */
|
84
|
+
};
|
85
|
+
|
86
|
+
enum { /* enumeration for write modes */
|
87
|
+
DP_DOVER, /* overwrite an existing value */
|
88
|
+
DP_DKEEP, /* keep an existing value */
|
89
|
+
DP_DCAT /* concatenate values */
|
90
|
+
};
|
91
|
+
|
92
|
+
|
93
|
+
/* String containing the version information. */
|
94
|
+
extern const char *dpversion;
|
95
|
+
|
96
|
+
|
97
|
+
/* Last happened error code. */
|
98
|
+
#define dpecode (*dpecodeptr())
|
99
|
+
|
100
|
+
|
101
|
+
/* Get a message string corresponding to an error code.
|
102
|
+
`ecode' specifies an error code.
|
103
|
+
The return value is the message string of the error code. The region of the return value
|
104
|
+
is not writable. */
|
105
|
+
const char *dperrmsg(int ecode);
|
106
|
+
|
107
|
+
|
108
|
+
/* Get a database handle.
|
109
|
+
`name' specifies the name of a database file.
|
110
|
+
`omode' specifies the connection mode: `DP_OWRITER' as a writer, `DP_OREADER' as a reader.
|
111
|
+
If the mode is `DP_OWRITER', the following may be added by bitwise or: `DP_OCREAT', which
|
112
|
+
means it creates a new database if not exist, `DP_OTRUNC', which means it creates a new
|
113
|
+
database regardless if one exists. Both of `DP_OREADER' and `DP_OWRITER' can be added to by
|
114
|
+
bitwise or: `DP_ONOLCK', which means it opens a database file without file locking.
|
115
|
+
`DP_OCREAT' can be added to by bitwise or: `DP_OSPARSE', which means it creates a database
|
116
|
+
file as a sparse file.
|
117
|
+
`bnum' specifies the number of elements of the bucket array. If it is not more than 0,
|
118
|
+
the default value is specified. The size of a bucket array is determined on creating,
|
119
|
+
and can not be changed except for by optimization of the database. Suggested size of a
|
120
|
+
bucket array is about from 0.5 to 4 times of the number of all records to store.
|
121
|
+
The return value is the database handle or `NULL' if it is not successful.
|
122
|
+
While connecting as a writer, an exclusive lock is invoked to the database file.
|
123
|
+
While connecting as a reader, a shared lock is invoked to the database file. The thread
|
124
|
+
blocks until the lock is achieved. If `DP_ONOLCK' is used, the application is responsible
|
125
|
+
for exclusion control. */
|
126
|
+
DEPOT *dpopen(const char *name, int omode, int bnum);
|
127
|
+
|
128
|
+
|
129
|
+
/* Close a database handle.
|
130
|
+
`depot' specifies a database handle.
|
131
|
+
If successful, the return value is true, else, it is false.
|
132
|
+
Because the region of a closed handle is released, it becomes impossible to use the handle.
|
133
|
+
Updating a database is assured to be written when the handle is closed. If a writer opens
|
134
|
+
a database but does not close it appropriately, the database will be broken. */
|
135
|
+
int dpclose(DEPOT *depot);
|
136
|
+
|
137
|
+
|
138
|
+
/* Store a record.
|
139
|
+
`depot' specifies a database handle connected as a writer.
|
140
|
+
`kbuf' specifies the pointer to the region of a key.
|
141
|
+
`ksiz' specifies the size of the region of the key. If it is negative, the size is assigned
|
142
|
+
with `strlen(kbuf)'.
|
143
|
+
`vbuf' specifies the pointer to the region of a value.
|
144
|
+
`vsiz' specifies the size of the region of the value. If it is negative, the size is
|
145
|
+
assigned with `strlen(vbuf)'.
|
146
|
+
`dmode' specifies behavior when the key overlaps, by the following values: `DP_DOVER',
|
147
|
+
which means the specified value overwrites the existing one, `DP_DKEEP', which means the
|
148
|
+
existing value is kept, `DP_DCAT', which means the specified value is concatenated at the
|
149
|
+
end of the existing value.
|
150
|
+
If successful, the return value is true, else, it is false. */
|
151
|
+
int dpput(DEPOT *depot, const char *kbuf, int ksiz, const char *vbuf, int vsiz, int dmode);
|
152
|
+
|
153
|
+
|
154
|
+
/* Delete a record.
|
155
|
+
`depot' specifies a database handle connected as a writer.
|
156
|
+
`kbuf' specifies the pointer to the region of a key.
|
157
|
+
`ksiz' specifies the size of the region of the key. If it is negative, the size is assigned
|
158
|
+
with `strlen(kbuf)'.
|
159
|
+
If successful, the return value is true, else, it is false. False is returned when no
|
160
|
+
record corresponds to the specified key. */
|
161
|
+
int dpout(DEPOT *depot, const char *kbuf, int ksiz);
|
162
|
+
|
163
|
+
|
164
|
+
/* Retrieve a record.
|
165
|
+
`depot' specifies a database handle.
|
166
|
+
`kbuf' specifies the pointer to the region of a key.
|
167
|
+
`ksiz' specifies the size of the region of the key. If it is negative, the size is assigned
|
168
|
+
with `strlen(kbuf)'.
|
169
|
+
`start' specifies the offset address of the beginning of the region of the value to be read.
|
170
|
+
`max' specifies the max size to be read. If it is negative, the size to read is unlimited.
|
171
|
+
`sp' specifies the pointer to a variable to which the size of the region of the return
|
172
|
+
value is assigned. If it is `NULL', it is not used.
|
173
|
+
If successful, the return value is the pointer to the region of the value of the
|
174
|
+
corresponding record, else, it is `NULL'. `NULL' is returned when no record corresponds to
|
175
|
+
the specified key or the size of the value of the corresponding record is less than `start'.
|
176
|
+
Because an additional zero code is appended at the end of the region of the return value,
|
177
|
+
the return value can be treated as a character string. Because the region of the return
|
178
|
+
value is allocated with the `malloc' call, it should be released with the `free' call if it
|
179
|
+
is no longer in use. */
|
180
|
+
char *dpget(DEPOT *depot, const char *kbuf, int ksiz, int start, int max, int *sp);
|
181
|
+
|
182
|
+
|
183
|
+
/* Retrieve a record and write the value into a buffer.
|
184
|
+
`depot' specifies a database handle.
|
185
|
+
`kbuf' specifies the pointer to the region of a key.
|
186
|
+
`ksiz' specifies the size of the region of the key. If it is negative, the size is assigned
|
187
|
+
with `strlen(kbuf)'.
|
188
|
+
`start' specifies the offset address of the beginning of the region of the value to be read.
|
189
|
+
`max' specifies the max size to be read. It shuld be equal to or less than the size of the
|
190
|
+
writing buffer.
|
191
|
+
`vbuf' specifies the pointer to a buffer into which the value of the corresponding record is
|
192
|
+
written.
|
193
|
+
If successful, the return value is the size of the written data, else, it is -1. -1 is
|
194
|
+
returned when no record corresponds to the specified key or the size of the value of the
|
195
|
+
corresponding record is less than `start'.
|
196
|
+
Note that no additional zero code is appended at the end of the region of the writing buffer. */
|
197
|
+
int dpgetwb(DEPOT *depot, const char *kbuf, int ksiz, int start, int max, char *vbuf);
|
198
|
+
|
199
|
+
|
200
|
+
/* Get the size of the value of a record.
|
201
|
+
`depot' specifies a database handle.
|
202
|
+
`kbuf' specifies the pointer to the region of a key.
|
203
|
+
`ksiz' specifies the size of the region of the key. If it is negative, the size is assigned
|
204
|
+
with `strlen(kbuf)'.
|
205
|
+
If successful, the return value is the size of the value of the corresponding record, else,
|
206
|
+
it is -1.
|
207
|
+
Because this function does not read the entity of a record, it is faster than `dpget'. */
|
208
|
+
int dpvsiz(DEPOT *depot, const char *kbuf, int ksiz);
|
209
|
+
|
210
|
+
|
211
|
+
/* Initialize the iterator of a database handle.
|
212
|
+
`depot' specifies a database handle.
|
213
|
+
If successful, the return value is true, else, it is false.
|
214
|
+
The iterator is used in order to access the key of every record stored in a database. */
|
215
|
+
int dpiterinit(DEPOT *depot);
|
216
|
+
|
217
|
+
|
218
|
+
/* Get the next key of the iterator.
|
219
|
+
`depot' specifies a database handle.
|
220
|
+
`sp' specifies the pointer to a variable to which the size of the region of the return
|
221
|
+
value is assigned. If it is `NULL', it is not used.
|
222
|
+
If successful, the return value is the pointer to the region of the next key, else, it is
|
223
|
+
`NULL'. `NULL' is returned when no record is to be get out of the iterator.
|
224
|
+
Because an additional zero code is appended at the end of the region of the return value,
|
225
|
+
the return value can be treated as a character string. Because the region of the return
|
226
|
+
value is allocated with the `malloc' call, it should be released with the `free' call if
|
227
|
+
it is no longer in use. It is possible to access every record by iteration of calling
|
228
|
+
this function. However, it is not assured if updating the database is occurred while the
|
229
|
+
iteration. Besides, the order of this traversal access method is arbitrary, so it is not
|
230
|
+
assured that the order of storing matches the one of the traversal access. */
|
231
|
+
char *dpiternext(DEPOT *depot, int *sp);
|
232
|
+
|
233
|
+
|
234
|
+
/* Set alignment of a database handle.
|
235
|
+
`depot' specifies a database handle connected as a writer.
|
236
|
+
`align' specifies the size of alignment.
|
237
|
+
If successful, the return value is true, else, it is false.
|
238
|
+
If alignment is set to a database, the efficiency of overwriting values are improved.
|
239
|
+
The size of alignment is suggested to be average size of the values of the records to be
|
240
|
+
stored. If alignment is positive, padding whose size is multiple number of the alignment
|
241
|
+
is placed. If alignment is negative, as `vsiz' is the size of a value, the size of padding
|
242
|
+
is calculated with `(vsiz / pow(2, abs(align) - 1))'. Because alignment setting is not
|
243
|
+
saved in a database, you should specify alignment every opening a database. */
|
244
|
+
int dpsetalign(DEPOT *depot, int align);
|
245
|
+
|
246
|
+
|
247
|
+
/* Synchronize updating contents with the file and the device.
|
248
|
+
`depot' specifies a database handle connected as a writer.
|
249
|
+
If successful, the return value is true, else, it is false.
|
250
|
+
This function is useful when another process uses the connected database file. */
|
251
|
+
int dpsync(DEPOT *depot);
|
252
|
+
|
253
|
+
|
254
|
+
/* Optimize a database.
|
255
|
+
`depot' specifies a database handle connected as a writer.
|
256
|
+
`bnum' specifies the number of the elements of the bucket array. If it is not more than 0,
|
257
|
+
the default value is specified.
|
258
|
+
If successful, the return value is true, else, it is false.
|
259
|
+
In an alternating succession of deleting and storing with overwrite or concatenate,
|
260
|
+
dispensable regions accumulate. This function is useful to do away with them. */
|
261
|
+
int dpoptimize(DEPOT *depot, int bnum);
|
262
|
+
|
263
|
+
|
264
|
+
/* Get the name of a database.
|
265
|
+
`depot' specifies a database handle.
|
266
|
+
If successful, the return value is the pointer to the region of the name of the database,
|
267
|
+
else, it is `NULL'.
|
268
|
+
Because the region of the return value is allocated with the `malloc' call, it should be
|
269
|
+
released with the `free' call if it is no longer in use. */
|
270
|
+
char *dpname(DEPOT *depot);
|
271
|
+
|
272
|
+
|
273
|
+
/* Get the size of a database file.
|
274
|
+
`depot' specifies a database handle.
|
275
|
+
If successful, the return value is the size of the database file, else, it is -1. */
|
276
|
+
int dpfsiz(DEPOT *depot);
|
277
|
+
|
278
|
+
|
279
|
+
/* Get the number of the elements of the bucket array.
|
280
|
+
`depot' specifies a database handle.
|
281
|
+
If successful, the return value is the number of the elements of the bucket array, else, it
|
282
|
+
is -1. */
|
283
|
+
int dpbnum(DEPOT *depot);
|
284
|
+
|
285
|
+
|
286
|
+
/* Get the number of the used elements of the bucket array.
|
287
|
+
`depot' specifies a database handle.
|
288
|
+
If successful, the return value is the number of the used elements of the bucket array,
|
289
|
+
else, it is -1.
|
290
|
+
This function is inefficient because it accesses all elements of the bucket array. */
|
291
|
+
int dpbusenum(DEPOT *depot);
|
292
|
+
|
293
|
+
|
294
|
+
/* Get the number of the records stored in a database.
|
295
|
+
`depot' specifies a database handle.
|
296
|
+
If successful, the return value is the number of the records stored in the database, else,
|
297
|
+
it is -1. */
|
298
|
+
int dprnum(DEPOT *depot);
|
299
|
+
|
300
|
+
|
301
|
+
/* Check whether a database handle is a writer or not.
|
302
|
+
`depot' specifies a database handle.
|
303
|
+
The return value is true if the handle is a writer, false if not. */
|
304
|
+
int dpwritable(DEPOT *depot);
|
305
|
+
|
306
|
+
|
307
|
+
/* Check whether a database has a fatal error or not.
|
308
|
+
`depot' specifies a database handle.
|
309
|
+
The return value is true if the database has a fatal error, false if not. */
|
310
|
+
int dpfatalerror(DEPOT *depot);
|
311
|
+
|
312
|
+
|
313
|
+
/* Get the inode number of a database file.
|
314
|
+
`depot' specifies a database handle.
|
315
|
+
The return value is the inode number of the database file. */
|
316
|
+
int dpinode(DEPOT *depot);
|
317
|
+
|
318
|
+
|
319
|
+
/* Get the last modified time of a database.
|
320
|
+
`depot' specifies a database handle.
|
321
|
+
The return value is the last modified time of the database. */
|
322
|
+
int dpmtime(DEPOT *depot);
|
323
|
+
|
324
|
+
|
325
|
+
/* Get the file descriptor of a database file.
|
326
|
+
`depot' specifies a database handle.
|
327
|
+
The return value is the file descriptor of the database file.
|
328
|
+
Handling the file descriptor of a database file directly is not suggested. */
|
329
|
+
int dpfdesc(DEPOT *depot);
|
330
|
+
|
331
|
+
|
332
|
+
/* Remove a database file.
|
333
|
+
`name' specifies the name of a database file.
|
334
|
+
If successful, the return value is true, else, it is false. */
|
335
|
+
int dpremove(const char *name);
|
336
|
+
|
337
|
+
|
338
|
+
/* Repair a broken database file.
|
339
|
+
`name' specifies the name of a database file.
|
340
|
+
If successful, the return value is true, else, it is false.
|
341
|
+
There is no guarantee that all records in a repaired database file correspond to the original
|
342
|
+
or expected state. */
|
343
|
+
int dprepair(const char *name);
|
344
|
+
|
345
|
+
|
346
|
+
/* Dump all records as endian independent data.
|
347
|
+
`depot' specifies a database handle.
|
348
|
+
`name' specifies the name of an output file.
|
349
|
+
If successful, the return value is true, else, it is false. */
|
350
|
+
int dpexportdb(DEPOT *depot, const char *name);
|
351
|
+
|
352
|
+
|
353
|
+
/* Load all records from endian independent data.
|
354
|
+
`depot' specifies a database handle connected as a writer. The database of the handle must
|
355
|
+
be empty.
|
356
|
+
`name' specifies the name of an input file.
|
357
|
+
If successful, the return value is true, else, it is false. */
|
358
|
+
int dpimportdb(DEPOT *depot, const char *name);
|
359
|
+
|
360
|
+
|
361
|
+
/* Hash function used inside Depot.
|
362
|
+
`kbuf' specifies the pointer to the region of a key.
|
363
|
+
`ksiz' specifies the size of the region of the key. If it is negative, the size is assigned
|
364
|
+
with `strlen(kbuf)'.
|
365
|
+
The return value is the hash value of 31 bits length computed from the key.
|
366
|
+
This function is useful when an application calculates the state of the inside bucket array. */
|
367
|
+
int dpinnerhash(const char *kbuf, int ksiz);
|
368
|
+
|
369
|
+
|
370
|
+
/* Hash function which is independent from the hash functions used inside Depot.
|
371
|
+
`kbuf' specifies the pointer to the region of a key.
|
372
|
+
`ksiz' specifies the size of the region of the key. If it is negative, the size is assigned
|
373
|
+
with `strlen(kbuf)'.
|
374
|
+
The return value is the hash value of 31 bits length computed from the key.
|
375
|
+
This function is useful when an application uses its own hash algorithm outside Depot. */
|
376
|
+
int dpouterhash(const char *kbuf, int ksiz);
|
377
|
+
|
378
|
+
|
379
|
+
/* Get a natural prime number not less than a number.
|
380
|
+
`num' specified a natural number.
|
381
|
+
The return value is a natural prime number not less than the specified number.
|
382
|
+
This function is useful when an application determines the size of a bucket array of its
|
383
|
+
own hash algorithm. */
|
384
|
+
int dpprimenum(int num);
|
385
|
+
|
386
|
+
|
387
|
+
|
388
|
+
/*************************************************************************************************
|
389
|
+
* features for experts
|
390
|
+
*************************************************************************************************/
|
391
|
+
|
392
|
+
|
393
|
+
/* Name of the operating system. */
|
394
|
+
extern char *dpsysname;
|
395
|
+
|
396
|
+
|
397
|
+
/* File descriptor for debugging output. */
|
398
|
+
extern int dpdbgfd;
|
399
|
+
|
400
|
+
|
401
|
+
/* Set the last happened error code.
|
402
|
+
`ecode' specifies the error code.
|
403
|
+
`line' specifies the number of the line where the error happened. */
|
404
|
+
void dpecodeset(int ecode, const char *file, int line);
|
405
|
+
|
406
|
+
|
407
|
+
/* Get the pointer of the variable of the last happened error code.
|
408
|
+
The return value is the pointer of the variable. */
|
409
|
+
int *dpecodeptr(void);
|
410
|
+
|
411
|
+
|
412
|
+
/* Synchronize updating contents on memory.
|
413
|
+
`depot' specifies a database handle connected as a writer.
|
414
|
+
If successful, the return value is true, else, it is false. */
|
415
|
+
int dpmemsync(DEPOT *depot);
|
416
|
+
|
417
|
+
|
418
|
+
/* Get flags of a database.
|
419
|
+
`depot' specifies a database handle.
|
420
|
+
The return value is the flags of a database. */
|
421
|
+
int dpgetflags(DEPOT *depot);
|
422
|
+
|
423
|
+
|
424
|
+
/* Set flags of a database.
|
425
|
+
`depot' specifies a database handle connected as a writer.
|
426
|
+
`flags' specifies flags to set.
|
427
|
+
If successful, the return value is true, else, it is false. */
|
428
|
+
int dpsetflags(DEPOT *depot, int flags);
|
429
|
+
|
430
|
+
|
431
|
+
|
432
|
+
#if defined(__cplusplus) /* export for C++ */
|
433
|
+
}
|
434
|
+
#endif
|
435
|
+
|
436
|
+
#endif /* duplication check */
|
437
|
+
|
438
|
+
|
439
|
+
/* END OF FILE */
|
@@ -0,0 +1,668 @@
|
|
1
|
+
/*************************************************************************************************
|
2
|
+
* Emulation of system calls
|
3
|
+
* Copyright (C) 2000-2005 Mikio Hirabayashi
|
4
|
+
* This file is part of QDBM, Quick Database Manager.
|
5
|
+
* QDBM is free software; you can redistribute it and/or modify it under the terms of the GNU
|
6
|
+
* Lesser General Public License as published by the Free Software Foundation; either version
|
7
|
+
* 2.1 of the License or any later version. QDBM is distributed in the hope that it will be
|
8
|
+
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
9
|
+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
|
10
|
+
* details.
|
11
|
+
* You should have received a copy of the GNU Lesser General Public License along with QDBM; if
|
12
|
+
* not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
13
|
+
* 02111-1307 USA.
|
14
|
+
*************************************************************************************************/
|
15
|
+
|
16
|
+
|
17
|
+
#include "myconf.h"
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
/*************************************************************************************************
|
22
|
+
* for POSIX thread
|
23
|
+
*************************************************************************************************/
|
24
|
+
|
25
|
+
|
26
|
+
#if defined(MYPTHREAD)
|
27
|
+
|
28
|
+
|
29
|
+
#include <pthread.h>
|
30
|
+
|
31
|
+
|
32
|
+
#define PTKEYMAX 8
|
33
|
+
|
34
|
+
|
35
|
+
static struct { void *ptr; pthread_key_t key; } _qdbm_ptkeys[PTKEYMAX];
|
36
|
+
static int _qdbm_ptknum = 0;
|
37
|
+
|
38
|
+
|
39
|
+
static void *_qdbm_gettsd(void *ptr, int size, const void *initval);
|
40
|
+
|
41
|
+
|
42
|
+
void *_qdbm_settsd(void *ptr, int size, const void *initval){
|
43
|
+
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
44
|
+
char *val;
|
45
|
+
if((val = _qdbm_gettsd(ptr, size, initval)) != NULL) return val;
|
46
|
+
if(pthread_mutex_lock(&mutex) != 0) return NULL;
|
47
|
+
if((val = _qdbm_gettsd(ptr, size, initval)) != NULL){
|
48
|
+
pthread_mutex_unlock(&mutex);
|
49
|
+
return val;
|
50
|
+
}
|
51
|
+
if(_qdbm_ptknum >= PTKEYMAX){
|
52
|
+
pthread_mutex_unlock(&mutex);
|
53
|
+
return NULL;
|
54
|
+
}
|
55
|
+
_qdbm_ptkeys[_qdbm_ptknum].ptr = ptr;
|
56
|
+
if(pthread_key_create(&(_qdbm_ptkeys[_qdbm_ptknum].key), free) != 0){
|
57
|
+
pthread_mutex_unlock(&mutex);
|
58
|
+
return NULL;
|
59
|
+
}
|
60
|
+
if(!(val = malloc(size))){
|
61
|
+
pthread_key_delete(_qdbm_ptkeys[_qdbm_ptknum].key);
|
62
|
+
pthread_mutex_unlock(&mutex);
|
63
|
+
return NULL;
|
64
|
+
}
|
65
|
+
memcpy(val, initval, size);
|
66
|
+
if(pthread_setspecific(_qdbm_ptkeys[_qdbm_ptknum].key, val) != 0){
|
67
|
+
free(val);
|
68
|
+
pthread_key_delete(_qdbm_ptkeys[_qdbm_ptknum].key);
|
69
|
+
pthread_mutex_unlock(&mutex);
|
70
|
+
return NULL;
|
71
|
+
}
|
72
|
+
_qdbm_ptknum++;
|
73
|
+
pthread_mutex_unlock(&mutex);
|
74
|
+
return val;
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
static void *_qdbm_gettsd(void *ptr, int size, const void *initval){
|
79
|
+
char *val;
|
80
|
+
int i;
|
81
|
+
for(i = 0; i < _qdbm_ptknum; i++){
|
82
|
+
if(_qdbm_ptkeys[i].ptr == ptr){
|
83
|
+
if(!(val = pthread_getspecific(_qdbm_ptkeys[i].key))){
|
84
|
+
if(!(val = malloc(size))) return NULL;
|
85
|
+
memcpy(val, initval, size);
|
86
|
+
if(pthread_setspecific(_qdbm_ptkeys[i].key, val) != 0){
|
87
|
+
free(val);
|
88
|
+
return NULL;
|
89
|
+
}
|
90
|
+
}
|
91
|
+
return val;
|
92
|
+
}
|
93
|
+
}
|
94
|
+
return NULL;
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
#endif
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
/*************************************************************************************************
|
103
|
+
* for systems without mmap
|
104
|
+
*************************************************************************************************/
|
105
|
+
|
106
|
+
|
107
|
+
#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_) || \
|
108
|
+
defined(_SYS_FREEBSD_) || defined(_SYS_NETBSD_) || defined(_SYS_OPENBSD_) || \
|
109
|
+
defined(_SYS_RISCOS_) || defined(MYNOMMAP)
|
110
|
+
|
111
|
+
|
112
|
+
void *_qdbm_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset){
|
113
|
+
char *buf, *wp;
|
114
|
+
int rv, rlen;
|
115
|
+
if(flags & MAP_FIXED) return MAP_FAILED;
|
116
|
+
if(lseek(fd, SEEK_SET, offset) == -1) return MAP_FAILED;
|
117
|
+
if(!(buf = malloc(sizeof(int) * 3 + length))) return MAP_FAILED;
|
118
|
+
wp = buf;
|
119
|
+
*(int *)wp = fd;
|
120
|
+
wp += sizeof(int);
|
121
|
+
*(int *)wp = offset;
|
122
|
+
wp += sizeof(int);
|
123
|
+
*(int *)wp = prot;
|
124
|
+
wp += sizeof(int);
|
125
|
+
rlen = 0;
|
126
|
+
while((rv = read(fd, wp + rlen, length - rlen)) > 0){
|
127
|
+
rlen += rv;
|
128
|
+
}
|
129
|
+
if(rv == -1 || rlen != length){
|
130
|
+
free(buf);
|
131
|
+
return MAP_FAILED;
|
132
|
+
}
|
133
|
+
return wp;
|
134
|
+
}
|
135
|
+
|
136
|
+
|
137
|
+
int _qdbm_munmap(void *start, size_t length){
|
138
|
+
char *buf, *rp;
|
139
|
+
int fd, offset, prot, rv, wlen;
|
140
|
+
buf = (char *)start - sizeof(int) * 3;
|
141
|
+
rp = buf;
|
142
|
+
fd = *(int *)rp;
|
143
|
+
rp += sizeof(int);
|
144
|
+
offset = *(int *)rp;
|
145
|
+
rp += sizeof(int);
|
146
|
+
prot = *(int *)rp;
|
147
|
+
rp += sizeof(int);
|
148
|
+
if(prot & PROT_WRITE){
|
149
|
+
if(lseek(fd, offset, SEEK_SET) == -1){
|
150
|
+
free(buf);
|
151
|
+
return -1;
|
152
|
+
}
|
153
|
+
wlen = 0;
|
154
|
+
while(wlen < (int)length){
|
155
|
+
rv = write(fd, rp + wlen, length - wlen);
|
156
|
+
if(rv == -1){
|
157
|
+
if(errno == EINTR) continue;
|
158
|
+
free(buf);
|
159
|
+
return -1;
|
160
|
+
}
|
161
|
+
wlen += rv;
|
162
|
+
}
|
163
|
+
}
|
164
|
+
free(buf);
|
165
|
+
return 0;
|
166
|
+
}
|
167
|
+
|
168
|
+
|
169
|
+
int _qdbm_msync(const void *start, size_t length, int flags){
|
170
|
+
char *buf, *rp;
|
171
|
+
int fd, offset, prot, rv, wlen;
|
172
|
+
buf = (char *)start - sizeof(int) * 3;
|
173
|
+
rp = buf;
|
174
|
+
fd = *(int *)rp;
|
175
|
+
rp += sizeof(int);
|
176
|
+
offset = *(int *)rp;
|
177
|
+
rp += sizeof(int);
|
178
|
+
prot = *(int *)rp;
|
179
|
+
rp += sizeof(int);
|
180
|
+
if(prot & PROT_WRITE){
|
181
|
+
if(lseek(fd, offset, SEEK_SET) == -1) return -1;
|
182
|
+
wlen = 0;
|
183
|
+
while(wlen < (int)length){
|
184
|
+
rv = write(fd, rp + wlen, length - wlen);
|
185
|
+
if(rv == -1){
|
186
|
+
if(errno == EINTR) continue;
|
187
|
+
return -1;
|
188
|
+
}
|
189
|
+
wlen += rv;
|
190
|
+
}
|
191
|
+
}
|
192
|
+
return 0;
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
#endif
|
197
|
+
|
198
|
+
|
199
|
+
|
200
|
+
/*************************************************************************************************
|
201
|
+
* for reentrant time routines
|
202
|
+
*************************************************************************************************/
|
203
|
+
|
204
|
+
|
205
|
+
#if defined(_SYS_LINUX_) || defined(_SYS_FREEBSD_) || defined(_SYS_OPENBSD_) || \
|
206
|
+
defined(_SYS_NETBSD_) || defined(_SYS_SUNOS_) || defined(_SYS_HPUX_) || \
|
207
|
+
defined(_SYS_MACOSX_) || defined(_SYS_CYGWIN_)
|
208
|
+
|
209
|
+
|
210
|
+
struct tm *_qdbm_gmtime(const time_t *timep, struct tm *result){
|
211
|
+
return gmtime_r(timep, result);
|
212
|
+
}
|
213
|
+
|
214
|
+
|
215
|
+
struct tm *_qdbm_localtime(const time_t *timep, struct tm *result){
|
216
|
+
return localtime_r(timep, result);
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
# else
|
221
|
+
|
222
|
+
|
223
|
+
struct tm *_qdbm_gmtime(const time_t *timep, struct tm *result){
|
224
|
+
return gmtime(timep);
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
struct tm *_qdbm_localtime(const time_t *timep, struct tm *result){
|
229
|
+
return localtime(timep);
|
230
|
+
}
|
231
|
+
|
232
|
+
|
233
|
+
# endif
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
/*************************************************************************************************
|
238
|
+
* for systems without times
|
239
|
+
*************************************************************************************************/
|
240
|
+
|
241
|
+
|
242
|
+
#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
|
243
|
+
|
244
|
+
|
245
|
+
clock_t _qdbm_times(struct tms *buf){
|
246
|
+
buf->tms_utime = clock();
|
247
|
+
buf->tms_stime = 0;
|
248
|
+
buf->tms_cutime = 0;
|
249
|
+
buf->tms_cstime = 0;
|
250
|
+
return 0;
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
#endif
|
255
|
+
|
256
|
+
|
257
|
+
|
258
|
+
/*************************************************************************************************
|
259
|
+
* for Win32
|
260
|
+
*************************************************************************************************/
|
261
|
+
|
262
|
+
|
263
|
+
#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
|
264
|
+
|
265
|
+
|
266
|
+
#define WINLOCKWAIT 100
|
267
|
+
|
268
|
+
|
269
|
+
int _qdbm_win32_fcntl(int fd, int cmd, struct flock *lock){
|
270
|
+
HANDLE fh;
|
271
|
+
OVERLAPPED ol;
|
272
|
+
fh = (HANDLE)_get_osfhandle(fd);
|
273
|
+
memset(&ol, 0, sizeof(OVERLAPPED));
|
274
|
+
ol.Offset = 0;
|
275
|
+
ol.OffsetHigh = 0;
|
276
|
+
ol.hEvent = 0;
|
277
|
+
if(!LockFileEx(fh, lock->l_type == F_WRLCK ? LOCKFILE_EXCLUSIVE_LOCK : 0, 0, 1, 0, &ol)){
|
278
|
+
if(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED){
|
279
|
+
while(TRUE){
|
280
|
+
if(LockFile(fh, 0, 0, 1, 0)) return 0;
|
281
|
+
Sleep(WINLOCKWAIT);
|
282
|
+
}
|
283
|
+
}
|
284
|
+
return -1;
|
285
|
+
}
|
286
|
+
return 0;
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
#endif
|
291
|
+
|
292
|
+
|
293
|
+
#if defined(_SYS_MSVC_)
|
294
|
+
|
295
|
+
|
296
|
+
DIR *_qdbm_win32_opendir(const char *name){
|
297
|
+
char expr[1024];
|
298
|
+
DIR *dir;
|
299
|
+
HANDLE fh;
|
300
|
+
WIN32_FIND_DATA data;
|
301
|
+
sprintf(expr, "%s%c*", name, MYPATHCHR);
|
302
|
+
if((fh = FindFirstFile(expr, &data)) == INVALID_HANDLE_VALUE) return NULL;
|
303
|
+
if(!(dir = malloc(sizeof(DIR)))){
|
304
|
+
FindClose(fh);
|
305
|
+
return NULL;
|
306
|
+
}
|
307
|
+
dir->fh = fh;
|
308
|
+
dir->data = data;
|
309
|
+
dir->first = TRUE;
|
310
|
+
return dir;
|
311
|
+
}
|
312
|
+
|
313
|
+
|
314
|
+
int _qdbm_win32_closedir(DIR *dir){
|
315
|
+
if(!FindClose(dir->fh)){
|
316
|
+
free(dir);
|
317
|
+
return -1;
|
318
|
+
}
|
319
|
+
free(dir);
|
320
|
+
return 0;
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
struct dirent *_qdbm_win32_readdir(DIR *dir){
|
325
|
+
if(dir->first){
|
326
|
+
sprintf(dir->de.d_name, "%s", dir->data.cFileName);
|
327
|
+
dir->first = FALSE;
|
328
|
+
return &(dir->de);
|
329
|
+
}
|
330
|
+
if(!FindNextFile(dir->fh, &(dir->data))) return NULL;
|
331
|
+
sprintf(dir->de.d_name, "%s", dir->data.cFileName);
|
332
|
+
return &(dir->de);
|
333
|
+
}
|
334
|
+
|
335
|
+
|
336
|
+
#endif
|
337
|
+
|
338
|
+
|
339
|
+
|
340
|
+
/*************************************************************************************************
|
341
|
+
* for ZLIB
|
342
|
+
*************************************************************************************************/
|
343
|
+
|
344
|
+
|
345
|
+
#if defined(MYZLIB)
|
346
|
+
|
347
|
+
|
348
|
+
#include <zlib.h>
|
349
|
+
|
350
|
+
#define ZLIBBUFSIZ 8192
|
351
|
+
#define ZLIBCLEVEL 5
|
352
|
+
|
353
|
+
|
354
|
+
static char *_qdbm_deflate_impl(const char *ptr, int size, int *sp);
|
355
|
+
static char *_qdbm_inflate_impl(const char *ptr, int size, int *sp);
|
356
|
+
static unsigned int _qdbm_getcrc_impl(const char *ptr, int size);
|
357
|
+
|
358
|
+
|
359
|
+
char *(*_qdbm_deflate)(const char *, int, int *) = _qdbm_deflate_impl;
|
360
|
+
char *(*_qdbm_inflate)(const char *, int, int *) = _qdbm_inflate_impl;
|
361
|
+
unsigned int (*_qdbm_getcrc)(const char *, int) = _qdbm_getcrc_impl;
|
362
|
+
|
363
|
+
|
364
|
+
static char *_qdbm_deflate_impl(const char *ptr, int size, int *sp){
|
365
|
+
z_stream zs;
|
366
|
+
char *buf, *swap;
|
367
|
+
unsigned char obuf[ZLIBBUFSIZ];
|
368
|
+
int rv, asiz, bsiz, osiz;
|
369
|
+
if(size < 0) size = strlen(ptr);
|
370
|
+
zs.zalloc = Z_NULL;
|
371
|
+
zs.zfree = Z_NULL;
|
372
|
+
zs.opaque = Z_NULL;
|
373
|
+
if(deflateInit(&zs, ZLIBCLEVEL) != Z_OK) return NULL;
|
374
|
+
asiz = ZLIBBUFSIZ;
|
375
|
+
if(!(buf = malloc(asiz))){
|
376
|
+
deflateEnd(&zs);
|
377
|
+
return NULL;
|
378
|
+
}
|
379
|
+
bsiz = 0;
|
380
|
+
zs.next_in = (unsigned char *)ptr;
|
381
|
+
zs.avail_in = size;
|
382
|
+
zs.next_out = obuf;
|
383
|
+
zs.avail_out = ZLIBBUFSIZ;
|
384
|
+
while((rv = deflate(&zs, Z_FINISH)) == Z_OK){
|
385
|
+
osiz = ZLIBBUFSIZ - zs.avail_out;
|
386
|
+
if(bsiz + osiz > asiz){
|
387
|
+
asiz = asiz * 2 + osiz;
|
388
|
+
if(!(swap = realloc(buf, asiz))){
|
389
|
+
free(buf);
|
390
|
+
deflateEnd(&zs);
|
391
|
+
return NULL;
|
392
|
+
}
|
393
|
+
buf = swap;
|
394
|
+
}
|
395
|
+
memcpy(buf + bsiz, obuf, osiz);
|
396
|
+
bsiz += osiz;
|
397
|
+
zs.next_out = obuf;
|
398
|
+
zs.avail_out = ZLIBBUFSIZ;
|
399
|
+
}
|
400
|
+
if(rv != Z_STREAM_END){
|
401
|
+
free(buf);
|
402
|
+
deflateEnd(&zs);
|
403
|
+
return NULL;
|
404
|
+
}
|
405
|
+
osiz = ZLIBBUFSIZ - zs.avail_out;
|
406
|
+
if(bsiz + osiz > asiz){
|
407
|
+
asiz = asiz * 2 + osiz;
|
408
|
+
if(!(swap = realloc(buf, asiz))){
|
409
|
+
free(buf);
|
410
|
+
deflateEnd(&zs);
|
411
|
+
return NULL;
|
412
|
+
}
|
413
|
+
buf = swap;
|
414
|
+
}
|
415
|
+
memcpy(buf + bsiz, obuf, osiz);
|
416
|
+
bsiz += osiz;
|
417
|
+
*sp = bsiz;
|
418
|
+
deflateEnd(&zs);
|
419
|
+
return buf;
|
420
|
+
}
|
421
|
+
|
422
|
+
|
423
|
+
static char *_qdbm_inflate_impl(const char *ptr, int size, int *sp){
|
424
|
+
z_stream zs;
|
425
|
+
char *buf, *swap;
|
426
|
+
unsigned char obuf[ZLIBBUFSIZ];
|
427
|
+
int rv, asiz, bsiz, osiz;
|
428
|
+
zs.zalloc = Z_NULL;
|
429
|
+
zs.zfree = Z_NULL;
|
430
|
+
zs.opaque = Z_NULL;
|
431
|
+
if(inflateInit(&zs) != Z_OK) return NULL;
|
432
|
+
asiz = ZLIBBUFSIZ;
|
433
|
+
if(!(buf = malloc(asiz))){
|
434
|
+
deflateEnd(&zs);
|
435
|
+
return NULL;
|
436
|
+
}
|
437
|
+
bsiz = 0;
|
438
|
+
zs.next_in = (unsigned char *)ptr;
|
439
|
+
zs.avail_in = size;
|
440
|
+
zs.next_out = obuf;
|
441
|
+
zs.avail_out = ZLIBBUFSIZ;
|
442
|
+
while((rv = inflate(&zs, Z_NO_FLUSH)) == Z_OK){
|
443
|
+
osiz = ZLIBBUFSIZ - zs.avail_out;
|
444
|
+
if(bsiz + osiz >= asiz){
|
445
|
+
asiz = asiz * 2 + osiz;
|
446
|
+
if(!(swap = realloc(buf, asiz))){
|
447
|
+
free(buf);
|
448
|
+
deflateEnd(&zs);
|
449
|
+
return NULL;
|
450
|
+
}
|
451
|
+
buf = swap;
|
452
|
+
}
|
453
|
+
memcpy(buf + bsiz, obuf, osiz);
|
454
|
+
bsiz += osiz;
|
455
|
+
zs.next_out = obuf;
|
456
|
+
zs.avail_out = ZLIBBUFSIZ;
|
457
|
+
}
|
458
|
+
if(rv != Z_STREAM_END){
|
459
|
+
free(buf);
|
460
|
+
inflateEnd(&zs);
|
461
|
+
return NULL;
|
462
|
+
}
|
463
|
+
osiz = ZLIBBUFSIZ - zs.avail_out;
|
464
|
+
if(bsiz + osiz >= asiz){
|
465
|
+
asiz = asiz * 2 + osiz;
|
466
|
+
if(!(swap = realloc(buf, asiz))){
|
467
|
+
free(buf);
|
468
|
+
deflateEnd(&zs);
|
469
|
+
return NULL;
|
470
|
+
}
|
471
|
+
buf = swap;
|
472
|
+
}
|
473
|
+
memcpy(buf + bsiz, obuf, osiz);
|
474
|
+
bsiz += osiz;
|
475
|
+
buf[bsiz] = '\0';
|
476
|
+
if(sp) *sp = bsiz;
|
477
|
+
inflateEnd(&zs);
|
478
|
+
return buf;
|
479
|
+
}
|
480
|
+
|
481
|
+
|
482
|
+
static unsigned int _qdbm_getcrc_impl(const char *ptr, int size){
|
483
|
+
int crc;
|
484
|
+
if(size < 0) size = strlen(ptr);
|
485
|
+
crc = crc32(0, Z_NULL, 0);
|
486
|
+
return crc32(crc, (unsigned char *)ptr, size);
|
487
|
+
}
|
488
|
+
|
489
|
+
|
490
|
+
#else
|
491
|
+
|
492
|
+
|
493
|
+
char *(*_qdbm_deflate)(const char *, int, int *) = NULL;
|
494
|
+
char *(*_qdbm_inflate)(const char *, int, int *) = NULL;
|
495
|
+
unsigned int (*_qdbm_getcrc)(const char *, int) = NULL;
|
496
|
+
|
497
|
+
|
498
|
+
#endif
|
499
|
+
|
500
|
+
|
501
|
+
|
502
|
+
/*************************************************************************************************
|
503
|
+
* for ICONV
|
504
|
+
*************************************************************************************************/
|
505
|
+
|
506
|
+
|
507
|
+
#if defined(MYICONV)
|
508
|
+
|
509
|
+
|
510
|
+
#include <iconv.h>
|
511
|
+
|
512
|
+
#define ICONVCHECKSIZ 32768
|
513
|
+
#define ICONVMISSMAX 256
|
514
|
+
#define ICONVALLWRAT 0.001
|
515
|
+
|
516
|
+
|
517
|
+
static char *_qdbm_iconv_impl(const char *ptr, int size,
|
518
|
+
const char *icode, const char *ocode, int *sp, int *mp);
|
519
|
+
static const char *_qdbm_encname_impl(const char *ptr, int size);
|
520
|
+
static int _qdbm_encmiss(const char *ptr, int size, const char *icode, const char *ocode);
|
521
|
+
|
522
|
+
|
523
|
+
char *(*_qdbm_iconv)(const char *, int, const char *, const char *,
|
524
|
+
int *, int *) = _qdbm_iconv_impl;
|
525
|
+
const char *(*_qdbm_encname)(const char *, int) = _qdbm_encname_impl;
|
526
|
+
|
527
|
+
|
528
|
+
static char *_qdbm_iconv_impl(const char *ptr, int size,
|
529
|
+
const char *icode, const char *ocode, int *sp, int *mp){
|
530
|
+
iconv_t ic;
|
531
|
+
char *obuf, *wp, *rp;
|
532
|
+
size_t isiz, osiz;
|
533
|
+
int miss;
|
534
|
+
if(size < 0) size = strlen(ptr);
|
535
|
+
isiz = size;
|
536
|
+
if((ic = iconv_open(ocode, icode)) == (iconv_t)-1) return NULL;
|
537
|
+
osiz = isiz * 5;
|
538
|
+
if(!(obuf = malloc(osiz + 1))){
|
539
|
+
iconv_close(ic);
|
540
|
+
return NULL;
|
541
|
+
}
|
542
|
+
wp = obuf;
|
543
|
+
rp = (char *)ptr;
|
544
|
+
miss = 0;
|
545
|
+
while(isiz > 0){
|
546
|
+
if(iconv(ic, (void *)&rp, &isiz, &wp, &osiz) == -1){
|
547
|
+
if(errno == EILSEQ && (*rp == 0x5c || *rp == 0x7e)){
|
548
|
+
*wp = *rp;
|
549
|
+
wp++;
|
550
|
+
rp++;
|
551
|
+
isiz--;
|
552
|
+
} else if(errno == EILSEQ || errno == EINVAL){
|
553
|
+
rp++;
|
554
|
+
isiz--;
|
555
|
+
miss++;
|
556
|
+
} else {
|
557
|
+
break;
|
558
|
+
}
|
559
|
+
}
|
560
|
+
}
|
561
|
+
*wp = '\0';
|
562
|
+
if(iconv_close(ic) == -1){
|
563
|
+
free(obuf);
|
564
|
+
return NULL;
|
565
|
+
}
|
566
|
+
if(sp) *sp = wp - obuf;
|
567
|
+
if(mp) *mp = miss;
|
568
|
+
return obuf;
|
569
|
+
}
|
570
|
+
|
571
|
+
|
572
|
+
static const char *_qdbm_encname_impl(const char *ptr, int size){
|
573
|
+
const char *hypo;
|
574
|
+
int i, miss, cr;
|
575
|
+
if(size < 0) size = strlen(ptr);
|
576
|
+
if(size > ICONVCHECKSIZ) size = ICONVCHECKSIZ;
|
577
|
+
if(size >= 2 && (!memcmp(ptr, "\xfe\xff", 2) || !memcmp(ptr, "\xff\xfe", 2))) return "UTF-16";
|
578
|
+
for(i = 0; i < size - 1; i += 2){
|
579
|
+
if(ptr[i] == 0 && ptr[i+1] != 0) return "UTF-16BE";
|
580
|
+
if(ptr[i+1] == 0 && ptr[i] != 0) return "UTF-16LE";
|
581
|
+
}
|
582
|
+
for(i = 0; i < size - 3; i++){
|
583
|
+
if(ptr[i] == 0x1b){
|
584
|
+
i++;
|
585
|
+
if(ptr[i] == '(' && strchr("BJHI", ptr[i+1])) return "ISO-2022-JP";
|
586
|
+
if(ptr[i] == '$' && strchr("@B(", ptr[i+1])) return "ISO-2022-JP";
|
587
|
+
}
|
588
|
+
}
|
589
|
+
if(_qdbm_encmiss(ptr, size, "US-ASCII", "UTF-16BE") < 1) return "US-ASCII";
|
590
|
+
if(_qdbm_encmiss(ptr, size, "UTF-8", "UTF-16BE") < 1) return "UTF-8";
|
591
|
+
hypo = NULL;
|
592
|
+
cr = FALSE;
|
593
|
+
for(i = 0; i < size; i++){
|
594
|
+
if(ptr[i] == 0xd){
|
595
|
+
cr = TRUE;
|
596
|
+
break;
|
597
|
+
}
|
598
|
+
}
|
599
|
+
if(cr){
|
600
|
+
if((miss = _qdbm_encmiss(ptr, size, "Shift_JIS", "EUC-JP")) < 1) return "Shift_JIS";
|
601
|
+
if(!hypo && miss / (double)size <= ICONVALLWRAT) hypo = "Shift_JIS";
|
602
|
+
if((miss = _qdbm_encmiss(ptr, size, "EUC-JP", "UTF-16BE")) < 1) return "EUC-JP";
|
603
|
+
if(!hypo && miss / (double)size <= ICONVALLWRAT) hypo = "EUC-JP";
|
604
|
+
} else {
|
605
|
+
if((miss = _qdbm_encmiss(ptr, size, "EUC-JP", "UTF-16BE")) < 1) return "EUC-JP";
|
606
|
+
if(!hypo && miss / (double)size <= ICONVALLWRAT) hypo = "EUC-JP";
|
607
|
+
if((miss = _qdbm_encmiss(ptr, size, "Shift_JIS", "EUC-JP")) < 1) return "Shift_JIS";
|
608
|
+
if(!hypo && miss / (double)size <= ICONVALLWRAT) hypo = "Shift_JIS";
|
609
|
+
}
|
610
|
+
if((miss = _qdbm_encmiss(ptr, size, "UTF-8", "UTF-16BE")) < 1) return "UTF-8";
|
611
|
+
if(!hypo && miss / (double)size <= ICONVALLWRAT) hypo = "UTF-8";
|
612
|
+
if((miss = _qdbm_encmiss(ptr, size, "CP932", "UTF-16BE")) < 1) return "CP932";
|
613
|
+
if(!hypo && miss / (double)size <= ICONVALLWRAT) hypo = "CP932";
|
614
|
+
return hypo ? hypo : "ISO-8859-1";
|
615
|
+
}
|
616
|
+
|
617
|
+
|
618
|
+
static int _qdbm_encmiss(const char *ptr, int size, const char *icode, const char *ocode){
|
619
|
+
iconv_t ic;
|
620
|
+
char obuf[ICONVCHECKSIZ], *wp, *rp;
|
621
|
+
size_t isiz, osiz;
|
622
|
+
int miss;
|
623
|
+
isiz = size;
|
624
|
+
if((ic = iconv_open(ocode, icode)) == (iconv_t)-1) return ICONVMISSMAX;
|
625
|
+
miss = 0;
|
626
|
+
rp = (char *)ptr;
|
627
|
+
while(isiz > 0){
|
628
|
+
osiz = ICONVCHECKSIZ;
|
629
|
+
wp = obuf;
|
630
|
+
if(iconv(ic, (void *)&rp, &isiz, &wp, &osiz) == -1){
|
631
|
+
if(errno == EILSEQ || errno == EINVAL){
|
632
|
+
rp++;
|
633
|
+
isiz--;
|
634
|
+
miss++;
|
635
|
+
if(miss >= ICONVMISSMAX) break;
|
636
|
+
} else {
|
637
|
+
break;
|
638
|
+
}
|
639
|
+
}
|
640
|
+
}
|
641
|
+
if(iconv_close(ic) == -1) return ICONVMISSMAX;
|
642
|
+
return miss;
|
643
|
+
}
|
644
|
+
|
645
|
+
|
646
|
+
#else
|
647
|
+
|
648
|
+
|
649
|
+
char *(*_qdbm_iconv)(const char *, int, const char *, const char *, int *, int *) = NULL;
|
650
|
+
const char *(*_qdbm_encname)(const char *, int) = NULL;
|
651
|
+
|
652
|
+
|
653
|
+
#endif
|
654
|
+
|
655
|
+
|
656
|
+
|
657
|
+
/*************************************************************************************************
|
658
|
+
* common settings
|
659
|
+
*************************************************************************************************/
|
660
|
+
|
661
|
+
|
662
|
+
int _qdbm_dummyfunc(void){
|
663
|
+
return 0;
|
664
|
+
}
|
665
|
+
|
666
|
+
|
667
|
+
|
668
|
+
/* END OF FILE */
|