00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #ifndef lint
00040 static const char RCSid[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/librt/binary_obj.c,v 14.17 2006/09/16 02:04:24 lbutler Exp $ (BRL)";
00041 #endif
00042
00043 #include "common.h"
00044
00045 #include <stdio.h>
00046 #include <sys/stat.h>
00047 #include <math.h>
00048 #ifdef HAVE_STRING_H
00049 # include <string.h>
00050 #else
00051 # include <strings.h>
00052 #endif
00053 #ifdef HAVE_SYS_TYPES_H
00054 # include <sys/types.h>
00055 #endif
00056 #include <limits.h>
00057
00058 #include "machine.h"
00059 #include "bu.h"
00060 #include "vmath.h"
00061 #include "bn.h"
00062 #include "rtgeom.h"
00063 #include "raytrace.h"
00064 #include "wdb.h"
00065
00066 #ifndef __LONG_MAX__
00067 # define __LONG_MAX__ 2147483647L
00068 #endif
00069
00070
00071 int
00072 rt_mk_binunif(struct rt_wdb *wdbp, const char *obj_name, const char *file_name, unsigned int minor_type, long max_count)
00073 {
00074 struct stat st;
00075 unsigned int major_type=DB5_MAJORTYPE_BINARY_UNIF;
00076 #if defined(_WIN32) && !defined(__CYGWIN__)
00077 __int64 num_items=-1;
00078 __int64 obj_length=-1;
00079 #else
00080 long long num_items=-1;
00081 long long obj_length=-1;
00082 #endif
00083 int item_length=0;
00084 struct bu_mapped_file *bu_fd;
00085 struct rt_binunif_internal *bip;
00086 struct rt_db_internal intern;
00087 struct bu_external body;
00088 struct bu_external bin_ext;
00089 struct directory *dp;
00090
00091 if( (item_length=db5_type_sizeof_h_binu( minor_type ) ) <= 0 ) {
00092 bu_log( "Unrecognized minor type!!!\n" );
00093 return -1;
00094 }
00095
00096 if( stat( file_name, &st ) ) {
00097 bu_log( "Cannot stat input file(%s)", file_name );
00098 return -1;
00099 }
00100
00101 if( (bu_fd=bu_open_mapped_file( file_name, NULL)) == NULL ) {
00102 bu_log( "Cannot open input file(%s) for reading",
00103 file_name );
00104 return -1;
00105 }
00106
00107
00108 GETSTRUCT( bip, rt_binunif_internal );
00109 bip->magic = RT_BINUNIF_INTERNAL_MAGIC;
00110 bip->type = minor_type;
00111
00112 num_items = (long)(st.st_size / item_length);
00113
00114
00115 if (max_count > 0 && max_count < num_items) {
00116 num_items = max_count;
00117 }
00118
00119 obj_length = num_items * item_length;
00120
00121 if (obj_length > __LONG_MAX__) {
00122 bu_log("Unable to create binary objects larger than %ld bytes\n", __LONG_MAX__);
00123 return -1;
00124 }
00125
00126
00127 bip->count = num_items;
00128 bip->u.int8 = (char *)bu_malloc( obj_length, "binary uniform object" );
00129 memcpy( bip->u.int8, bu_fd->buf, obj_length );
00130
00131 bu_close_mapped_file( bu_fd );
00132
00133
00134 RT_INIT_DB_INTERNAL( &intern );
00135 intern.idb_major_type = major_type;
00136 intern.idb_minor_type = minor_type;
00137 intern.idb_ptr = (genptr_t)bip;
00138 intern.idb_meth = &rt_functab[ID_BINUNIF];
00139
00140
00141 if( intern.idb_meth->ft_export5( &body, &intern, 1.0, wdbp->dbip, wdbp->wdb_resp, intern.idb_minor_type ) ) {
00142
00143 bu_log( "Error while attemptimg to export %s\n", obj_name );
00144 rt_db_free_internal( &intern, wdbp->wdb_resp );
00145 return -1;
00146 }
00147
00148
00149 db5_export_object3( &bin_ext, DB5HDR_HFLAGS_DLI_APPLICATION_DATA_OBJECT,
00150 obj_name, 0, NULL, &body,
00151 intern.idb_major_type, intern.idb_minor_type,
00152 DB5_ZZZ_UNCOMPRESSED, DB5_ZZZ_UNCOMPRESSED );
00153
00154 rt_db_free_internal( &intern, wdbp->wdb_resp );
00155 bu_free_external( &body );
00156
00157
00158 if( (dp=db_diradd5( wdbp->dbip, obj_name, -1, major_type,
00159 minor_type, 0, 0, NULL )) == DIR_NULL ) {
00160 bu_log( "Error while attemptimg to add new name (%s) to the database",
00161 obj_name );
00162 bu_free_external( &bin_ext );
00163 return -1;
00164 }
00165
00166
00167 if( db_put_external5( &bin_ext, dp, wdbp->dbip ) ) {
00168 bu_log( "Error while adding new binary object (%s) to the database",
00169 obj_name );
00170 bu_free_external( &bin_ext );
00171 return -1;
00172 }
00173
00174 bu_free_external( &bin_ext );
00175
00176 return 0;
00177 }
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187