[2759] | 1 | #ifndef MS_SUA |
---|
| 2 | # include <stdio.h> |
---|
| 3 | # include <stdlib.h> |
---|
| 4 | #endif |
---|
| 5 | #include <string.h> |
---|
| 6 | |
---|
| 7 | #ifndef CRAY |
---|
| 8 | # ifdef NOUNDERSCORE |
---|
| 9 | # define INT_PACK_DATA int_pack_data |
---|
| 10 | # define INT_GET_TI_HEADER_C int_get_ti_header_c |
---|
| 11 | # define INT_GEN_TI_HEADER_C int_gen_ti_header_c |
---|
| 12 | # define ADD_TO_BUFSIZE_FOR_FIELD_C add_to_bufsize_for_field_c |
---|
| 13 | # define STORE_PIECE_OF_FIELD_C store_piece_of_field_c |
---|
| 14 | # define RETRIEVE_PIECES_OF_FIELD_C retrieve_pieces_of_field_c |
---|
| 15 | # define INIT_STORE_PIECE_OF_FIELD init_store_piece_of_field |
---|
| 16 | # define INIT_RETRIEVE_PIECES_OF_FIELD init_retrieve_pieces_of_field |
---|
| 17 | # define PERTURB_REAL perturb_real |
---|
| 18 | # define INSPECT_HEADER inspect_header |
---|
| 19 | # else |
---|
| 20 | # ifdef F2CSTYLE |
---|
| 21 | # define INT_PACK_DATA int_pack_data__ |
---|
| 22 | # define INT_GET_TI_HEADER_C int_get_ti_header_c__ |
---|
| 23 | # define INT_GEN_TI_HEADER_C int_gen_ti_header_c__ |
---|
| 24 | # define ADD_TO_BUFSIZE_FOR_FIELD_C add_to_bufsize_for_field_c__ |
---|
| 25 | # define STORE_PIECE_OF_FIELD_C store_piece_of_field_c__ |
---|
| 26 | # define RETRIEVE_PIECES_OF_FIELD_C retrieve_pieces_of_field_c__ |
---|
| 27 | # define INIT_STORE_PIECE_OF_FIELD init_store_piece_of_field__ |
---|
| 28 | # define INIT_RETRIEVE_PIECES_OF_FIELD init_retrieve_pieces_of_field__ |
---|
| 29 | # define PERTURB_REAL perturb_real__ |
---|
| 30 | # define INSPECT_HEADER inspect_header__ |
---|
| 31 | # else |
---|
| 32 | # define INT_PACK_DATA int_pack_data_ |
---|
| 33 | # define INT_GET_TI_HEADER_C int_get_ti_header_c_ |
---|
| 34 | # define INT_GEN_TI_HEADER_C int_gen_ti_header_c_ |
---|
| 35 | # define ADD_TO_BUFSIZE_FOR_FIELD_C add_to_bufsize_for_field_c_ |
---|
| 36 | # define STORE_PIECE_OF_FIELD_C store_piece_of_field_c_ |
---|
| 37 | # define RETRIEVE_PIECES_OF_FIELD_C retrieve_pieces_of_field_c_ |
---|
| 38 | # define INIT_STORE_PIECE_OF_FIELD init_store_piece_of_field_ |
---|
| 39 | # define INIT_RETRIEVE_PIECES_OF_FIELD init_retrieve_pieces_of_field_ |
---|
| 40 | # define PERTURB_REAL perturb_real_ |
---|
| 41 | # define INSPECT_HEADER inspect_header_ |
---|
| 42 | # endif |
---|
| 43 | # endif |
---|
| 44 | #endif |
---|
| 45 | |
---|
| 46 | #ifdef MEMCPY_FOR_BCOPY |
---|
| 47 | # define bcopy(A,B,C) memcpy((B),(A),(C)) |
---|
| 48 | #endif |
---|
| 49 | |
---|
| 50 | /* CALL int_pack_data ( hdrbuf , hdrbufsiz * inttypesize , int_local_output_buffer, int_local_output_cursor ) */ |
---|
| 51 | |
---|
| 52 | INT_PACK_DATA ( unsigned char *buf , int *ninbytes , unsigned char *obuf, int *cursor ) |
---|
| 53 | { |
---|
| 54 | int i, lcurs ; |
---|
| 55 | lcurs = *cursor - 1 ; |
---|
| 56 | for ( i = 0 ; i < *ninbytes ; i++ ) |
---|
| 57 | { |
---|
| 58 | obuf[lcurs++] = buf[i] ; |
---|
| 59 | } |
---|
| 60 | *cursor = lcurs+1 ; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | int |
---|
| 64 | INT_GEN_TI_HEADER_C ( char * hdrbuf, int * hdrbufsize, /* hdrbufsize is in bytes */ |
---|
| 65 | int * itypesize, int * typesize, |
---|
| 66 | int * DataHandle, char * Data, |
---|
| 67 | int * Count, int * code ) |
---|
| 68 | { |
---|
| 69 | int i ; |
---|
| 70 | char * p ; |
---|
| 71 | p = hdrbuf ; |
---|
| 72 | p += sizeof(int) ; |
---|
| 73 | bcopy( code, p, sizeof(int) ) ; p += sizeof(int) ; /* 2 */ |
---|
| 74 | bcopy( DataHandle, p, sizeof(int) ) ; p += sizeof(int) ; /* 3 */ |
---|
| 75 | bcopy( typesize, p, sizeof(int) ) ; p += sizeof(int) ; /* 4 */ |
---|
| 76 | bcopy( Count, p, sizeof(int) ) ; p += sizeof(int) ; /* 5 */ |
---|
| 77 | bcopy( Data, p, *Count * *typesize ) ; p += *Count * *typesize ; /* 6++ */ |
---|
| 78 | *hdrbufsize = (int) (p - hdrbuf) ; |
---|
| 79 | bcopy( hdrbufsize, hdrbuf, sizeof(int) ) ; |
---|
| 80 | return(0) ; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | int |
---|
| 84 | INT_GET_TI_HEADER_C ( char * hdrbuf, int * hdrbufsize, int * n, /* hdrbufsize and n are in bytes */ |
---|
| 85 | int * itypesize, int * typesize, |
---|
| 86 | int * DataHandle, char * Data, |
---|
| 87 | int * Count, int * code ) |
---|
| 88 | { |
---|
| 89 | int i ; |
---|
| 90 | char * p ; |
---|
| 91 | p = hdrbuf ; |
---|
| 92 | bcopy( p, hdrbufsize, sizeof(int) ) ; p += sizeof(int) ; /* 1 */ |
---|
| 93 | bcopy( p, code, sizeof(int) ) ; p += sizeof(int) ; /* 2 */ |
---|
| 94 | bcopy( p, DataHandle, sizeof(int) ) ; p += sizeof(int) ; /* 3 */ |
---|
| 95 | bcopy( p, typesize, sizeof(int) ) ; p += sizeof(int) ; /* 4 */ |
---|
| 96 | bcopy( p, Count, sizeof(int) ) ; p += sizeof(int) ; /* 5 */ |
---|
| 97 | if ( *Count * *typesize > 0 ) { |
---|
| 98 | bcopy( p, Data, *Count * *typesize ) ; p += *Count * *typesize ; /* 6++ */ |
---|
| 99 | } |
---|
| 100 | *n = (int)( p - hdrbuf ) ; |
---|
| 101 | return(0) ; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | #define MAX_FLDS 2000 |
---|
| 105 | static char fld_name[MAX_FLDS][256] ; |
---|
| 106 | static char *fld_cache[MAX_FLDS] ; |
---|
| 107 | static int fld_curs[MAX_FLDS] ; |
---|
| 108 | static int fld_bufsize[MAX_FLDS] ; |
---|
| 109 | static int fld = 0 ; |
---|
| 110 | static int numflds = 0 ; |
---|
| 111 | static int frst = 1 ; |
---|
| 112 | |
---|
| 113 | int INIT_STORE_PIECE_OF_FIELD () |
---|
| 114 | { |
---|
| 115 | int i ; |
---|
| 116 | if ( frst ) { |
---|
| 117 | for ( i = 0 ; i < MAX_FLDS ; i++ ) { |
---|
| 118 | fld_cache[i] = NULL ; |
---|
| 119 | } |
---|
| 120 | frst = 0 ; |
---|
| 121 | } |
---|
| 122 | numflds = 0 ; |
---|
| 123 | for ( i = 0 ; i < MAX_FLDS ; i++ ) { |
---|
| 124 | strcpy( fld_name[i], "" ) ; |
---|
| 125 | if ( fld_cache[i] != NULL ) free( fld_cache[i] ) ; |
---|
| 126 | fld_cache[i] = NULL ; |
---|
| 127 | fld_curs[i] = 0 ; |
---|
| 128 | fld_bufsize[i] = 0 ; |
---|
| 129 | } |
---|
| 130 | return(0) ; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | int INIT_RETRIEVE_PIECES_OF_FIELD () |
---|
| 134 | { |
---|
| 135 | fld = 0 ; |
---|
| 136 | return(0) ; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | int |
---|
| 140 | ADD_TO_BUFSIZE_FOR_FIELD_C ( int varname[], int * chunksize ) |
---|
| 141 | { |
---|
| 142 | int i, n ; |
---|
| 143 | int found ; |
---|
| 144 | char vname[256] ; |
---|
| 145 | |
---|
| 146 | n = varname[0] ; |
---|
| 147 | for ( i = 1; i <= n ; i++ ) { vname[i-1] = varname[i] ; } |
---|
| 148 | vname[n] = '\0' ; |
---|
| 149 | |
---|
| 150 | found = -1 ; |
---|
| 151 | for ( i = 0 ; i < numflds ; i++ ) { if ( !strcmp( fld_name[i], vname ) ) { found = i ; break ; } } |
---|
| 152 | if ( found == -1 ) { |
---|
| 153 | found = numflds++ ; |
---|
| 154 | strcpy( fld_name[found], vname ) ; |
---|
| 155 | fld_bufsize[found] = *chunksize ; |
---|
| 156 | } |
---|
| 157 | else |
---|
| 158 | { |
---|
| 159 | fld_bufsize[found] += *chunksize ; |
---|
| 160 | } |
---|
| 161 | if ( fld_cache[found] != NULL ) { free( fld_cache[found] ) ; } |
---|
| 162 | fld_cache[found] = NULL ; |
---|
| 163 | return(0) ; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | int |
---|
| 167 | STORE_PIECE_OF_FIELD_C ( char * buf , int varname[], int * chunksize, int *retval ) |
---|
| 168 | { |
---|
| 169 | int i, n ; |
---|
| 170 | int found ; |
---|
| 171 | char vname[256] ; |
---|
| 172 | |
---|
| 173 | n = varname[0] ; |
---|
| 174 | for ( i = 1; i <= n ; i++ ) { vname[i-1] = varname[i] ; } |
---|
| 175 | vname[n] = '\0' ; |
---|
| 176 | |
---|
| 177 | found = -1 ; |
---|
| 178 | for ( i = 0 ; i < numflds ; i++ ) { if ( !strcmp( fld_name[i], vname ) ) { found = i ; break ; } } |
---|
| 179 | if ( found == -1 ) { |
---|
| 180 | #ifndef MS_SUA |
---|
| 181 | fprintf(stderr,"frame/pack_utils.c: field (%s) not found; was not set up with add_to_bufsize_for_field\n",vname ) ; |
---|
| 182 | #endif |
---|
| 183 | *retval = 1 ; |
---|
| 184 | return(0) ; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | if ( fld_cache[found] == NULL ) { |
---|
| 188 | fld_cache[found] = (char *) malloc( fld_bufsize[found] ) ; |
---|
| 189 | fld_curs[found] = 0 ; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | if ( fld_curs[found] + *chunksize > fld_bufsize[found] ) { |
---|
| 193 | #ifndef MS_SUA |
---|
| 194 | fprintf(stderr, |
---|
| 195 | "frame/pack_utils.c: %s would overwrite %d + %d > %d [%d]\n",vname, fld_curs[found], *chunksize, fld_bufsize[found], found ) ; |
---|
| 196 | #endif |
---|
| 197 | *retval = 1 ; |
---|
| 198 | return(0) ; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | bcopy( buf, fld_cache[found]+fld_curs[found], *chunksize ) ; |
---|
| 202 | fld_curs[found] += *chunksize ; |
---|
| 203 | *retval = 0 ; |
---|
| 204 | return(0) ; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | int |
---|
| 208 | RETRIEVE_PIECES_OF_FIELD_C ( char * buf , int varname[], int * insize, int * outsize, int *retval ) |
---|
| 209 | { |
---|
| 210 | int i, n ; |
---|
| 211 | int found ; |
---|
| 212 | char vname[256] ; |
---|
| 213 | |
---|
| 214 | if ( fld < numflds ) { |
---|
| 215 | #ifndef MS_SUA |
---|
| 216 | if ( fld_curs[fld] > *insize ) { |
---|
| 217 | fprintf(stderr,"retrieve: fld_curs[%d] (%d) > *insize (%d)\n",fld,fld_curs[fld], *insize ) ; |
---|
| 218 | } |
---|
| 219 | #endif |
---|
| 220 | *outsize = ( fld_curs[fld] <= *insize ) ? fld_curs[fld] : *insize ; |
---|
| 221 | bcopy( fld_cache[fld], buf, *outsize ) ; |
---|
| 222 | varname[0] = (int) strlen( fld_name[fld] ) ; |
---|
| 223 | for ( i = 1 ; i <= varname[0] ; i++ ) varname[i] = fld_name[fld][i-1] ; |
---|
| 224 | if ( fld_cache[fld] != NULL ) free ( fld_cache[fld] ) ; |
---|
| 225 | fld_cache[fld] = NULL ; |
---|
| 226 | fld_bufsize[fld] = 0 ; |
---|
| 227 | fld++ ; |
---|
| 228 | *retval = 0 ; |
---|
| 229 | } |
---|
| 230 | else { |
---|
| 231 | numflds = 0 ; |
---|
| 232 | *retval = -1 ; |
---|
| 233 | } |
---|
| 234 | return(0) ; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | #define INDEX_2(A,B,NB) ( (B) + (A)*(NB) ) |
---|
| 238 | #define INDEX_3(A,B,C) INDEX_2( (A), INDEX_2( (B), (C), (me[1]-ms[1]+1) ), (me[1]-ms[1]+1)*(me[0]-ms[0]+1) ) |
---|
| 239 | /* flip low order bit of fp number */ |
---|
| 240 | int |
---|
| 241 | PERTURB_REAL ( float * field, int ds[], int de[], int ms[], int me[], int ps[], int pe[] ) |
---|
| 242 | { |
---|
| 243 | int i,j,k ; |
---|
| 244 | int le ; /* index of little end */ |
---|
| 245 | float x = 2.0 ; |
---|
| 246 | unsigned int y ; |
---|
| 247 | unsigned char a[4], *p ; |
---|
| 248 | if ( sizeof(float) != 4 ) return(-1) ; |
---|
| 249 | /* check endianness of machine */ |
---|
| 250 | bcopy ( &x, a, 4 ) ; |
---|
| 251 | le = 0 ; |
---|
| 252 | if ( a[0] == 0x40 ) le = 3 ; |
---|
| 253 | for ( k = ps[2]-ms[2] ; k <= pe[2]-ms[2] ; k++ ) |
---|
| 254 | for ( j = ps[1]-ms[1] ; j <= pe[1]-ms[1] ; j++ ) |
---|
| 255 | for ( i = ps[0]-ms[0] ; i <= pe[0]-ms[0] ; i++ ) |
---|
| 256 | { |
---|
| 257 | /* do not change zeros */ |
---|
| 258 | if ( field[ INDEX_3(k,j,i) ] != 0.0 ) { |
---|
| 259 | p = (unsigned char *)&(field[ INDEX_3(k,j,i) ] ) ; |
---|
| 260 | if ( *(p+le) & 1 ) { *(p+le) &= 0x7e ; } |
---|
| 261 | else { *(p+le) |= 1 ; } |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | return(0) ; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | int INSPECT_HEADER ( char * buf, int * sz, int * line ) |
---|
| 268 | { |
---|
| 269 | int i ; |
---|
| 270 | #ifndef MS_SUA |
---|
| 271 | fprintf(stderr,"INSPECT_HEADER: line = %d ", *line ) ; |
---|
| 272 | if ( buf != NULL && sz != NULL ) { |
---|
| 273 | for ( i = 0 ; i < *sz && i < 256 ; i++ ) { if ( (buf[i] >= 'a' && buf[i] <= 'z') || buf[i] == '_' || |
---|
| 274 | (buf[i] >= 'A' && buf[i] <= 'Z') || |
---|
| 275 | (buf[i] >= '0' && buf[i] <= '9') ) fprintf(stderr,"%c",buf[i]) ; |
---|
| 276 | } |
---|
| 277 | fprintf(stderr,"\n") ; |
---|
| 278 | } |
---|
| 279 | #endif |
---|
| 280 | return(0) ; |
---|
| 281 | } |
---|
| 282 | |
---|