BRL-CAD
db_io.h
Go to the documentation of this file.
1 /* D B _ I O . H
2  * BRL-CAD
3  *
4  * Copyright (c) 1993-2024 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file rt/db_io.h
21  *
22  */
23 
24 #ifndef RT_DB_IO_H
25 #define RT_DB_IO_H
26 
27 #include "common.h"
28 
29 /* system headers */
30 #include <stdio.h> /* for FILE */
31 
32 /* interface headers */
33 #include "vmath.h"
34 #include "bu/avs.h"
35 #include "rt/db5.h"
36 #include "rt/defines.h"
37 
38 __BEGIN_DECLS
39 
40 
41 struct rt_db_internal; /* forward declaration */
42 
43 
44 /* db_open.c */
45 /**
46  * Ensure that the on-disk database has been completely written out of
47  * the operating system's cache.
48  */
49 RT_EXPORT extern void db_sync(struct db_i *dbip);
50 
51 
52 /**
53  * for db_open(), open the specified file as read-only
54  */
55 #define DB_OPEN_READONLY "r"
56 
57 /**
58  * for db_open(), open the specified file as read-write
59  */
60 #define DB_OPEN_READWRITE "rw"
61 
62 /**
63  * Open the named database.
64  *
65  * The 'name' parameter specifies the file or filepath to a .g
66  * geometry database file for reading and/or writing.
67  *
68  * The 'mode' parameter specifies whether to open read-only or in
69  * read-write mode, specified via the DB_OPEN_READONLY and
70  * DB_OPEN_READWRITE symbols respectively.
71  *
72  * As a convenience, the returned db_t structure's dbi_filepath field
73  * is a C-style argv array of dirs to search when attempting to open
74  * related files (such as data files for EBM solids or texture-maps).
75  * The default values are "." and the directory containing the ".g"
76  * file. They may be overridden by setting the environment variable
77  * BRLCAD_FILE_PATH.
78  *
79  * Returns:
80  * DBI_NULL error
81  * db_i * success
82  */
83 RT_EXPORT extern struct db_i *
84 db_open(const char *name, const char *mode);
85 
86 
87 /* create a new model database */
88 /**
89  * Create a new database containing just a header record, regardless
90  * of whether the database previously existed or not, and open it for
91  * reading and writing.
92  *
93  * This routine also calls db_dirbuild(), so the caller doesn't need
94  * to.
95  *
96  * Returns:
97  * DBI_NULL on error
98  * db_i * on success
99  */
100 RT_EXPORT extern struct db_i *
101 db_create(const char *name, int version);
102 
103 
104 /* close a model database */
105 /**
106  * De-register a client of this database instance, if provided, and
107  * close out the instance.
108  */
109 RT_EXPORT extern void db_close_client(struct db_i *dbip,
110  long *client);
111 
112 /**
113  * Close a database, releasing dynamic memory Wait until last user is
114  * done, though.
115  */
116 RT_EXPORT extern void db_close(struct db_i *dbip);
117 
118 /**
119  * Check if a specified path is a .g database file, based on identification (or
120  * not) of a valid .g file header. Returns the dbi_version if the file has a
121  * valid header, and -1 otherwise.
122  *
123  * Note that this routine does NOT do the endian-flipping tests to spot
124  * big-endian v4 .g files in the case where a valid header is not found - files
125  * are assumed to be either v5 or little endian v4 formatted. Codes willing to
126  * take the performance vs robustness tradeoff of the endian flipping check
127  * (which will do more file reading than this basic check) should use db_open.
128  */
129 RT_EXPORT extern int db_filetype(const char *file_path);
130 
131 /* dump a full copy of a database */
132 /**
133  * Dump a full copy of one database into another. This is a good way
134  * of committing a ".inmem" database to a ".g" file. The input is a
135  * database instance, the output is a LIBWDB object, which could be a
136  * disk file or another database instance.
137  *
138  * Returns -
139  * -1 error
140  * 0 success
141  */
142 RT_EXPORT extern int db_dump(struct rt_wdb *wdbp,
143  struct db_i *dbip);
144 
145 /**
146  * Obtain an additional instance of this same database. A new client
147  * is registered at the same time if one is specified.
148  */
149 RT_EXPORT extern struct db_i *db_clone_dbi(struct db_i *dbip,
150  long *client);
151 
152 
153 /**
154  * Create a v5 database "free" object of the specified size, and place
155  * it at the indicated location in the database.
156  *
157  * There are two interesting cases:
158  * - The free object is "small". Just write it all at once.
159  * - The free object is "large". Write header and trailer
160  * separately
161  *
162  * @return 0 OK
163  * @return -1 Fail. This is a horrible error.
164  */
165 RT_EXPORT extern int db5_write_free(struct db_i *dbip,
166  struct directory *dp,
167  size_t length);
168 
169 
170 /**
171  * Change the size of a v5 database object.
172  *
173  * If the object is getting smaller, break it into two pieces, and
174  * write out free objects for both. The caller is expected to
175  * re-write new data on the first one.
176  *
177  * If the object is getting larger, seek a suitable "hole" large
178  * enough to hold it, throwing back any surplus, properly marked.
179  *
180  * If the object is getting larger and there is no suitable "hole" in
181  * the database, extend the file, write a free object in the new
182  * space, and write a free object in the old space.
183  *
184  * There is no point to trying to extend in place, that would require
185  * two searches through the memory map, and doesn't save any disk I/O.
186  *
187  * Returns -
188  * 0 OK
189  * -1 Failure
190  */
191 RT_EXPORT extern int db5_realloc(struct db_i *dbip,
192  struct directory *dp,
193  struct bu_external *ep);
194 
195 
196 /**
197  * A routine for merging together the three optional parts of an
198  * object into the final on-disk format. Results in extra data
199  * copies, but serves as a starting point for testing. Any of name,
200  * attrib, and body may be null.
201  */
202 RT_EXPORT extern void db5_export_object3(struct bu_external *out,
203  int dli,
204  const char *name,
205  const unsigned char hidden,
206  const struct bu_external *attrib,
207  const struct bu_external *body,
208  int major,
209  int minor,
210  int a_zzz,
211  int b_zzz);
212 
213 
214 /**
215  * The attributes are taken from ip->idb_avs
216  *
217  * If present, convert attributes to on-disk format. This must happen
218  * after exporting the body, in case the ft_export5() method happened
219  * to extend the attribute set. Combinations are one "solid" which
220  * does this.
221  *
222  * The internal representation is NOT freed, that's the caller's job.
223  *
224  * The 'ext' pointer is accepted in uninitialized form, and an
225  * initialized structure is always returned, so that the caller may
226  * free it even when an error return is given.
227  *
228  * Returns -
229  * 0 OK
230  * -1 FAIL
231  */
232 RT_EXPORT extern int rt_db_cvt_to_external5(struct bu_external *ext,
233  const char *name,
234  const struct rt_db_internal *ip,
235  double conv2mm,
236  struct db_i *dbip,
237  struct resource *resp,
238  const int major);
239 
240 
241 /*
242  * Modify name of external object, if necessary.
243  */
244 RT_EXPORT extern int db_wrap_v5_external(struct bu_external *ep,
245  const char *name);
246 
247 
248 /**
249  * Given an external representation of a database object, convert
250  * it into its internal representation.
251  *
252  * Returns -
253  * <0 On error
254  * id On success.
255  */
256 RT_EXPORT extern int
258  struct rt_db_internal *ip,
259  const struct bu_external *ep,
260  const char *name,
261  const struct db_i *dbip,
262  const mat_t mat,
263  struct resource *resp);
264 
265 /**
266  * Get an object from the database, and convert it into its internal
267  * representation.
268  *
269  * Applications and middleware shouldn't call this directly, they
270  * should use the generic interface "rt_db_get_internal()".
271  *
272  * Returns -
273  * <0 On error
274  * id On success.
275  */
276 RT_EXPORT extern int rt_db_get_internal5(struct rt_db_internal *ip,
277  const struct directory *dp,
278  const struct db_i *dbip,
279  const mat_t mat,
280  struct resource *resp);
281 
282 
283 /**
284  * Convert the internal representation of a solid to the external one,
285  * and write it into the database.
286  *
287  * Applications and middleware shouldn't call this directly, they
288  * should use the version-generic interface "rt_db_put_internal()".
289  *
290  * The internal representation is always freed. (Not the pointer,
291  * just the contents).
292  *
293  * Returns -
294  * <0 error
295  * 0 success
296  */
297 RT_EXPORT extern int rt_db_put_internal5(struct directory *dp,
298  struct db_i *dbip,
299  struct rt_db_internal *ip,
300  struct resource *resp,
301  const int major);
302 
303 
304 /**
305  * Make only the front (header) portion of a free object. This is
306  * used when operating on very large contiguous free objects in the
307  * database (e.g. 50 MBytes).
308  */
309 RT_EXPORT extern void db5_make_free_object_hdr(struct bu_external *ep,
310  size_t length);
311 
312 
313 /**
314  * Make a complete, zero-filled, free object. Note that free objects
315  * can sometimes get quite large.
316  */
317 RT_EXPORT extern void db5_make_free_object(struct bu_external *ep,
318  size_t length);
319 
320 
321 /**
322  * Given a variable-width length field character pointer (cp) in
323  * network order (XDR), store it in *lenp.
324  *
325  * Format is typically expected to be one of:
326  * DB5HDR_WIDTHCODE_8BIT
327  * DB5HDR_WIDTHCODE_16BIT
328  * DB5HDR_WIDTHCODE_32BIT
329  * DB5HDR_WIDTHCODE_64BIT
330  *
331  * Returns -
332  * The number of bytes of input that were decoded.
333  */
334 RT_EXPORT extern size_t db5_decode_signed(size_t *lenp,
335  const unsigned char *cp,
336  int format);
337 
338 /**
339  * Given a variable-width length field in network order (XDR), store
340  * it in *lenp.
341  *
342  * This routine processes unsigned values.
343  *
344  * Returns -
345  * The number of bytes of input that were decoded.
346  */
347 RT_EXPORT extern size_t db5_decode_length(size_t *lenp,
348  const unsigned char *cp,
349  int format);
350 #if defined(USE_BINARY_ATTRIBUTES)
351 /**
352  * Given a pointer to a binary attribute, determine its length.
353  */
354 RT_EXPORT extern void decode_binary_attribute(const size_t len,
355  const unsigned char *cp);
356 #endif
357 
358 /**
359  * Given a number to encode, decide which is the smallest encoding
360  * format which will contain it.
361  */
362 RT_EXPORT extern int db5_select_length_encoding(size_t len);
363 
364 
365 RT_EXPORT extern void db5_import_color_table(char *cp);
366 
367 /**
368  * Given a value and a variable-width format spec, store it in network
369  * order.
370  *
371  * Returns -
372  * pointer to next available byte.
373  */
374 RT_EXPORT extern unsigned char *db5_encode_length(unsigned char *cp,
375  size_t val,
376  int format);
377 
378 /**
379  * Given a pointer to the memory for a serialized database object, get
380  * a raw internal representation.
381  *
382  * Returns -
383  * on success, pointer to next unused byte in 'ip' after object got;
384  * NULL, on error.
385  */
386 RT_EXPORT extern const unsigned char *db5_get_raw_internal_ptr(struct db5_raw_internal *rip,
387  const unsigned char *ip);
388 
389 
390 /**
391  * Given a file pointer to an open geometry database positioned on a
392  * serialized object, get a raw internal representation.
393  *
394  * Returns -
395  * 0 on success
396  * -1 on EOF
397  * -2 on error
398  */
399 RT_EXPORT extern int db5_get_raw_internal_fp(struct db5_raw_internal *rip,
400  FILE *fp);
401 
402 
403 /**
404  * Verify that this is a valid header for a BRL-CAD v5 database.
405  *
406  * Returns -
407  * 0 Not valid v5 header
408  * 1 Valid v5 header
409  */
410 RT_EXPORT extern int db5_header_is_valid(const unsigned char *hp);
411 
412 
413 /**
414  * write an ident header (title and units) to the provided file
415  * pointer.
416  *
417  * Returns -
418  * 0 Success
419  * -1 Error
420  */
421 RT_EXPORT extern int db5_fwrite_ident(FILE *,
422  const char *,
423  double);
424 
425 
426 /**
427  *
428  * Given that caller already has an external representation of the
429  * database object, update it to have a new name (taken from
430  * dp->d_namep) in that external representation, and write the new
431  * object into the database, obtaining different storage if the size
432  * has changed.
433  *
434  * Changing the name on a v5 object is a relatively expensive
435  * operation.
436  *
437  * Caller is responsible for freeing memory of external
438  * representation, using bu_free_external().
439  *
440  * This routine is used to efficiently support MGED's "cp" and "keep"
441  * commands, which don't need to import and decompress objects just to
442  * rename and copy them.
443  *
444  * Returns -
445  * -1 error
446  * 0 success
447  */
448 RT_EXPORT extern int db_put_external5(struct bu_external *ep,
449  struct directory *dp,
450  struct db_i *dbip);
451 
452 /**
453  * As the v4 database does not really have the notion of "wrapping",
454  * this function writes the object name into the proper place (a
455  * standard location in all granules).
456  */
457 RT_EXPORT extern void db_wrap_v4_external(struct bu_external *op,
458  const char *name);
459 
460 
461 /* db_io.c */
462 RT_EXPORT extern int db_write(struct db_i *dbip,
463  const void * addr,
464  size_t count,
465  b_off_t offset);
466 
467 /**
468  * Add name from dp->d_namep to external representation of solid, and
469  * write it into a file.
470  *
471  * Caller is responsible for freeing memory of external
472  * representation, using bu_free_external().
473  *
474  * The 'name' field of the external representation is modified to
475  * contain the desired name. The 'ep' parameter cannot be const.
476  *
477  * THIS ROUTINE ONLY SUPPORTS WRITING V4 GEOMETRY.
478  *
479  * Returns -
480  * <0 error
481  * 0 OK
482  *
483  * NOTE: Callers of this should be using wdb_export_external()
484  * instead.
485  */
486 RT_EXPORT extern int db_fwrite_external(FILE *fp,
487  const char *name,
488  struct bu_external *ep);
489 
490 /* malloc & read records */
491 
492 /**
493  * Retrieve all records in the database pertaining to an object, and
494  * place them in malloc()'ed storage, which the caller is responsible
495  * for free()'ing.
496  *
497  * This loads the combination into a local record buffer. This is in
498  * external v4 format.
499  *
500  * Returns -
501  * union record * - OK
502  * (union record *)0 - FAILURE
503  */
504 RT_EXPORT extern union record *db_getmrec(const struct db_i *,
505  const struct directory *dp);
506 /* get several records from db */
507 
508 /**
509  * Retrieve 'len' records from the database, "offset" granules into
510  * this entry.
511  *
512  * Returns -
513  * 0 OK
514  * -1 FAILURE
515  */
516 RT_EXPORT extern int db_get(const struct db_i *,
517  const struct directory *dp,
518  union record *where,
519  b_off_t offset,
520  size_t len);
521 /* put several records into db */
522 
523 /**
524  * Store 'len' records to the database, "offset" granules into this
525  * entry.
526  *
527  * Returns:
528  * 0 OK
529  * non-0 FAILURE
530  */
531 RT_EXPORT extern int db_put(struct db_i *,
532  const struct directory *dp,
533  union record *where,
534  b_off_t offset, size_t len);
535 
536 /**
537  * Obtains a object from the database, leaving it in external
538  * (on-disk) format.
539  *
540  * The bu_external structure represented by 'ep' is initialized here,
541  * the caller need not pre-initialize it. On error, 'ep' is left
542  * un-initialized and need not be freed, to simplify error recovery.
543  * On success, the caller is responsible for calling
544  * bu_free_external(ep);
545  *
546  * Returns -
547  * -1 error
548  * 0 success
549  */
550 RT_EXPORT extern int db_get_external(struct bu_external *ep,
551  const struct directory *dp,
552  const struct db_i *dbip);
553 
554 /**
555  * Given that caller already has an external representation of the
556  * database object, update it to have a new name (taken from
557  * dp->d_namep) in that external representation, and write the new
558  * object into the database, obtaining different storage if the size
559  * has changed.
560  *
561  * Caller is responsible for freeing memory of external
562  * representation, using bu_free_external().
563  *
564  * This routine is used to efficiently support MGED's "cp" and "keep"
565  * commands, which don't need to import objects just to rename and
566  * copy them.
567  *
568  * Returns -
569  * <0 error
570  * 0 success
571  */
572 RT_EXPORT extern int db_put_external(struct bu_external *ep,
573  struct directory *dp,
574  struct db_i *dbip);
575 
576 /* db_scan.c */
577 /* read db (to build directory) */
578 RT_EXPORT extern int db_scan(struct db_i *,
579  int (*handler)(struct db_i *,
580  const char *name,
581  b_off_t addr,
582  size_t nrec,
583  int flags,
584  void *client_data),
585  int do_old_matter,
586  void *client_data);
587 /* update db unit conversions */
588 #define db_ident(a, b, c) +++error+++
589 
590 /**
591  * Update the _GLOBAL object, which in v5 serves the place of the
592  * "ident" header record in v4 as the place to stash global
593  * information. Since every database will have one of these things,
594  * it's no problem to update it.
595  *
596  * Returns -
597  * 0 Success
598  * -1 Fatal Error
599  */
600 RT_EXPORT extern int db_update_ident(struct db_i *dbip,
601  const char *title,
602  double local2mm);
603 
604 /**
605  * Create a header for a v5 database.
606  *
607  * This routine has the same calling sequence as db_fwrite_ident()
608  * which makes a v4 database header.
609  *
610  * In the v5 database, two database objects must be created to match
611  * the semantics of what was in the v4 header:
612  *
613  * First, a database header object.
614  *
615  * Second, create a specially named attribute-only object which
616  * contains the attributes "title=" and "units=" with the values of
617  * title and local2mm respectively.
618  *
619  * Note that the current working units are specified as a conversion
620  * factor to millimeters because database dimensional values are
621  * always stored as millimeters (mm). The units conversion factor
622  * only affects the display and conversion of input values. This
623  * helps prevent error accumulation and improves numerical stability
624  * when calculations are made.
625  *
626  * This routine should only be used by db_create(). Everyone else
627  * should use db5_update_ident().
628  *
629  * Returns -
630  * 0 Success
631  * -1 Fatal Error
632  */
633 RT_EXPORT extern int db_fwrite_ident(FILE *fp,
634  const char *title,
635  double local2mm);
636 
637 /**
638  * Initialize conversion factors given the v4 database unit
639  */
640 RT_EXPORT extern void db_conversions(struct db_i *,
641  int units);
642 
643 /**
644  * Given a string, return the V4 database code representing the user's
645  * preferred editing units. The v4 database format does not have many
646  * choices.
647  *
648  * Returns -
649  * -1 Not a legal V4 database code
650  * # The V4 database code number
651  */
652 RT_EXPORT extern int db_v4_get_units_code(const char *str);
653 
654 /* db5_scan.c */
655 
656 /**
657  * A generic routine to determine the type of the database, (v4 or v5)
658  * and to invoke the appropriate db_scan()-like routine to build the
659  * in-memory directory.
660  *
661  * It is the caller's responsibility to close the database in case of
662  * error.
663  *
664  * Called from rt_dirbuild() and other places directly where a
665  * raytrace instance is not required.
666  *
667  * Returns -
668  * 0 OK
669  * -1 failure
670  */
671 RT_EXPORT extern int db_dirbuild(struct db_i *dbip);
672 RT_EXPORT extern int db_dirbuild_inmem(struct db_i *dbip, const void *data, b_off_t data_size);
673 RT_EXPORT extern struct directory *db5_diradd(struct db_i *dbip,
674  const struct db5_raw_internal *rip,
675  b_off_t laddr,
676  void *client_data);
677 
678 /**
679  * Scan a v5 database, sending each object off to a handler.
680  *
681  * Returns -
682  * 0 Success
683  * -1 Fatal Error
684  */
685 RT_EXPORT extern int db5_scan(struct db_i *dbip,
686  void (*handler)(struct db_i *,
687  const struct db5_raw_internal *,
688  b_off_t addr,
689  void *client_data),
690  void *client_data);
691 RT_EXPORT extern int db5_scan_inmem(struct db_i *dbip,
692  void (*handler)(struct db_i *,
693  const struct db5_raw_internal *,
694  b_off_t addr,
695  void *client_data),
696  void *client_data,
697  const void *data,
698  b_off_t data_size);
699 
700 /**
701  * Obtain the database version for a given database instance.
702  *
703  * Returns 4 or 5 accordingly for v4 or v5 geometry database files.
704  * Returns -1 if dbip is invalid.
705  */
706 RT_EXPORT extern int db_version(const struct db_i *dbip);
707 RT_EXPORT extern int db_version_inmem(const struct db_i *dbip, const void *data, b_off_t data_size);
708 
709 
710 /* db_corrupt.c */
711 
712 /**
713  * Detect whether a given geometry database file seems to be corrupt
714  * or invalid due to flipped endianness. Only relevant for v4
715  * geometry files that are binary-incompatible with the runtime
716  * platform.
717  *
718  * Returns true if flipping the endian type fixes all combination
719  * member matrices.
720  */
721 RT_EXPORT extern int rt_db_flip_endian(struct db_i *dbip);
722 
723 
724 /**
725  * "open" an in-memory-only database instance. this initializes a
726  * dbip for use, creating an inmem dbi_wdbp as the means to add
727  * geometry to the directory (use wdb_export_external()).
728  */
729 RT_EXPORT extern struct db_i * db_open_inmem(void);
730 
731 
732 /**
733  * creates an in-memory-only database. this is very similar to
734  * db_open_inmem() with the exception that the this routine adds a
735  * default _GLOBAL object.
736  */
737 RT_EXPORT extern struct db_i * db_create_inmem(void);
738 
739 
740 /**
741  * Transmogrify an existing directory entry to be an in-memory-only
742  * one, stealing the external representation from 'ext'.
743  */
744 RT_EXPORT extern void db_inmem(struct directory *dp,
745  struct bu_external *ext,
746  int flags,
747  struct db_i *dbip);
748 
749 /* db_lookup.c */
750 
751 /**
752  * Return the number of "struct directory" nodes in the given
753  * database.
754  */
755 RT_EXPORT extern size_t db_directory_size(const struct db_i *dbip);
756 
757 /**
758  * For debugging, ensure that all the linked-lists for the directory
759  * structure are intact.
760  */
761 RT_EXPORT extern void db_ck_directory(const struct db_i *dbip);
762 
763 /**
764  * Returns -
765  * 0 if the in-memory directory is empty
766  * 1 if the in-memory directory has entries,
767  * which implies that a db_scan() has already been performed.
768  */
769 RT_EXPORT extern int db_is_directory_non_empty(const struct db_i *dbip);
770 
771 /**
772  * Returns a hash index for a given string that corresponds with the
773  * head of that string's hash chain.
774  */
775 RT_EXPORT extern int db_dirhash(const char *str);
776 
777 /**
778  * This routine ensures that ret_name is not already in the
779  * directory. If it is, it tries a fixed number of times to modify
780  * ret_name before giving up. Note - most of the time, the hash for
781  * ret_name is computed once.
782  *
783  * Inputs -
784  * dbip database instance pointer
785  * ret_name the original name
786  * noisy to blather or not
787  *
788  * Outputs -
789  * ret_name the name to use
790  * headp pointer to the first (struct directory *) in the bucket
791  *
792  * Returns -
793  * 0 success
794  * <0 fail
795  */
796 RT_EXPORT extern int db_dircheck(struct db_i *dbip,
797  struct bu_vls *ret_name,
798  int noisy,
799  struct directory ***headp);
800 /* convert name to directory ptr */
801 
802 /**
803  * This routine takes a path or a name and returns the current directory
804  * pointer (if any) associated with the object.
805  *
806  * If given an object name, it will look up the object name in the directory
807  * table. If the name is present, a pointer to the directory struct element is
808  * returned, otherwise NULL is returned.
809  *
810  * If given a path, it will validate that the path is a valid path in the
811  * current database. If it is, a pointer to the current directory in the path
812  * (i.e. the leaf object on the path) is returned, otherwise NULL is returned.
813  *
814  * If noisy is non-zero, a print occurs, else only the return code indicates
815  * failure.
816  *
817  * Returns -
818  * struct directory if name is found
819  * RT_DIR_NULL on failure
820  */
821 RT_EXPORT extern struct directory *db_lookup(const struct db_i *,
822  const char *name,
823  int noisy);
824 
825 /* add entry to directory */
826 
827 /**
828  * Add an entry to the directory. Try to make the regular path
829  * through the code as fast as possible, to speed up building the
830  * table of contents.
831  *
832  * dbip is a pointer to a valid/opened database instance
833  *
834  * name is the string name of the object being added
835  *
836  * laddr is the offset into the file to the object
837  *
838  * len is the length of the object, number of db granules used
839  *
840  * flags are defined in raytrace.h (RT_DIR_SOLID, RT_DIR_COMB, RT_DIR_REGION,
841  * RT_DIR_INMEM, etc.) for db version 5, ptr is the minor_type
842  * (non-null pointer to valid unsigned char code)
843  *
844  * an laddr of RT_DIR_PHONY_ADDR means that database storage has not
845  * been allocated yet.
846  */
847 RT_EXPORT extern struct directory *db_diradd(struct db_i *,
848  const char *name,
849  b_off_t laddr,
850  size_t len,
851  int flags,
852  void *ptr);
853 RT_EXPORT extern struct directory *db_diradd5(struct db_i *dbip,
854  const char *name,
855  b_off_t laddr,
856  unsigned char major_type,
857  unsigned char minor_type,
858  unsigned char name_hidden,
859  size_t object_length,
860  struct bu_attribute_value_set *avs);
861 
862 /* delete entry from directory */
863 
864 /**
865  * Given a pointer to a directory entry, remove it from the linked
866  * list, and free the associated memory.
867  *
868  * It is the responsibility of the caller to have released whatever
869  * structures have been hung on the d_use_hd bu_list, first.
870  *
871  * Returns -
872  * 0 on success
873  * non-0 on failure
874  */
875 RT_EXPORT extern int db_dirdelete(struct db_i *,
876  struct directory *dp);
877 RT_EXPORT extern int db_fwrite_ident(FILE *,
878  const char *,
879  double);
880 
881 /**
882  * For debugging, print the entire contents of the database directory.
883  */
884 RT_EXPORT extern void db_pr_dir(const struct db_i *dbip);
885 
886 /**
887  * Change the name string of a directory entry. Because of the
888  * hashing function, this takes some extra work.
889  *
890  * Returns -
891  * 0 on success
892  * non-0 on failure
893  */
894 RT_EXPORT extern int db_rename(struct db_i *,
895  struct directory *,
896  const char *newname);
897 
898 
899 /**
900  * Updates the d_nref fields (which count the number of times a given
901  * entry is referenced by a COMBination in the database).
902  *
903  */
904 RT_EXPORT extern void db_update_nref(struct db_i *dbip,
905  struct resource *resp);
906 
907 
908 /* db_flags.c */
909 /**
910  * Given the internal form of a database object, return the
911  * appropriate 'flags' word for stashing in the in-memory directory of
912  * objects.
913  */
914 RT_EXPORT extern int db_flags_internal(const struct rt_db_internal *intern);
915 
916 
917 /* XXX - should use in db5_diradd() */
918 /**
919  * Given a database object in "raw" internal form, return the
920  * appropriate 'flags' word for stashing in the in-memory directory of
921  * objects.
922  */
923 RT_EXPORT extern int db_flags_raw_internal(const struct db5_raw_internal *intern);
924 
925 /* db_alloc.c */
926 
927 /* allocate "count" granules */
928 RT_EXPORT extern int db_alloc(struct db_i *,
929  struct directory *dp,
930  size_t count);
931 /* delete "recnum" from entry */
932 RT_EXPORT extern int db_delrec(struct db_i *,
933  struct directory *dp,
934  int recnum);
935 /* delete all granules assigned dp */
936 RT_EXPORT extern int db_delete(struct db_i *,
937  struct directory *dp);
938 /* write FREE records from 'start' */
939 RT_EXPORT extern int db_zapper(struct db_i *,
940  struct directory *dp,
941  size_t start);
942 
943 /**
944  * This routine is called by the RT_GET_DIRECTORY macro when the
945  * freelist is exhausted. Rather than simply getting one additional
946  * structure, we get a whole batch, saving overhead.
947  */
948 RT_EXPORT extern void db_alloc_directory_block(struct resource *resp);
949 
950 /**
951  * This routine is called by the GET_SEG macro when the freelist is
952  * exhausted. Rather than simply getting one additional structure, we
953  * get a whole batch, saving overhead. When this routine is called,
954  * the seg resource must already be locked. malloc() locking is done
955  * in bu_malloc.
956  */
957 RT_EXPORT extern void rt_alloc_seg_block(struct resource *res);
958 
959 
960 /**
961  * Read named MGED db, build toc.
962  */
963 RT_EXPORT extern struct rt_i *rt_dirbuild(const char *filename, char *buf, int len);
964 RT_EXPORT extern struct rt_i *rt_dirbuild_inmem(const void *data, b_off_t data_size, char *buf, int len);
965 
966 
967 /* db5_types.c */
968 RT_EXPORT extern int db5_type_tag_from_major(char **tag,
969  const int major);
970 
971 RT_EXPORT extern int db5_type_descrip_from_major(char **descrip,
972  const int major);
973 
974 RT_EXPORT extern int db5_type_tag_from_codes(char **tag,
975  const int major,
976  const int minor);
977 
978 RT_EXPORT extern int db5_type_descrip_from_codes(char **descrip,
979  const int major,
980  const int minor);
981 
982 RT_EXPORT extern int db5_type_codes_from_tag(int *major,
983  int *minor,
984  const char *tag);
985 
986 RT_EXPORT extern int db5_type_codes_from_descrip(int *major,
987  int *minor,
988  const char *descrip);
989 
990 RT_EXPORT extern size_t db5_type_sizeof_h_binu(const int minor);
991 
992 RT_EXPORT extern size_t db5_type_sizeof_n_binu(const int minor);
993 
994 __END_DECLS
995 
996 #endif /* RT_DB_IO_H */
997 
998 /*
999  * Local Variables:
1000  * tab-width: 8
1001  * mode: C
1002  * indent-tabs-mode: t
1003  * c-file-style: "stroustrup"
1004  * End:
1005  * ex: shiftwidth=4 tabstop=8
1006  */
Header file for the BRL-CAD common definitions.
Definition of the BRL-CAD "v5" database format used for new ".g" files.
void db5_make_free_object_hdr(struct bu_external *ep, size_t length)
int db_put_external(struct bu_external *ep, struct directory *dp, struct db_i *dbip)
void db5_import_color_table(char *cp)
int db_get_external(struct bu_external *ep, const struct directory *dp, const struct db_i *dbip)
void db5_make_free_object(struct bu_external *ep, size_t length)
int db_wrap_v5_external(struct bu_external *ep, const char *name)
void db_close(struct db_i *dbip)
int db5_write_free(struct db_i *dbip, struct directory *dp, size_t length)
void db_close_client(struct db_i *dbip, long *client)
struct db_i * db_create(const char *name, int version)
struct directory * db_diradd(struct db_i *, const char *name, b_off_t laddr, size_t len, int flags, void *ptr)
int db_update_ident(struct db_i *dbip, const char *title, double local2mm)
int db_scan(struct db_i *, int(*handler)(struct db_i *, const char *name, b_off_t addr, size_t nrec, int flags, void *client_data), int do_old_matter, void *client_data)
int db5_type_descrip_from_major(char **descrip, const int major)
int db_rename(struct db_i *, struct directory *, const char *newname)
size_t db5_decode_length(size_t *lenp, const unsigned char *cp, int format)
int db_dirbuild(struct db_i *dbip)
int db5_type_tag_from_major(char **tag, const int major)
int db5_type_tag_from_codes(char **tag, const int major, const int minor)
size_t db5_type_sizeof_h_binu(const int minor)
int db_filetype(const char *file_path)
int rt_db_put_internal5(struct directory *dp, struct db_i *dbip, struct rt_db_internal *ip, struct resource *resp, const int major)
int db_flags_raw_internal(const struct db5_raw_internal *intern)
int db_delete(struct db_i *, struct directory *dp)
const unsigned char * db5_get_raw_internal_ptr(struct db5_raw_internal *rip, const unsigned char *ip)
int db_dirhash(const char *str)
int db_write(struct db_i *dbip, const void *addr, size_t count, b_off_t offset)
int rt_db_external5_to_internal5(struct rt_db_internal *ip, const struct bu_external *ep, const char *name, const struct db_i *dbip, const mat_t mat, struct resource *resp)
void rt_alloc_seg_block(struct resource *res)
int db_dirbuild_inmem(struct db_i *dbip, const void *data, b_off_t data_size)
int db_version(const struct db_i *dbip)
int db_v4_get_units_code(const char *str)
int db_alloc(struct db_i *, struct directory *dp, size_t count)
int db_dirdelete(struct db_i *, struct directory *dp)
int db_put(struct db_i *, const struct directory *dp, union record *where, b_off_t offset, size_t len)
int db5_scan_inmem(struct db_i *dbip, void(*handler)(struct db_i *, const struct db5_raw_internal *, b_off_t addr, void *client_data), void *client_data, const void *data, b_off_t data_size)
int db5_realloc(struct db_i *dbip, struct directory *dp, struct bu_external *ep)
int db5_fwrite_ident(FILE *, const char *, double)
int db_put_external5(struct bu_external *ep, struct directory *dp, struct db_i *dbip)
int db_fwrite_external(FILE *fp, const char *name, struct bu_external *ep)
int db5_select_length_encoding(size_t len)
int rt_db_flip_endian(struct db_i *dbip)
void db_sync(struct db_i *dbip)
int db_zapper(struct db_i *, struct directory *dp, size_t start)
int rt_db_cvt_to_external5(struct bu_external *ext, const char *name, const struct rt_db_internal *ip, double conv2mm, struct db_i *dbip, struct resource *resp, const int major)
int db_get(const struct db_i *, const struct directory *dp, union record *where, b_off_t offset, size_t len)
void db_update_nref(struct db_i *dbip, struct resource *resp)
int db_dump(struct rt_wdb *wdbp, struct db_i *dbip)
int db_flags_internal(const struct rt_db_internal *intern)
struct db_i * db_open(const char *name, const char *mode)
void db_ck_directory(const struct db_i *dbip)
size_t db5_decode_signed(size_t *lenp, const unsigned char *cp, int format)
struct db_i * db_open_inmem(void)
int db5_type_codes_from_tag(int *major, int *minor, const char *tag)
int db5_type_codes_from_descrip(int *major, int *minor, const char *descrip)
void db_pr_dir(const struct db_i *dbip)
int db5_get_raw_internal_fp(struct db5_raw_internal *rip, FILE *fp)
struct db_i * db_create_inmem(void)
int db_delrec(struct db_i *, struct directory *dp, int recnum)
void db_conversions(struct db_i *, int units)
void db_inmem(struct directory *dp, struct bu_external *ext, int flags, struct db_i *dbip)
struct rt_i * rt_dirbuild(const char *filename, char *buf, int len)
int rt_db_get_internal5(struct rt_db_internal *ip, const struct directory *dp, const struct db_i *dbip, const mat_t mat, struct resource *resp)
unsigned char * db5_encode_length(unsigned char *cp, size_t val, int format)
int db_is_directory_non_empty(const struct db_i *dbip)
struct directory * db_diradd5(struct db_i *dbip, const char *name, b_off_t laddr, unsigned char major_type, unsigned char minor_type, unsigned char name_hidden, size_t object_length, struct bu_attribute_value_set *avs)
struct rt_i * rt_dirbuild_inmem(const void *data, b_off_t data_size, char *buf, int len)
int db5_header_is_valid(const unsigned char *hp)
struct directory * db5_diradd(struct db_i *dbip, const struct db5_raw_internal *rip, b_off_t laddr, void *client_data)
void db_alloc_directory_block(struct resource *resp)
int db_dircheck(struct db_i *dbip, struct bu_vls *ret_name, int noisy, struct directory ***headp)
int db5_type_descrip_from_codes(char **descrip, const int major, const int minor)
void db5_export_object3(struct bu_external *out, int dli, const char *name, const unsigned char hidden, const struct bu_external *attrib, const struct bu_external *body, int major, int minor, int a_zzz, int b_zzz)
int db_version_inmem(const struct db_i *dbip, const void *data, b_off_t data_size)
struct directory * db_lookup(const struct db_i *, const char *name, int noisy)
union record * db_getmrec(const struct db_i *, const struct directory *dp)
int db5_scan(struct db_i *dbip, void(*handler)(struct db_i *, const struct db5_raw_internal *, b_off_t addr, void *client_data), void *client_data)
size_t db5_type_sizeof_n_binu(const int minor)
size_t db_directory_size(const struct db_i *dbip)
struct db_i * db_clone_dbi(struct db_i *dbip, long *client)
int db_fwrite_ident(FILE *fp, const char *title, double local2mm)
void db_wrap_v4_external(struct bu_external *op, const char *name)
void int char * mode
Definition: tig.h:179
void int char int * length
Definition: tig.h:180
#define b_off_t
Definition: common.h:223
fastf_t mat_t[ELEMENTS_PER_MAT]
4x4 matrix
Definition: vmath.h:370
Definition: vls.h:53
void * ptr
ptr to in-memory-only obj
Definition: directory.h:64
Definition: wdb.h:62
Definition: db4.h:413
fundamental vector, matrix, quaternion math macros