/*----------------------------------------------------------------------*/ /* CHARARRY.C, a package of routines for manipulating 2-dimensional */ /* character arrays */ /*----------------------------------------------------------------------*/ /*------------------------- Carrmve ------------------------------------*/ /* A General Purpose Function For Moving Rectangular Areas Within */ /* Character Arrays */ /*--------------------------- */ carrmve(char *array, /* is the array acted upon */ int dim1, int dim2, /* are the dimensions */ int m1, int m2, /* are the displacements moved */ int low1, int high1, /* maximum and minimum values for index 1 */ int low2, int high2, /* for index 2 */ char fill /* character that fills the vacated region */ ) /*-----------------------------------------*/ { int srt1, srt2, not1, not2, inc1, inc2; unsigned int x1,x2; if(m1 >= 0){ srt1= high1; not1= low1-1; inc1= -1;} else { srt1= low1; not1= high1+1; inc1= 1; } if(m2 >= 0){ srt2= high2; not2= low2-1; inc2= -1;} else { srt2= low2; not2= high2+1; inc2= 1; } for(x1= srt1; x1 != not1 ; x1=x1+inc1) for(x2= srt2; x2 != not2 ; x2=x2+inc2) { array[(x1+m1)*dim2+(x2+m2)] = array[x1*dim2+x2]; array[x1*dim2+x2] = fill; } } /*------------------------------------------------------------------------*/ /* reduce the next three routines into one when you have */ /* the time */ /*----------------------- Carrmsk ----------------------------------------*/ /*----------------------- */ carrmsk( char *mask, /* if a given position of this array is */ char cond, /* equal (cond = '=') or unequal (cond != '=') */ char match, /* to this character */ char *dest, /* the corresponding position of this array */ int dim1, int dim2, /* (of these dimensions) */ char fill) /* is set to this character */ /*-----------------------------------------*/ { unsigned int i, j; j=dim1*dim2; if(cond == '=') for( i=0 ; i < j ; i++){ if(mask[i] == match) dest[i] = fill; } else for( i=0 ; i < j ; i++){ if(mask[i] != match) dest[i] = fill; } } /*------------------------- Carrmcpy --------------------------------------*/ carrmcpy( char *mask, char cond, char match, char *sorc, char *dest, int dim1, int dim2 ) { unsigned int i, j; j=dim1*dim2; if(cond == '=') for( i=0 ; i < j ; i++){ if(mask[i] == match) dest[i] = sorc[i]; } else for( i=0 ; i < j ; i++){ if(mask[i] != match) dest[i] = sorc[i]; } } /*---------------------- Carrcpy ------------------------------------------*/ carrcpy( char *sorc, /* copies this array to */ char *dest, /* this one */ int dim1, /* */ int dim2 /* where these are the dimensions */ ) /*------------------------------------------*/ { unsigned int i, j; j=dim1*dim2; for( i=0 ; i < j ; i++) dest[i] = sorc[i]; } /*---------------------- Carrset --------------------------------------*/ carrset(char *array, /* sets all the positions of this array */ int dim1, /* */ int dim2, /* of these dimensions */ char fill /* to this character */ ) /*-----------------------------------------*/ { unsigned int i, j; j=dim1*dim2; for( i=0 ; i < j ; i++) array[i] = fill; } /*-------------------------- Carrcbnd -------------------------------------*/ /* finds the maximum and minimum indices of the minimum rectangular */ /* region within which all those values lie which: */ /* a) do not equal MATCH (COND = '!') */ /* or */ /* b) do equal MATCH (COND = '=') */ /* */ /*-------------------------------------------------------------------------*/ carrcbnd( char *array, int fdim1, int fdim2, int *fldim1, int *fhdim1, int *fldim2, int *fhdim2, char cond, char match ) { int d1, d2, ld1, hd1, ld2, hd2; hd1=0; hd2=0; ld1=fdim1; ld2=fdim2; if(cond == '=') for( d1=0 ; d1 < fdim1 ; d1++) for( d2=0 ; d2 < fdim2 ; d2++) { if( array[d1*fdim2+d2] == match) { if( d1 > hd1) hd1 = d1; if( d1 < ld1) ld1 = d1; if( d2 > hd2) hd2 = d2; if( d2 < ld2) ld2 = d2; } } else for( d1=0 ; d1 < fdim1 ; d1++) for( d2=0 ; d2 < fdim2 ; d2++) { if( array[d1*fdim2+d2] != match) { if( d1 > hd1) hd1 = d1; if( d1 < ld1) ld1 = d1; if( d2 > hd2) hd2 = d2; if( d2 < ld2) ld2 = d2; } } *fldim1 =ld1; *fhdim1=hd1; *fldim2=ld2; *fhdim2=hd2; } /*---------------------- Carrlts --------------------------------------*/ carrlts( char *array, char *str, int lnum, int dim2, fill ) { unsigned int first_pos, last_pos, null_pos, i, k; first_pos=dim2*lnum; last_pos=first_pos+dim2-1; for( i=last_pos; i >= first_pos; i--) if( array[i] != fill) break; null_pos=i+1; for(k=first_pos; k < null_pos; k++) str[k-first_pos]=array[k]; str[k-first_pos]=NULL_BYTE; } /*-------------------- Carrstl -----------------------------------------*/ carrstl( char *str, char *array, int lnum, int dim2, char fill ) { unsigned int first_pos, last_pos, i,j; first_pos=dim2*lnum; last_pos=first_pos+dim2-1; for(i=first_pos; i <= last_pos; i++) { if(str[i-first_pos] != NULL_BYTE) array[i]=str[i-first_pos]; else break; } for(j=i;j <= last_pos; j++) array[j]=fill; } /*------------------------------------------------------------------------*/ /*------------------- Carrfldf -------------------------------------------*/ /*----------------------------- Does a Flood Fill on a two dimesional */ carrfldf( /* charcter array. */ char *arr, /* array acted on */ int ival1, /* starting position */ int ival2, /* */ int d1, /* array dimensions */ int d2, /* */ char bnd /* fill char that also forms perimeter */ ) /*-----------------------------------------*/ { int k1, k2, k1p, x2, mx2, mn2, stakndx, i, j, l; short unsigned stak1[512], stak2[512]; arr[ival1*d2+ival2]=~bnd; stak1[0]=ival1; stak2[0]=ival2; stakndx=1; while(stakndx >= 1) { stakndx--; k1=stak1[stakndx]; k2=stak2[stakndx]; i=k1*d2; l=d2-1; arr[i+k2]=bnd; for(mx2=k2; (arr[i+mx2+1] != bnd) && (mx2 < l); mx2++) arr[i+mx2+1]=bnd; for(mn2=k2; (arr[i+mn2-1] != bnd) && (mn2 > 0); mn2--) arr[i+mn2-1]=bnd; for(k1p=k1-1; k1p <= k1+1;k1p+=2) /*for k1p=k-1 and k1p=k+1 */ { if((k1p < 0) || (k1p >= d1)) continue; i=k1p*d2; for(x2=mx2; x2 >= mn2; x2--) { j=i+x2; if( (arr[j] != bnd) && ((arr[j+1] == bnd) || (x2 == mx2)) && (stakndx < 512) ) { stak1[stakndx]=k1p; stak2[stakndx]=x2; stakndx++; } } } } } /* After: James D. Foley and Andries Van Dam, Fundumentals of */ /* Interactive Computer Graphics, Addison-Wesley Systems Programing */ /* Series, Addison-Wesley Publishing Co., 1983, orig. pub. 1982, pg 446-450 */ /* My thanks to Steve Van Devender, University of Oregon, */ /* for calling this work to my attention */ /*--------------------------------------------------------------------------*/