Mercurial > hg > cdb
comparison cdb.c @ 1:41b9b6a160d1
access functions,
use all 32 bits for anything to do with a mapped cdb file
| author | Henry S. Thompson <ht@inf.ed.ac.uk> |
|---|---|
| date | Wed, 29 Jan 2025 12:34:14 +0000 |
| parents | 2cb46628feec |
| children | 0e21568dec72 |
comparison
equal
deleted
inserted
replaced
| 0:2cb46628feec | 1:41b9b6a160d1 |
|---|---|
| 7 #include "error.h" | 7 #include "error.h" |
| 8 #include "seek.h" | 8 #include "seek.h" |
| 9 #include "byte.h" | 9 #include "byte.h" |
| 10 #include "cdb.h" | 10 #include "cdb.h" |
| 11 | 11 |
| 12 void cdb_free(struct cdb *c) | 12 |
| 13 static Cdb c; | |
| 14 | |
| 15 Cdb* cdb_new(void) | |
| 16 { | |
| 17 return &c; | |
| 18 } | |
| 19 | |
| 20 void cdb_free(Cdb *c) | |
| 13 { | 21 { |
| 14 if (c->map) { | 22 if (c->map) { |
| 15 munmap(c->map,c->size); | 23 munmap(c->map,c->size); |
| 16 c->map = 0; | 24 c->map = 0; |
| 17 } | 25 } |
| 18 } | 26 } |
| 19 | 27 |
| 20 void cdb_findstart(struct cdb *c) | 28 uint32 cdb_len() { |
| 29 return c.dlen; | |
| 30 } | |
| 31 | |
| 32 uint32 cdb_pos() { | |
| 33 return c.dpos; | |
| 34 } | |
| 35 | |
| 36 char *cdb_mmap() { | |
| 37 return c.map; | |
| 38 } | |
| 39 | |
| 40 uint32 cdb_msize() { | |
| 41 return c.size; | |
| 42 } | |
| 43 | |
| 44 void cdb_findstart(Cdb *c) | |
| 21 { | 45 { |
| 22 c->loop = 0; | 46 c->loop = 0; |
| 23 } | 47 } |
| 24 | 48 |
| 25 void cdb_init(struct cdb *c,int fd) | 49 void cdb_init(Cdb *c,int fd) |
| 26 { | 50 { |
| 27 struct stat st; | 51 struct stat st; |
| 28 char *x; | 52 char *x; |
| 29 | 53 |
| 30 cdb_free(c); | 54 cdb_free(c); |
| 39 c->map = x; | 63 c->map = x; |
| 40 } | 64 } |
| 41 } | 65 } |
| 42 } | 66 } |
| 43 | 67 |
| 44 int cdb_read(struct cdb *c,char *buf,unsigned int len,uint32 pos) | 68 int cdb_read(Cdb *c,char *buf,unsigned int len,uint32 pos) |
| 45 { | 69 { |
| 46 if (c->map) { | 70 if (c->map) { |
| 47 if ((pos > c->size) || (c->size - pos < len)) goto FORMAT; | 71 if ((pos > c->size) || (c->size - pos < len)) goto FORMAT; |
| 48 byte_copy(buf,len,c->map + pos); | 72 byte_copy(buf,len,c->map + pos); |
| 49 } | 73 } |
| 65 FORMAT: | 89 FORMAT: |
| 66 errno = error_proto; | 90 errno = error_proto; |
| 67 return -1; | 91 return -1; |
| 68 } | 92 } |
| 69 | 93 |
| 70 static int match(struct cdb *c,char *key,unsigned int len,uint32 pos) | 94 static int match(Cdb *c,char *key,unsigned int len,uint32 pos) |
| 71 { | 95 { |
| 72 char buf[32]; | 96 char buf[32]; |
| 73 int n; | 97 int n; |
| 74 | 98 |
| 75 while (len > 0) { | 99 while (len > 0) { |
| 82 len -= n; | 106 len -= n; |
| 83 } | 107 } |
| 84 return 1; | 108 return 1; |
| 85 } | 109 } |
| 86 | 110 |
| 87 int cdb_findnext(struct cdb *c,char *key,unsigned int len) | 111 int cdb_findnext(Cdb *c,char *key,unsigned int len) |
| 88 { | 112 { |
| 89 char buf[8]; | 113 char buf[8]; |
| 90 uint32 pos; | 114 uint32 pos; |
| 91 uint32 u; | 115 uint32 u; |
| 92 | 116 |
| 127 } | 151 } |
| 128 | 152 |
| 129 return 0; | 153 return 0; |
| 130 } | 154 } |
| 131 | 155 |
| 132 int cdb_find(struct cdb *c,char *key,unsigned int len) | 156 int cdb_find(Cdb *c,char *key,unsigned int len) |
| 133 { | 157 { |
| 134 cdb_findstart(c); | 158 cdb_findstart(c); |
| 135 return cdb_findnext(c,key,len); | 159 return cdb_findnext(c,key,len); |
| 136 } | 160 } |
