binary_obj.c

Go to the documentation of this file.
00001 /*                    B I N A R Y _ O B J . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 2001-2006 United States Government as represented by
00005  * the U.S. Army Research Laboratory.
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License
00009  * as published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Library General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this file; see the file named COPYING for more
00019  * information.
00020  */
00021 
00022 /** @addtogroup db5  */
00023 /*@{*/
00024 /** @file binary_obj.c
00025  *  Routines for writing binary objects to a BRL-CAD database
00026  *  Assumes that some of the structure of such databases are known
00027  *  by the calling routines.
00028  *
00029  *  Return codes of 0 are OK, -1 signal an error.
00030  *
00031  *  Authors -
00032  *      John R. Anderson
00033  *  Source -
00034  *      The U. S. Army Research Laboratory
00035  *      Aberdeen Proving Ground, Maryland  21005-5068  USA
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         /* create the rt_binunif internal form */
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         /* maybe only a partial file read */
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         /* just copy the bytes */
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         /* create the rt_internal form */
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         /* create body portion of external form */
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         /* create entire external form */
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         /* add this object to the directory */
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         /* and write it to the database */
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  * Local Variables:
00181  * mode: C
00182  * tab-width: 8
00183  * c-basic-offset: 4
00184  * indent-tabs-mode: t
00185  * End:
00186  * ex: shiftwidth=4 tabstop=8
00187  */

Generated on Mon Sep 18 01:24:48 2006 for BRL-CAD by  doxygen 1.4.6