cdb-full 0.5a

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. data/COPYING +6 -0
  2. data/FILES +88 -0
  3. data/README +163 -0
  4. data/VERSION +1 -0
  5. data/cdb-0.75/CHANGES +16 -0
  6. data/cdb-0.75/FILES +78 -0
  7. data/cdb-0.75/Makefile +320 -0
  8. data/cdb-0.75/README +7 -0
  9. data/cdb-0.75/SYSDEPS +3 -0
  10. data/cdb-0.75/TARGETS +66 -0
  11. data/cdb-0.75/TODO +1 -0
  12. data/cdb-0.75/VERSION +1 -0
  13. data/cdb-0.75/alloc.c +32 -0
  14. data/cdb-0.75/alloc.h +8 -0
  15. data/cdb-0.75/auto-str.c +44 -0
  16. data/cdb-0.75/auto_home.h +6 -0
  17. data/cdb-0.75/buffer.c +10 -0
  18. data/cdb-0.75/buffer.h +56 -0
  19. data/cdb-0.75/buffer_0.c +12 -0
  20. data/cdb-0.75/buffer_1.c +6 -0
  21. data/cdb-0.75/buffer_1s.c +6 -0
  22. data/cdb-0.75/buffer_2.c +6 -0
  23. data/cdb-0.75/buffer_copy.c +16 -0
  24. data/cdb-0.75/buffer_get.c +67 -0
  25. data/cdb-0.75/buffer_put.c +88 -0
  26. data/cdb-0.75/byte.h +13 -0
  27. data/cdb-0.75/byte_copy.c +14 -0
  28. data/cdb-0.75/byte_cr.c +16 -0
  29. data/cdb-0.75/byte_diff.c +16 -0
  30. data/cdb-0.75/cdb.c +136 -0
  31. data/cdb-0.75/cdb.h +37 -0
  32. data/cdb-0.75/cdb_hash.c +21 -0
  33. data/cdb-0.75/cdb_make.c +153 -0
  34. data/cdb-0.75/cdb_make.h +39 -0
  35. data/cdb-0.75/cdbdump.c +83 -0
  36. data/cdb-0.75/cdbget.c +67 -0
  37. data/cdb-0.75/cdbmake-12.sh +8 -0
  38. data/cdb-0.75/cdbmake-sv.sh +22 -0
  39. data/cdb-0.75/cdbmake.c +110 -0
  40. data/cdb-0.75/cdbstats.c +125 -0
  41. data/cdb-0.75/cdbtest.c +119 -0
  42. data/cdb-0.75/choose.sh +18 -0
  43. data/cdb-0.75/conf-cc +3 -0
  44. data/cdb-0.75/conf-home +3 -0
  45. data/cdb-0.75/conf-ld +3 -0
  46. data/cdb-0.75/error.c +109 -0
  47. data/cdb-0.75/error.h +25 -0
  48. data/cdb-0.75/error_str.c +271 -0
  49. data/cdb-0.75/exit.h +6 -0
  50. data/cdb-0.75/find-systype.sh +159 -0
  51. data/cdb-0.75/fmt.h +25 -0
  52. data/cdb-0.75/fmt_ulong.c +13 -0
  53. data/cdb-0.75/hier.c +15 -0
  54. data/cdb-0.75/install.c +149 -0
  55. data/cdb-0.75/instcheck.c +108 -0
  56. data/cdb-0.75/open.h +10 -0
  57. data/cdb-0.75/open_read.c +6 -0
  58. data/cdb-0.75/open_trunc.c +6 -0
  59. data/cdb-0.75/random.cdb +0 -0
  60. data/cdb-0.75/readwrite.h +7 -0
  61. data/cdb-0.75/rts.exp +251 -0
  62. data/cdb-0.75/rts.sh +1 -0
  63. data/cdb-0.75/rts.tests +210 -0
  64. data/cdb-0.75/scan.h +28 -0
  65. data/cdb-0.75/scan_ulong.c +11 -0
  66. data/cdb-0.75/seek.h +15 -0
  67. data/cdb-0.75/seek_cur.c +7 -0
  68. data/cdb-0.75/seek_set.c +7 -0
  69. data/cdb-0.75/str.h +14 -0
  70. data/cdb-0.75/str_len.c +14 -0
  71. data/cdb-0.75/strerr.h +78 -0
  72. data/cdb-0.75/strerr_die.c +31 -0
  73. data/cdb-0.75/strerr_sys.c +12 -0
  74. data/cdb-0.75/testzero.c +35 -0
  75. data/cdb-0.75/trycpp.c +7 -0
  76. data/cdb-0.75/tryulong32.c +11 -0
  77. data/cdb-0.75/uint32.h1 +11 -0
  78. data/cdb-0.75/uint32.h2 +11 -0
  79. data/cdb-0.75/uint32_pack.c +21 -0
  80. data/cdb-0.75/uint32_unpack.c +31 -0
  81. data/cdb-0.75/warn-auto.sh +2 -0
  82. data/cdb-0.75/x86cpuid.c +38 -0
  83. data/cdb.c +372 -0
  84. data/depend +1 -0
  85. data/extconf.rb +11 -0
  86. data/lib/cdb.rb +90 -0
  87. data/sample/sample.rb +35 -0
  88. metadata +152 -0
