comparison src/database.c @ 173:8eaf7971accc r20-3b13

Import from CVS: tag r20-3b13
author cvs
date Mon, 13 Aug 2007 09:49:09 +0200
parents 6b37e6ddd302
children 6075d714658b
comparison
equal deleted inserted replaced
172:a38aed19690b 173:8eaf7971accc
84 { 84 {
85 struct lcrecord_header header; 85 struct lcrecord_header header;
86 Lisp_Object fname; 86 Lisp_Object fname;
87 XEMACS_DB_TYPE type; 87 XEMACS_DB_TYPE type;
88 int mode; 88 int mode;
89 int ackcess; 89 int access_;
90 int dberrno; 90 int dberrno;
91 void *db_handle; 91 void *db_handle;
92 DB_FUNCS *funcs; 92 DB_FUNCS *funcs;
93 #ifdef MULE 93 #ifdef MULE
94 Lisp_Object coding_system; 94 Lisp_Object coding_system;
115 struct database_struct *dbase 115 struct database_struct *dbase
116 = alloc_lcrecord (sizeof (struct database_struct), lrecord_database); 116 = alloc_lcrecord (sizeof (struct database_struct), lrecord_database);
117 117
118 dbase->fname = Qnil; 118 dbase->fname = Qnil;
119 dbase->db_handle = NULL; 119 dbase->db_handle = NULL;
120 dbase->ackcess = 0; 120 dbase->access_ = 0;
121 dbase->mode = 0; 121 dbase->mode = 0;
122 dbase->dberrno = 0; 122 dbase->dberrno = 0;
123 dbase->type = DB_UNKNOWN; 123 dbase->type = DB_UNKNOWN;
124 #ifdef MULE 124 #ifdef MULE
125 dbase->coding_system = Fget_coding_system (Qbinary); 125 dbase->coding_system = Fget_coding_system (Qbinary);
126 #endif 126 #endif
127 return (dbase); 127 return dbase;
128 } 128 }
129 129
130 static Lisp_Object 130 static Lisp_Object
131 mark_database (Lisp_Object obj, void (*markobj) (Lisp_Object)) 131 mark_database (Lisp_Object obj, void (*markobj) (Lisp_Object))
132 { 132 {
133 struct database_struct *dbase = XDATABASE (obj); 133 struct database_struct *dbase = XDATABASE (obj);
134 134
135 ((markobj) (dbase->fname)); 135 ((markobj) (dbase->fname));
136 return (Qnil); 136 return Qnil;
137 } 137 }
138 138
139 static void 139 static void
140 print_database (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag) 140 print_database (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
141 { 141 {
151 CONST char *type; 151 CONST char *type;
152 CONST char *subtype; 152 CONST char *subtype;
153 CONST char *perms; 153 CONST char *perms;
154 154
155 perms = (!dbase->db_handle) ? "closed" : 155 perms = (!dbase->db_handle) ? "closed" :
156 (dbase->ackcess & O_WRONLY) ? "writeonly" : 156 (dbase->access_ & O_WRONLY) ? "writeonly" :
157 (dbase->ackcess & O_RDWR) ? "readwrite" : "readonly"; 157 (dbase->access_ & O_RDWR) ? "readwrite" : "readonly";
158 158
159 type = dbase->funcs->get_type (dbase); 159 type = dbase->funcs->get_type (dbase);
160 subtype = dbase->funcs->get_subtype (dbase); 160 subtype = dbase->funcs->get_subtype (dbase);
161 161
162 sprintf (buf, "#<database %s (%s/%s/%s) 0x%x>", 162 sprintf (buf, "#<database %s (%s/%s/%s) 0x%x>",
194 if (DATABASE_LIVE_P (db)) 194 if (DATABASE_LIVE_P (db))
195 db->funcs->close (db); 195 db->funcs->close (db);
196 else 196 else
197 signal_simple_error ("Attempting to access closed database", obj); 197 signal_simple_error ("Attempting to access closed database", obj);
198 198
199 return (Qnil); 199 return Qnil;
200 } 200 }
201 201
202 DEFUN ("database-type", Fdatabase_type, 1, 1, 0, /* 202 DEFUN ("database-type", Fdatabase_type, 1, 1, 0, /*
203 Return the type of database OBJ. 203 Return the type of database OBJ.
204 */ 204 */
219 struct database_struct *db; 219 struct database_struct *db;
220 220
221 CHECK_DATABASE (obj); 221 CHECK_DATABASE (obj);
222 db = XDATABASE (obj); 222 db = XDATABASE (obj);
223 223
224 return (intern (db->funcs->get_subtype (db))); 224 return intern (db->funcs->get_subtype (db));
225 } 225 }
226 226
227 DEFUN ("database-live-p", Fdatabase_live_p, 1, 1, 0, /* 227 DEFUN ("database-live-p", Fdatabase_live_p, 1, 1, 0, /*
228 Return t iff OBJ is an active database, else nil. 228 Return t iff OBJ is an active database, else nil.
229 */ 229 */
231 { 231 {
232 struct database_struct *db; 232 struct database_struct *db;
233 CHECK_DATABASE (obj); 233 CHECK_DATABASE (obj);
234 db = XDATABASE (obj); 234 db = XDATABASE (obj);
235 235
236 return (DATABASE_LIVE_P (db) ? Qt : Qnil); 236 return DATABASE_LIVE_P (db) ? Qt : Qnil;
237 } 237 }
238 238
239 DEFUN ("database-file-name", Fdatabase_file_name, 1, 1, 0, /* 239 DEFUN ("database-file-name", Fdatabase_file_name, 1, 1, 0, /*
240 Return the filename associated with the database OBJ. 240 Return the filename associated with the database OBJ.
241 */ 241 */
242 (obj)) 242 (obj))
243 { 243 {
244 struct database_struct *db; 244 struct database_struct *db;
245 CHECK_DATABASE (obj); 245 CHECK_DATABASE (obj);
246 db = XDATABASE (obj); 246 db = XDATABASE (obj);
247 return (db->fname); 247 return db->fname;
248 } 248 }
249 249
250 DEFUN ("databasep", Fdatabasep, 1, 1, 0, /* 250 DEFUN ("databasep", Fdatabasep, 1, 1, 0, /*
251 Return t iff OBJ is a database, else nil. 251 Return t iff OBJ is a database, else nil.
252 */ 252 */
253 (obj)) 253 (obj))
254 { 254 {
255 return ((DATABASEP (obj)) ? Qt : Qnil); 255 return DATABASEP (obj) ? Qt : Qnil;
256 } 256 }
257 257
258 #ifdef HAVE_DBM 258 #ifdef HAVE_DBM
259 static void 259 static void
260 dbm_map (struct database_struct *db, Lisp_Object func) 260 dbm_map (struct database_struct *db, Lisp_Object func)
310 dbm_remove (struct database_struct *db, Lisp_Object key) 310 dbm_remove (struct database_struct *db, Lisp_Object key)
311 { 311 {
312 datum keydatum; 312 datum keydatum;
313 keydatum.dptr = (char *) XSTRING_DATA (key); 313 keydatum.dptr = (char *) XSTRING_DATA (key);
314 keydatum.dsize = XSTRING_LENGTH (key); 314 keydatum.dsize = XSTRING_LENGTH (key);
315 return (dbm_delete (db->db_handle, keydatum)); 315 return dbm_delete (db->db_handle, keydatum);
316 } 316 }
317 317
318 static Lisp_Object 318 static Lisp_Object
319 dbm_lisp_type (struct database_struct *db) 319 dbm_lisp_type (struct database_struct *db)
320 { 320 {
321 return (Qdbm); 321 return Qdbm;
322 } 322 }
323 323
324 static CONST char * 324 static CONST char *
325 dbm_type (struct database_struct *db) 325 dbm_type (struct database_struct *db)
326 { 326 {
327 return ("dbm"); 327 return "dbm";
328 } 328 }
329 329
330 static CONST char * 330 static CONST char *
331 dbm_subtype (struct database_struct *db) 331 dbm_subtype (struct database_struct *db)
332 { 332 {
333 return ("nil"); 333 return "nil";
334 } 334 }
335 335
336 static void * 336 static void *
337 new_dbm_file (CONST char *file, Lisp_Object subtype, int ackcess, int mode) 337 new_dbm_file (CONST char *file, Lisp_Object subtype, int access_, int mode)
338 { 338 {
339 DBM *db = NULL; 339 DBM *db = NULL;
340 db = dbm_open ((char *) file, ackcess, mode); 340 db = dbm_open ((char *) file, access_, mode);
341 return (void *) db; 341 return (void *) db;
342 } 342 }
343 343
344 static Lisp_Object 344 static Lisp_Object
345 dbm_lasterr (struct database_struct *dbp) 345 dbm_lasterr (struct database_struct *dbp)
372 372
373 #ifdef HAVE_BERKELEY_DB 373 #ifdef HAVE_BERKELEY_DB
374 static Lisp_Object 374 static Lisp_Object
375 berkdb_lisp_type (struct database_struct *db) 375 berkdb_lisp_type (struct database_struct *db)
376 { 376 {
377 return (Qberkeley_db); 377 return Qberkeley_db;
378 } 378 }
379 379
380 static CONST char * 380 static CONST char *
381 berkdb_type (struct database_struct *db) 381 berkdb_type (struct database_struct *db)
382 { 382 {
383 return ("berkeley"); 383 return "berkeley";
384 } 384 }
385 385
386 static CONST char * 386 static CONST char *
387 berkdb_subtype (struct database_struct *db) 387 berkdb_subtype (struct database_struct *db)
388 { 388 {
389 DB *temp = (DB *)db->db_handle; 389 DB *temp = (DB *)db->db_handle;
390 390
391 if (!temp) 391 if (!temp)
392 return ("nil"); 392 return "nil";
393 393
394 switch (temp->type) 394 switch (temp->type)
395 { 395 {
396 case DB_BTREE: 396 case DB_BTREE:
397 return ("btree"); 397 return "btree";
398 case DB_HASH: 398 case DB_HASH:
399 return ("hash"); 399 return "hash";
400 case DB_RECNO: 400 case DB_RECNO:
401 return ("recno"); 401 return "recno";
402 } 402 }
403 return ("unknown"); 403 return "unknown";
404 } 404 }
405 405
406 static void * 406 static void *
407 berkdb_open (CONST char *file, Lisp_Object subtype, int ackcess, int mode) 407 berkdb_open (CONST char *file, Lisp_Object subtype, int access_, int mode)
408 { 408 {
409 DB *db; 409 DB *db;
410 DBTYPE real_subtype; 410 DBTYPE real_subtype;
411 411
412 if (EQ (subtype, Qhash) || NILP (subtype)) 412 if (EQ (subtype, Qhash) || NILP (subtype))
416 else if (EQ (subtype, Qrecno)) 416 else if (EQ (subtype, Qrecno))
417 real_subtype = DB_RECNO; 417 real_subtype = DB_RECNO;
418 else 418 else
419 signal_simple_error ("Unsupported subtype", subtype); 419 signal_simple_error ("Unsupported subtype", subtype);
420 420
421 db = dbopen (file, ackcess, mode, real_subtype, NULL); 421 db = dbopen (file, access_, mode, real_subtype, NULL);
422 422
423 return (void *) db; 423 return (void *) db;
424 } 424 }
425 425
426 static Lisp_Object 426 static Lisp_Object
440 keydatum.size = XSTRING_LENGTH (key); 440 keydatum.size = XSTRING_LENGTH (key);
441 441
442 status = dbp->get (dbp, &keydatum, &valdatum, 0); 442 status = dbp->get (dbp, &keydatum, &valdatum, 0);
443 443
444 if (!status) 444 if (!status)
445 return (make_string (valdatum.data, valdatum.size)); 445 return make_string (valdatum.data, valdatum.size);
446 446
447 db->dberrno = (status == 1) ? -1 : errno; 447 db->dberrno = (status == 1) ? -1 : errno;
448 return (Qnil); 448 return Qnil;
449 } 449 }
450 450
451 static int 451 static int
452 berkdb_put (struct database_struct *db, 452 berkdb_put (struct database_struct *db,
453 Lisp_Object key, 453 Lisp_Object key,
541 if (NILP (obj)) 541 if (NILP (obj))
542 return lisp_strerror (errno); 542 return lisp_strerror (errno);
543 543
544 CHECK_DATABASE (obj); 544 CHECK_DATABASE (obj);
545 db = XDATABASE (obj); 545 db = XDATABASE (obj);
546 return (db->funcs->last_error (db)); 546 return db->funcs->last_error (db);
547 } 547 }
548 548
549 DEFUN ("open-database", Fmake_database, 1, 5, 0, /* 549 DEFUN ("open-database", Fmake_database, 1, 5, 0, /*
550 Open database FILE, using database method TYPE and SUBTYPE, with 550 Open database FILE, using database method TYPE and SUBTYPE, with
551 access rights ACCESS and permissions MODE. ACCESS can be any 551 access rights ACCESS and permissions MODE. ACCESS can be any
552 combination of 'r' 'w' and '+', for read, write, and creation flags. 552 combination of 'r' 'w' and '+', for read, write, and creation flags.
553 */ 553 */
554 (file, type, subtype, ackcess, mode)) 554 (file, type, subtype, access_, mode))
555 { 555 {
556 Lisp_Object retval = Qnil; 556 Lisp_Object retval = Qnil;
557 int modemask; 557 int modemask;
558 int accessmask = 0; 558 int accessmask = 0;
559 XEMACS_DB_TYPE the_type; 559 XEMACS_DB_TYPE the_type;
561 struct database_struct *dbase = NULL; 561 struct database_struct *dbase = NULL;
562 void *db = NULL; 562 void *db = NULL;
563 563
564 CHECK_STRING (file); 564 CHECK_STRING (file);
565 565
566 if (NILP (ackcess)) 566 if (NILP (access_))
567 { 567 {
568 accessmask = O_RDWR | O_CREAT; 568 accessmask = O_RDWR | O_CREAT;
569 } 569 }
570 else 570 else
571 { 571 {
572 char *acc; 572 char *acc;
573 CHECK_STRING (ackcess); 573 CHECK_STRING (access_);
574 acc = (char *) XSTRING_DATA (ackcess); 574 acc = (char *) XSTRING_DATA (access_);
575 575
576 if (strchr (acc, '+')) 576 if (strchr (acc, '+'))
577 accessmask |= O_CREAT; 577 accessmask |= O_CREAT;
578 578
579 if (strchr (acc, 'r') && strchr (acc, 'w')) 579 if (strchr (acc, 'r') && strchr (acc, 'w'))
618 goto db_done; 618 goto db_done;
619 } 619 }
620 #endif 620 #endif
621 621
622 signal_simple_error ("Unsupported database type", type); 622 signal_simple_error ("Unsupported database type", type);
623 return (Qnil); 623 return Qnil;
624 624
625 db_done: 625 db_done:
626 db = funcblock->open_file ((char *) XSTRING_DATA (file), subtype, 626 db = funcblock->open_file ((char *) XSTRING_DATA (file), subtype,
627 accessmask, modemask); 627 accessmask, modemask);
628 628
629 if (!db) 629 if (!db)
630 { 630 {
631 return (Qnil); 631 return Qnil;
632 } 632 }
633 633
634 dbase = new_database (); 634 dbase = new_database ();
635 dbase->fname = file; 635 dbase->fname = file;
636 dbase->type = the_type; 636 dbase->type = the_type;
637 dbase->mode = modemask; 637 dbase->mode = modemask;
638 dbase->ackcess = accessmask; 638 dbase->access_ = accessmask;
639 dbase->db_handle = db; 639 dbase->db_handle = db;
640 dbase->funcs = funcblock; 640 dbase->funcs = funcblock;
641 XSETDATABASE (retval, dbase); 641 XSETDATABASE (retval, dbase);
642 642
643 return (retval); 643 return retval;
644 } 644 }
645 645
646 DEFUN ("put-database", Fputdatabase, 3, 4, 0, /* 646 DEFUN ("put-database", Fputdatabase, 3, 4, 0, /*
647 Store KEY and VAL in DATABASE. If optinal fourth arg REPLACE is 647 Store KEY and VAL in DATABASE. If optinal fourth arg REPLACE is
648 non-nil, replace any existing entry in the database. 648 non-nil, replace any existing entry in the database.
682 682
683 DEFUN ("get-database", Fgetdatabase, 2, 3, 0, /* 683 DEFUN ("get-database", Fgetdatabase, 2, 3, 0, /*
684 Find value for KEY in DATABASE. 684 Find value for KEY in DATABASE.
685 If there is no corresponding value, return DEFAULT (defaults to nil). 685 If there is no corresponding value, return DEFAULT (defaults to nil).
686 */ 686 */
687 (key, dbase, defalt)) 687 (key, dbase, default_))
688 { 688 {
689 Lisp_Object retval; 689 Lisp_Object retval;
690 struct database_struct *db; 690 struct database_struct *db;
691 691
692 CHECK_DATABASE (dbase); 692 CHECK_DATABASE (dbase);
695 if (!DATABASE_LIVE_P (db)) 695 if (!DATABASE_LIVE_P (db))
696 signal_simple_error ("Attempting to access closed database", dbase); 696 signal_simple_error ("Attempting to access closed database", dbase);
697 697
698 retval = db->funcs->get (db, key); 698 retval = db->funcs->get (db, key);
699 699
700 return (NILP (retval) ? defalt : retval); 700 return NILP (retval) ? default_ : retval;
701 } 701 }
702 702
703 DEFUN ("map-database", Fmapdatabase, 2, 2, 0, /* 703 DEFUN ("map-database", Fmapdatabase, 2, 2, 0, /*
704 Map FUNCTION over entries in DATABASE, calling it with two args, 704 Map FUNCTION over entries in DATABASE, calling it with two args,
705 each key and value in the database. 705 each key and value in the database.