regionfix.c

Go to the documentation of this file.
00001 /*                     R E G I O N F I X . C
00002  * BRL-CAD
00003  *
00004  * Copyright (c) 1989-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 librt */
00023 /*@{*/
00024 
00025 /** @file regionfix.c
00026  *  Subroutines for adjusting old GIFT-style region-IDs,
00027  *  to take into account the presence of instancing.
00028  *
00029  *  Author -
00030  *      Michael John Muuss
00031  *
00032  *  Source -
00033  *      SECAD/VLD Computing Consortium, Bldg 394
00034  *      The U. S. Army Ballistic Research Laboratory
00035  *      Aberdeen Proving Ground, Maryland  21005-5066
00036  *
00037  */
00038 /*@}*/
00039 
00040 #ifndef lint
00041 static const char RCSregionfix[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/librt/regionfix.c,v 14.11 2006/09/16 02:04:26 lbutler Exp $ (BRL)";
00042 #endif
00043 
00044 #include "common.h"
00045 
00046 #include <stdlib.h>
00047 #include <sys/types.h>
00048 #include <stdio.h>
00049 #include <ctype.h>
00050 #if BUILD_REGEX
00051 #  include "regex.h"
00052 #elif defined(HAVE_REGEX_H)
00053 #  include <regex.h>
00054 #endif
00055 #ifdef HAVE_STRING_H
00056 #  include <string.h>
00057 #else
00058 #  include <strings.h>
00059 #endif
00060 
00061 #include "machine.h"
00062 #include "vmath.h"
00063 #include "raytrace.h"
00064 
00065 #include "./debug.h"
00066 
00067 
00068 /*
00069  *                      R T_ R E G I O N F I X
00070  *
00071  *  Apply any deltas to reg_regionid values
00072  *  to allow old applications that use the reg_regionid number
00073  *  to distinguish between different instances of the same
00074  *  prototype region.
00075  *
00076  *  Called once, from rt_prep(), before raytracing begins.
00077  */
00078 void
00079 rt_regionfix(struct rt_i *rtip)
00080 {
00081         FILE    *fp;
00082         char    *file;
00083         char    *line;
00084         char    *tabp;
00085         int     linenum = 0;
00086         register struct region  *rp;
00087         int     ret;
00088         int     oldid;
00089         int     newid;
00090         struct bu_vls   name;
00091 
00092         RT_CK_RTI(rtip);
00093 
00094         /*  If application has provided an alternative file name
00095          *  before rt_prep() was called, then use that.
00096          *  Otherwise, replace ".g" suffix on database name
00097          *  with ".regexp".
00098          */
00099         bu_vls_init(&name);
00100         file = rtip->rti_region_fix_file;
00101         if( file == (char *)NULL )  {
00102                 bu_vls_strcpy( &name, rtip->rti_dbip->dbi_filename );
00103                 if( (tabp = strrchr( bu_vls_addr(&name), '.' )) != NULL )  {
00104                         /* Chop off "." and suffix */
00105                         bu_vls_trunc( &name, tabp-bu_vls_addr(&name) );
00106                 }
00107                 bu_vls_strcat( &name, ".regexp" );
00108                 file = bu_vls_addr(&name);
00109         }
00110 
00111         if( (fp = fopen( file, "r" )) == NULL )  {
00112                 if( rtip->rti_region_fix_file ) perror(file);
00113                 bu_vls_free(&name);
00114                 return;
00115         }
00116         bu_log("librt/rt_regionfix(%s):  Modifying instanced region-ids.\n", file);
00117 
00118         while( (line = rt_read_cmd( fp )) != (char *) 0 )  {
00119                 regex_t re_space;
00120                 linenum++;
00121                 /*  For now, establish a simple format:
00122                  *  regexp TAB [more_white_space] formula SEMICOLON
00123                  */
00124                 if( (tabp = strchr( line, '\t' )) == (char *)0 )  {
00125                         bu_log("%s: missing TAB on line %d:\n%s\n", file, linenum, line );
00126                         continue;               /* just ignore it */
00127                 }
00128                 *tabp++ = '\0';
00129                 while( *tabp && isspace( *tabp ) )  tabp++;
00130                 if( (ret = regcomp(&re_space,line,0)) != 0 )  {
00131                         bu_log("%s: line %d, regcomp error '%d'\n", file, line, ret );
00132                         continue;               /* just ignore it */
00133                 }
00134 
00135                 for( BU_LIST_FOR( rp, region, &(rtip->HeadRegion) ) )  {
00136                         ret = regexec(&re_space, (char *)rp->reg_name, 0, 0,0);
00137                         if(RT_G_DEBUG&DEBUG_INSTANCE)  {
00138                                 bu_log("'%s' %s '%s'\n", line,
00139                                         ret==1 ? "==" : "!=",
00140                                         rp->reg_name);
00141                         }
00142                         if( (ret) == 0  )
00143                                 continue;       /* didn't match */
00144                         if( ret == -1 )  {
00145                                 bu_log("%s: line %d, invalid regular expression\n", file, linenum);
00146                                 break;          /* on to next RE */
00147                         }
00148                         /*
00149                          *  RE matched this name, perform indicated operation
00150                          *  For now, choices are limited.  Later this might
00151                          *  become an interpreted expression.  For now:
00152                          *      99      replace old region id with "num"
00153                          *      +99     increment old region id with "num"
00154                          *              (which may itself be a negative number)
00155                          *      +uses   increment old region id by the
00156                          *              current instance (use) count.
00157                          */
00158                         oldid = rp->reg_regionid;
00159                         if( strcmp( tabp, "+uses" ) == 0  )  {
00160                                 newid = oldid + rp->reg_instnum;
00161                         } else if( *tabp == '+' )  {
00162                                 newid = oldid + atoi( tabp+1 );
00163                         } else {
00164                                 newid = atoi( tabp );
00165                                 if( newid == 0 )  bu_log("%s, line %d Warning:  new id = 0\n", file, linenum );
00166                         }
00167                         if(RT_G_DEBUG&DEBUG_INSTANCE)  {
00168                                 bu_log("%s instance %d:  region id changed from %d to %d\n",
00169                                         rp->reg_name, rp->reg_instnum,
00170                                         oldid, newid );
00171                         }
00172                         rp->reg_regionid = newid;
00173                 }
00174 #if HAVE_REGFREE
00175                 regfree(&re_space);
00176 #endif
00177                 bu_free( line, "reg_expr line");
00178         }
00179         fclose( fp );
00180         bu_vls_free(&name);
00181 }
00182 
00183 /*
00184  * Local Variables:
00185  * mode: C
00186  * tab-width: 8
00187  * c-basic-offset: 4
00188  * indent-tabs-mode: t
00189  * End:
00190  * ex: shiftwidth=4 tabstop=8
00191  */

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