@@ -0,0 +1,28 @@
1
+ #ifndef SCAN_H
2
+ #define SCAN_H
3
+
4
+ extern unsigned int scan_uint(char *,unsigned int *);
5
+ extern unsigned int scan_xint(char *,unsigned int *);
6
+ extern unsigned int scan_nbbint(char *,unsigned int,unsigned int,unsigned int,unsigned int *);
7
+ extern unsigned int scan_ushort(char *,unsigned short *);
8
+ extern unsigned int scan_xshort(char *,unsigned short *);
9
+ extern unsigned int scan_nbbshort(char *,unsigned int,unsigned int,unsigned int,unsigned short *);
10
+ extern unsigned int scan_ulong(char *,unsigned long *);
11
+ extern unsigned int scan_xlong(char *,unsigned long *);
12
+ extern unsigned int scan_nbblong(char *,unsigned int,unsigned int,unsigned int,unsigned long *);
13
+
14
+ extern unsigned int scan_plusminus(char *,int *);
15
+ extern unsigned int scan_0x(char *,unsigned int *);
16
+
17
+ extern unsigned int scan_whitenskip(char *,unsigned int);
18
+ extern unsigned int scan_nonwhitenskip(char *,unsigned int);
19
+ extern unsigned int scan_charsetnskip(char *,char *,unsigned int);
20
+ extern unsigned int scan_noncharsetnskip(char *,char *,unsigned int);
21
+
22
+ extern unsigned int scan_strncmp(char *,char *,unsigned int);
23
+ extern unsigned int scan_memcmp(char *,char *,unsigned int);
24
+
25
+ extern unsigned int scan_long(char *,long *);
26
+ extern unsigned int scan_8long(char *,unsigned long *);
27
+
28
+ #endif
@@ -0,0 +1,11 @@
1
+ #include "scan.h"
2
+
3
+ unsigned int scan_ulong(register char *s,register unsigned long *u)
4
+ {
5
+ register unsigned int pos; register unsigned long result;
6
+ register unsigned long c;
7
+ pos = 0; result = 0;
8
+ while ((c = (unsigned long) (unsigned char) (s[pos] - '0')) < 10)
9
+ { result = result * 10 + c; ++pos; }
10
+ *u = result; return pos;
11
+ }
@@ -0,0 +1,15 @@
1
+ #ifndef SEEK_H
2
+ #define SEEK_H
3
+
4
+ typedef unsigned long seek_pos;
5
+
6
+ extern seek_pos seek_cur(int);
7
+
8
+ extern int seek_set(int,seek_pos);
9
+ extern int seek_end(int);
10
+
11
+ extern int seek_trunc(int,seek_pos);
12
+
13
+ #define seek_begin(fd) (seek_set((fd),(seek_pos) 0))
14
+
15
+ #endif
@@ -0,0 +1,7 @@
1
+ #include <sys/types.h>
2
+ #include "seek.h"
3
+
4
+ #define CUR 1 /* sigh */
5
+
6
+ seek_pos seek_cur(int fd)
7
+ { return lseek(fd,(off_t) 0,CUR); }
@@ -0,0 +1,7 @@
1
+ #include <sys/types.h>
2
+ #include "seek.h"
3
+
4
+ #define SET 0 /* sigh */
5
+
6
+ int seek_set(int fd,seek_pos pos)
7
+ { if (lseek(fd,(off_t) pos,SET) == -1) return -1; return 0; }
@@ -0,0 +1,14 @@
1
+ #ifndef STR_H
2
+ #define STR_H
3
+
4
+ extern unsigned int str_copy(char *,char *);
5
+ extern int str_diff(char *,char *);
6
+ extern int str_diffn(char *,char *,unsigned int);
7
+ extern unsigned int str_len(char *);
8
+ extern unsigned int str_chr(char *,int);
9
+ extern unsigned int str_rchr(char *,int);
10
+ extern int str_start(char *,char *);
11
+
12
+ #define str_equal(s,t) (!str_diff((s),(t)))
13
+
14
+ #endif
@@ -0,0 +1,14 @@
1
+ #include "str.h"
2
+
3
+ unsigned int str_len(char *s)
4
+ {
5
+ register char *t;
6
+
7
+ t = s;
8
+ for (;;) {
9
+ if (!*t) return t - s; ++t;
10
+ if (!*t) return t - s; ++t;
11
+ if (!*t) return t - s; ++t;
12
+ if (!*t) return t - s; ++t;
13
+ }
14
+ }
@@ -0,0 +1,78 @@
1
+ #ifndef STRERR_H
2
+ #define STRERR_H
3
+
4
+ struct strerr {
5
+ struct strerr *who;
6
+ char *x;
7
+ char *y;
8
+ char *z;
9
+ } ;
10
+
11
+ extern struct strerr strerr_sys;
12
+ extern void strerr_sysinit(void);
13
+
14
+ extern char *strerr(struct strerr *);
15
+ extern void strerr_warn(char *,char *,char *,char *,char *,char *,struct strerr *);
16
+ extern void strerr_die(int,char *,char *,char *,char *,char *,char *,struct strerr *);
17
+
18
+ #define STRERR(r,se,a) \
19
+ { se.who = 0; se.x = a; se.y = 0; se.z = 0; return r; }
20
+
21
+ #define STRERR_SYS(r,se,a) \
22
+ { se.who = &strerr_sys; se.x = a; se.y = 0; se.z = 0; return r; }
23
+ #define STRERR_SYS3(r,se,a,b,c) \
24
+ { se.who = &strerr_sys; se.x = a; se.y = b; se.z = c; return r; }
25
+
26
+ #define strerr_warn6(x1,x2,x3,x4,x5,x6,se) \
27
+ strerr_warn((x1),(x2),(x3),(x4),(x5),(x6),(se))
28
+ #define strerr_warn5(x1,x2,x3,x4,x5,se) \
29
+ strerr_warn((x1),(x2),(x3),(x4),(x5),0,(se))
30
+ #define strerr_warn4(x1,x2,x3,x4,se) \
31
+ strerr_warn((x1),(x2),(x3),(x4),0,0,(se))
32
+ #define strerr_warn3(x1,x2,x3,se) \
33
+ strerr_warn((x1),(x2),(x3),0,0,0,(se))
34
+ #define strerr_warn2(x1,x2,se) \
35
+ strerr_warn((x1),(x2),0,0,0,0,(se))
36
+ #define strerr_warn1(x1,se) \
37
+ strerr_warn((x1),0,0,0,0,0,(se))
38
+
39
+ #define strerr_die6(e,x1,x2,x3,x4,x5,x6,se) \
40
+ strerr_die((e),(x1),(x2),(x3),(x4),(x5),(x6),(se))
41
+ #define strerr_die5(e,x1,x2,x3,x4,x5,se) \
42
+ strerr_die((e),(x1),(x2),(x3),(x4),(x5),0,(se))
43
+ #define strerr_die4(e,x1,x2,x3,x4,se) \
44
+ strerr_die((e),(x1),(x2),(x3),(x4),0,0,(se))
45
+ #define strerr_die3(e,x1,x2,x3,se) \
46
+ strerr_die((e),(x1),(x2),(x3),0,0,0,(se))
47
+ #define strerr_die2(e,x1,x2,se) \
48
+ strerr_die((e),(x1),(x2),0,0,0,0,(se))
49
+ #define strerr_die1(e,x1,se) \
50
+ strerr_die((e),(x1),0,0,0,0,0,(se))
51
+
52
+ #define strerr_die6sys(e,x1,x2,x3,x4,x5,x6) \
53
+ strerr_die((e),(x1),(x2),(x3),(x4),(x5),(x6),&strerr_sys)
54
+ #define strerr_die5sys(e,x1,x2,x3,x4,x5) \
55
+ strerr_die((e),(x1),(x2),(x3),(x4),(x5),0,&strerr_sys)
56
+ #define strerr_die4sys(e,x1,x2,x3,x4) \
57
+ strerr_die((e),(x1),(x2),(x3),(x4),0,0,&strerr_sys)
58
+ #define strerr_die3sys(e,x1,x2,x3) \
59
+ strerr_die((e),(x1),(x2),(x3),0,0,0,&strerr_sys)
60
+ #define strerr_die2sys(e,x1,x2) \
61
+ strerr_die((e),(x1),(x2),0,0,0,0,&strerr_sys)
62
+ #define strerr_die1sys(e,x1) \
63
+ strerr_die((e),(x1),0,0,0,0,0,&strerr_sys)
64
+
65
+ #define strerr_die6x(e,x1,x2,x3,x4,x5,x6) \
66
+ strerr_die((e),(x1),(x2),(x3),(x4),(x5),(x6),0)
67
+ #define strerr_die5x(e,x1,x2,x3,x4,x5) \
68
+ strerr_die((e),(x1),(x2),(x3),(x4),(x5),0,0)
69
+ #define strerr_die4x(e,x1,x2,x3,x4) \
70
+ strerr_die((e),(x1),(x2),(x3),(x4),0,0,0)
71
+ #define strerr_die3x(e,x1,x2,x3) \
72
+ strerr_die((e),(x1),(x2),(x3),0,0,0,0)
73
+ #define strerr_die2x(e,x1,x2) \
74
+ strerr_die((e),(x1),(x2),0,0,0,0,0)
75
+ #define strerr_die1x(e,x1) \
76
+ strerr_die((e),(x1),0,0,0,0,0,0)
77
+
78
+ #endif
@@ -0,0 +1,31 @@
1
+ #include "buffer.h"
2
+ #include "exit.h"
3
+ #include "strerr.h"
4
+
5
+ void strerr_warn(char *x1,char *x2,char *x3,char *x4,char *x5,char *x6,struct strerr *se)
6
+ {
7
+ strerr_sysinit();
8
+
9
+ if (x1) buffer_puts(buffer_2,x1);
10
+ if (x2) buffer_puts(buffer_2,x2);
11
+ if (x3) buffer_puts(buffer_2,x3);
12
+ if (x4) buffer_puts(buffer_2,x4);
13
+ if (x5) buffer_puts(buffer_2,x5);
14
+ if (x6) buffer_puts(buffer_2,x6);
15
+
16
+ while(se) {
17
+ if (se->x) buffer_puts(buffer_2,se->x);
18
+ if (se->y) buffer_puts(buffer_2,se->y);
19
+ if (se->z) buffer_puts(buffer_2,se->z);
20
+ se = se->who;
21
+ }
22
+
23
+ buffer_puts(buffer_2,"\n");
24
+ buffer_flush(buffer_2);
25
+ }
26
+
27
+ void strerr_die(int e,char *x1,char *x2,char *x3,char *x4,char *x5,char *x6,struct strerr *se)
28
+ {
29
+ strerr_warn(x1,x2,x3,x4,x5,x6,se);
30
+ _exit(e);
31
+ }
@@ -0,0 +1,12 @@
1
+ #include "error.h"
2
+ #include "strerr.h"
3
+
4
+ struct strerr strerr_sys;
5
+
6
+ void strerr_sysinit(void)
7
+ {
8
+ strerr_sys.who = 0;
9
+ strerr_sys.x = error_str(errno);
10
+ strerr_sys.y = "";
11
+ strerr_sys.z = "";
12
+ }
@@ -0,0 +1,35 @@
1
+ #include "uint32.h"
2
+ #include "scan.h"
3
+ #include "strerr.h"
4
+ #include "cdb_make.h"
5
+
6
+ #define FATAL "testzero: fatal: "
7
+
8
+ void die_write(void)
9
+ {
10
+ strerr_die2sys(111,FATAL,"unable to write: ");
11
+ }
12
+
13
+ static char key[4];
14
+ static char data[65536];
15
+ struct cdb_make c;
16
+
17
+ main(int argc,char **argv)
18
+ {
19
+ int fd;
20
+ unsigned long loop;
21
+
22
+ if (!*argv) _exit(0);
23
+ if (!*++argv) _exit(0);
24
+ scan_ulong(*argv,&loop);
25
+
26
+ if (cdb_make_start(&c,1) == -1) die_write();
27
+
28
+ while (loop) {
29
+ uint32_pack(key,--loop);
30
+ if (cdb_make_add(&c,key,4,data,sizeof data) == -1) die_write();
31
+ }
32
+
33
+ if (cdb_make_finish(&c) == -1) die_write();
34
+ _exit(0);
35
+ }
@@ -0,0 +1,7 @@
1
+ main()
2
+ {
3
+ #ifdef NeXT
4
+ printf("nextstep\n"); exit(0);
5
+ #endif
6
+ printf("unknown\n"); exit(0);
7
+ }
@@ -0,0 +1,11 @@
1
+ void main()
2
+ {
3
+ unsigned long u;
4
+ u = 1;
5
+ u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u;
6
+ u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u;
7
+ u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u;
8
+ u += u; u += u; u += u; u += u; u += u; u += u; u += u; u += u;
9
+ if (!u) _exit(0);
10
+ _exit(1);
11
+ }
@@ -0,0 +1,11 @@
1
+ #ifndef UINT32_H
2
+ #define UINT32_H
3
+
4
+ typedef unsigned int uint32;
5
+
6
+ extern void uint32_pack(char *,uint32);
7
+ extern void uint32_pack_big(char *,uint32);
8
+ extern void uint32_unpack(char *,uint32 *);
9
+ extern void uint32_unpack_big(char *,uint32 *);
10
+
11
+ #endif
@@ -0,0 +1,11 @@
1
+ #ifndef UINT32_H
2
+ #define UINT32_H
3
+
4
+ typedef unsigned long uint32;
5
+
6
+ extern void uint32_pack(char *,uint32);
7
+ extern void uint32_pack_big(char *,uint32);
8
+ extern void uint32_unpack(char *,uint32 *);
9
+ extern void uint32_unpack_big(char *,uint32 *);
10
+
11
+ #endif
@@ -0,0 +1,21 @@
1
+ #include "uint32.h"
2
+
3
+ void uint32_pack(char s[4],uint32 u)
4
+ {
5
+ s[0] = u & 255;
6
+ u >>= 8;
7
+ s[1] = u & 255;
8
+ u >>= 8;
9
+ s[2] = u & 255;
10
+ s[3] = u >> 8;
11
+ }
12
+
13
+ void uint32_pack_big(char s[4],uint32 u)
14
+ {
15
+ s[3] = u & 255;
16
+ u >>= 8;
17
+ s[2] = u & 255;
18
+ u >>= 8;
19
+ s[1] = u & 255;
20
+ s[0] = u >> 8;
21
+ }
@@ -0,0 +1,31 @@
1
+ #include "uint32.h"
2
+
3
+ void uint32_unpack(char s[4],uint32 *u)
4
+ {
5
+ uint32 result;
6
+
7
+ result = (unsigned char) s[3];
8
+ result <<= 8;
9
+ result += (unsigned char) s[2];
10
+ result <<= 8;
11
+ result += (unsigned char) s[1];
12
+ result <<= 8;
13
+ result += (unsigned char) s[0];
14
+
15
+ *u = result;
16
+ }
17
+
18
+ void uint32_unpack_big(char s[4],uint32 *u)
19
+ {
20
+ uint32 result;
21
+
22
+ result = (unsigned char) s[0];
23
+ result <<= 8;
24
+ result += (unsigned char) s[1];
25
+ result <<= 8;
26
+ result += (unsigned char) s[2];
27
+ result <<= 8;
28
+ result += (unsigned char) s[3];
29
+
30
+ *u = result;
31
+ }
@@ -0,0 +1,2 @@
1
+ #!/bin/sh
2
+ # WARNING: This file was auto-generated. Do not edit!
@@ -0,0 +1,38 @@
1
+ #include <signal.h>
2
+
3
+ void nope()
4
+ {
5
+ exit(1);
6
+ }
7
+
8
+ main()
9
+ {
10
+ unsigned long x[4];
11
+ unsigned long y[4];
12
+ int i;
13
+ int j;
14
+ char c;
15
+
16
+ signal(SIGILL,nope);
17
+
18
+ x[0] = 0;
19
+ x[1] = 0;
20
+ x[2] = 0;
21
+ x[3] = 0;
22
+
23
+ asm volatile(".byte 15;.byte 162" : "=a"(x[0]),"=b"(x[1]),"=c"(x[3]),"=d"(x[2]) : "0"(0) );
24
+ if (!x[0]) return 0;
25
+ asm volatile(".byte 15;.byte 162" : "=a"(y[0]),"=b"(y[1]),"=c"(y[2]),"=d"(y[3]) : "0"(1) );
26
+
27
+ for (i = 1;i < 4;++i)
28
+ for (j = 0;j < 4;++j) {
29
+ c = x[i] >> (8 * j);
30
+ if (c < 32) c = 32;
31
+ if (c > 126) c = 126;
32
+ putchar(c);
33
+ }
34
+
35
+ printf("-%08x-%08x\n",y[0],y[3]);
36
+
37
+ return 0;
38
+ }
data/cdb.c ADDED
@@ -0,0 +1,372 @@
1
+ #include <fcntl.h>
2
+ #include <errno.h>
3
+ #include <sys/stat.h>
4
+
5
+ #include "ruby.h"
6
+ #include "cdb/alloc.h"
7
+ #include "cdb/uint32.h"
8
+ #include "cdb/cdb.h"
9
+ #include "cdb/cdb_make.h"
10
+
11
+ #define Get_CDB(obj, var) Data_Get_Struct(obj, struct cdb, var)
12
+ #define Get_CDBMake(obj, var) Data_Get_Struct(obj, struct cdb_make, var)
13
+
14
+ #define Check_FD(cdb) if (cdb->fd == -1) rb_raise(rb_eCDB_Error, "file already closed")
15
+
16
+ VALUE rb_eCDB_Error;
17
+
18
+
19
+
20
+ static void
21
+ _xread (fd, buf, len)
22
+ int fd;
23
+ char *buf;
24
+ unsigned int len;
25
+ {
26
+ int ret;
27
+ while (len > 0) {
28
+ do
29
+ ret = read(fd, buf, len);
30
+ while ((ret == -1) && (errno == EINTR));
31
+ if (ret == -1) rb_sys_fail(0);
32
+ if (ret == 0) rb_raise(rb_eCDB_Error, "data format error");
33
+ len -= ret;
34
+ buf += ret;
35
+ }
36
+ }
37
+
38
+
39
+ /*
40
+ * class CDB
41
+ */
42
+
43
+ VALUE rb_cCDB;
44
+
45
+ static void
46
+ _cdb_free (cdb)
47
+ struct cdb *cdb;
48
+ {
49
+ cdb_free(cdb);
50
+ if (cdb->fd != -1) close(cdb->fd);
51
+ free(cdb);
52
+ }
53
+
54
+
55
+ static VALUE
56
+ rb_cdb_new (klass, file)
57
+ VALUE klass, file;
58
+ {
59
+ int fd;
60
+ VALUE obj;
61
+ struct cdb *cdb;
62
+
63
+ Check_Type(file, T_STRING);
64
+
65
+ fd = open(StringValuePtr(file), O_RDONLY, 0);
66
+ if (fd == -1) rb_sys_fail(0);
67
+
68
+ obj = Data_Make_Struct(klass, struct cdb, 0, _cdb_free, cdb);
69
+ cdb_init(cdb, fd);
70
+
71
+ return obj;
72
+ }
73
+
74
+
75
+ static VALUE
76
+ rb_cdb_close (obj)
77
+ VALUE obj;
78
+ {
79
+ struct cdb *cdb;
80
+ Get_CDB(obj, cdb);
81
+ Check_FD(cdb);
82
+ cdb_free(cdb);
83
+ close(cdb->fd);
84
+ cdb->fd = -1;
85
+ return Qnil;
86
+ }
87
+
88
+
89
+ static VALUE
90
+ rb_cdb_open (klass, file)
91
+ VALUE klass, file;
92
+ {
93
+ VALUE obj;
94
+ obj = rb_cdb_new(klass, file);
95
+ return rb_ensure(rb_yield, obj, rb_cdb_close, obj);
96
+ }
97
+
98
+
99
+ static VALUE
100
+ _cdb_read (cdb, pos, len)
101
+ struct cdb *cdb;
102
+ uint32 pos;
103
+ unsigned int len;
104
+ {
105
+ if (cdb->map) {
106
+ if ((pos > cdb->size) || (cdb->size - pos < len))
107
+ rb_raise(rb_eCDB_Error, "data format error");
108
+ return rb_str_new(cdb->map + pos, len);
109
+ }
110
+ else {
111
+ int ret;
112
+ VALUE s;
113
+
114
+ if (seek_set(cdb->fd, pos) == -1) rb_sys_fail(0);
115
+ s = rb_str_new(0, len);
116
+ _xread(cdb->fd, RSTRING_PTR(s), len);
117
+ return s;
118
+ }
119
+ }
120
+
121
+
122
+ static VALUE
123
+ rb_cdb_read (obj, p, l)
124
+ VALUE obj, p, l;
125
+ {
126
+ struct cdb *cdb;
127
+ uint32 pos = NUM2INT(p);
128
+ unsigned int len = NUM2INT(l);
129
+
130
+ Get_CDB(obj, cdb);
131
+ Check_FD(cdb);
132
+ return _cdb_read(cdb, pos, len);
133
+ }
134
+
135
+
136
+ static VALUE
137
+ rb_cdb_find (obj, key)
138
+ VALUE obj, key;
139
+ {
140
+ struct cdb *cdb;
141
+ uint32 pos;
142
+
143
+ Get_CDB(obj, cdb);
144
+ Check_FD(cdb);
145
+ StringValue(key);
146
+ switch (cdb_find(cdb, RSTRING_PTR(key), RSTRING_LEN(key))) {
147
+ case 1:
148
+ return _cdb_read(cdb, cdb_datapos(cdb), cdb_datalen(cdb));
149
+ case 0:
150
+ return Qnil;
151
+ case -1:
152
+ rb_sys_fail(0);
153
+ default:
154
+ rb_raise(rb_eCDB_Error, "cdb_find returned unexpected value");
155
+ }
156
+ }
157
+
158
+
159
+ static void
160
+ _read_uint32 (fd, num)
161
+ int fd;
162
+ uint32 *num;
163
+ {
164
+ char buf[4];
165
+ _xread(fd, buf, 4);
166
+ uint32_unpack(buf, num);
167
+ }
168
+
169
+
170
+ static VALUE
171
+ rb_cdb_each (argc, argv, obj)
172
+ int argc;
173
+ VALUE *argv, obj;
174
+ {
175
+ int ret;
176
+ VALUE key;
177
+ struct cdb *cdb;
178
+
179
+ Get_CDB(obj, cdb);
180
+ Check_FD(cdb);
181
+
182
+ if (rb_scan_args(argc, argv, "01", &key) == 1) {
183
+ StringValue(key);
184
+ cdb_findstart(cdb);
185
+
186
+ while ((ret = cdb_findnext(cdb, RSTRING_PTR(key), RSTRING_LEN(key))) == 1)
187
+ rb_yield(_cdb_read(cdb, cdb_datapos(cdb), cdb_datalen(cdb)));
188
+
189
+ switch (ret) {
190
+ case 0: return obj;
191
+ case -1: rb_sys_fail(0);
192
+ default: rb_raise(rb_eCDB_Error, "cdb_find returned unexpected value");
193
+ }
194
+ }
195
+ else {
196
+ int fd;
197
+ VALUE data;
198
+ struct stat sb;
199
+ uint32 eod, klen, dlen, pos;
200
+
201
+ fd = cdb->fd;
202
+ if (fstat(fd, &sb) == -1) rb_sys_fail(0);
203
+ if (cdb->map) {
204
+ char *eodp, *p = cdb->map;
205
+
206
+ uint32_unpack(p, &eod);
207
+ if (sb.st_size < eod)
208
+ rb_raise(rb_eCDB_Error, "data format error");
209
+ eodp = p + eod;
210
+ p += 2048;
211
+
212
+ while (p < eodp) {
213
+ uint32_unpack(p, &klen); p += 4;
214
+ uint32_unpack(p, &dlen); p += 4;
215
+ rb_yield(rb_assoc_new(rb_str_new(p, klen),
216
+ rb_str_new(p + klen, dlen)));
217
+ p += klen + dlen;
218
+ }
219
+ }
220
+ else {
221
+ if (seek_set(fd, 0) == -1) rb_sys_fail(0);
222
+ _read_uint32(fd, &eod);
223
+ if (sb.st_size < eod)
224
+ rb_raise(rb_eCDB_Error, "data format error");
225
+
226
+ if (seek_set(fd, 2048) == -1) rb_sys_fail(0);
227
+ pos = 2048;
228
+
229
+ while (pos < eod) {
230
+ _read_uint32(fd, &klen);
231
+ _read_uint32(fd, &dlen);
232
+
233
+ key = rb_str_new(0, klen);
234
+ data = rb_str_new(0, dlen);
235
+ _xread(fd, RSTRING_PTR(key), klen);
236
+ _xread(fd, RSTRING_PTR(data), dlen);
237
+
238
+ rb_yield(rb_assoc_new(key, data));
239
+ pos += 8 + klen + dlen;
240
+ }
241
+ }
242
+ return obj;
243
+ }
244
+ }
245
+
246
+
247
+
248
+ /*
249
+ * class CDBMake
250
+ */
251
+
252
+ VALUE rb_cCDBMake;
253
+
254
+ static void
255
+ _cdbmake_free (cdb)
256
+ struct cdb_make *cdb;
257
+ {
258
+ struct cdb_hplist *head, *next;
259
+ head = cdb->head;
260
+ while (head) {
261
+ next = head->next;
262
+ alloc_free(head);
263
+ head = next;
264
+ }
265
+ if (cdb->split) alloc_free(cdb->split);
266
+ if (cdb->fd != -1) close(cdb->fd);
267
+ free(cdb);
268
+ }
269
+
270
+
271
+ static VALUE
272
+ rb_cdbmake_new (argc, argv, klass)
273
+ int argc;
274
+ VALUE *argv, klass;
275
+ {
276
+ int fd;
277
+ mode_t mode = 0644;
278
+ VALUE obj, arg1, arg2;
279
+ struct cdb_make *cdb;
280
+
281
+ if (rb_scan_args(argc, argv, "11", &arg1, &arg2) == 2)
282
+ mode = NUM2INT(arg2);
283
+
284
+ Check_Type(arg1, T_STRING);
285
+
286
+ fd = open(StringValuePtr(arg1), O_WRONLY|O_CREAT|O_TRUNC, mode);
287
+ if (fd == -1) rb_sys_fail(0);
288
+
289
+ obj = Data_Make_Struct(klass, struct cdb_make, 0, _cdbmake_free, cdb);
290
+ if (cdb_make_start(cdb, fd) == -1) rb_sys_fail(0);
291
+
292
+ return obj;
293
+ }
294
+
295
+
296
+ static VALUE
297
+ rb_cdbmake_finish (obj)
298
+ VALUE obj;
299
+ {
300
+ struct cdb_make *cdb;
301
+
302
+ Get_CDBMake(obj, cdb);
303
+ Check_FD(cdb);
304
+ if (cdb_make_finish(cdb) == -1) rb_sys_fail(0);
305
+ if (fsync(cdb->fd) == -1) rb_sys_fail(0);
306
+ close(cdb->fd);
307
+ cdb->fd = -1;
308
+ return Qnil;
309
+ }
310
+
311
+
312
+ static VALUE
313
+ rb_cdbmake_open (argc, argv, klass)
314
+ int argc;
315
+ VALUE *argv, klass;
316
+ {
317
+ VALUE obj;
318
+ obj = rb_cdbmake_new(argc, argv, klass);
319
+ return rb_ensure(rb_yield, obj, rb_cdbmake_finish, obj);
320
+ }
321
+
322
+
323
+ static VALUE
324
+ rb_cdbmake_add (obj, key, data)
325
+ VALUE obj, key, data;
326
+ {
327
+ struct cdb_make *cdb;
328
+
329
+ Get_CDBMake(obj, cdb);
330
+ Check_FD(cdb);
331
+ StringValue(key);
332
+ StringValue(data);
333
+ if (cdb_make_add(cdb, RSTRING_PTR(key), RSTRING_LEN(key),
334
+ RSTRING_PTR(data), RSTRING_LEN(data)) == -1) rb_sys_fail(0);
335
+ return obj;
336
+ }
337
+
338
+
339
+
340
+ /*
341
+ * Module Initialization
342
+ */
343
+
344
+ void
345
+ Init_cdb ()
346
+ {
347
+ rb_cCDB = rb_define_class("CDB", rb_cObject);
348
+ rb_cCDBMake = rb_define_class("CDBMake", rb_cObject);
349
+ rb_eCDB_Error = rb_define_class("CDBError", rb_eRuntimeError);
350
+
351
+ rb_include_module(rb_cCDB, rb_mEnumerable);
352
+
353
+ rb_define_singleton_method(rb_cCDB, "new", rb_cdb_new, 1);
354
+ rb_define_singleton_method(rb_cCDB, "open", rb_cdb_open, 1);
355
+
356
+ rb_define_singleton_method(rb_cCDBMake, "new", rb_cdbmake_new, -1);
357
+ rb_define_singleton_method(rb_cCDBMake, "open", rb_cdbmake_open, -1);
358
+
359
+ rb_define_method(rb_cCDB, "find", rb_cdb_find, 1);
360
+ rb_define_method(rb_cCDB, "[]", rb_cdb_find, 1);
361
+ rb_define_method(rb_cCDB, "has", rb_cdb_find, 1);
362
+ rb_define_method(rb_cCDB, "each", rb_cdb_each, -1);
363
+ rb_define_method(rb_cCDB, "read", rb_cdb_read, 2);
364
+ rb_define_method(rb_cCDB, "close", rb_cdb_close, 0);
365
+
366
+ rb_define_method(rb_cCDBMake, "add", rb_cdbmake_add, 2);
367
+ rb_define_method(rb_cCDBMake, "[]=", rb_cdbmake_add, 2);
368
+ rb_define_method(rb_cCDBMake, "finish", rb_cdbmake_finish, 0);
369
+ }
370
+
371
+
372
+