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
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
00070
00071
00072
00073
00074
00075
00076
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
00095
00096
00097
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
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
00122
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;
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;
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;
00144 if( ret == -1 ) {
00145 bu_log("%s: line %d, invalid regular expression\n", file, linenum);
00146 break;
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156
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
00185
00186
00187
00188
00189
00190
00191