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
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 #include <sys/types.h>
00170 #include <time.h>
00171 #include <stdio.h>
00172 #include <errno.h>
00173 #include <string.h>
00174 #include <stdlib.h>
00175 #include <fcntl.h>
00176 #if defined(_WIN32) && !defined(__NUTC__)
00177 #include <io.h>
00178 #else
00179 #include <unistd.h>
00180 #include <sys/param.h>
00181 #endif
00182 #include "ADF.h"
00183 #include "ADF_internals.h"
00184 #ifdef MEM_DEBUG
00185 #include "cg_malloc.h"
00186 #endif
00187 #ifdef USE_CGIO
00188 #include "cgns_io.h"
00189 #endif
00190
00191 #if 0
00192 #define CHECK_ABORT(E) if(E!=NO_ERROR){int a=0;int b=1/a;}
00193 #else
00194 #define CHECK_ABORT(E) if(E!=NO_ERROR)return;
00195 #endif
00196
00197
00198
00199
00200
00201 #ifdef USE_STREAM_IO
00202 typedef long file_offset_t;
00203 # define ADFI_read(I,S,B) fread(B,1,S,ADF_file[I].file)
00204 # define ADFI_write(I,S,B) fwrite(B,1,S,ADF_file[I].file)
00205 #else
00206 # ifdef HAVE_OPEN64
00207 # define file_open open64
00208 # else
00209 # define file_open open
00210 # endif
00211 # ifdef HAVE_LSEEK64
00212 # ifdef _WIN32
00213 typedef __int64 file_offset_t;
00214 # define file_seek _lseeki64
00215 # else
00216 typedef off64_t file_offset_t;
00217 # define file_seek lseek64
00218 # endif
00219 # else
00220 typedef off_t file_offset_t;
00221 # define file_seek lseek
00222 # endif
00223 #endif
00224
00225 extern int ADF_sys_err;
00226
00227
00228 #define ADF_FILE_INC 5
00229
00230
00231 ADF_FILE *ADF_file;
00232 int maximum_files = 0;
00233
00239 static char ADF_this_machine_format = UNDEFINED_FORMAT_CHAR ;
00240 static char ADF_this_machine_os_size = UNDEFINED_FORMAT_CHAR ;
00241
00243 static char block_of_ZZ[ SMALLEST_CHUNK_SIZE ] ;
00244 static int block_of_ZZ_initialized = FALSE ;
00246 static char block_of_XX[ DISK_BLOCK_SIZE ] ;
00247 static int block_of_XX_initialized = FALSE ;
00249 static char block_of_00[ DISK_BLOCK_SIZE ] ;
00250 static int block_of_00_initialized = FALSE ;
00251
00253 #define CONVERSION_BUFF_SIZE 100000
00254 static unsigned char from_to_data[ CONVERSION_BUFF_SIZE ] ;
00255
00257 static char rd_block_buffer[DISK_BLOCK_SIZE] ;
00258 static long last_rd_block = -1 ;
00259 static long last_rd_file = -1 ;
00260 static long num_in_rd_block = -1 ;
00261 static char wr_block_buffer[DISK_BLOCK_SIZE] ;
00262 static long last_wr_block = -2 ;
00263 static long last_wr_file = -2 ;
00264 static int flush_wr_block = -2 ;
00265 static double last_link_ID = 0.0;
00266 static double last_link_LID = 0.0;
00267 enum { FLUSH, FLUSH_CLOSE };
00268
00273 enum { TO_FILE_FORMAT, FROM_FILE_FORMAT } ;
00274 #define NUMBER_KNOWN_MACHINES 5
00275 static size_t machine_sizes[NUMBER_KNOWN_MACHINES][16] = {
00276 { 1, 1, 1, 2, 2, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4 },
00277 { 1, 1, 1, 2, 2, 4, 4, 4, 4, 4, 8, 4, 4, 4, 4, 4 },
00278 { 1, 1, 1, 2, 2, 4, 4, 8, 8, 4, 8, 8, 8, 8, 8, 8 },
00279 { 1, 1, 1, 2, 2, 4, 4, 8, 8, 4, 8, 8, 8, 8, 8, 8 },
00280 { 1, 1, 1, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 } } ;
00281
00282
00283
00284
00285
00286 static const unsigned int pows[8] = {
00287 1, 16, 256, 4096, 65536, 1048576, 16777216, 268435456 } ;
00288 static const char ASCII_Hex[16] = {
00289 '0', '1', '2', '3', '4', '5', '6', '7',
00290 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' } ;
00291
00292
00293
00294
00295 static char *file_header_tags[] = {
00296 "AdF0", "AdF1", "AdF2", "AdF3", "AdF4", "AdF5" } ;
00297 static char node_start_tag[] = "NoDe" ;
00298 static char node_end_tag[] = "TaiL" ;
00299 static char free_chunk_table_start_tag[] = "fCbt" ;
00300 static char free_chunk_table_end_tag[] = "Fcte" ;
00301 static char free_chunk_start_tag[] = "FreE" ;
00302 static char free_chunk_end_tag[] = "EndC" ;
00303 static char sub_node_start_tag[] = "SNTb" ;
00304 static char sub_node_end_tag[] = "snTE" ;
00305 static char data_chunk_table_start_tag[] = "DCtb" ;
00306 static char data_chunk_table_end_tag[] = "dcTE" ;
00307 char data_chunk_start_tag[] = "DaTa" ;
00308 static char data_chunk_end_tag[] = "dEnD" ;
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 #define MAX_STACK 50
00320 static struct {
00321 int file_index;
00322 unsigned int file_block;
00323 unsigned int block_offset;
00324 int stack_type;
00325 char *stack_data;
00326 int priority_level;
00327 } PRISTK[MAX_STACK] ;
00328
00329 enum { FILE_STK=1, NODE_STK, DISK_PTR_STK, FREE_CHUNK_STK, SUBNODE_STK };
00330
00331 enum { INIT_STK, CLEAR_STK, CLEAR_STK_TYPE, DEL_STK_ENTRY, GET_STK, SET_STK };
00332
00333
00334
00335 #define EVAL_2_BYTES( C0, C1 ) (((C0)<<8)+((C1)))
00336 #define EVAL_4_BYTES( C0, C1, C2, C3 ) (((C0)<<24)+((C1)<<16)+((C2)<<8)+((C3)))
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 void ADFI_ASCII_Hex_2_unsigned_int(
00361 const unsigned int minimum,
00362 const unsigned int maximum,
00363 const unsigned int string_length,
00364 const char string[],
00365 unsigned int *number,
00366 int *error_return )
00367 {
00368 unsigned int i,
00369 ir,
00370 j,
00371 num ;
00373 if( string == NULL ) {
00374 *error_return = NULL_STRING_POINTER ;
00375 return ;
00376 }
00377
00378 if( string_length == 0 ) {
00379 *error_return = STRING_LENGTH_ZERO ;
00380 return ;
00381 }
00382
00383 if( number == NULL ) {
00384 *error_return = NULL_POINTER ;
00385 return ;
00386 }
00387
00388 if( string_length > 8 ) {
00389 *error_return = STRING_LENGTH_TOO_BIG ;
00390 return ;
00391 }
00392
00393 if( minimum > maximum ) {
00394 *error_return = MINIMUM_GT_MAXIMUM ;
00395 return ;
00396 }
00397
00398 *error_return = NO_ERROR ;
00399
00401 num = 0 ;
00402 ir = (string_length - 1) << 2;
00403 for( i=0; i<string_length; i++) {
00404 if (string[i] >= '0' && string[i] <= '9')
00405 j = string[i] - 48;
00406 else if (string[i] >= 'A' && string[i] <= 'F')
00407 j = string[i] - 55;
00408 else if (string[i] >= 'a' && string[i] <= 'f')
00409 j = string[i] - 87;
00410 else {
00411 *error_return = STRING_NOT_A_HEX_STRING ;
00412 return ;
00413 }
00414 num += (j << ir);
00415 ir -= 4;
00416 }
00417
00418 if( num < minimum ) {
00419 *error_return = NUMBER_LESS_THAN_MINIMUM ;
00420 return ;
00421 }
00422
00423 if( num > maximum ) {
00424 *error_return = NUMBER_GREATER_THAN_MAXIMUM ;
00425 return ;
00426 }
00427
00429 *number = num ;
00430 }
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440 void ADFI_Abort(
00441 const int error_code )
00442 {
00443 fprintf(stderr,"ADF Aborted: Exiting\n" ) ;
00444 exit( error_code ) ;
00445 }
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470 void ADFI_ID_2_file_block_offset(
00471 const double ID,
00472 unsigned int *file_index,
00473 unsigned long *file_block,
00474 unsigned long *block_offset,
00475 int *error_return )
00476 {
00477 unsigned char * cc;
00478
00479 if( (file_index == NULL) || (file_block == NULL) || (block_offset == NULL) ) {
00480 *error_return = NULL_POINTER ;
00481 return ;
00482 }
00483
00484 if( ID == 0.0 ) {
00485 *error_return = NODE_ID_ZERO ;
00486 return ;
00487 }
00488
00489 *error_return = NO_ERROR ;
00490 cc = (unsigned char *) &ID;
00491 #ifdef PRINT_STUFF
00492 printf("In ADFI_ID_2_file_block_offset: ID=%lf\n",ID);
00493 printf("cc[0-7] = %02X %02X %02X %02X %02X %02X %02X %02X \n",
00494 cc[0], cc[1], cc[2], cc[3],
00495 cc[4], cc[5], cc[6], cc[7] ) ;
00496 #endif
00497
00498 if ( ADF_this_machine_format == IEEE_BIG_FORMAT_CHAR ) {
00499 *file_index = cc[1] + ((cc[0] & 0x003f) << 8) ;
00500 *file_block = cc[2] + (cc[3]<<8) +
00501 (cc[4]<<16) + (cc[5]<<24) ;
00502 *block_offset = cc[6] + (cc[7]<<8) ;
00503 }
00504 else if ( ADF_this_machine_format == IEEE_LITTLE_FORMAT_CHAR ) {
00505 *file_index = cc[6] + ((cc[7] & 0x003f) << 8) ;
00506 *file_block = cc[2] + (cc[3]<<8) +
00507 (cc[4]<<16) + (cc[5]<<24) ;
00508 *block_offset = cc[0] + (cc[1]<<8) ;
00509 }
00510 else {
00511 *file_index = cc[0] + (cc[1]<<8) ;
00512 *file_block = cc[2] + (cc[3]<<8) +
00513 (cc[4]<<16) + (cc[5]<<24) ;
00514 *block_offset = cc[6] + (cc[7]<<8) ;
00515 }
00516
00517 #ifdef PRINT_STUFF
00518 printf("*file_index=%d, *file_block=%d, *block_offset=%d\n",
00519 *file_index, *file_block, *block_offset);
00520 #endif
00521
00522 if( *file_index >= maximum_files ) {
00523 *error_return = FILE_INDEX_OUT_OF_RANGE ;
00524 return ;
00525 }
00526
00527 if( *block_offset >= DISK_BLOCK_SIZE ) {
00528 *error_return = BLOCK_OFFSET_OUT_OF_RANGE ;
00529 return ;
00530 }
00531 }
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550 void ADFI_add_2_sub_node_table(
00551 const int file_index,
00552 const struct DISK_POINTER *parent,
00553 const struct DISK_POINTER *child,
00554 int *error_return )
00555 {
00556 struct NODE_HEADER parent_node, child_node ;
00557 struct SUB_NODE_TABLE_ENTRY *sub_node_table ;
00558 struct DISK_POINTER tmp_disk_ptr ;
00559 unsigned int old_num_entries ;
00560 int i ;
00561
00562 if( (parent == NULL) || (child == NULL) ) {
00563 *error_return = NULL_POINTER ;
00564 return ;
00565 }
00566
00567 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
00568 *error_return = ADF_FILE_NOT_OPENED ;
00569 return ;
00570 }
00571
00572 *error_return = NO_ERROR ;
00573
00575 ADFI_read_node_header( file_index, parent, &parent_node, error_return ) ;
00576 if( *error_return != NO_ERROR )
00577 return ;
00578
00580 ADFI_read_node_header( file_index, child, &child_node, error_return ) ;
00581 if( *error_return != NO_ERROR )
00582 return ;
00583
00585 if( parent_node.entries_for_sub_nodes <= parent_node.num_sub_nodes ) {
00586 old_num_entries = parent_node.entries_for_sub_nodes ;
00587
00589 if( parent_node.entries_for_sub_nodes == 0 )
00590 parent_node.entries_for_sub_nodes = LIST_CHUNK ;
00591 else
00592 parent_node.entries_for_sub_nodes = (unsigned int) (
00593 (float) parent_node.entries_for_sub_nodes * LIST_CHUNK_GROW_FACTOR ) ;
00594
00595 if( parent_node.entries_for_sub_nodes <= parent_node.num_sub_nodes ) {
00596 *error_return = SUB_NODE_TABLE_ENTRIES_BAD ;
00597 return ;
00598 }
00599
00601 sub_node_table = (struct SUB_NODE_TABLE_ENTRY *)
00602 malloc( parent_node.entries_for_sub_nodes *
00603 sizeof( *sub_node_table ) ) ;
00604 if( sub_node_table == NULL ) {
00605 *error_return = MEMORY_ALLOCATION_FAILED ;
00606 return ;
00607 }
00608
00610 if( old_num_entries > 0 ) {
00611 ADFI_read_sub_node_table( file_index, &parent_node.sub_node_table,
00612 sub_node_table, error_return ) ;
00613 if( *error_return != NO_ERROR )
00614 return ;
00615 }
00616
00618 for( i=parent_node.num_sub_nodes; i<(int) parent_node.entries_for_sub_nodes;
00619 i++ ) {
00620 strncpy( sub_node_table[i].child_name,
00621
00622 "unused entry in sub-node-table ", ADF_NAME_LENGTH ) ;
00623 sub_node_table[i].child_location.block = 0 ;
00624 sub_node_table[i].child_location.offset = DISK_BLOCK_SIZE ;
00625 }
00626
00628 if( parent_node.num_sub_nodes > 0 ) {
00629 ADFI_delete_sub_node_table( file_index, &parent_node.sub_node_table,
00630 old_num_entries, error_return ) ;
00631 if( *error_return != NO_ERROR )
00632 return ;
00633 }
00634
00635 ADFI_file_malloc( file_index, TAG_SIZE + DISK_POINTER_SIZE + TAG_SIZE +
00636 parent_node.entries_for_sub_nodes * (ADF_NAME_LENGTH + DISK_POINTER_SIZE),
00637 &tmp_disk_ptr, error_return ) ;
00638 if( *error_return != NO_ERROR )
00639 return ;
00640
00641 parent_node.sub_node_table.block = tmp_disk_ptr.block ;
00642 parent_node.sub_node_table.offset = tmp_disk_ptr.offset ;
00643
00645 ADFI_write_sub_node_table( file_index, &parent_node.sub_node_table,
00646 parent_node.entries_for_sub_nodes,
00647 (struct SUB_NODE_TABLE_ENTRY *)sub_node_table, error_return ) ;
00648 free( sub_node_table ) ;
00649 if( *error_return != NO_ERROR )
00650 return ;
00651 }
00652
00654 tmp_disk_ptr.block = parent_node.sub_node_table.block ;
00655 tmp_disk_ptr.offset = parent_node.sub_node_table.offset +
00656 TAG_SIZE + DISK_POINTER_SIZE +
00657 parent_node.num_sub_nodes * (ADF_NAME_LENGTH + DISK_POINTER_SIZE) ;
00658
00659 ADFI_adjust_disk_pointer( &tmp_disk_ptr, error_return ) ;
00660 if( *error_return != NO_ERROR )
00661 return ;
00662
00664 ADFI_write_file( file_index, tmp_disk_ptr.block, tmp_disk_ptr.offset,
00665 ADF_NAME_LENGTH, child_node.name, error_return ) ;
00666 if( *error_return != NO_ERROR )
00667 return ;
00668
00670 tmp_disk_ptr.offset += ADF_NAME_LENGTH ;
00671 ADFI_adjust_disk_pointer( &tmp_disk_ptr, error_return ) ;
00672 if( *error_return != NO_ERROR )
00673 return ;
00674
00675 ADFI_write_disk_pointer_2_disk( file_index, tmp_disk_ptr.block,
00676 tmp_disk_ptr.offset, child, error_return ) ;
00677 if( *error_return != NO_ERROR )
00678 return ;
00679
00681 parent_node.num_sub_nodes++ ;
00682 ADFI_write_node_header( file_index, parent, &parent_node, error_return ) ;
00683 if( *error_return != NO_ERROR )
00684 return ;
00685
00686 }
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702 void ADFI_adjust_disk_pointer(
00703 struct DISK_POINTER *block_offset,
00704 int *error_return )
00705 {
00706 unsigned long oblock ;
00707 unsigned long nblock ;
00708
00709 if( block_offset == NULL ) {
00710 *error_return = NULL_POINTER ;
00711 return ;
00712 }
00713
00714 *error_return = NO_ERROR ;
00715
00716 if ( block_offset->offset < DISK_BLOCK_SIZE ) return ;
00717
00719 nblock = (unsigned long) (block_offset->offset / DISK_BLOCK_SIZE) ;
00720
00722 oblock = block_offset->block ;
00723 block_offset->block += nblock ;
00724 block_offset->offset -= nblock * DISK_BLOCK_SIZE ;
00725 if ( block_offset->block < oblock ) {
00726 *error_return = BLOCK_OFFSET_OUT_OF_RANGE ;
00727 return ;
00728 }
00729
00730 }
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800 void ADFI_big_endian_32_swap_64(
00801 const char from_format,
00802 const char from_os_size,
00803 const char to_format,
00804 const char to_os_size,
00805 const char data_type[2],
00806 const unsigned long delta_from_bytes,
00807 const unsigned long delta_to_bytes,
00808 const unsigned char *from_data,
00809 unsigned char *to_data,
00810 int *error_return )
00811 {
00812
00813 if( (from_data == NULL) || (to_data == NULL) ) {
00814 *error_return = NULL_STRING_POINTER ;
00815 return ;
00816 }
00817
00818 if( (delta_from_bytes == 0) || (delta_to_bytes == 0) ) {
00819 *error_return = NULL_POINTER ;
00820 return ;
00821 }
00822
00823 if( (from_format == 'N') || (to_format == 'N') ) {
00824 *error_return = CANNOT_CONVERT_NATIVE_FORMAT ;
00825 return ;
00826 }
00827
00828 *error_return = NO_ERROR ;
00829
00830 if ( delta_to_bytes == delta_from_bytes ) {
00831 memcpy( to_data, from_data, delta_from_bytes ) ;
00832 }
00833 else if ( delta_from_bytes < delta_to_bytes ) {
00834 switch( EVAL_2_BYTES( data_type[0], data_type[1] ) ) {
00835 case EVAL_2_BYTES( 'I', '8' ):
00836 if( (from_data[0] & 0x80) == 0x80 ) {
00837 to_data[0] = 0xff ;
00838 to_data[1] = 0xff ;
00839 to_data[2] = 0xff ;
00840 to_data[3] = 0xff ;
00841 }
00842 else {
00843 to_data[0] = 0x00 ;
00844 to_data[1] = 0x00 ;
00845 to_data[2] = 0x00 ;
00846 to_data[3] = 0x00 ;
00847 }
00848 to_data[4] = from_data[0] ;
00849 to_data[5] = from_data[1] ;
00850 to_data[6] = from_data[2] ;
00851 to_data[7] = from_data[3] ;
00852 break ;
00853 default:
00854 *error_return = INVALID_DATA_TYPE ;
00855 return ;
00856 }
00857 }
00858 else {
00859 switch( EVAL_2_BYTES( data_type[0], data_type[1] ) ) {
00860 case EVAL_2_BYTES( 'I', '8' ):
00861 to_data[0] = from_data[4] ;
00862 to_data[1] = from_data[5] ;
00863 to_data[2] = from_data[6] ;
00864 to_data[3] = from_data[7] ;
00865 break ;
00866 default:
00867 *error_return = INVALID_DATA_TYPE ;
00868 return ;
00869 }
00870 }
00871
00872 }
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939 void ADFI_big_endian_to_cray(
00940 const char from_format,
00941 const char from_os_size,
00942 const char to_format,
00943 const char to_os_size,
00944 const char data_type[2],
00945 const unsigned long delta_from_bytes,
00946 const unsigned long delta_to_bytes,
00947 const unsigned char *from_data,
00948 unsigned char *to_data,
00949 int *error_return )
00950 {
00951 int i, exp ;
00952
00953 if( (from_data == NULL) || (to_data == NULL) ) {
00954 *error_return = NULL_STRING_POINTER ;
00955 return ;
00956 }
00957
00958 if( (delta_from_bytes == 0) || (delta_to_bytes == 0) ) {
00959 *error_return = NULL_POINTER ;
00960 return ;
00961 }
00962
00963 if( (from_format == 'N') || (to_format == 'N') ) {
00964 *error_return = CANNOT_CONVERT_NATIVE_FORMAT ;
00965 return ;
00966 }
00967
00968 *error_return = NO_ERROR ;
00969
00970 switch( EVAL_2_BYTES( data_type[0], data_type[1] ) ) {
00971
00972 case EVAL_2_BYTES( 'M', 'T' ):
00973 *error_return = NO_DATA ;
00974 return ;
00975
00976 case EVAL_2_BYTES( 'C', '1' ):
00977 case EVAL_2_BYTES( 'B', '1' ):
00978 to_data[0] = from_data[0] ;
00979 break ;
00980
00981 case EVAL_2_BYTES( 'I', '4' ):
00982 if( (from_data[0] & 0x80) == 0x80 ) {
00983 to_data[0] = 0xff ;
00984 to_data[1] = 0xff ;
00985 to_data[2] = 0xff ;
00986 to_data[3] = 0xff ;
00987 }
00988 else {
00989 to_data[0] = 0x00 ;
00990 to_data[1] = 0x00 ;
00991 to_data[2] = 0x00 ;
00992 to_data[3] = 0x00 ;
00993 }
00994 to_data[4] = from_data[0] ;
00995 to_data[5] = from_data[1] ;
00996 to_data[6] = from_data[2] ;
00997 to_data[7] = from_data[3] ;
00998 break ;
00999
01000 case EVAL_2_BYTES( 'U', '4' ):
01001 to_data[0] = 0x00 ;
01002 to_data[1] = 0x00 ;
01003 to_data[2] = 0x00 ;
01004 to_data[3] = 0x00 ;
01005 to_data[4] = from_data[0] ;
01006 to_data[5] = from_data[1] ;
01007 to_data[6] = from_data[2] ;
01008 to_data[7] = from_data[3] ;
01009 break ;
01010
01011 case EVAL_2_BYTES( 'I', '8' ):
01012 if( (from_data[0] & 0x80) == 0x80 ) {
01013 to_data[0] = 0xff ;
01014 to_data[1] = 0xff ;
01015 to_data[2] = 0xff ;
01016 to_data[3] = 0xff ;
01017 }
01018 else {
01019 to_data[0] = 0x00 ;
01020 to_data[1] = 0x00 ;
01021 to_data[2] = 0x00 ;
01022 to_data[3] = 0x00 ;
01023 }
01024 for( i=0; i<(int)delta_from_bytes; i++ )
01025 to_data[8-delta_from_bytes+i] = from_data[i] ;
01026 break ;
01027
01028 case EVAL_2_BYTES( 'U', '8' ):
01029 to_data[0] = 0x00 ;
01030 to_data[1] = 0x00 ;
01031 to_data[2] = 0x00 ;
01032 to_data[3] = 0x00 ;
01033 for( i=0; i<(int)delta_from_bytes; i++ )
01034 to_data[8-delta_from_bytes+i] = from_data[i] ;
01035 break ;
01036
01037 case EVAL_2_BYTES( 'R', '4' ):
01038 for( i=0; i<8; i++ )
01039 to_data[i] = 0x00 ;
01040
01042 if( (from_data[0] == 0x00) && (from_data[1] == 0x00) &&
01043 (from_data[2] == 0x00) && (from_data[3] == 0x00) )
01044 break ;
01045
01047 to_data[0] = from_data[0] & 0x80 ;
01048
01052 exp = (from_data[0] & 0x3f) << 1 ;
01053 if( (from_data[1] & 0x80) == 0x80 )
01054 exp += 1 ;
01055 if( (from_data[0] & 0x40) == 0x00 )
01056 exp -= 128 ;
01057 exp += 2 ;
01058
01059 to_data[1] = exp & 0xff ;
01060 if( exp < 0 )
01061 to_data[0] |= 0x3f ;
01062 else
01063 to_data[0] |= 0x40 ;
01064
01067 to_data[2] = from_data[1] | 0x80 ;
01068 to_data[3] = from_data[2] ;
01069 to_data[4] = from_data[3] ;
01070 break ;
01071
01072 case EVAL_2_BYTES( 'R', '8' ):
01073 for( i=0; i<8; i++ )
01074 to_data[i] = 0x00 ;
01075
01077 if( (from_data[0] == 0x00) && (from_data[1] == 0x00) &&
01078 (from_data[2] == 0x00) && (from_data[3] == 0x00) )
01079 break ;
01080
01082 to_data[0] = from_data[0] & 0x80 ;
01083
01087 exp = ((from_data[0] & 0x3f) << 4) + ((from_data[1]>>4)&0x0f) ;
01088
01089 if( (from_data[0] & 0x40) == 0x00 )
01090 exp -= 1024 ;
01091 exp += 2 ;
01092
01093 to_data[1] = (unsigned int)(exp & 0xff) ;
01094 to_data[0] |= ((exp>>8) & 0x03) ;
01095 if( exp < 0 )
01096 to_data[0] |= 0x3c ;
01097 else
01098 to_data[0] |= 0x40 ;
01099
01102 to_data[2] = 0x80 | ((from_data[1]<<3)&0x78) |
01103 ((from_data[2]>>5)&0x07) ;
01104 for( i=3; i<8; i++ )
01105 to_data[i] = ((from_data[i-1]<<3)&0xF8) |
01106 ((from_data[i]>>5)&0x07) ;
01107 #ifdef PRINT_STUFF
01108 printf("from:" ) ;
01109 for( i=0; i<8; i++ )
01110 printf("%02x ", from_data[i] ) ;
01111 printf("to:" ) ;
01112 for( i=0; i<8; i++ )
01113 printf("%02x ", to_data[i] ) ;
01114 printf("\n" ) ;
01115 #endif
01116 break ;
01117
01118 case EVAL_2_BYTES( 'X', '4' ):
01119 ADFI_big_endian_to_cray( from_format, from_os_size,
01120 to_format, to_os_size, "R4", delta_from_bytes,
01121 delta_to_bytes, from_data, to_data, error_return ) ;
01122 if( *error_return != NO_ERROR )
01123 return ;
01124
01125 ADFI_big_endian_to_cray( from_format, from_os_size,
01126 to_format, to_os_size, "R4", delta_from_bytes,
01127 delta_to_bytes, &from_data[4], &to_data[8], error_return ) ;
01128 if( *error_return != NO_ERROR )
01129 return ;
01130 break ;
01131
01132 case EVAL_2_BYTES( 'X', '8' ):
01133 ADFI_big_endian_to_cray( from_format, from_os_size,
01134 to_format, to_os_size, "R8", delta_from_bytes,
01135 delta_to_bytes, from_data, to_data, error_return ) ;
01136 if( *error_return != NO_ERROR )
01137 return ;
01138
01139 ADFI_big_endian_to_cray( from_format, from_os_size,
01140 to_format, to_os_size, "R8", delta_from_bytes,
01141 delta_to_bytes, &from_data[8], &to_data[8], error_return ) ;
01142 if( *error_return != NO_ERROR )
01143 return ;
01144 break ;
01145
01146 default:
01147 *error_return = INVALID_DATA_TYPE ;
01148 return ;
01149 }
01150 }
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220 void ADFI_big_little_endian_swap(
01221 const char from_format,
01222 const char from_os_size,
01223 const char to_format,
01224 const char to_os_size,
01225 const char data_type[2],
01226 const unsigned long delta_from_bytes,
01227 const unsigned long delta_to_bytes,
01228 const unsigned char *from_data,
01229 unsigned char *to_data,
01230 int *error_return )
01231 {
01232 int i ;
01233
01234 if( (from_data == NULL) || (to_data == NULL) ) {
01235 *error_return = NULL_STRING_POINTER ;
01236 return ;
01237 }
01238
01239 if( (delta_from_bytes == 0) || (delta_to_bytes == 0) ) {
01240 *error_return = NULL_POINTER ;
01241 return ;
01242 }
01243
01244 if( (from_format == 'N') || (to_format == 'N') ) {
01245 *error_return = CANNOT_CONVERT_NATIVE_FORMAT ;
01246 return ;
01247 }
01248
01249 if ( from_os_size != to_os_size || delta_to_bytes != delta_from_bytes ) {
01250 *error_return = DATA_TYPE_NOT_SUPPORTED ;
01251 return ;
01252 }
01254 *error_return = NO_ERROR ;
01255
01256 for ( i=0; i<(int)delta_from_bytes; i++ )
01257 to_data[i] = from_data[delta_from_bytes-1-i] ;
01258
01259 }
01260
01261
01262
01263
01264
01265
01266
01267
01268 void ADFI_blank_fill_string(
01269 char *str,
01270 const int length )
01271 {
01272 int i ;
01273 for( i=strlen( str ); i<length; i++ )
01274 str[ i ] = ' ' ;
01275 }
01276
01277
01278
01279
01280
01281
01282
01283 void ADFI_find_file(char *filename, int *error_return)
01284 {
01285 #ifdef CGNS_IO_H
01286 char pathname[ADF_FILENAME_LENGTH+1];
01287
01288 if (cgio_find_file(filename, CGIO_FILE_ADF, sizeof(pathname), pathname)) {
01289 *error_return = LINKED_TO_FILE_NOT_THERE;
01290 }
01291 else {
01292 strcpy(filename, pathname);
01293 *error_return = NO_ERROR;
01294 }
01295 #else
01296 *error_return = NO_ERROR;
01297 #endif
01298 }
01299
01300
01301
01302
01303
01304
01305
01306
01307
01308 static void ADFI_link_open(
01309 char *linkfile,
01310 char *status,
01311 double *link_ID,
01312 unsigned int *link_index,
01313 int *error_return)
01314 {
01315 unsigned long file_block, block_offset;
01316
01317 ADF_Database_Open( linkfile, status, "", link_ID, error_return ) ;
01318 if (*error_return == NO_ERROR) {
01319 ADFI_ID_2_file_block_offset(*link_ID, link_index,
01320 &file_block, &block_offset, error_return);
01321 }
01322 }
01323
01324
01325
01326
01327
01328
01329
01330 static void ADFI_link_add(
01331 unsigned int file_index,
01332 unsigned int link_index,
01333 int found)
01334 {
01335 int n, nlinks;
01336 unsigned int *links;
01337
01338 if (file_index == link_index) return;
01339 nlinks = ADF_file[file_index].nlinks;
01340 if (nlinks == 0) {
01341 links = (unsigned int *) malloc (sizeof(unsigned int));
01342 }
01343 else {
01344 links = ADF_file[file_index].links;
01345 for (n = 0; n < nlinks; n++) {
01346 if (links[n] == link_index) return;
01347 }
01348 links = (unsigned int *) malloc ((nlinks + 1) * sizeof(unsigned int));
01349 }
01350 if (links == NULL) return;
01351 if (nlinks) {
01352 for (n = 0; n < nlinks; n++)
01353 links[n] = ADF_file[file_index].links[n];
01354 free (ADF_file[file_index].links);
01355 }
01356 links[nlinks] = link_index;
01357 ADF_file[file_index].nlinks = nlinks + 1;
01358 ADF_file[file_index].links = links;
01359 if (found) {
01360 (ADF_file[link_index].in_use)++;
01361 }
01362 }
01363
01364
01365
01366
01367
01368
01369
01370
01371
01372
01373
01374
01375
01376
01377 void ADFI_chase_link(
01378 const double ID,
01379 double *LID,
01380 unsigned int *file_index,
01381 struct DISK_POINTER *block_offset,
01382 struct NODE_HEADER *node_header,
01383 int *error_return )
01384 {
01385 double Link_ID, temp_ID ;
01386 int done = FALSE ;
01387 int link_depth = 0 ;
01388 int found ;
01389 unsigned int link_file_index ;
01390 char status[10] ;
01391 char link_file[ADF_FILENAME_LENGTH+1],
01392 link_path[ADF_MAX_LINK_DATA_SIZE+1] ;
01393
01394 if( (LID == NULL) || (file_index == NULL) || (block_offset == NULL) ||
01395 (node_header == NULL ) ) {
01396 *error_return = NULL_POINTER ;
01397 return ;
01398 }
01399
01400 if (ID == last_link_ID) {
01401 *LID = last_link_LID;
01402 ADFI_ID_2_file_block_offset( last_link_LID, file_index, &block_offset->block,
01403 &block_offset->offset, error_return ) ;
01404 if( *error_return != NO_ERROR )
01405 return ;
01406 ADFI_read_node_header( *file_index, block_offset, node_header,
01407 error_return ) ;
01408 return ;
01409 }
01410
01411 Link_ID = ID ;
01412 while( done == FALSE ) {
01414 ADFI_ID_2_file_block_offset( Link_ID, file_index, &block_offset->block,
01415 &block_offset->offset, error_return ) ;
01416 CHECK_ABORT(*error_return);
01417
01419 ADFI_read_node_header( *file_index, block_offset, node_header,
01420 error_return ) ;
01421 CHECK_ABORT(*error_return);
01422
01423 if( (node_header->data_type[0] == 'L') &&
01424 (node_header->data_type[1] == 'K')) {
01425
01427 ADF_Get_Link_Path( Link_ID, link_file, link_path, error_return ) ;
01428 CHECK_ABORT(*error_return);
01429
01430 if( link_file[0] != '\0' ) {
01431
01432 ADFI_find_file(link_file, error_return);
01433 CHECK_ABORT(*error_return);
01434
01439 ADFI_get_file_index_from_name( link_file, &found, &link_file_index,
01440 &Link_ID, error_return ) ;
01441 if( ! found ) {
01442 if (access(link_file,2))
01443 strcpy (status, "READ_ONLY");
01444 else
01445 strcpy (status, ADF_file[*file_index].open_mode) ;
01446 if ( ADFI_stridx_c(status, "READ_ONLY" ) != 0 )
01447 strcpy (status, "OLD") ;
01448 ADFI_link_open( link_file, status, &Link_ID,
01449 &link_file_index, error_return );
01450 CHECK_ABORT(*error_return);
01451 }
01452 ADFI_link_add( *file_index, link_file_index, found );
01453 }
01454 else {
01455 ADF_Get_Node_ID( Link_ID, "/", &temp_ID, error_return ) ;
01456 CHECK_ABORT(*error_return);
01457 Link_ID = temp_ID ;
01458 }
01459
01461 ADF_Get_Node_ID( Link_ID, link_path, &temp_ID, error_return ) ;
01462 if( *error_return == CHILD_NOT_OF_GIVEN_PARENT )
01463 *error_return = LINK_TARGET_NOT_THERE ;
01464 CHECK_ABORT(*error_return);
01465
01466 Link_ID = temp_ID ;
01467 if( ++link_depth > ADF_MAXIMUM_LINK_DEPTH ) {
01468 *error_return = LINKS_TOO_DEEP ;
01469 return ;
01470 }
01471 }
01472 else {
01473 done = TRUE ;
01474 }
01475 }
01476
01477 *LID = Link_ID ;
01478 if (Link_ID != ID) {
01479 last_link_ID = ID;
01480 last_link_LID = Link_ID;
01481 }
01482
01483 }
01484
01485
01486
01487
01488
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500
01501
01502
01503
01504 void ADFI_check_4_child_name(
01505 const int file_index,
01506 const struct DISK_POINTER *parent,
01507 const char *name,
01508 int *found,
01509 struct DISK_POINTER *sub_node_entry_location,
01510 struct SUB_NODE_TABLE_ENTRY *sub_node_entry,
01511 int *error_return )
01512 {
01513 struct NODE_HEADER parent_node ;
01514 struct SUB_NODE_TABLE_ENTRY *sub_node_table ;
01515 int i ;
01516
01517 if( (parent == NULL) || (found == NULL) || (sub_node_entry_location == NULL) ||
01518 (sub_node_entry == NULL ) ) {
01519 *error_return = NULL_POINTER ;
01520 return ;
01521 }
01522
01523 if( name == NULL ) {
01524 *error_return = NULL_STRING_POINTER ;
01525 return ;
01526 }
01527
01528 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
01529 *error_return = ADF_FILE_NOT_OPENED ;
01530 return ;
01531 }
01532
01533 *error_return = NO_ERROR ;
01534 *found = 0 ;
01535
01537 ADFI_read_node_header( file_index, parent, &parent_node, error_return ) ;
01538 if( *error_return != NO_ERROR )
01539 return ;
01540
01544 if( parent_node.num_sub_nodes == 0 ) {
01545 *found = 0 ;
01546 return ;
01547 }
01548
01550 sub_node_table = (struct SUB_NODE_TABLE_ENTRY *)
01551 malloc( parent_node.entries_for_sub_nodes *
01552 sizeof( *sub_node_table ) ) ;
01553 if( sub_node_table == NULL ) {
01554 *error_return = MEMORY_ALLOCATION_FAILED ;
01555 return ;
01556 }
01557
01558 if( parent_node.entries_for_sub_nodes > 0 ) {
01559 ADFI_read_sub_node_table( file_index, &parent_node.sub_node_table,
01560 sub_node_table, error_return ) ;
01561 if( *error_return != NO_ERROR )
01562 return ;
01563 }
01564
01566 for( i=0; i<(int)parent_node.num_sub_nodes; i++ ) {
01567 ADFI_compare_node_names( sub_node_table[i].child_name, name,
01568 found, error_return ) ;
01569 if( *error_return != NO_ERROR )
01570 break ;
01571 if( *found == 1 ) {
01572 sub_node_entry_location->block = parent_node.sub_node_table.block ;
01573 sub_node_entry_location->offset = parent_node.sub_node_table.offset +
01574 TAG_SIZE + DISK_POINTER_SIZE +
01575 (ADF_NAME_LENGTH + DISK_POINTER_SIZE) * i ;
01576
01577 ADFI_adjust_disk_pointer( sub_node_entry_location, error_return ) ;
01578 if( *error_return != NO_ERROR )
01579 return ;
01580
01582 strncpy( sub_node_entry->child_name, sub_node_table[i].child_name,
01583 ADF_NAME_LENGTH ) ;
01584 sub_node_entry->child_location.block =
01585 sub_node_table[i].child_location.block ;
01586 sub_node_entry->child_location.offset =
01587 sub_node_table[i].child_location.offset ;
01588
01590 break ;
01591 }
01592 }
01593
01594 free( sub_node_table ) ;
01595 }
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606
01607
01608
01609
01610
01611
01612
01613
01614
01615 void ADFI_check_string_length(
01616 const char *str,
01617 const int max_length,
01618 int *error_return )
01619 {
01620 int str_length, i ;
01621
01622 if( str == NULL ) {
01623 *error_return = NULL_STRING_POINTER ;
01624 return ;
01625 }
01626
01627 str_length = strlen( str ) ;
01628 if( str_length == 0 ) {
01629 *error_return = STRING_LENGTH_ZERO ;
01630 return ;
01631 }
01632
01633 if( (int) strlen( str ) > max_length ) {
01634 *error_return = STRING_LENGTH_TOO_BIG ;
01635 return ;
01636 }
01637
01639 *error_return = STRING_LENGTH_ZERO ;
01640 for( i=0; i<str_length; i++ ) {
01641 if( (str[i] != ' ') && (str[i] != '\t') ) {
01642 *error_return = NO_ERROR ;
01643 break ;
01644 }
01645 }
01646 }
01647
01648
01649
01650
01651
01652
01653
01654
01655
01656
01657
01658
01659
01660
01661 void ADFI_close_file(
01662 const int file_index,
01663 int *error_return )
01664 {
01665 int index ;
01666
01667 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
01668 *error_return = ADF_FILE_NOT_OPENED ;
01669 return ;
01670 }
01671
01672 *error_return = NO_ERROR ;
01673
01674
01675 for (index = 0; index < ADF_file[file_index].nlinks; index++) {
01676 ADFI_close_file( ADF_file[file_index].links[index], error_return);
01677 }
01678
01679
01680 index = ADF_file[file_index].in_use - 1;
01681 if ( index == 0) {
01682 #ifdef USE_STREAM_IO
01683 if( ADF_file[file_index].file != 0 ) {
01684 ADFI_flush_buffers( file_index, FLUSH_CLOSE, error_return );
01685 if( fclose( ADF_file[file_index].file ) != 0 )
01686 *error_return = FILE_CLOSE_ERROR ;
01687 }
01688 ADF_file[file_index].file = NULL ;
01689 #else
01690 ADF_sys_err = 0;
01691 if( ADF_file[file_index].file >= 0 ) {
01692 ADFI_flush_buffers( file_index, FLUSH_CLOSE, error_return );
01693 if( close( ADF_file[file_index].file ) < 0 ) {
01694 ADF_sys_err = errno;
01695 *error_return = FILE_CLOSE_ERROR ;
01696 }
01697 }
01698 ADF_file[file_index].file = -1 ;
01699 #endif
01700
01701 ADFI_stack_control(file_index,0,0,CLEAR_STK,0,0,NULL);
01702
01703 if (ADF_file[file_index].nlinks) {
01704 free (ADF_file[file_index].links);
01705 ADF_file[file_index].nlinks = 0;
01706 }
01707 if (ADF_file[file_index].file_name != NULL) {
01708 free (ADF_file[file_index].file_name);
01709 ADF_file[file_index].file_name = NULL;
01710 }
01711 }
01712 ADF_file[file_index].in_use = index;
01713
01714
01715
01716 for (index = 0; index < maximum_files; index++) {
01717 if (ADF_file[index].in_use) return;
01718 }
01719 free (ADF_file);
01720 maximum_files = 0;
01721
01722 }
01723
01724
01725
01726
01727
01728
01729
01730
01731
01732
01733
01734
01735
01736
01737
01738 void ADFI_compare_node_names(
01739 const char *name,
01740 const char *new_name,
01741 int *names_match,
01742 int *error_return )
01743 {
01744 int i, new_length ;
01745
01746 if( (name == NULL) || (new_name == NULL) ) {
01747 *error_return = NULL_STRING_POINTER ;
01748 return ;
01749 }
01750
01751 if( names_match == NULL ) {
01752 *error_return = NULL_POINTER ;
01753 return ;
01754 }
01755
01756 *error_return = NO_ERROR ;
01757 *names_match = 0 ;
01758
01759 new_length = strlen( new_name ) ;
01760 for( i=0; i<MIN( new_length, ADF_NAME_LENGTH ); i++ ) {
01761 if( name[i] != new_name[i] ) {
01762 *names_match = 0 ;
01763 return ;
01764 }
01765 }
01766
01770 for( ; i<ADF_NAME_LENGTH; i++ ) {
01771 if( name[i] != ' ' ) {
01772 *names_match = 0 ;
01773 return ;
01774 }
01775 }
01776
01777 *names_match = 1 ;
01778 }
01779
01780
01781
01782
01783
01784
01785
01786
01787
01788
01789
01790
01791
01792
01793
01794
01795
01796
01797
01798
01799
01800
01801
01802
01803
01804
01805
01806
01807
01808
01809
01810
01811
01812
01813
01814
01815
01816
01817
01818
01819
01820
01821
01822
01823
01824
01825
01826
01827
01828
01829
01830
01831
01832
01833
01834
01835
01836
01837
01838
01839
01840
01841
01842
01843
01844
01845
01846
01847
01848
01849
01850
01851
01852
01853
01854
01855
01856
01857
01858 void ADFI_convert_number_format(
01859 const char from_format,
01860 const char from_os_size,
01861 const char to_format,
01862 const char to_os_size,
01863 const int convert_dir,
01864 const struct TOKENIZED_DATA_TYPE *tokenized_data_type,
01865 const unsigned int length,
01866 unsigned char *from_data,
01867 unsigned char *to_data,
01868 int *error_return )
01869 {
01870 unsigned char temp_data[16] ;
01871 char data_type[2] ;
01872 int current_token ;
01873 int array_size ;
01874 int l, s ;
01875 unsigned long delta_from_bytes, delta_to_bytes ;
01876
01877 if( (from_data == NULL) || (to_data == NULL) ) {
01878 *error_return = NULL_STRING_POINTER ;
01879 return ;
01880 }
01881
01882 if( length == 0 ) {
01883 *error_return = NUMBER_LESS_THAN_MINIMUM ;
01884 return ;
01885 }
01886
01887 if( (from_format == 'N') || (to_format == 'N') ) {
01888 *error_return = CANNOT_CONVERT_NATIVE_FORMAT ;
01889 return ;
01890 }
01891
01892 switch( EVAL_4_BYTES( from_format, to_format, from_os_size, to_os_size ) ) {
01893 case EVAL_4_BYTES( 'B', 'B', 'B', 'B' ):
01894 case EVAL_4_BYTES( 'C', 'C', 'B', 'B' ):
01895 case EVAL_4_BYTES( 'L', 'L', 'B', 'B' ):
01896 case EVAL_4_BYTES( 'B', 'B', 'L', 'L' ):
01897 case EVAL_4_BYTES( 'C', 'C', 'L', 'L' ):
01898 case EVAL_4_BYTES( 'L', 'L', 'L', 'L' ):
01899 *error_return = CONVERSION_FORMATS_EQUAL ;
01900 return ;
01901 }
01902
01903 *error_return = NO_ERROR ;
01904
01906 for ( l=0; l<(int)length; l++ ) {
01907 current_token = -1 ;
01908 while( tokenized_data_type[ ++current_token ].type[0] != 0 ) {
01909 data_type[0] = tokenized_data_type[ current_token ].type[0] ;
01910 data_type[1] = tokenized_data_type[ current_token ].type[1] ;
01911 array_size = tokenized_data_type[ current_token ].length ;
01912 if ( convert_dir == FROM_FILE_FORMAT ) {
01913 delta_from_bytes=tokenized_data_type[ current_token ].file_type_size ;
01914 delta_to_bytes =tokenized_data_type[ current_token ].machine_type_size ;
01915 }
01916 else {
01917 delta_to_bytes =tokenized_data_type[ current_token ].file_type_size ;
01918 delta_from_bytes=tokenized_data_type[ current_token ].machine_type_size ;
01919 }
01921 for ( s=0; s<array_size; s++ ) {
01922 switch( EVAL_4_BYTES( from_format,to_format,from_os_size,to_os_size ) ) {
01923
01924 case EVAL_4_BYTES( 'B', 'B', 'L', 'B' ):
01925 case EVAL_4_BYTES( 'B', 'B', 'B', 'L' ):
01926 ADFI_big_endian_32_swap_64( from_format, from_os_size,
01927 to_format, to_os_size, data_type,
01928 delta_from_bytes, delta_to_bytes,
01929 from_data, to_data, error_return );
01930 break ;
01931
01932 case EVAL_4_BYTES( 'L', 'L', 'L', 'B' ):
01933 case EVAL_4_BYTES( 'L', 'L', 'B', 'L' ):
01934 ADFI_little_endian_32_swap_64( from_format, from_os_size,
01935 to_format, to_os_size, data_type,
01936 delta_from_bytes, delta_to_bytes,
01937 from_data, to_data, error_return );
01938 break ;
01939
01940 case EVAL_4_BYTES( 'B', 'C', 'L', 'B' ):
01941 case EVAL_4_BYTES( 'B', 'C', 'B', 'B' ):
01942 ADFI_big_endian_to_cray( from_format, from_os_size,
01943 to_format, to_os_size, data_type,
01944 delta_from_bytes, delta_to_bytes,
01945 from_data, to_data, error_return );
01946 break ;
01947
01948 case EVAL_4_BYTES( 'C', 'B', 'B', 'L' ):
01949 case EVAL_4_BYTES( 'C', 'B', 'B', 'B' ):
01950 ADFI_cray_to_big_endian( from_format, from_os_size,
01951 to_format, to_os_size, data_type,
01952 delta_from_bytes, delta_to_bytes,
01953 from_data, to_data, error_return );
01954 break ;
01955
01956 case EVAL_4_BYTES( 'B', 'L', 'B', 'L' ):
01957 case EVAL_4_BYTES( 'B', 'L', 'L', 'B' ):
01958 ADFI_big_endian_32_swap_64( from_format, from_os_size,
01959 from_format, to_os_size, data_type,
01960 delta_from_bytes, delta_to_bytes,
01961 from_data, temp_data, error_return );
01962 ADFI_big_little_endian_swap( from_format, to_os_size,
01963 to_format, to_os_size, data_type,
01964 delta_to_bytes, delta_to_bytes,
01965 temp_data, to_data, error_return );
01966 break ;
01967
01968 case EVAL_4_BYTES( 'L', 'B', 'B', 'L' ):
01969 case EVAL_4_BYTES( 'L', 'B', 'L', 'B' ):
01970 ADFI_little_endian_32_swap_64( from_format, from_os_size,
01971 from_format, to_os_size, data_type,
01972 delta_from_bytes, delta_to_bytes,
01973 from_data, temp_data, error_return );
01974 ADFI_big_little_endian_swap( from_format, to_os_size,
01975 to_format, to_os_size, data_type,
01976 delta_to_bytes, delta_to_bytes,
01977 temp_data, to_data, error_return );
01978 break ;
01979
01980 case EVAL_4_BYTES( 'B', 'L', 'L', 'L' ):
01981 case EVAL_4_BYTES( 'L', 'B', 'L', 'L' ):
01982 case EVAL_4_BYTES( 'B', 'L', 'B', 'B' ):
01983 case EVAL_4_BYTES( 'L', 'B', 'B', 'B' ):
01984 ADFI_big_little_endian_swap( from_format, from_os_size,
01985 to_format, to_os_size, data_type,
01986 delta_from_bytes, delta_to_bytes,
01987 from_data, to_data, error_return );
01988 break ;
01989
01990 case EVAL_4_BYTES( 'C', 'L', 'B', 'L' ):
01991 case EVAL_4_BYTES( 'C', 'L', 'B', 'B' ):
01992 ADFI_cray_to_little_endian( from_format, from_os_size,
01993 to_format, to_os_size, data_type,
01994 delta_from_bytes, delta_to_bytes,
01995 from_data, to_data, error_return );
01996 break ;
01997
01998 case EVAL_4_BYTES( 'L', 'C', 'L', 'B' ):
01999 case EVAL_4_BYTES( 'L', 'C', 'B', 'B' ):
02000 ADFI_little_endian_to_cray( from_format, from_os_size,
02001 to_format, to_os_size, data_type,
02002 delta_from_bytes, delta_to_bytes,
02003 from_data, to_data, error_return );
02004 break ;
02005
02006 default:
02007 *error_return = MACHINE_FORMAT_NOT_RECOGNIZED ;
02008 return ;
02009 }
02010 if ( *error_return != NO_ERROR )
02011 return ;
02013 to_data += delta_to_bytes ;
02014 from_data += delta_from_bytes ;
02015 }
02016 }
02017 }
02018
02019 }
02020
02021
02022
02023
02024
02025
02026
02027
02028
02029
02030
02031
02032
02033
02034
02035
02036
02037
02038
02039
02040
02041
02042
02043
02044
02045
02046
02047 void ADFI_count_total_array_points(
02048 const unsigned int ndim,
02049 const unsigned int dims[],
02050 const int dim_start[],
02051 const int dim_end[],
02052 const int dim_stride[],
02053 unsigned long *total_points,
02054 unsigned long *starting_offset,
02055 int *error_return )
02056 {
02057 unsigned int i ;
02058 unsigned long total, offset ;
02059 unsigned long accumlated_size ;
02060
02061 if( (dims == NULL) || (dim_start == NULL) || (dim_end == NULL) ||
02062 (dim_stride == NULL) || (total_points == NULL) ||
02063 (starting_offset == NULL) ) {
02064 *error_return = NULL_POINTER ;
02065 return ;
02066 }
02067
02068 if( (ndim <= 0) || (ndim > 12) ) {
02069 *error_return = BAD_NUMBER_OF_DIMENSIONS ;
02070 return ;
02071 }
02072
02073 *error_return = NO_ERROR ;
02074
02076 for( i=0; i<ndim; i++ ) {
02077
02079 if( dims[i] < 1 ) {
02080 *error_return = BAD_DIMENSION_VALUE ;
02081 return ;
02082 }
02083
02085 if( (dim_start[i] < 1) || (dim_start[i] > (int) dims[i]) ) {
02086 *error_return = START_OUT_OF_DEFINED_RANGE ;
02087 return ;
02088 }
02089
02091 if( (dim_end[i] < 1) || (dim_end[i] > (int) dims[i]) ) {
02092 *error_return = END_OUT_OF_DEFINED_RANGE ;
02093 return ;
02094 }
02095 if( dim_end[i] < dim_start[i] ) {
02096 *error_return = MINIMUM_GT_MAXIMUM ;
02097 return ;
02098 }
02099
02101 if( dim_stride[i] < 1 ) {
02102 *error_return = BAD_STRIDE_VALUE ;
02103 return ;
02104 }
02105 }
02106
02107 total = 1 ;
02108 offset = 0 ;
02109 accumlated_size = 1 ;
02110 for( i=0; i<ndim; i++ ) {
02111 total *= (dim_end[i] - dim_start[i] + dim_stride[i]) / dim_stride[i] ;
02112 offset += (dim_start[i] - 1) * accumlated_size ;
02113 accumlated_size *= dims[i] ;
02114 }
02115 *total_points = total ;
02116 *starting_offset = offset ;
02117 }
02118
02119
02120
02121
02122
02123
02124
02125
02126
02127
02128
02129
02130
02131
02132
02133
02134
02135
02136
02137
02138
02139
02140
02141
02142
02143
02144
02145
02146
02147
02148
02149
02150
02151
02152
02153
02154
02155
02156
02157
02158
02159
02160
02161
02162
02163
02164
02165
02166
02167
02168
02169
02170
02171
02172
02173
02174
02175
02176
02177
02178
02179
02180
02181
02182
02183 void ADFI_cray_to_big_endian(
02184 const char from_format,
02185 const char from_os_size,
02186 const char to_format,
02187 const char to_os_size,
02188 const char data_type[2],
02189 const unsigned long delta_from_bytes,
02190 const unsigned long delta_to_bytes,
02191 const unsigned char *from_data,
02192 unsigned char *to_data,
02193 int *error_return )
02194 {
02195 int i, exp ;
02196
02197 if( (from_data == NULL) || (to_data == NULL) ) {
02198 *error_return = NULL_STRING_POINTER ;
02199 return ;
02200 }
02201
02202 if( (delta_from_bytes == 0) || (delta_to_bytes == 0) ) {
02203 *error_return = NULL_POINTER ;
02204 return ;
02205 }
02206
02207 if( (from_format == 'N') || (to_format == 'N') ) {
02208 *error_return = CANNOT_CONVERT_NATIVE_FORMAT ;
02209 return ;
02210 }
02211
02212 *error_return = NO_ERROR ;
02213
02214 switch( EVAL_2_BYTES( data_type[0], data_type[1] ) ) {
02215 case EVAL_2_BYTES( 'M', 'T' ):
02216 *error_return = NO_DATA ;
02217 return ;
02218
02219 case EVAL_2_BYTES( 'C', '1' ):
02220 case EVAL_2_BYTES( 'B', '1' ):
02221 to_data[0] = from_data[0] ;
02222 break ;
02223
02224 case EVAL_2_BYTES( 'I', '4' ):
02225 case EVAL_2_BYTES( 'U', '4' ):
02226 to_data[0] = from_data[4] ;
02227 to_data[1] = from_data[5] ;
02228 to_data[2] = from_data[6] ;
02229 to_data[3] = from_data[7] ;
02230 break ;
02231
02232 case EVAL_2_BYTES( 'I', '8' ):
02233 case EVAL_2_BYTES( 'U', '8' ):
02234 for( i=0; i<(int) delta_to_bytes; i++ )
02235 to_data[i] = from_data[8-delta_to_bytes+i] ;
02236 break ;
02237
02238 case EVAL_2_BYTES( 'R', '4' ):
02239 for( i=0; i<4; i++ )
02240 to_data[i] = 0x00 ;
02241
02243 if( (from_data[0] == 0x00) && (from_data[1] == 0x00) &&
02244 (from_data[2] == 0x00) && (from_data[3] == 0x00) &&
02245 (from_data[4] == 0x00) && (from_data[5] == 0x00) &&
02246 (from_data[6] == 0x00) && (from_data[7] == 0x00) )
02247 break ;
02248
02250 to_data[0] = from_data[0] & 0x80 ;
02251
02255 exp = from_data[1] + ((from_data[0]&0x3f)<<8) ;
02256 if( (from_data[0] & 0x40) == 0x00 )
02257 exp -= 16384 ;
02258 exp -= 2 ;
02259
02260 if( exp >= 128 ) {
02261 *error_return = NUMERIC_OVERFLOW ;
02262 return ;
02263 }
02264 else if ( exp < -128 ) {
02265 for( i=0; i<4; i++ ) to_data[i] = 0x00 ;
02266 break;
02267 }
02268
02269 to_data[0] |= ((exp&0x7F) >> 1) ;
02270 if( (exp & 0x01) == 0x01 )
02271 to_data[1] |= 0x80 ;
02272
02273 if( exp >= 0 )
02274 to_data[0] |= 0x40 ;
02275
02278 to_data[1] |= (from_data[2] & 0x7f) ;
02279 to_data[2] = from_data[3] ;
02280 to_data[3] = from_data[4] ;
02281 break ;
02282
02283 case EVAL_2_BYTES( 'R', '8' ):
02284 for( i=0; i<8; i++ )
02285 to_data[i] = 0x00 ;
02286
02288 if( (from_data[0] == 0x00) && (from_data[1] == 0x00) &&
02289 (from_data[2] == 0x00) && (from_data[3] == 0x00) )
02290 break ;
02291
02293 to_data[0] = from_data[0] & 0x80 ;
02294
02298 exp = from_data[1] + ((from_data[0]&0x3f)<<8) ;
02299
02300 if( (exp != 0) && ((from_data[0] & 0x40) == 0x00) )
02301 exp -= 16384 ;
02302 exp -= 2 ;
02303
02304 if( exp >= 1024 ) {
02305 *error_return = NUMERIC_OVERFLOW ;
02306 return ;
02307 }
02308 else if ( exp < -1024 ) {
02309 for( i=0; i<4; i++ ) to_data[i] = 0x00 ;
02310 break;
02311 }
02312
02313 to_data[0] |= ((exp & 0x03F0) >> 4) ;
02314 to_data[1] |= ((exp & 0x000F) << 4) ;
02315
02316 if( exp >= 0 )
02317 to_data[0] |= 0x40 ;
02318
02321 to_data[1] |= ((from_data[2] & 0x78) >> 3) ;
02322 for( i=2; i<7; i++ )
02323 to_data[i] = ((from_data[i] & 0x07) << 5) |
02324 ((from_data[i+1] & 0xf8) >> 3) ;
02325 to_data[7] = ((from_data[7] & 0x07) << 5) ;
02326
02327 #ifdef PRINT_STUFF
02328 printf("from:" ) ;
02329 for( i=0; i<8; i++ )
02330 printf("%02x ", from_data[i] ) ;
02331 printf("to:" ) ;
02332 for( i=0; i<8; i++ )
02333 printf("%02x ", to_data[i] ) ;
02334 printf("\n" ) ;
02335 #endif
02336 break ;
02337
02338 case EVAL_2_BYTES( 'X', '4' ):
02339 ADFI_cray_to_big_endian( from_format, from_os_size,
02340 to_format, to_os_size, "R4", delta_from_bytes,
02341 delta_to_bytes, from_data, to_data, error_return ) ;
02342 if( *error_return != NO_ERROR )
02343 return ;
02344
02345 ADFI_cray_to_big_endian( from_format, from_os_size,
02346 to_format, to_os_size, "R4", delta_from_bytes,
02347 delta_to_bytes, &from_data[8], &to_data[4], error_return ) ;
02348 if( *error_return != NO_ERROR )
02349 return ;
02350 break ;
02351
02352 case EVAL_2_BYTES( 'X', '8' ):
02353 ADFI_cray_to_big_endian( from_format, from_os_size,
02354 to_format, to_os_size, "R8", delta_from_bytes,
02355 delta_to_bytes, from_data, to_data, error_return ) ;
02356 if( *error_return != NO_ERROR )
02357 return ;
02358
02359 ADFI_cray_to_big_endian( from_format, from_os_size,
02360 to_format, to_os_size, "R8", delta_from_bytes,
02361 delta_to_bytes, &from_data[8], &to_data[8], error_return ) ;
02362 if( *error_return != NO_ERROR )
02363 return ;
02364 break ;
02365
02366 default:
02367 *error_return = INVALID_DATA_TYPE ;
02368 return ;
02369
02370 }
02371 }
02372
02373
02374
02375
02376
02377
02378
02379
02380
02381
02382
02383
02384
02385
02386
02387
02388
02389
02390
02391
02392
02393
02394
02395
02396
02397
02398
02399
02400
02401
02402
02403
02404
02405
02406
02407
02408
02409
02410
02411
02412
02413
02414
02415
02416
02417
02418
02419
02420
02421
02422
02423
02424
02425
02426
02427
02428
02429
02430
02431
02432
02433
02434
02435
02436
02437 void ADFI_cray_to_little_endian(
02438 const char from_format,
02439 const char from_os_size,
02440 const char to_format,
02441 const char to_os_size,
02442 const char data_type[2],
02443 const unsigned long delta_from_bytes,
02444 const unsigned long delta_to_bytes,
02445 const unsigned char *from_data,
02446 unsigned char *to_data,
02447 int *error_return )
02448 {
02449 int i, exp ;
02450
02451 if( (from_data == NULL) || (to_data == NULL) ) {
02452 *error_return = NULL_STRING_POINTER ;
02453 return ;
02454 }
02455
02456 if( (delta_from_bytes == 0) || (delta_to_bytes == 0) ) {
02457 *error_return = NULL_POINTER ;
02458 return ;
02459 }
02460
02461 if( (from_format == 'N') || (to_format == 'N') ) {
02462 *error_return = CANNOT_CONVERT_NATIVE_FORMAT ;
02463 return ;
02464 }
02465
02466 *error_return = NO_ERROR ;
02467
02468 switch( EVAL_2_BYTES( data_type[0], data_type[1] ) ) {
02469 case EVAL_2_BYTES( 'M', 'T' ):
02470 *error_return = NO_DATA ;
02471 return ;
02472
02473 case EVAL_2_BYTES( 'C', '1' ):
02474 case EVAL_2_BYTES( 'B', '1' ):
02475 to_data[0] = from_data[0] ;
02476 break ;
02477
02478 case EVAL_2_BYTES( 'I', '4' ):
02479 case EVAL_2_BYTES( 'U', '4' ):
02480 to_data[3] = from_data[4] ;
02481 to_data[2] = from_data[5] ;
02482 to_data[1] = from_data[6] ;
02483 to_data[0] = from_data[7] ;
02484 break ;
02485
02486 case EVAL_2_BYTES( 'I', '8' ):
02487 case EVAL_2_BYTES( 'U', '8' ):
02488 for( i=0; i<(int) delta_to_bytes; i++ )
02489 to_data[delta_to_bytes-1-i] = from_data[8-delta_to_bytes+i] ;
02490 break ;
02491
02492 case EVAL_2_BYTES( 'R', '4' ):
02493 for( i=0; i<4; i++ )
02494 to_data[i] = 0x00 ;
02495
02497 if( (from_data[0] == 0x00) && (from_data[1] == 0x00) &&
02498 (from_data[2] == 0x00) && (from_data[3] == 0x00) &&
02499 (from_data[4] == 0x00) && (from_data[5] == 0x00) &&
02500 (from_data[6] == 0x00) && (from_data[7] == 0x00) )
02501 break ;
02502
02504 to_data[3] = from_data[0] & 0x80 ;
02505
02509 exp = from_data[1] + ((from_data[0]&0x3f)<<8) ;
02510 if( (from_data[0] & 0x40) == 0x00 )
02511 exp -= 16384 ;
02512 exp -= 2 ;
02513
02514 if( exp >= 128 ) {
02515 *error_return = NUMERIC_OVERFLOW ;
02516 return ;
02517 }
02518 else if ( exp < -128 ) {
02519 for( i=0; i<4; i++ ) to_data[i] = 0x00 ;
02520 break;
02521 }
02522
02523 to_data[3] |= ((exp&0x7F) >> 1) ;
02524 if( (exp & 0x01) == 0x01 )
02525 to_data[2] |= 0x80 ;
02526
02527 if( exp >= 0 )
02528 to_data[3] |= 0x40 ;
02529
02532 to_data[2] |= (from_data[2] & 0x7f) ;
02533 to_data[1] = from_data[3] ;
02534 to_data[0] = from_data[4] ;
02535 break ;
02536
02537 case EVAL_2_BYTES( 'R', '8' ):
02538 for( i=0; i<8; i++ )
02539 to_data[i] = 0x00 ;
02540
02542 if( (from_data[0] == 0x00) && (from_data[1] == 0x00) &&
02543 (from_data[2] == 0x00) && (from_data[3] == 0x00) )
02544 break ;
02545
02547 to_data[7] = from_data[0] & 0x80 ;
02548
02552 exp = from_data[1] + ((from_data[0]&0x3f)<<8) ;
02553
02554 if( (exp != 0) && ((from_data[0] & 0x40) == 0x00) )
02555 exp -= 16384 ;
02556 exp -= 2 ;
02557
02558 if( exp >= 1024 ) {
02559 *error_return = NUMERIC_OVERFLOW ;
02560 return ;
02561 }
02562 else if ( exp < -1024 ) {
02563 for( i=0; i<4; i++ ) to_data[i] = 0x00 ;
02564 break;
02565 }
02566
02567 to_data[7] |= ((exp & 0x03F0) >> 4) ;
02568 to_data[6] |= ((exp & 0x000F) << 4) ;
02569
02570 if( exp >= 0 )
02571 to_data[7] |= 0x40 ;
02572
02575 to_data[6] |= ((from_data[2] & 0x78) >> 3) ;
02576 for( i=2; i<7; i++ )
02577 to_data[7-i] = ((from_data[i] & 0x07) << 5) |
02578 ((from_data[i+1] & 0xf8) >> 3) ;
02579 to_data[0] = ((from_data[7] & 0x07) << 5) ;
02580
02581 #ifdef PRINT_STUFF
02582 printf("from:" ) ;
02583 for( i=0; i<8; i++ )
02584 printf("%02x ", from_data[i] ) ;
02585 printf("to:" ) ;
02586 for( i=0; i<8; i++ )
02587 printf("%02x ", to_data[i] ) ;
02588 printf("\n" ) ;
02589 #endif
02590 break ;
02591
02592 case EVAL_2_BYTES( 'X', '4' ):
02593 ADFI_cray_to_little_endian( from_format, from_os_size,
02594 to_format, to_os_size, "R4", delta_from_bytes,
02595 delta_to_bytes, from_data, to_data, error_return ) ;
02596 if( *error_return != NO_ERROR )
02597 return ;
02598
02599 ADFI_cray_to_little_endian( from_format, from_os_size,
02600 to_format, to_os_size, "R4", delta_from_bytes,
02601 delta_to_bytes, &from_data[8], &to_data[4], error_return ) ;
02602 if( *error_return != NO_ERROR )
02603 return ;
02604 break ;
02605
02606 case EVAL_2_BYTES( 'X', '8' ):
02607 ADFI_cray_to_little_endian( from_format, from_os_size,
02608 to_format, to_os_size, "R8", delta_from_bytes,
02609 delta_to_bytes, from_data, to_data, error_return ) ;
02610 if( *error_return != NO_ERROR )
02611 return ;
02612
02613 ADFI_cray_to_little_endian( from_format, from_os_size,
02614 to_format, to_os_size, "R8", delta_from_bytes,
02615 delta_to_bytes, &from_data[8], &to_data[8], error_return ) ;
02616 if( *error_return != NO_ERROR )
02617 return ;
02618 break ;
02619
02620 default:
02621 *error_return = INVALID_DATA_TYPE ;
02622 return ;
02623
02624 }
02625 }
02626
02627
02628
02629
02630
02631
02632
02633
02634
02635
02636
02637 void ADFI_delete_data(
02638 const int file_index,
02639 const struct NODE_HEADER *node_header,
02640 int *error_return )
02641 {
02642 struct DATA_CHUNK_TABLE_ENTRY *data_chunk_table ;
02643 int i ;
02644
02645 *error_return = NO_ERROR ;
02646
02647 if( node_header == NULL ) {
02648 *error_return = NULL_POINTER ;
02649 return ;
02650 }
02651
02652 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
02653 *error_return = ADF_FILE_NOT_OPENED ;
02654 return ;
02655 }
02656
02657 switch( node_header->number_of_data_chunks ) {
02658 case 0 :
02659 return ;
02660
02661 case 1 :
02662 ADFI_file_free( file_index, &node_header->data_chunks, 0, error_return ) ;
02663 if( *error_return != NO_ERROR )
02664 return ;
02665 break ;
02666
02667 default :
02670 data_chunk_table = (struct DATA_CHUNK_TABLE_ENTRY *)
02671 malloc( node_header->number_of_data_chunks * sizeof( *data_chunk_table ) ) ;
02672 if( data_chunk_table == NULL ) {
02673 *error_return = MEMORY_ALLOCATION_FAILED ;
02674 return ;
02675 }
02676
02678 ADFI_read_data_chunk_table( file_index, &node_header->data_chunks,
02679 data_chunk_table, error_return ) ;
02680 if( *error_return != NO_ERROR )
02681 return ;
02682
02684 for( i=0; i<(int)node_header->number_of_data_chunks; i++ ) {
02685 ADFI_file_free( file_index, &data_chunk_table[i].start,
02686 0, error_return ) ;
02687 if( *error_return != NO_ERROR )
02688 return ;
02689 }
02690 free( data_chunk_table ) ;
02691 ADFI_file_free( file_index, &node_header->data_chunks, 0, error_return ) ;
02692 if( *error_return != NO_ERROR )
02693 return ;
02694 break ;
02695 }
02696
02698 ADFI_stack_control(file_index, 0, 0, CLEAR_STK_TYPE, DISK_PTR_STK,
02699 0, NULL ) ;
02700
02701 }
02702
02703
02704
02705
02706
02707
02708
02709
02710
02711
02712
02713
02714
02715
02716
02717
02718 void ADFI_delete_from_sub_node_table(
02719 const int file_index,
02720 const struct DISK_POINTER *parent,
02721 const struct DISK_POINTER *child,
02722 int *error_return )
02723 {
02724
02725 int i, found ;
02726 struct NODE_HEADER parent_node ;
02727 struct SUB_NODE_TABLE_ENTRY *sub_node_table ;
02728
02729 if( (parent == NULL) || (child == NULL) ) {
02730 *error_return = NULL_POINTER ;
02731 return ;
02732 }
02733
02734 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
02735 *error_return = ADF_FILE_NOT_OPENED ;
02736 return ;
02737 }
02738
02739 *error_return = NO_ERROR ;
02740
02741 ADFI_read_node_header( file_index, parent, &parent_node, error_return ) ;
02742 if( *error_return != NO_ERROR )
02743 return ;
02744
02745 sub_node_table = (struct SUB_NODE_TABLE_ENTRY *)
02746 malloc( parent_node.entries_for_sub_nodes *
02747 sizeof( struct SUB_NODE_TABLE_ENTRY ) ) ;
02748 if( sub_node_table == NULL ) {
02749 *error_return = MEMORY_ALLOCATION_FAILED ;
02750 return ;
02751 }
02752
02753 ADFI_read_sub_node_table( file_index, &parent_node.sub_node_table,
02754 sub_node_table, error_return ) ;
02755 if( *error_return != NO_ERROR )
02756 return ;
02757
02759 for( i=0, found = -1 ; i<(int)parent_node.num_sub_nodes ; i++ ) {
02760 if( child->block == sub_node_table[i].child_location.block &&
02761 child->offset == sub_node_table[i].child_location.offset ) {
02762 found = i ;
02763 break ;
02764 }
02765 }
02766
02767 if( found == -1 ) {
02768 *error_return = SUB_NODE_TABLE_ENTRIES_BAD ;
02769 return ;
02770 }
02771
02773 for( i=found ; i<(int) (parent_node.num_sub_nodes-1) ; i++ ) {
02774 sub_node_table[i].child_location.block =
02775 sub_node_table[i+1].child_location.block ;
02776 sub_node_table[i].child_location.offset =
02777 sub_node_table[i+1].child_location.offset ;
02778 strncpy ( sub_node_table[i].child_name, sub_node_table[i+1].child_name,
02779 ADF_NAME_LENGTH ) ;
02780 }
02781
02782 i = parent_node.num_sub_nodes - 1 ;
02783 sub_node_table[i].child_location.block = 0 ;
02784 sub_node_table[i].child_location.offset = 0 ;
02785 strncpy ( sub_node_table[i].child_name,
02786 "unused entry in sub-node-table ", ADF_NAME_LENGTH ) ;
02787
02790 ADFI_write_sub_node_table( file_index, &parent_node.sub_node_table,
02791 parent_node.entries_for_sub_nodes,
02792 sub_node_table, error_return ) ;
02793 if( *error_return != NO_ERROR )
02794 return ;
02795
02797 parent_node.num_sub_nodes -= 1;
02798 ADFI_write_node_header( file_index, parent, &parent_node, error_return ) ;
02799 if( *error_return != NO_ERROR )
02800 return ;
02801
02803 ADFI_stack_control(file_index, 0, 0, CLEAR_STK_TYPE, SUBNODE_STK,
02804 0, NULL ) ;
02805 ADFI_stack_control(file_index, 0, 0, CLEAR_STK_TYPE, DISK_PTR_STK,
02806 0, NULL ) ;
02807
02808 free(sub_node_table);
02809
02810 }
02811
02812
02813
02814
02815
02816
02817
02818
02819
02820
02821
02822
02823
02824
02825
02826
02827
02828
02829
02830
02831
02832
02833 void ADFI_delete_sub_node_table(
02834 const int file_index,
02835 const struct DISK_POINTER *block_offset,
02836 const unsigned int size_sub_node_table,
02837 int *error_return )
02838 {
02839 unsigned int num_bytes ;
02840
02841
02842 *error_return = NO_ERROR ;
02843
02844 if( block_offset == NULL ) {
02845 *error_return = NULL_POINTER ;
02846 return ;
02847 }
02848
02849 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
02850 *error_return = ADF_FILE_NOT_OPENED ;
02851 return ;
02852 }
02853
02854 if( size_sub_node_table == 0 )
02855 return ;
02856
02857
02858 num_bytes = TAG_SIZE + TAG_SIZE + DISK_POINTER_SIZE +
02859 size_sub_node_table * (ADF_NAME_LENGTH + DISK_POINTER_SIZE);
02860
02861 ADFI_file_free( file_index, block_offset, num_bytes, error_return ) ;
02862 if( *error_return != NO_ERROR )
02863 return ;
02864
02866 ADFI_stack_control(file_index, 0, 0, CLEAR_STK_TYPE, SUBNODE_STK,
02867 0, NULL ) ;
02868 ADFI_stack_control(file_index, 0, 0, CLEAR_STK_TYPE, DISK_PTR_STK,
02869 0, NULL ) ;
02870
02871 }
02872
02873
02874
02875
02876
02877
02878
02879
02880
02881
02882
02883
02884
02885
02886
02887
02888 void ADFI_disk_pointer_2_ASCII_Hex(
02889 const struct DISK_POINTER *block_offset,
02890 char block[8],
02891 char offset[4],
02892 int *error_return )
02893 {
02894
02895 if( block_offset == NULL ) {
02896 *error_return = NULL_POINTER ;
02897 return ;
02898 }
02899
02900 if( (block == NULL) || (offset == NULL) ) {
02901 *error_return = NULL_STRING_POINTER ;
02902 return ;
02903 }
02904
02905 *error_return = NO_ERROR ;
02906
02908 ADFI_unsigned_int_2_ASCII_Hex( block_offset->block, 0, MAXIMUM_32_BITS,
02909 8, block, error_return ) ;
02910 if( *error_return != NO_ERROR )
02911 return ;
02912
02913 ADFI_unsigned_int_2_ASCII_Hex( block_offset->offset, 0, DISK_BLOCK_SIZE,
02914 4, offset, error_return ) ;
02915 if( *error_return != NO_ERROR )
02916 return ;
02917
02918 }
02919
02920
02921
02922
02923
02924
02925
02926
02927
02928
02929
02930
02931
02932
02933
02934
02935 void ADFI_disk_pointer_from_ASCII_Hex(
02936 const char block[8],
02937 const char offset[4],
02938 struct DISK_POINTER *block_offset,
02939 int *error_return )
02940 {
02941 unsigned int tmp ;
02942
02943 if( (block == NULL) || (offset == NULL) ) {
02944 *error_return = NULL_STRING_POINTER ;
02945 return ;
02946 }
02947
02948 if( block_offset == NULL ) {
02949 *error_return = NULL_POINTER ;
02950 return ;
02951 }
02952
02953 *error_return = NO_ERROR ;
02954
02956 ADFI_ASCII_Hex_2_unsigned_int( 0, MAXIMUM_32_BITS, 8, block,
02957 &tmp, error_return ) ;
02958 if( *error_return != NO_ERROR )
02959 return ;
02960 block_offset->block = tmp ;
02961
02962 ADFI_ASCII_Hex_2_unsigned_int( 0, DISK_BLOCK_SIZE, 4, offset,
02963 &tmp, error_return ) ;
02964 if( *error_return != NO_ERROR )
02965 return ;
02966
02967 block_offset->offset = tmp ;
02968 }
02969
02970
02971
02972
02973
02974
02975
02976
02977
02978
02979
02980
02981
02982
02983
02984
02985
02986
02987
02988
02989
02990
02991
02992
02993
02994
02995
02996
02997
02998
02999
03000
03001
03002
03003
03004
03005
03006
03007 void ADFI_evaluate_datatype(
03008 const int file_index,
03009 const char data_type[],
03010 int *file_bytes,
03011 int *machine_bytes,
03012 struct TOKENIZED_DATA_TYPE *tokenized_data_type,
03013 char *file_format,
03014 char *machine_format,
03015 int *error_return )
03016 {
03017 int str_position = 0 ;
03018 int current_token = 0 ;
03019 int i, str_len, size_file, size_machine ;
03020 char data_type_string[ADF_DATA_TYPE_LENGTH + 1 ] ;
03021 struct FILE_HEADER file_header ;
03022
03023 if( (file_format == NULL) || (machine_format == NULL) ) {
03024 *error_return = NULL_STRING_POINTER ;
03025 return ;
03026 }
03027
03028 if( (file_bytes == NULL) || (machine_bytes == NULL) ) {
03029 *error_return = NULL_POINTER ;
03030 return ;
03031 }
03032
03033 *file_bytes = 0 ;
03034 *machine_bytes = 0 ;
03035 *error_return = NO_ERROR ;
03036
03038 if( file_index >= maximum_files ) {
03039 *error_return = FILE_INDEX_OUT_OF_RANGE ;
03040 return ;
03041 }
03042 *file_format = ADF_file[file_index].format ;
03043 *machine_format = ADF_this_machine_format ;
03044
03046 ADFI_string_2_C_string( data_type, ADF_DATA_TYPE_LENGTH, data_type_string,
03047 error_return ) ;
03048 if( *error_return != NO_ERROR )
03049 return ;
03050
03052 str_len = strlen( data_type_string ) ;
03053 if ( str_len == 0 ) {
03054 *error_return = STRING_LENGTH_ZERO ;
03055 return ;
03056 }
03057 for( i=0; i<str_len; i++ )
03058 data_type_string[i] = TO_UPPER( data_type_string[i] ) ;
03059
03061 ADFI_read_file_header( file_index, &file_header, error_return ) ;
03062 if( *error_return != NO_ERROR )
03063 return ;
03064
03066 while( data_type_string[ str_position ] != '\0' ) {
03067 size_file = 0 ;
03068 size_machine = 0 ;
03069
03071 switch( EVAL_2_BYTES( data_type_string[str_position],
03072 data_type_string[str_position+1])) {
03073 case EVAL_2_BYTES( 'M', 'T' ) :
03074 tokenized_data_type[ current_token ].type[0] = 'M' ;
03075 tokenized_data_type[ current_token ].type[1] = 'T' ;
03076 if( (str_position == 0) && (data_type_string[ 2 ] == '\0') )
03077 return ;
03078 else {
03079 *error_return = INVALID_DATA_TYPE ;
03080 return ;
03081 }
03082
03083 case EVAL_2_BYTES( 'I', '4' ) :
03084 size_file = file_header.sizeof_int ;
03085 size_machine = sizeof( int ) ;
03086 tokenized_data_type[ current_token ].type[0] = 'I' ;
03087 tokenized_data_type[ current_token ].type[1] = '4' ;
03088 break ;
03089
03090 case EVAL_2_BYTES( 'I', '8' ) :
03091 size_file = file_header.sizeof_long ;
03092 size_machine = sizeof( long ) ;
03093 tokenized_data_type[ current_token ].type[0] = 'I' ;
03094 tokenized_data_type[ current_token ].type[1] = '8' ;
03095 break ;
03096
03097 case EVAL_2_BYTES( 'U', '4' ) :
03098 size_file = file_header.sizeof_int ;
03099 size_machine = sizeof( int ) ;
03100 tokenized_data_type[ current_token ].type[0] = 'U' ;
03101 tokenized_data_type[ current_token ].type[1] = '4' ;
03102 break ;
03103
03104 case EVAL_2_BYTES( 'U', '8' ) :
03105 size_file = file_header.sizeof_long ;
03106 size_machine = sizeof( long ) ;
03107 tokenized_data_type[ current_token ].type[0] = 'U' ;
03108 tokenized_data_type[ current_token ].type[1] = '8' ;
03109 break ;
03110
03111 case EVAL_2_BYTES( 'R', '4' ) :
03112 size_file = file_header.sizeof_float ;
03113 size_machine = sizeof( float ) ;
03114 tokenized_data_type[ current_token ].type[0] = 'R' ;
03115 tokenized_data_type[ current_token ].type[1] = '4' ;
03116 break ;
03117
03118 case EVAL_2_BYTES( 'R', '8' ) :
03119 size_file = file_header.sizeof_double ;
03120 size_machine = sizeof( double ) ;
03121 tokenized_data_type[ current_token ].type[0] = 'R' ;
03122 tokenized_data_type[ current_token ].type[1] = '8' ;
03123 break ;
03124
03125 case EVAL_2_BYTES( 'X', '4' ) :
03126 size_file = 2 * file_header.sizeof_float ;
03127 size_machine = 2 * sizeof( float ) ;
03128 tokenized_data_type[ current_token ].type[0] = 'X' ;
03129 tokenized_data_type[ current_token ].type[1] = '4' ;
03130 break ;
03131
03132 case EVAL_2_BYTES( 'X', '8' ) :
03133 size_file = 2 * file_header.sizeof_double ;
03134 size_machine = 2 * sizeof( double ) ;
03135 tokenized_data_type[ current_token ].type[0] = 'X' ;
03136 tokenized_data_type[ current_token ].type[1] = '8' ;
03137 break ;
03138
03139 case EVAL_2_BYTES( 'B', '1' ) :
03140 size_file = 1 ;
03141 size_machine = 1 ;
03142 tokenized_data_type[ current_token ].type[0] = 'B' ;
03143 tokenized_data_type[ current_token ].type[1] = '1' ;
03144 break ;
03145
03146 case EVAL_2_BYTES( 'C', '1' ) :
03147 case EVAL_2_BYTES( 'L', 'K' ) :
03148 size_file = file_header.sizeof_char ;
03149 size_machine = sizeof( char ) ;
03150 tokenized_data_type[ current_token ].type[0] = 'C' ;
03151 tokenized_data_type[ current_token ].type[1] = '1' ;
03152 break ;
03153
03154 default :
03155 *error_return = INVALID_DATA_TYPE ;
03156 return ;
03157 }
03158
03159 tokenized_data_type[ current_token ].file_type_size = size_file ;
03160 tokenized_data_type[ current_token ].machine_type_size = size_machine ;
03161 str_position += 2 ;
03162
03164 switch( data_type_string[ str_position ] ) {
03165 case '\0' :
03166 *file_bytes = *file_bytes + size_file ;
03167 *machine_bytes = *machine_bytes + size_machine ;
03168 tokenized_data_type[ current_token++ ].length = 1 ;
03169 break ;
03170
03171 case '[' :
03172 {
03173 int array_size = 0 ;
03174 str_position += 1 ;
03175 while( (data_type_string[ str_position ] >= '0') &&
03176 (data_type_string[ str_position ] <= '9') ) {
03177 array_size = array_size * 10 +
03178 (data_type_string[ str_position ] - '0') ;
03179 str_position += 1 ;
03180 }
03181 if( data_type_string[ str_position ] != ']' ) {
03182 *error_return = INVALID_DATA_TYPE ;
03183 return ;
03184 }
03185 str_position += 1 ;
03187 if( data_type_string[ str_position ] == ',' ) {
03188 str_position += 1 ;
03189 }
03190 *file_bytes = *file_bytes + size_file * array_size ;
03191 *machine_bytes = *machine_bytes + size_machine * array_size ;
03192 tokenized_data_type[ current_token++ ].length = array_size ;
03193 }
03194 break ;
03195
03196 case ',' :
03197 str_position += 1 ;
03198 *file_bytes = *file_bytes + size_file ;
03199 *machine_bytes = *machine_bytes + size_machine ;
03200 break ;
03201
03202 default :
03203 *error_return = INVALID_DATA_TYPE ;
03204 return ;
03205 }
03206 }
03207 tokenized_data_type[ current_token ].type[0] = 0x00 ;
03208 tokenized_data_type[ current_token ].type[1] = 0x00 ;
03209 tokenized_data_type[ current_token ].file_type_size = *file_bytes;
03210 tokenized_data_type[ current_token ].machine_type_size = *machine_bytes ;
03211 }
03212
03213
03214
03215
03216
03217
03218
03219
03220
03221
03222
03223
03224
03225
03226 void ADFI_fflush_file(
03227 const unsigned int file_index,
03228 int *error_return )
03229 {
03230
03231
03232 int iret ;
03233
03234 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
03235 *error_return = ADF_FILE_NOT_OPENED ;
03236 return ;
03237 }
03238
03239 *error_return = NO_ERROR ;
03240
03241 #ifdef USE_STREAM_IO
03242 iret = fflush( ADF_file[file_index].file ) ;
03243 if( iret != 0 ) {
03244 *error_return = FFLUSH_ERROR ;
03245 }
03246 #else
03247 ADF_sys_err = 0;
03248 # ifdef _WIN32
03249 iret = _commit( ADF_file[file_index].file ) ;
03250 # else
03251 iret = fsync( ADF_file[file_index].file ) ;
03252 # endif
03253 if (iret < 0) {
03254 ADF_sys_err = errno;
03255 *error_return = FFLUSH_ERROR ;
03256 }
03257 #endif
03258 }
03259
03260
03261
03262
03263
03264
03265
03266
03267
03268
03269
03270
03271
03272
03273
03274
03275
03276
03277
03278
03279
03280 static unsigned char bits[NUMBER_KNOWN_MACHINES][8][8] = {
03281
03282 { { 0x07, 0x5B, 0xCD, 0x15, 0x00, 0x00, 0x00, 0x00 },
03283 { 0xF8, 0xA4, 0x32, 0xEB, 0x00, 0x00, 0x00, 0x00 },
03284 { 0x49, 0x96, 0x02, 0xD2, 0x00, 0x00, 0x00, 0x00 },
03285 { 0xB6, 0x69, 0xFD, 0x2E, 0x00, 0x00, 0x00, 0x00 },
03286 { 0x46, 0x40, 0xE6, 0xB7, 0x00, 0x00, 0x00, 0x00 },
03287 { 0xC6, 0x40, 0xE6, 0xB7, 0x00, 0x00, 0x00, 0x00 },
03288 { 0x40, 0xC8, 0x1C, 0xD6, 0xE6, 0x31, 0xF8, 0xA1 },
03289 { 0xC0, 0xC8, 0x1C, 0xD6, 0xE6, 0x31, 0xF8, 0xA1 } },
03290
03291
03292 { { 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00 },
03293 { 0xEB, 0x32, 0xA4, 0xF8, 0x00, 0x00, 0x00, 0x00 },
03294 { 0xD2, 0x02, 0x96, 0x49, 0x00, 0x00, 0x00, 0x00 },
03295 { 0x2E, 0xFD, 0x69, 0xB6, 0x00, 0x00, 0x00, 0x00 },
03296 { 0xB7, 0xE6, 0x40, 0x46, 0x00, 0x00, 0x00, 0x00 },
03297 { 0xB7, 0xE6, 0x40, 0xC6, 0x00, 0x00, 0x00, 0x00 },
03298 { 0xA1, 0xF8, 0x31, 0xE6, 0xD6, 0x1C, 0xC8, 0x40 },
03299 { 0xA1, 0xF8, 0x31, 0xE6, 0xD6, 0x1C, 0xC8, 0xC0 } },
03300
03301
03302 { { 0x07, 0x5B, 0xCD, 0x15, 0x00, 0x00, 0x00, 0x00 },
03303 { 0xF8, 0xA4, 0x32, 0xEB, 0x00, 0x00, 0x00, 0x00 },
03304 { 0x00, 0x00, 0x00, 0x00, 0x49, 0x96, 0x02, 0xD2 },
03305 { 0xFF, 0xFF, 0xFF, 0xFF, 0xB6, 0x69, 0xFD, 0x2E },
03306 { 0x46, 0x40, 0xE6, 0xB7, 0x00, 0x00, 0x00, 0x00 },
03307 { 0xC6, 0x40, 0xE6, 0xB7, 0x00, 0x00, 0x00, 0x00 },
03308 { 0x40, 0xC8, 0x1C, 0xD6, 0xE6, 0x31, 0xF8, 0xA1 },
03309 { 0xC0, 0xC8, 0x1C, 0xD6, 0xE6, 0x31, 0xF8, 0xA1 } },
03310
03311
03312 { { 0x15, 0xCD, 0x5B, 0x07, 0x00, 0x00, 0x00, 0x00 },
03313 { 0xEB, 0x32, 0xA4, 0xF8, 0x00, 0x00, 0x00, 0x00 },
03314 { 0xD2, 0x02, 0x96, 0x49, 0x00, 0x00, 0x00, 0x00 },
03315 { 0x2E, 0xFD, 0x69, 0xB6, 0xFF, 0xFF, 0xFF, 0xFF },
03316 { 0xB7, 0xE6, 0x40, 0x46, 0x00, 0x00, 0x00, 0x00 },
03317 { 0xB7, 0xE6, 0x40, 0xC6, 0x00, 0x00, 0x00, 0x00 },
03318 { 0xA1, 0xF8, 0x31, 0xE6, 0xD6, 0x1C, 0xC8, 0x40 },
03319 { 0xA1, 0xF8, 0x31, 0xE6, 0xD6, 0x1C, 0xC8, 0xC0 } },
03320
03321
03322 { { 0x00, 0x00, 0x00, 0x00, 0x07, 0x5B, 0xCD, 0x15 },
03323 { 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0xA4, 0x32, 0xEB },
03324 { 0x00, 0x00, 0x00, 0x00, 0x49, 0x96, 0x02, 0xD2 },
03325 { 0xFF, 0xFF, 0xFF, 0xFF, 0xB6, 0x69, 0xFD, 0x2E },
03326 { 0x40, 0x0E, 0xC0, 0xE6, 0xB7, 0x31, 0x8F, 0xC5 },
03327 { 0xC0, 0x0E, 0xC0, 0xE6, 0xB7, 0x31, 0x8F, 0xC5 },
03328 { 0x40, 0x0E, 0xC0, 0xE6, 0xB7, 0x31, 0x8F, 0xC5 },
03329 { 0xC0, 0x0E, 0xC0, 0xE6, 0xB7, 0x31, 0x8F, 0xC5 } }
03330 } ;
03331
03332 void ADFI_figure_machine_format(
03333 const char *format,
03334 char *machine_format,
03335 char *format_to_use,
03336 char *os_to_use,
03337 int *error_return )
03338 {
03339 char requested_format, requested_os, machine_os_size = OS_32_BIT ;
03340 union { int i; long l; float f; double d; unsigned char bytes[8]; } u ;
03341 int i, k, OK ;
03342
03343 if( (machine_format == NULL) || (format_to_use == NULL) ||
03344 (os_to_use == NULL) ) {
03345 *error_return = NULL_STRING_POINTER ;
03346 return ;
03347 }
03348
03349 *error_return = NO_ERROR ;
03350
03352 if( format == NULL ) {
03353 requested_format = NATIVE_FORMAT_CHAR ;
03354 requested_os = OS_32_BIT ;
03355 }
03356 else if( (format[0] == '\0') || (format[0] == ' ') ) {
03357 requested_format = NATIVE_FORMAT_CHAR ;
03358 requested_os = OS_32_BIT ;
03359 }
03360 else if( ADFI_stridx_c( IEEE_BIG_32_FORMAT_STRING, format ) == 0 ) {
03361 requested_format = IEEE_BIG_FORMAT_CHAR ;
03362 requested_os = OS_32_BIT ;
03363 }
03364 else if( ADFI_stridx_c( IEEE_LITTLE_32_FORMAT_STRING, format ) == 0 ) {
03365 requested_format = IEEE_LITTLE_FORMAT_CHAR ;
03366 requested_os = OS_32_BIT ;
03367 }
03368 else if( ADFI_stridx_c( IEEE_BIG_64_FORMAT_STRING, format ) == 0 ) {
03369 requested_format = IEEE_BIG_FORMAT_CHAR ;
03370 requested_os = OS_64_BIT ;
03371 }
03372 else if( ADFI_stridx_c( IEEE_LITTLE_64_FORMAT_STRING, format ) == 0 ) {
03373 requested_format = IEEE_LITTLE_FORMAT_CHAR ;
03374 requested_os = OS_64_BIT ;
03375 }
03376 else if( ADFI_stridx_c( CRAY_FORMAT_STRING, format ) == 0 ) {
03377 requested_format = CRAY_FORMAT_CHAR ;
03378 requested_os = OS_64_BIT ;
03379 }
03380 else if( ADFI_stridx_c( NATIVE_FORMAT_STRING, format ) == 0 ) {
03381 requested_format = NATIVE_FORMAT_CHAR ;
03382 requested_os = OS_32_BIT ;
03383 }
03384 else {
03385 *error_return = ADF_FILE_FORMAT_NOT_RECOGNIZED ;
03386 return ;
03387 }
03388
03389
03391 #define ZERO_UNION() \
03392 for( k=0; k<8; k++ ) \
03393 u.bytes[k] = '\0' ;
03394 #define CHECK_UNION(B) \
03395 if( (u.bytes[0] != B[0]) || (u.bytes[1] != B[1]) || \
03396 (u.bytes[2] != B[2]) || (u.bytes[3] != B[3]) || \
03397 (u.bytes[4] != B[4]) || (u.bytes[5] != B[5]) || \
03398 (u.bytes[6] != B[6]) || (u.bytes[7] != B[7]) ) continue ;
03399
03400 OK = FALSE ;
03401 *machine_format = NATIVE_FORMAT_CHAR ;
03402 for( i=0; i<NUMBER_KNOWN_MACHINES; i++ ) {
03403 ZERO_UNION() ;
03404 u.i = 123456789 ;
03405 CHECK_UNION( bits[i][0] ) ;
03406
03407 ZERO_UNION() ;
03408 u.i = -123456789 ;
03409 CHECK_UNION( bits[i][1] ) ;
03410
03411 ZERO_UNION() ;
03412 u.l = 1234567890L ;
03413 CHECK_UNION( bits[i][2] ) ;
03414
03415 ZERO_UNION() ;
03416 u.l = -1234567890L ;
03417 CHECK_UNION( bits[i][3] ) ;
03418
03419 ZERO_UNION() ;
03420 u.f = (float) 12345.6789 ;
03421 CHECK_UNION( bits[i][4] ) ;
03422
03423 ZERO_UNION() ;
03424 u.f = (float) -12345.6789 ;
03425 CHECK_UNION( bits[i][5] ) ;
03426
03427 ZERO_UNION() ;
03428 u.d = 12345.6789 ;
03429 CHECK_UNION( bits[i][6] ) ;
03430
03431 ZERO_UNION() ;
03432 u.d = -12345.6789 ;
03433 CHECK_UNION( bits[i][7] ) ;
03434
03435 OK = TRUE ;
03436 switch( i + 1 ) {
03437 case IEEE_BIG_32_FORMAT:
03438 *machine_format = IEEE_BIG_FORMAT_CHAR ;
03439 machine_os_size = OS_32_BIT ;
03440 break ;
03441
03442 case IEEE_LITTLE_32_FORMAT:
03443 *machine_format = IEEE_LITTLE_FORMAT_CHAR ;
03444 machine_os_size = OS_32_BIT ;
03445 break ;
03446
03447 case IEEE_BIG_64_FORMAT:
03448 *machine_format = IEEE_BIG_FORMAT_CHAR ;
03449 machine_os_size = OS_64_BIT ;
03450 break ;
03451
03452 case IEEE_LITTLE_64_FORMAT:
03453 *machine_format = IEEE_LITTLE_FORMAT_CHAR ;
03454 machine_os_size = OS_64_BIT ;
03455 break ;
03456
03457 case CRAY_FORMAT:
03458 *machine_format = CRAY_FORMAT_CHAR ;
03459 machine_os_size = OS_64_BIT ;
03460 break ;
03461
03462 default:
03463 *machine_format = NATIVE_FORMAT_CHAR ;
03464 break ;
03465
03466 }
03467 break ;
03468 }
03469
03470 if( OK == TRUE ) {
03471
03472 if( sizeof( char ) != machine_sizes[i][ 0] ) OK = FALSE ;
03473 if( sizeof( unsigned char ) != machine_sizes[i][ 1] ) OK = FALSE ;
03474 if( sizeof( signed char ) != machine_sizes[i][ 2] ) OK = FALSE ;
03475 if( sizeof( short ) != machine_sizes[i][ 3] ) OK = FALSE ;
03476 if( sizeof( unsigned short ) != machine_sizes[i][ 4] ) OK = FALSE ;
03477 if( sizeof( int ) != machine_sizes[i][ 5] ) OK = FALSE ;
03478 if( sizeof( unsigned int ) != machine_sizes[i][ 6] ) OK = FALSE ;
03479 if( sizeof( long ) != machine_sizes[i][ 7] ) OK = FALSE ;
03480 if( sizeof( unsigned long ) != machine_sizes[i][ 8] ) OK = FALSE ;
03481 if( sizeof( float ) != machine_sizes[i][ 9] ) OK = FALSE ;
03482 if( sizeof( double ) != machine_sizes[i][10] ) OK = FALSE ;
03483
03484
03485
03486
03487
03488 #if 0
03489 if( sizeof( char * ) != machine_sizes[i][11] ) OK = FALSE ;
03490 if( sizeof( int * ) != machine_sizes[i][12] ) OK = FALSE ;
03491 if( sizeof( long * ) != machine_sizes[i][13] ) OK = FALSE ;
03492 if( sizeof( float * ) != machine_sizes[i][14] ) OK = FALSE ;
03493 if( sizeof( double * ) != machine_sizes[i][15] ) OK = FALSE ;
03494 #endif
03495 }
03496
03497 if( OK == FALSE ) {
03498 *machine_format = NATIVE_FORMAT_CHAR ;
03499 if ( sizeof( double * ) >= 8 ) machine_os_size = OS_64_BIT ;
03500 else machine_os_size = OS_32_BIT ;
03501 }
03502
03503 if( ADF_this_machine_format == UNDEFINED_FORMAT_CHAR ) {
03504 ADF_this_machine_format = *machine_format ;
03505 ADF_this_machine_os_size = machine_os_size ;
03506 }
03507
03508 if( requested_format == NATIVE_FORMAT_CHAR ) {
03509 *format_to_use = *machine_format ;
03510 *os_to_use = machine_os_size ;
03511 }
03512 else {
03513 *format_to_use = requested_format ;
03514 *os_to_use = requested_os ;
03515 }
03516
03517 if( *machine_format == NATIVE_FORMAT_CHAR )
03518 *error_return = MACHINE_FORMAT_NOT_RECOGNIZED ;
03519
03520 }
03521
03522
03523
03524
03525
03526
03527
03528
03529
03530
03531
03532
03533
03534
03535 void ADFI_file_and_machine_compare(
03536 const int file_index,
03537 const struct TOKENIZED_DATA_TYPE *tokenized_data_type,
03538 int *compare,
03539 int *error_return )
03540 {
03541 int machine_size, file_size, token ;
03542 *compare = 0 ;
03543 *error_return = NO_ERROR ;
03544
03545 if( file_index < 0 || file_index >= maximum_files ) {
03546 *error_return = FILE_INDEX_OUT_OF_RANGE ;
03547 return ;
03548 }
03549
03550 if( ADF_this_machine_format == NATIVE_FORMAT_CHAR ||
03551 ADF_file[file_index].format == NATIVE_FORMAT_CHAR ) {
03552 struct FILE_HEADER file_header ;
03554 ADFI_read_file_header( file_index, &file_header, error_return ) ;
03555 if( *error_return != NO_ERROR )
03556 return ;
03558 if ( ADF_file[file_index].format != NATIVE_FORMAT_CHAR ||
03559 file_header.sizeof_char != sizeof( char ) ||
03560 file_header.sizeof_short != sizeof( short ) ||
03561 file_header.sizeof_int != sizeof( int ) ||
03562 file_header.sizeof_long != sizeof( long ) ||
03563 file_header.sizeof_float != sizeof( float ) ||
03564 #if 0
03565 file_header.sizeof_double != sizeof( double ) ||
03566 file_header.sizeof_char_p != sizeof( char * ) ||
03567 file_header.sizeof_short_p != sizeof( short * ) ||
03568 file_header.sizeof_int_p != sizeof( int * ) ||
03569 file_header.sizeof_long_p != sizeof( long * ) ||
03570 file_header.sizeof_float_p != sizeof( float * ) ||
03571 file_header.sizeof_double_p != sizeof( double * ) ) {
03572 #else
03573 file_header.sizeof_double != sizeof( double ) ) {
03574 #endif
03575 *error_return = MACHINE_FILE_INCOMPATABLE ;
03576 return ;
03577 }
03578 }
03580 if( ADF_file[file_index].format == ADF_this_machine_format &&
03581 ADF_file[file_index].os_size == ADF_this_machine_os_size ) {
03582 *compare = 1 ;
03583 } else if( ADF_file[file_index].format == ADF_this_machine_format ) {
03588 if ( tokenized_data_type == NULL ) return ;
03589 token = -1 ;
03590 *compare = 1 ;
03591 do {
03592 token++ ;
03593 machine_size = tokenized_data_type[ token ].machine_type_size ;
03594 file_size = tokenized_data_type[ token ].file_type_size ;
03595 if ( machine_size != file_size ) {
03596 *compare = 0 ;
03597 break ;
03598 }
03599 } while( tokenized_data_type[ token ].type[0] != 0 ) ;
03600 }
03601 }
03602
03603
03604
03605
03606
03607
03608
03609
03610
03611
03612
03613
03614
03615
03616
03617
03618
03619
03620 void ADFI_file_block_offset_2_ID(
03621 const int file_index,
03622 const unsigned long file_block,
03623 const unsigned long block_offset,
03624 double *ID,
03625 int *error_return )
03626 {
03627 double dd;
03628 unsigned char * cc;
03629
03630 if( ID == NULL ) {
03631 *error_return = NULL_POINTER ;
03632 return ;
03633 }
03634
03635 *error_return = NO_ERROR ;
03636 if( file_index >= maximum_files ) {
03637 *error_return = FILE_INDEX_OUT_OF_RANGE ;
03638 return ;
03639 }
03640
03641 if( block_offset >= DISK_BLOCK_SIZE ) {
03642 *error_return = BLOCK_OFFSET_OUT_OF_RANGE ;
03643 return ;
03644 }
03645
03647
03648
03649
03650
03651
03652
03653
03654
03655
03656
03657 cc = (unsigned char *) ⅆ
03658 if ( ADF_this_machine_format == IEEE_BIG_FORMAT_CHAR ) {
03659 cc[1] = (unsigned char) (file_index & 0x00ff) ;
03660 cc[0] = (unsigned char) (64 + (( file_index >> 8) & 0x003f)) ;
03661
03662 cc[2] = (unsigned char) (file_block & 0x000000ff) ;
03663 cc[3] = (unsigned char) ((file_block >> 8) & 0x000000ff) ;
03664 cc[4] = (unsigned char) ((file_block >> 16) & 0x000000ff) ;
03665 cc[5] = (unsigned char) ((file_block >> 24) & 0x000000ff) ;
03666
03667 cc[6] = (unsigned char) (block_offset & 0x00ff) ;
03668 cc[7] = (unsigned char) ((block_offset >> 8) & 0x00ff) ;
03669 }
03670 else if ( ADF_this_machine_format == IEEE_LITTLE_FORMAT_CHAR ) {
03671 cc[6] = (unsigned char) (file_index & 0x00ff) ;
03672 cc[7] = (unsigned char) (64 + (( file_index >> 8) & 0x003f)) ;
03673
03674 cc[2] = (unsigned char) (file_block & 0x000000ff) ;
03675 cc[3] = (unsigned char) ((file_block >> 8) & 0x000000ff) ;
03676 cc[4] = (unsigned char) ((file_block >> 16) & 0x000000ff) ;
03677 cc[5] = (unsigned char) ((file_block >> 24) & 0x000000ff) ;
03678
03679 cc[0] = (unsigned char) (block_offset & 0x00ff) ;
03680 cc[1] = (unsigned char) ((block_offset >> 8) & 0x00ff) ;
03681 }
03682 else {
03683 cc[0] = (unsigned char) (file_index & 0x00ff) ;
03684 cc[1] = (unsigned char) ((file_index >> 8) & 0x00ff) ;
03685
03686 cc[2] = (unsigned char) (file_block & 0x000000ff) ;
03687 cc[3] = (unsigned char) ((file_block >> 8) & 0x000000ff) ;
03688 cc[4] = (unsigned char) ((file_block >> 16) & 0x000000ff) ;
03689 cc[5] = (unsigned char) ((file_block >> 24) & 0x000000ff) ;
03690
03691 cc[6] = (unsigned char) (block_offset & 0x00ff) ;
03692 cc[7] = (unsigned char) ((block_offset >> 8) & 0x00ff) ;
03693 }
03694
03695 *ID = dd;
03696 #ifdef PRINT_STUFF
03697 printf("cc[0-7] = %02X %02X %02X %02X %02X %02X %02X %02X \n",
03698 cc[0], cc[1], cc[2], cc[3],
03699 cc[4], cc[5], cc[6], cc[7] ) ;
03700 printf("In ADFI_file_block_offset_2_ID: ID=%lf\n",*ID);
03701 #endif
03702
03703 }
03704
03705
03706
03707
03708
03709
03710
03711
03712
03713
03714
03715
03716
03717
03718
03719
03720
03721
03722
03723
03724 void ADFI_file_free(
03725 const int file_index,
03726 const struct DISK_POINTER *block_offset,
03727 const long in_number_of_bytes,
03728 int *error_return )
03729 {
03730 char tag[TAG_SIZE + 1] ;
03731 struct DISK_POINTER end_of_chunk_tag ;
03732 struct DISK_POINTER tmp_blk_ofst ;
03733 struct FREE_CHUNK_TABLE free_chunk_table ;
03734 struct FREE_CHUNK free_chunk ;
03735 int i ;
03736 long number_of_bytes = in_number_of_bytes ;
03737
03738 if( block_offset == NULL ) {
03739 *error_return = NULL_POINTER ;
03740 return ;
03741 }
03742
03743 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
03744 *error_return = ADF_FILE_NOT_OPENED ;
03745 return ;
03746 }
03747
03748 if( number_of_bytes == 0 ) {
03749
03752 ADFI_read_file( file_index, block_offset->block, block_offset->offset,
03753 TAG_SIZE, tag, error_return ) ;
03754 if( *error_return != NO_ERROR )
03755 return ;
03756
03757 tag[TAG_SIZE] = '\0' ;
03758
03759 end_of_chunk_tag.block = 0 ;
03760 end_of_chunk_tag.offset = 0 ;
03761 if( ADFI_stridx_c( tag, node_start_tag ) == 0 ) {
03762 if( (block_offset->block == ROOT_NODE_BLOCK) &&
03763 (block_offset->offset == ROOT_NODE_OFFSET) ) {
03764 *error_return = FREE_OF_ROOT_NODE ;
03765 return ;
03766 }
03767 end_of_chunk_tag.block = block_offset->block ;
03768 end_of_chunk_tag.offset = block_offset->offset + NODE_HEADER_SIZE -
03769 TAG_SIZE ;
03770 if ( end_of_chunk_tag.offset > DISK_BLOCK_SIZE ) {
03771 ADFI_adjust_disk_pointer( &end_of_chunk_tag, error_return ) ;
03772 if( *error_return != NO_ERROR )
03773 return ;
03774 }
03775
03777 ADFI_read_file( file_index, end_of_chunk_tag.block,
03778 end_of_chunk_tag.offset, TAG_SIZE, tag, error_return ) ;
03779 if( *error_return != NO_ERROR )
03780 return ;
03781 if( ADFI_stridx_c( tag, node_end_tag ) != 0 ) {
03782 *error_return = ADF_DISK_TAG_ERROR ;
03783 return ;
03784 }
03785 }
03786 else if( ADFI_stridx_c( tag, free_chunk_table_start_tag ) == 0 ) {
03788 *error_return = FREE_OF_FREE_CHUNK_TABLE ;
03789 return ;
03790 }
03791 else if( ADFI_stridx_c( tag, free_chunk_start_tag ) == 0 ) {
03792
03794 tmp_blk_ofst.block = block_offset->block ;
03795 tmp_blk_ofst.offset = block_offset->offset + TAG_SIZE ;
03796 if ( tmp_blk_ofst.offset > DISK_BLOCK_SIZE ) {
03797 ADFI_adjust_disk_pointer( &tmp_blk_ofst, error_return ) ;
03798 if( *error_return != NO_ERROR )
03799 return ;
03800 }
03802 ADFI_read_disk_pointer_from_disk( file_index, tmp_blk_ofst.block,
03803 tmp_blk_ofst.offset, &end_of_chunk_tag, error_return ) ;
03804 if( *error_return != NO_ERROR )
03805 return ;
03806
03808 ADFI_read_file( file_index, end_of_chunk_tag.block,
03809 end_of_chunk_tag.offset, TAG_SIZE, tag, error_return ) ;
03810 if( *error_return != NO_ERROR )
03811 return ;
03812 if( ADFI_stridx_c( tag, free_chunk_end_tag ) != 0 ) {
03813 *error_return = ADF_DISK_TAG_ERROR ;
03814 return ;
03815 }
03816 }
03817 else if( ADFI_stridx_c( tag, sub_node_start_tag ) == 0 ) {
03818
03820 tmp_blk_ofst.block = block_offset->block ;
03821 tmp_blk_ofst.offset = block_offset->offset + TAG_SIZE ;
03822 if ( tmp_blk_ofst.offset > DISK_BLOCK_SIZE ) {
03823 ADFI_adjust_disk_pointer( &tmp_blk_ofst, error_return ) ;
03824 if( *error_return != NO_ERROR )
03825 return ;
03826 }
03827
03829 ADFI_read_disk_pointer_from_disk( file_index, tmp_blk_ofst.block,
03830 tmp_blk_ofst.offset, &end_of_chunk_tag, error_return ) ;
03831 if( *error_return != NO_ERROR )
03832 return ;
03833
03835 ADFI_read_file( file_index, end_of_chunk_tag.block,
03836 end_of_chunk_tag.offset, TAG_SIZE, tag, error_return ) ;
03837 if( *error_return != NO_ERROR )
03838 return ;
03839 if( ADFI_stridx_c( tag, sub_node_end_tag ) != 0 ) {
03840 *error_return = ADF_DISK_TAG_ERROR ;
03841 return ;
03842 }
03843 }
03844 else if( ADFI_stridx_c( tag, data_chunk_table_start_tag ) == 0 ) {
03845
03847 tmp_blk_ofst.block = block_offset->block ;
03848 tmp_blk_ofst.offset = block_offset->offset + TAG_SIZE ;
03849 if ( tmp_blk_ofst.offset > DISK_BLOCK_SIZE ) {
03850 ADFI_adjust_disk_pointer( &tmp_blk_ofst, error_return ) ;
03851 if( *error_return != NO_ERROR )
03852 return ;
03853 }
03854
03856 ADFI_read_disk_pointer_from_disk( file_index, tmp_blk_ofst.block,
03857 tmp_blk_ofst.offset, &end_of_chunk_tag, error_return ) ;
03858 if( *error_return != NO_ERROR )
03859 return ;
03860
03862 ADFI_read_file( file_index, end_of_chunk_tag.block,
03863 end_of_chunk_tag.offset, TAG_SIZE, tag, error_return ) ;
03864 if( *error_return != NO_ERROR )
03865 return ;
03866 if( ADFI_stridx_c( tag, data_chunk_table_end_tag ) != 0 ) {
03867 *error_return = ADF_DISK_TAG_ERROR ;
03868 return ;
03869 }
03870 }
03871 else if( ADFI_stridx_c( tag, data_chunk_start_tag ) == 0 ) {
03872
03874 tmp_blk_ofst.block = block_offset->block ;
03875 tmp_blk_ofst.offset = block_offset->offset + TAG_SIZE ;
03876 if ( tmp_blk_ofst.offset > DISK_BLOCK_SIZE ) {
03877 ADFI_adjust_disk_pointer( &tmp_blk_ofst, error_return ) ;
03878 if( *error_return != NO_ERROR )
03879 return ;
03880 }
03881
03883 ADFI_read_disk_pointer_from_disk( file_index, tmp_blk_ofst.block,
03884 tmp_blk_ofst.offset, &end_of_chunk_tag, error_return ) ;
03885 if( *error_return != NO_ERROR )
03886 return ;
03887
03889 ADFI_read_file( file_index, end_of_chunk_tag.block,
03890 end_of_chunk_tag.offset, TAG_SIZE, tag, error_return ) ;
03891 if( *error_return != NO_ERROR )
03892 return ;
03893 if( ADFI_stridx_c( tag, data_chunk_end_tag ) != 0 ) {
03894 *error_return = ADF_DISK_TAG_ERROR ;
03895 return ;
03896 }
03897 }
03898 else {
03899 *error_return = ADF_DISK_TAG_ERROR ;
03900 return ;
03901 }
03902 number_of_bytes = (end_of_chunk_tag.block - block_offset->block) *
03903 DISK_BLOCK_SIZE + (end_of_chunk_tag.offset - block_offset->offset +
03904 TAG_SIZE) ;
03905 }
03906 else {
03907 end_of_chunk_tag.block = block_offset->block ;
03908 end_of_chunk_tag.offset = block_offset->offset + number_of_bytes - TAG_SIZE ;
03909 ADFI_adjust_disk_pointer( &end_of_chunk_tag, error_return ) ;
03910 if( *error_return != NO_ERROR )
03911 return ;
03912 }
03913
03914 if( number_of_bytes <= SMALLEST_CHUNK_SIZE ) {
03916 if( block_of_ZZ_initialized == FALSE ) {
03917 for( i=0; i<SMALLEST_CHUNK_SIZE; i++ )
03918 block_of_ZZ[ i ] = 'z' ;
03919 block_of_ZZ_initialized = TRUE ;
03920 }
03921
03922 ADFI_write_file( file_index, block_offset->block, block_offset->offset,
03923 number_of_bytes, block_of_ZZ, error_return ) ;
03924 if( *error_return != NO_ERROR )
03925 return ;
03926 }
03927 else {
03929 ADFI_read_free_chunk_table( file_index, &free_chunk_table, error_return ) ;
03930 if( *error_return != NO_ERROR )
03931 return ;
03932
03933 if( block_offset->block == end_of_chunk_tag.block ) {
03934 if( (end_of_chunk_tag.offset + TAG_SIZE - block_offset->offset) <=
03935 SMALL_CHUNK_MAXIMUM ) {
03936 free_chunk.end_of_chunk_tag.block = end_of_chunk_tag.block ;
03937 free_chunk.end_of_chunk_tag.offset = end_of_chunk_tag.offset ;
03938 free_chunk.next_chunk.block = free_chunk_table.small_first_block.block;
03939 free_chunk.next_chunk.offset =
03940 free_chunk_table.small_first_block.offset ;
03941
03942 free_chunk_table.small_first_block.block = block_offset->block ;
03943 free_chunk_table.small_first_block.offset = block_offset->offset ;
03944
03946 if( free_chunk.next_chunk.offset == BLANK_BLOCK_OFFSET ) {
03947 free_chunk_table.small_last_block.block = block_offset->block ;
03948 free_chunk_table.small_last_block.offset = block_offset->offset ;
03949 }
03950 }
03951 else {
03952 free_chunk.end_of_chunk_tag.block = end_of_chunk_tag.block ;
03953 free_chunk.end_of_chunk_tag.offset = end_of_chunk_tag.offset ;
03954 free_chunk.next_chunk.block =
03955 free_chunk_table.medium_first_block.block ;
03956 free_chunk.next_chunk.offset =
03957 free_chunk_table.medium_first_block.offset;
03958
03959 free_chunk_table.medium_first_block.block = block_offset->block ;
03960 free_chunk_table.medium_first_block.offset = block_offset->offset ;
03961
03963 if( free_chunk.next_chunk.offset == BLANK_BLOCK_OFFSET ) {
03964 free_chunk_table.medium_last_block.block = block_offset->block ;
03965 free_chunk_table.medium_last_block.offset = block_offset->offset ;
03966 }
03967 }
03968 }
03969 else {
03970 free_chunk.end_of_chunk_tag.block = end_of_chunk_tag.block ;
03971 free_chunk.end_of_chunk_tag.offset = end_of_chunk_tag.offset ;
03972 free_chunk.next_chunk.block = free_chunk_table.large_first_block.block;
03973 free_chunk.next_chunk.offset =
03974 free_chunk_table.large_first_block.offset ;
03975
03976 free_chunk_table.large_first_block.block = block_offset->block ;
03977 free_chunk_table.large_first_block.offset = block_offset->offset ;
03978
03980 if( free_chunk.next_chunk.offset == BLANK_BLOCK_OFFSET ) {
03981 free_chunk_table.large_last_block.block = block_offset->block ;
03982 free_chunk_table.large_last_block.offset = block_offset->offset ;
03983 }
03984 }
03985
03987 strncpy( free_chunk.start_tag, free_chunk_start_tag, TAG_SIZE ) ;
03988 strncpy( free_chunk.end_tag, free_chunk_end_tag, TAG_SIZE ) ;
03989
03991 ADFI_write_free_chunk( file_index, block_offset, &free_chunk, error_return );
03992 if( *error_return != NO_ERROR )
03993 return ;
03995 ADFI_write_free_chunk_table( file_index, &free_chunk_table, error_return ) ;
03996 if( *error_return != NO_ERROR )
03997 return ;
03998 }
03999
04001 ADFI_stack_control(file_index, block_offset->block, block_offset->offset,
04002 DEL_STK_ENTRY, 0, 0, NULL ) ;
04003
04004 }
04005
04006
04007
04008
04009
04010
04011
04012
04013
04014
04015
04016
04017
04018
04019
04020
04021 void ADFI_file_malloc(
04022 const int file_index,
04023 const long size_bytes,
04024 struct DISK_POINTER *block_offset,
04025 int *error_return )
04026 {
04027 struct FILE_HEADER file_header ;
04028 int memory_found = FALSE ;
04029 #if 0
04030 struct FREE_CHUNK_TABLE free_chunk_table ;
04031 struct DISK_POINTER disk_pointer, previous_disk_pointer ;
04032 struct DISK_POINTER *first_free_block, *last_free_block ;
04033 struct FREE_CHUNK free_chunk, previous_free_chunk ;
04034 int i ;
04035 unsigned long size ;
04036 #endif
04037
04038 if( block_offset == NULL ) {
04039 *error_return = NULL_POINTER ;
04040 return ;
04041 }
04042
04043 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
04044 *error_return = ADF_FILE_NOT_OPENED ;
04045 return ;
04046 }
04047 #if 0
04048
04049
04050
04051
04053 ADFI_read_free_chunk_table( file_index, &free_chunk_table, error_return ) ;
04054 if( *error_return != NO_ERROR )
04055 return ;
04056
04062 for( i=0; i<3; i++ ) {
04063 if( memory_found == TRUE || size_bytes <= SMALLEST_CHUNK_SIZE )
04064 break ;
04065 ADFI_set_blank_disk_pointer( &previous_disk_pointer ) ;
04066 switch( i ) {
04067 case 0:
04068 if( size_bytes > SMALL_CHUNK_MAXIMUM )
04069 continue ;
04070 first_free_block = &free_chunk_table.small_first_block ;
04071 last_free_block = &free_chunk_table.small_last_block ;
04072 break ;
04073 case 1:
04074 if( size_bytes > MEDIUM_CHUNK_MAXIMUM )
04075 continue ;
04076 first_free_block = &free_chunk_table.medium_first_block ;
04077 last_free_block = &free_chunk_table.medium_last_block ;
04078 break ;
04079 case 2:
04080 first_free_block = &free_chunk_table.large_first_block ;
04081 last_free_block = &free_chunk_table.large_last_block ;
04082 break ;
04083 }
04084
04085 disk_pointer = *first_free_block ;
04086 while( (memory_found != TRUE) &&
04087 ((disk_pointer.block != BLANK_FILE_BLOCK) ||
04088 (disk_pointer.offset != BLANK_BLOCK_OFFSET)) ) {
04089 ADFI_read_free_chunk( file_index, &disk_pointer, &free_chunk,
04090 error_return ) ;
04091 if( *error_return != NO_ERROR )
04092 return ;
04093 size = (free_chunk.end_of_chunk_tag.block - disk_pointer.block) *
04094 DISK_BLOCK_SIZE +
04095 (free_chunk.end_of_chunk_tag.offset - disk_pointer.offset) +
04096 TAG_SIZE ;
04097 if( (long int) size >= size_bytes ) {
04098 *block_offset = disk_pointer ;
04099 if( (previous_disk_pointer.block != BLANK_FILE_BLOCK) ||
04100 (previous_disk_pointer.offset != BLANK_BLOCK_OFFSET) ) {
04101
04105 ADFI_read_free_chunk( file_index, &previous_disk_pointer,
04106 &previous_free_chunk, error_return ) ;
04107 if( *error_return != NO_ERROR )
04108 return ;
04109 previous_free_chunk.next_chunk = free_chunk.next_chunk ;
04110 ADFI_write_free_chunk( file_index, &previous_disk_pointer,
04111 &previous_free_chunk, error_return ) ;
04112 if( *error_return != NO_ERROR )
04113 return ;
04114 }
04115 else {
04116
04118 *first_free_block = free_chunk.next_chunk ;
04119 ADFI_write_free_chunk_table( file_index, &free_chunk_table,
04120 error_return ) ;
04121 if( *error_return != NO_ERROR )
04122 return ;
04123 }
04124
04125 if((last_free_block->block == disk_pointer.block) &&
04126 (last_free_block->offset == disk_pointer.offset)){
04127 if( (previous_disk_pointer.block != BLANK_FILE_BLOCK) ||
04128 (previous_disk_pointer.offset != BLANK_BLOCK_OFFSET) ) {
04129 *last_free_block = previous_disk_pointer ;
04130 }
04131 else {
04132 ADFI_set_blank_disk_pointer( last_free_block ) ;
04133 }
04134 ADFI_write_free_chunk_table( file_index, &free_chunk_table,
04135 error_return ) ;
04136 if( *error_return != NO_ERROR )
04137 return ;
04138 }
04139
04140 size -= size_bytes ;
04141 if ( size > 0 ) {
04142 disk_pointer.offset += size_bytes ;
04143 ADFI_adjust_disk_pointer( &disk_pointer, error_return ) ;
04144 if( *error_return != NO_ERROR )
04145 return ;
04146 ADFI_file_free( file_index, &disk_pointer, size, error_return ) ;
04147 if( *error_return != NO_ERROR )
04148 return ;
04149 }
04150 memory_found = TRUE ;
04151 }
04152 else {
04153 previous_disk_pointer = disk_pointer ;
04154 disk_pointer = free_chunk.next_chunk ;
04155 }
04156 }
04157 }
04158 #endif
04159
04162 if( memory_found != TRUE ) {
04163 ADFI_read_file_header( file_index, &file_header, error_return ) ;
04164 if( *error_return != NO_ERROR )
04165 return ;
04172 if( file_header.end_of_file.offset != DISK_BLOCK_SIZE - 1 ) {
04173 if( (file_header.end_of_file.offset+size_bytes) >= DISK_BLOCK_SIZE &&
04174 size_bytes <= DISK_BLOCK_SIZE ) {
04176 file_header.end_of_file.offset++ ;
04177 ADFI_file_free( file_index, &file_header.end_of_file,
04178 DISK_BLOCK_SIZE - file_header.end_of_file.offset, error_return ) ;
04179 if( *error_return != NO_ERROR )
04180 return ;
04181 block_offset->block = file_header.end_of_file.block + 1 ;
04182 block_offset->offset = 0 ;
04183 file_header.end_of_file.block++ ;
04184 file_header.end_of_file.offset = size_bytes - 1 ;
04185 ADFI_adjust_disk_pointer( &file_header.end_of_file, error_return ) ;
04186 if( *error_return != NO_ERROR )
04187 return ;
04188
04189 }
04190 else {
04191 block_offset->block = file_header.end_of_file.block ;
04192 block_offset->offset = file_header.end_of_file.offset + 1 ;
04193 file_header.end_of_file.offset += size_bytes ;
04194 ADFI_adjust_disk_pointer( &file_header.end_of_file, error_return ) ;
04195 if( *error_return != NO_ERROR )
04196 return ;
04197 }
04198 }
04199 else {
04200 block_offset->block = file_header.end_of_file.block + 1 ;
04201 block_offset->offset = 0 ;
04202 file_header.end_of_file.block++ ;
04203 file_header.end_of_file.offset = size_bytes - 1 ;
04204 ADFI_adjust_disk_pointer( &file_header.end_of_file, error_return ) ;
04205 if( *error_return != NO_ERROR )
04206 return ;
04207 }
04208
04209
04211 ADFI_write_file_header( file_index, &file_header, error_return ) ;
04212 if( *error_return != NO_ERROR )
04213 return ;
04214 }
04215
04216 }
04217
04218
04219
04220
04221
04222
04223
04224
04225
04226
04227
04228
04229
04230
04231
04232
04233
04234
04235 void ADFI_fill_initial_file_header(
04236 const char format,
04237 const char os_size,
04238 const char *what_string,
04239 struct FILE_HEADER *file_header,
04240 int *error_return )
04241 {
04242 int i ;
04243
04244 if( what_string == NULL ) {
04245 *error_return = NULL_STRING_POINTER ;
04246 return ;
04247 }
04248
04249 if( file_header == NULL ) {
04250 *error_return = NULL_POINTER ;
04251 return ;
04252 }
04253
04254 if( (format != IEEE_BIG_FORMAT_CHAR) && (format != IEEE_LITTLE_FORMAT_CHAR) &&
04255 (format != CRAY_FORMAT_CHAR) && (format != NATIVE_FORMAT_CHAR) ) {
04256 *error_return = ADF_FILE_FORMAT_NOT_RECOGNIZED ;
04257 return ;
04258 }
04259
04261 strncpy( file_header->tag0, file_header_tags[0], TAG_SIZE ) ;
04262 strncpy( file_header->tag1, file_header_tags[1], TAG_SIZE ) ;
04263 strncpy( file_header->tag2, file_header_tags[2], TAG_SIZE ) ;
04264 strncpy( file_header->tag3, file_header_tags[3], TAG_SIZE ) ;
04265 strncpy( file_header->tag4, file_header_tags[4], TAG_SIZE ) ;
04266 strncpy( file_header->tag5, file_header_tags[5], TAG_SIZE ) ;
04267
04269 strncpy( file_header->what, what_string, WHAT_STRING_SIZE ) ;
04270 if ( strlen(what_string) < WHAT_STRING_SIZE )
04271 {
04272 ADFI_blank_fill_string ( file_header->what, WHAT_STRING_SIZE ) ;
04273 }
04274
04276 ADFI_get_current_date( file_header->creation_date ) ;
04277
04279 strncpy( file_header->modification_date, file_header->creation_date,
04280 DATE_TIME_SIZE ) ;
04281
04282 file_header->numeric_format = format ;
04283 file_header->os_size = os_size ;
04284
04286 if( (format==ADF_this_machine_format && os_size==ADF_this_machine_os_size) ||
04287 format==NATIVE_FORMAT_CHAR )
04288 {
04289 file_header->sizeof_char = sizeof( char ) ;
04290 file_header->sizeof_short = sizeof( short ) ;
04291 file_header->sizeof_int = sizeof( int ) ;
04292 file_header->sizeof_long = sizeof( long ) ;
04293 file_header->sizeof_float = sizeof( float ) ;
04294 file_header->sizeof_double = sizeof( double ) ;
04295 file_header->sizeof_char_p = sizeof( char * ) ;
04296 file_header->sizeof_short_p = sizeof( short * ) ;
04297 file_header->sizeof_int_p = sizeof( int * ) ;
04298 file_header->sizeof_long_p = sizeof( long * ) ;
04299 file_header->sizeof_float_p = sizeof( float * ) ;
04300 file_header->sizeof_double_p = sizeof( double * ) ;
04301 }
04302 else
04303 {
04304 switch( EVAL_2_BYTES( format, os_size ) ) {
04305 case EVAL_2_BYTES( 'B', 'L' ):
04306 i = IEEE_BIG_32_FORMAT - 1 ;
04307 break ;
04308 case EVAL_2_BYTES( 'L', 'L' ):
04309 i = IEEE_LITTLE_32_FORMAT - 1 ;
04310 break ;
04311 case EVAL_2_BYTES( 'B', 'B' ):
04312 i = IEEE_BIG_64_FORMAT - 1 ;
04313 break ;
04314 case EVAL_2_BYTES( 'L', 'B' ):
04315 i = IEEE_LITTLE_64_FORMAT - 1 ;
04316 break ;
04317 case EVAL_2_BYTES( 'C', 'B' ):
04318 i = CRAY_FORMAT - 1 ;
04319 break ;
04320 default:
04321 *error_return = MACHINE_FORMAT_NOT_RECOGNIZED ;
04322 return ;
04323 }
04324
04325 file_header->sizeof_char = machine_sizes[i][ 0] ;
04326 file_header->sizeof_short = machine_sizes[i][ 3] ;
04327 file_header->sizeof_int = machine_sizes[i][ 5] ;
04328 file_header->sizeof_long = machine_sizes[i][ 7] ;
04329 file_header->sizeof_float = machine_sizes[i][ 9] ;
04330 file_header->sizeof_double = machine_sizes[i][10] ;
04331 file_header->sizeof_char_p = machine_sizes[i][11] ;
04332 file_header->sizeof_short_p = machine_sizes[i][12] ;
04333 file_header->sizeof_int_p = machine_sizes[i][12] ;
04334 file_header->sizeof_long_p = machine_sizes[i][13] ;
04335 file_header->sizeof_float_p = machine_sizes[i][14] ;
04336 file_header->sizeof_double_p = machine_sizes[i][15] ;
04337 }
04340 file_header->root_node.block = ROOT_NODE_BLOCK ;
04341 file_header->root_node.offset = ROOT_NODE_OFFSET ;
04342 file_header->end_of_file.block = ROOT_NODE_BLOCK ;
04343 file_header->end_of_file.offset = ROOT_NODE_OFFSET + NODE_HEADER_SIZE - 1 ;
04344 file_header->free_chunks.block = FREE_CHUNKS_BLOCK ;
04345 file_header->free_chunks.offset = FREE_CHUNKS_OFFSET ;
04346 ADFI_set_blank_disk_pointer( &file_header->extra ) ;
04347
04348 }
04349
04350
04351
04352
04353
04354
04355
04356
04357
04358
04359
04360
04361
04362 void ADFI_fill_initial_free_chunk_table(
04363 struct FREE_CHUNK_TABLE *free_chunk_table,
04364 int *error_return )
04365 {
04366
04367 if( free_chunk_table == NULL ) {
04368 *error_return = NULL_POINTER ;
04369 return ;
04370 }
04371
04372 strncpy( free_chunk_table->start_tag, free_chunk_table_start_tag, TAG_SIZE ) ;
04373 strncpy( free_chunk_table->end_tag, free_chunk_table_end_tag, TAG_SIZE ) ;
04374
04376 ADFI_set_blank_disk_pointer( &free_chunk_table->small_first_block ) ;
04377 ADFI_set_blank_disk_pointer( &free_chunk_table->small_last_block ) ;
04378
04380 ADFI_set_blank_disk_pointer( &free_chunk_table->medium_first_block ) ;
04381 ADFI_set_blank_disk_pointer( &free_chunk_table->medium_last_block ) ;
04382
04384 ADFI_set_blank_disk_pointer( &free_chunk_table->large_first_block ) ;
04385 ADFI_set_blank_disk_pointer( &free_chunk_table->large_last_block ) ;
04386 }
04387
04388
04389
04390
04391
04392
04393
04394
04395
04396
04397
04398
04399
04400 void ADFI_fill_initial_node_header(
04401 struct NODE_HEADER *node_header,
04402 int *error_return )
04403 {
04404 int i ;
04405
04406 if( node_header == NULL ) {
04407 *error_return = NULL_POINTER ;
04408 return ;
04409 }
04410
04411 strncpy( node_header->node_start_tag, node_start_tag, TAG_SIZE ) ;
04412 strncpy( node_header->node_end_tag, node_end_tag, TAG_SIZE ) ;
04413
04415 for( i=0; i<ADF_NAME_LENGTH; i++ )
04416 node_header->name[i] = ' ' ;
04417
04419 for( i=0; i<ADF_LABEL_LENGTH; i++ )
04420 node_header->label[i] = ' ' ;
04421
04423 node_header->num_sub_nodes = 0 ;
04424 node_header->entries_for_sub_nodes = 0 ;
04425 ADFI_set_blank_disk_pointer( &node_header->sub_node_table ) ;
04426
04428 for( i=2; i<ADF_DATA_TYPE_LENGTH; i++ )
04429 node_header->data_type[i] = ' ' ;
04430 node_header->data_type[0] = 'M' ;
04431 node_header->data_type[1] = 'T' ;
04432
04433
04435 node_header->number_of_dimensions = 0 ;
04436 for( i=0; i<ADF_MAX_DIMENSIONS; i++ )
04437 node_header->dimension_values[i] = 0 ;
04438
04440 node_header->number_of_data_chunks = 0 ;
04441 ADFI_set_blank_disk_pointer( &node_header->data_chunks ) ;
04442 }
04443
04444
04445
04446
04447
04448
04449
04450
04451
04452
04453
04454
04455
04456 void ADFI_flush_buffers(
04457 const unsigned int file_index,
04458 int flush_mode,
04459 int *error_return )
04460 {
04461 char data;
04462
04463 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
04464 *error_return = ADF_FILE_NOT_OPENED ;
04465 return ;
04466 }
04467
04468 *error_return = NO_ERROR ;
04469
04470 if ( (long int) file_index == last_wr_file ) {
04473 ADFI_write_file ( file_index, MAXIMUM_32_BITS, 0, 0, &data, error_return ) ;
04475 if ( flush_mode == FLUSH_CLOSE )
04476 last_wr_block = last_wr_file = flush_wr_block = -2 ;
04477 }
04478
04479 if ( (long int) file_index == last_rd_file && flush_mode == FLUSH_CLOSE ) {
04481 last_rd_block = last_rd_file = num_in_rd_block = -1 ;
04482 }
04483
04484 }
04485
04486
04487
04488
04489
04490
04491
04492
04493
04494
04495
04496
04497
04498
04499
04500
04501
04502
04503 void ADFI_fseek_file(
04504 const unsigned int file_index,
04505 const unsigned long file_block,
04506 const unsigned long block_offset,
04507 int *error_return )
04508 {
04509 file_offset_t offset;
04510 #ifdef USE_STREAM_IO
04511 int iret ;
04512 #else
04513 file_offset_t iret;
04514 #endif
04515
04516 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
04517 *error_return = ADF_FILE_NOT_OPENED ;
04518 return ;
04519 }
04520
04521 offset = (file_offset_t)file_block * DISK_BLOCK_SIZE + block_offset ;
04522 if (offset < 0) {
04523 *error_return = MAX_FILE_SIZE_EXCEEDED;
04524 return;
04525 }
04526
04527 *error_return = NO_ERROR ;
04528
04529 #ifdef USE_STREAM_IO
04530 iret = fseek( ADF_file[file_index].file, offset, SEEK_SET ) ;
04531 if( iret != 0 ) {
04532 *error_return = FSEEK_ERROR ;
04533 }
04534 #else
04535 ADF_sys_err = 0;
04536 iret = file_seek( ADF_file[file_index].file, offset, SEEK_SET ) ;
04537 if( iret < 0 ) {
04538 ADF_sys_err = errno;
04539 *error_return = FSEEK_ERROR ;
04540 }
04541 #endif
04542 }
04543
04544
04545
04546
04547
04548
04549
04550
04551
04552
04553
04554 void ADFI_get_current_date(
04555 char date[] )
04556 {
04557 time_t ct ;
04558 int i_len ;
04559 char *current_time_p ;
04560
04561
04563 ct = time( (time_t *)NULL ) ;
04564 current_time_p = ctime( &ct ) ;
04565
04567 i_len = strcspn ( current_time_p, "\n" ) ;
04568 strcpy( date, current_time_p ) ;
04569 date[i_len] = '\0' ;
04570
04572 ADFI_blank_fill_string ( date, DATE_TIME_SIZE ) ;
04573
04574 }
04575
04576
04577
04578
04579
04580
04581
04582
04583
04584
04585
04586
04587
04588
04589
04590
04591
04592
04593
04594
04595
04596
04597
04598
04599
04600
04601
04602
04603
04604
04605 void ADFI_get_direct_children_ids(
04606 const unsigned int file_index,
04607 const struct DISK_POINTER *node_block_offset,
04608 int *num_ids,
04609 double **ids,
04610 int *error_return )
04611 {
04612 int i ;
04613 struct DISK_POINTER sub_node_block_offset ;
04614 struct NODE_HEADER node ;
04615 struct SUB_NODE_TABLE_ENTRY sub_node_table_entry ;
04616
04617 *error_return = NO_ERROR ;
04618
04619 if( num_ids == NULL || ids == NULL ) {
04620 *error_return = NULL_POINTER ;
04621 return ;
04622 }
04623
04624 *num_ids = 0 ;
04625 *ids = NULL ;
04626
04627 ADFI_read_node_header( file_index, node_block_offset, &node, error_return ) ;
04628 if( *error_return != NO_ERROR )
04629 return ;
04630
04632 if( node.num_sub_nodes == 0 ) {
04633 return ;
04634 }
04635
04636 *ids = (double *) malloc ( node.num_sub_nodes * sizeof(double) ) ;
04637 if( *ids == NULL ) {
04638 *error_return = MEMORY_ALLOCATION_FAILED ;
04639 return ;
04640 }
04641
04643 sub_node_block_offset.block = node.sub_node_table.block ;
04644 sub_node_block_offset.offset = node.sub_node_table.offset +
04645 (TAG_SIZE + DISK_POINTER_SIZE ) ;
04646
04648 *num_ids = node.num_sub_nodes ;
04649 for( i=0; i< *num_ids; i++ ) {
04650 ADFI_adjust_disk_pointer( &sub_node_block_offset, error_return ) ;
04651 if( *error_return != NO_ERROR )
04652 return ;
04653
04655 ADFI_read_sub_node_table_entry( file_index, &sub_node_block_offset,
04656 &sub_node_table_entry, error_return ) ;
04657 if( *error_return != NO_ERROR )
04658 return ;
04659
04661 ADFI_file_block_offset_2_ID( file_index,
04662 sub_node_table_entry.child_location.block,
04663 sub_node_table_entry.child_location.offset, &(*ids)[i],
04664 error_return ) ;
04665 if( *error_return != NO_ERROR )
04666 return ;
04667
04669 sub_node_block_offset.offset += (ADF_NAME_LENGTH + DISK_POINTER_SIZE) ;
04670 }
04671 }
04672
04673
04674
04675
04676
04677
04678
04679
04680
04681
04682
04683
04684
04685
04686 void ADFI_get_file_index_from_name(
04687 const char *file_name,
04688 int *found,
04689 unsigned int *file_index,
04690 double *ID,
04691 int *error_return )
04692 {
04693 double root_ID ;
04694 int i ;
04695
04696
04697 *error_return = NO_ERROR ;
04698 *found = 0;
04699
04700 if( (file_index == NULL) || (ID == NULL) || (found == NULL) ) {
04701 *error_return = NULL_POINTER ;
04702 return ;
04703 }
04704
04705 if( file_name == NULL ) {
04706 *error_return = NULL_STRING_POINTER ;
04707 return ;
04708 }
04709
04710 for( i=0; i<maximum_files; i++ ) {
04711 if( ADF_file[i].in_use && ADF_file[i].file_name != NULL ) {
04712 if( strcmp( file_name, ADF_file[i].file_name ) == 0 ) {
04714 ADFI_file_block_offset_2_ID( i, ROOT_NODE_BLOCK, ROOT_NODE_OFFSET,
04715 &root_ID, error_return ) ;
04716 *ID = root_ID ;
04717 *file_index = i ;
04718 *found = 1 ;
04719 return ;
04720 }
04721 }
04722 }
04723 }
04724
04725
04726
04727
04728
04729
04730
04731
04732
04733
04734
04735
04736
04737
04738
04739
04740
04741
04742
04743
04744
04745
04746 void ADFI_increment_array(
04747 const unsigned int ndim,
04748 const unsigned int dims[],
04749 const int dim_start[],
04750 const int dim_end[],
04751 const int dim_stride[],
04752 int current_position[],
04753 unsigned long *element_offset,
04754 int *error_return )
04755 {
04756 int i ;
04757 unsigned long offset, accumlated_size ;
04758
04759 if( (dims == NULL) || (dim_start == NULL) || (dim_end == NULL) ||
04760 (dim_stride == NULL) || (current_position == NULL) ||
04761 (element_offset == NULL) ) {
04762 *error_return = NULL_POINTER ;
04763 return ;
04764 }
04765
04766 if( (ndim <= 0) || (ndim > 12) ) {
04767 *error_return = BAD_NUMBER_OF_DIMENSIONS ;
04768 return ;
04769 }
04770
04771 *error_return = NO_ERROR ;
04772
04773 offset = 0 ;
04774 accumlated_size = 1 ;
04775 for( i=0; i<ndim; i++ ) {
04776 if( current_position[i] + dim_stride[i] <= dim_end[i] ) {
04777 current_position[i] += dim_stride[i] ;
04778 offset += 1 + (dim_stride[i] - 1) * accumlated_size ;
04779 break ;
04780 }
04781 else {
04782
04783 offset += (dims[i] - current_position[i] + dim_start[i] - 1) *
04784 accumlated_size ;
04786 current_position[i] = dim_start[i] ;
04787 accumlated_size *= dims[i] ;
04788 }
04789 }
04790 *element_offset = offset ;
04791
04792 }
04793
04794
04795
04796
04797
04798
04799
04800
04801 void ADFI_is_block_in_core()
04802 {
04803 fprintf(stderr,"Subroutine ADFI_is_block_in_core is not yet implemented...\n" ) ;
04804 }
04805
04806
04807
04808
04809
04810
04811
04812
04813
04814
04815
04816
04817
04818
04819
04820
04821
04822
04823
04824
04825
04826
04827
04828
04829
04830
04831
04832
04833
04834
04835
04836
04837
04838
04839
04840
04841
04842
04843
04844
04845
04846
04847
04848
04849
04850
04851
04852
04853
04854
04855
04856
04857
04858
04859
04860
04861
04862
04863
04864
04865
04866
04867
04868
04869
04870
04871
04872
04873
04874 void ADFI_little_endian_32_swap_64(
04875 const char from_format,
04876 const char from_os_size,
04877 const char to_format,
04878 const char to_os_size,
04879 const char data_type[2],
04880 const unsigned long delta_from_bytes,
04881 const unsigned long delta_to_bytes,
04882 const unsigned char *from_data,
04883 unsigned char *to_data,
04884 int *error_return )
04885 {
04886
04887 if( (from_data == NULL) || (to_data == NULL) ) {
04888 *error_return = NULL_STRING_POINTER ;
04889 return ;
04890 }
04891
04892 if( (delta_from_bytes == 0) || (delta_to_bytes == 0) ) {
04893 *error_return = NULL_POINTER ;
04894 return ;
04895 }
04896
04897 if( (from_format == 'N') || (to_format == 'N') ) {
04898 *error_return = CANNOT_CONVERT_NATIVE_FORMAT ;
04899 return ;
04900 }
04901
04902 *error_return = NO_ERROR ;
04903
04904 if ( delta_to_bytes == delta_from_bytes ) {
04905 memcpy( to_data, from_data, delta_from_bytes ) ;
04906 }
04907 else if ( delta_from_bytes < delta_to_bytes ) {
04908 switch( EVAL_2_BYTES( data_type[0], data_type[1] ) ) {
04909 case EVAL_2_BYTES( 'I', '8' ):
04910 if( (from_data[3] & 0x80) == 0x80 ) {
04911 to_data[7] = 0xff ;
04912 to_data[6] = 0xff ;
04913 to_data[5] = 0xff ;
04914 to_data[4] = 0xff ;
04915 }
04916 else {
04917 to_data[7] = 0x00 ;
04918 to_data[6] = 0x00 ;
04919 to_data[5] = 0x00 ;
04920 to_data[4] = 0x00 ;
04921 }
04922 to_data[3] = from_data[3] ;
04923 to_data[2] = from_data[2] ;
04924 to_data[1] = from_data[1] ;
04925 to_data[0] = from_data[0] ;
04926 break ;
04927 default:
04928 *error_return = INVALID_DATA_TYPE ;
04929 return ;
04930 }
04931 }
04932 else {
04933 switch( EVAL_2_BYTES( data_type[0], data_type[1] ) ) {
04934 case EVAL_2_BYTES( 'I', '8' ):
04935 to_data[3] = from_data[3] ;
04936 to_data[2] = from_data[2] ;
04937 to_data[1] = from_data[1] ;
04938 to_data[0] = from_data[0] ;
04939 break ;
04940 default:
04941 *error_return = INVALID_DATA_TYPE ;
04942 return ;
04943 }
04944 }
04945
04946 }
04947
04948
04949
04950
04951
04952
04953
04954
04955
04956
04957
04958
04959
04960
04961
04962
04963
04964
04965
04966
04967
04968
04969
04970
04971
04972
04973
04974
04975
04976
04977
04978
04979
04980
04981
04982
04983
04984
04985
04986
04987
04988
04989
04990
04991
04992
04993
04994
04995
04996
04997
04998
04999
05000
05001
05002
05003
05004
05005
05006
05007
05008
05009
05010
05011
05012
05013 void ADFI_little_endian_to_cray(
05014 const char from_format,
05015 const char from_os_size,
05016 const char to_format,
05017 const char to_os_size,
05018 const char data_type[2],
05019 const unsigned long delta_from_bytes,
05020 const unsigned long delta_to_bytes,
05021 const unsigned char *from_data,
05022 unsigned char *to_data,
05023 int *error_return )
05024 {
05025 int i, exp ;
05026
05027 if( (from_data == NULL) || (to_data == NULL) ) {
05028 *error_return = NULL_STRING_POINTER ;
05029 return ;
05030 }
05031
05032 if( (delta_from_bytes == 0) || (delta_to_bytes == 0) ) {
05033 *error_return = NULL_POINTER ;
05034 return ;
05035 }
05036
05037 if( (from_format == 'N') || (to_format == 'N') ) {
05038 *error_return = CANNOT_CONVERT_NATIVE_FORMAT ;
05039 return ;
05040 }
05041
05042 *error_return = NO_ERROR ;
05043
05044 switch( EVAL_2_BYTES( data_type[0], data_type[1] ) ) {
05045
05046 case EVAL_2_BYTES( 'M', 'T' ):
05047 *error_return = NO_DATA ;
05048 return ;
05049
05050 case EVAL_2_BYTES( 'C', '1' ):
05051 case EVAL_2_BYTES( 'B', '1' ):
05052 to_data[0] = from_data[0] ;
05053 break ;
05054
05055 case EVAL_2_BYTES( 'I', '4' ):
05056 if( (from_data[3] & 0x80) == 0x80 ) {
05057 to_data[0] = 0xff ;
05058 to_data[1] = 0xff ;
05059 to_data[2] = 0xff ;
05060 to_data[3] = 0xff ;
05061 }
05062 else {
05063 to_data[0] = 0x00 ;
05064 to_data[1] = 0x00 ;
05065 to_data[2] = 0x00 ;
05066 to_data[3] = 0x00 ;
05067 }
05068 to_data[4] = from_data[3] ;
05069 to_data[5] = from_data[2] ;
05070 to_data[6] = from_data[1] ;
05071 to_data[7] = from_data[0] ;
05072 break ;
05073
05074 case EVAL_2_BYTES( 'U', '4' ):
05075 to_data[0] = 0x00 ;
05076 to_data[1] = 0x00 ;
05077 to_data[2] = 0x00 ;
05078 to_data[3] = 0x00 ;
05079 to_data[4] = from_data[3] ;
05080 to_data[5] = from_data[2] ;
05081 to_data[6] = from_data[1] ;
05082 to_data[7] = from_data[0] ;
05083 break ;
05084
05085 case EVAL_2_BYTES( 'I', '8' ):
05086 if( (from_data[3] & 0x80) == 0x80 ) {
05087 to_data[0] = 0xff ;
05088 to_data[1] = 0xff ;
05089 to_data[2] = 0xff ;
05090 to_data[3] = 0xff ;
05091 }
05092 else {
05093 to_data[0] = 0x00 ;
05094 to_data[1] = 0x00 ;
05095 to_data[2] = 0x00 ;
05096 to_data[3] = 0x00 ;
05097 }
05098 for( i=0; i<(int)delta_from_bytes; i++ )
05099 to_data[8-delta_from_bytes+i] = from_data[delta_from_bytes-1-i] ;
05100 break ;
05101
05102 case EVAL_2_BYTES( 'U', '8' ):
05103 to_data[0] = 0x00 ;
05104 to_data[1] = 0x00 ;
05105 to_data[2] = 0x00 ;
05106 to_data[3] = 0x00 ;
05107 for( i=0; i<(int)delta_from_bytes; i++ )
05108 to_data[8-delta_from_bytes+i] = from_data[delta_from_bytes-1-i] ;
05109 break ;
05110
05111 case EVAL_2_BYTES( 'R', '4' ):
05112 for( i=0; i<8; i++ )
05113 to_data[i] = 0x00 ;
05114
05116 if( (from_data[3] == 0x00) && (from_data[2] == 0x00) &&
05117 (from_data[1] == 0x00) && (from_data[0] == 0x00) )
05118 break ;
05119
05121 to_data[0] = from_data[3] & 0x80 ;
05122
05126 exp = (from_data[3] & 0x3f) << 1 ;
05127 if( (from_data[2] & 0x80) == 0x80 )
05128 exp += 1 ;
05129 if( (from_data[3] & 0x40) == 0x00 )
05130 exp -= 128 ;
05131 exp += 2 ;
05132
05133 to_data[1] = exp & 0xff ;
05134 if( exp < 0 )
05135 to_data[0] |= 0x3f ;
05136 else
05137 to_data[0] |= 0x40 ;
05138
05141 to_data[2] = from_data[2] | 0x80 ;
05142 to_data[3] = from_data[1] ;
05143 to_data[4] = from_data[0] ;
05144 break ;
05145
05146 case EVAL_2_BYTES( 'R', '8' ):
05147 for( i=0; i<8; i++ )
05148 to_data[i] = 0x00 ;
05149
05151 if( (from_data[7] == 0x00) && (from_data[6] == 0x00) &&
05152 (from_data[5] == 0x00) && (from_data[4] == 0x00) )
05153 break ;
05154
05156 to_data[0] = from_data[7] & 0x80 ;
05157
05161 exp = ((from_data[7] & 0x3f) << 4) + ((from_data[6]>>4)&0x0f) ;
05162
05163 if( (from_data[7] & 0x40) == 0x00 )
05164 exp -= 1024 ;
05165 exp += 2 ;
05166
05167 to_data[1] = (unsigned int)(exp & 0xff) ;
05168 to_data[0] |= ((exp>>8) & 0x03) ;
05169 if( exp < 0 )
05170 to_data[0] |= 0x3c ;
05171 else
05172 to_data[0] |= 0x40 ;
05173
05176 to_data[2] = 0x80 | ((from_data[6]<<3)&0x78) |
05177 ((from_data[5]>>5)&0x07) ;
05178 for( i=3; i<8; i++ )
05179 to_data[i] = ((from_data[7-i+1]<<3)&0xF8) |
05180 ((from_data[7-i]>>5)&0x07) ;
05181 #ifdef PRINT_STUFF
05182 printf("from:" ) ;
05183 for( i=0; i<8; i++ )
05184 printf("%02x ", from_data[i] ) ;
05185 printf("to:" ) ;
05186 for( i=0; i<8; i++ )
05187 printf("%02x ", to_data[i] ) ;
05188 printf("\n" ) ;
05189 #endif
05190 break ;
05191
05192 case EVAL_2_BYTES( 'X', '4' ):
05193 ADFI_little_endian_to_cray( from_format, from_os_size,
05194 to_format, to_os_size, "R4", delta_from_bytes,
05195 delta_to_bytes, from_data, to_data, error_return ) ;
05196 if( *error_return != NO_ERROR )
05197 return ;
05198
05199 ADFI_little_endian_to_cray( from_format, from_os_size,
05200 to_format, to_os_size, "R4", delta_from_bytes,
05201 delta_to_bytes, &from_data[4], &to_data[8], error_return ) ;
05202 if( *error_return != NO_ERROR )
05203 return ;
05204 break ;
05205
05206 case EVAL_2_BYTES( 'X', '8' ):
05207 ADFI_little_endian_to_cray( from_format, from_os_size,
05208 to_format, to_os_size, "R8", delta_from_bytes,
05209 delta_to_bytes, from_data, to_data, error_return ) ;
05210 if( *error_return != NO_ERROR )
05211 return ;
05212
05213 ADFI_little_endian_to_cray( from_format, from_os_size,
05214 to_format, to_os_size, "R8", delta_from_bytes,
05215 delta_to_bytes, &from_data[8], &to_data[8], error_return ) ;
05216 if( *error_return != NO_ERROR )
05217 return ;
05218 break ;
05219
05220 default:
05221 *error_return = INVALID_DATA_TYPE ;
05222 return ;
05223 }
05224 }
05225
05226
05227
05228
05229
05230
05231
05232
05233
05234
05235
05236
05237
05238
05239
05240
05241
05242
05243
05244
05245
05246
05247
05248
05249
05250
05251
05252
05253
05254 void ADFI_open_file(
05255 const char *file,
05256 const char *status,
05257 unsigned int *file_index,
05258 int *error_return )
05259 {
05260 int index ;
05261 #ifdef USE_STREAM_IO
05262 FILE *f_ret ;
05263 #else
05264 int f_ret, f_mode;
05265 #endif
05266
05267 if( (status == NULL) ||
05268 ((file == NULL) && (ADFI_stridx_c( status, "SCRATCH" ) != 0) ) ) {
05269 *error_return = NULL_STRING_POINTER ;
05270 return ;
05271 }
05272
05273 if( file_index == NULL ) {
05274 *error_return = NULL_POINTER ;
05275 return ;
05276 }
05277
05278 *error_return = NO_ERROR ;
05279
05280 for( index=0; index<maximum_files; index++ ) {
05281 if( ADF_file[index].in_use == 0 )
05282 break ;
05283 }
05284
05285 if( index >= maximum_files ) {
05286 ADF_FILE *files;
05287
05288
05289 files = (ADF_FILE *) calloc (maximum_files + ADF_FILE_INC, sizeof(ADF_FILE));
05290 if (files == NULL) {
05291 *error_return = MEMORY_ALLOCATION_FAILED ;
05292 return ;
05293 }
05294 if (maximum_files) {
05295 memcpy (files, ADF_file, maximum_files * sizeof(ADF_FILE));
05296 free (ADF_file);
05297 } else {
05298 ADFI_stack_control(0,0,0,INIT_STK,0,0,NULL);
05299 }
05300 ADF_file = files;
05301 index = maximum_files;
05302 maximum_files += ADF_FILE_INC;
05303 }
05304
05305 if (index > MAXIMUM_FILES) {
05306 *error_return = TOO_MANY_ADF_FILES_OPENED;
05307 return;
05308 }
05309
05310 ADF_file[index].in_use = 1 ;
05311 ADF_file[index].nlinks = 0;
05312 ADF_file[index].links = NULL;
05313 ADF_file[index].file_name = NULL;
05314 ADF_file[index].version_update[0] = '\0' ;
05315 ADF_file[index].format = UNDEFINED_FORMAT ;
05316 ADF_file[index].os_size = UNDEFINED_FORMAT ;
05317 ADF_file[index].link_separator = '>' ;
05318
05319
05320
05321
05322
05323
05324
05325
05326 #ifdef USE_STREAM_IO
05327
05328 ADF_file[index].file = NULL;
05329
05330 if( ADFI_stridx_c( status, "READ_ONLY" ) == 0 ) {
05331 #ifdef _WIN32
05332 f_ret = fopen( file, "rb" ) ;
05333 #else
05334 f_ret = fopen( file, "r" ) ;
05335 #endif
05336 }
05337 else if( ADFI_stridx_c( status, "OLD" ) == 0 ) {
05338 #ifdef _WIN32
05339 f_ret = fopen( file, "rb+" ) ;
05340 #else
05341 f_ret = fopen( file, "r+" ) ;
05342 #endif
05343 }
05344 else if( ADFI_stridx_c( status, "NEW" ) == 0 ) {
05345 #ifdef _WIN32
05346 f_ret = fopen( file, "wb+" ) ;
05347 #else
05348 f_ret = fopen( file, "w+" ) ;
05349 #endif
05350 }
05351 else if( ADFI_stridx_c( status, "SCRATCH" ) == 0 ) {
05352 f_ret = tmpfile();
05353 }
05354 else if( ADFI_stridx_c( status, "UNKNOWN" ) == 0 ) {
05355 #ifdef _WIN32
05356 f_ret = fopen( file, "ab+" ) ;
05357 #else
05358 f_ret = fopen( file, "a+" ) ;
05359 #endif
05360 }
05361 else {
05362 *error_return = ADF_FILE_STATUS_NOT_RECOGNIZED ;
05363 goto Error_Exit ;
05364 }
05365
05366 if( f_ret == NULL ) {
05367 if (errno == EMFILE)
05368 *error_return = TOO_MANY_ADF_FILES_OPENED;
05369 else
05370 *error_return = FILE_OPEN_ERROR ;
05371 goto Error_Exit ;
05372 }
05373
05374 #else
05375
05376 ADF_file[index].file = -1;
05377 ADF_sys_err = 0;
05378 #ifdef _WIN32
05379 f_mode = O_BINARY ;
05380 #else
05381 f_mode = 0;
05382 #endif
05383 if( ADFI_stridx_c( status, "READ_ONLY" ) == 0 )
05384 f_ret = file_open( file, f_mode | O_RDONLY, 0666);
05385 else if( ADFI_stridx_c( status, "OLD" ) == 0 )
05386 f_ret = file_open( file, f_mode | O_RDWR, 0666);
05387 else if( ADFI_stridx_c( status, "NEW" ) == 0 )
05388 f_ret = file_open( file, f_mode | O_RDWR | O_CREAT, 0666);
05389 else if( ADFI_stridx_c( status, "SCRATCH" ) == 0 ) {
05390 FILE *ftmp = tmpfile();
05391 f_ret = ftmp == NULL ? -1 : fileno(ftmp);
05392 }
05393 else if( ADFI_stridx_c( status, "UNKNOWN" ) == 0 )
05394 f_ret = file_open( file, f_mode | O_RDWR | O_CREAT, 0666);
05395 else {
05396 *error_return = ADF_FILE_STATUS_NOT_RECOGNIZED ;
05397 goto Error_Exit ;
05398 }
05399
05400 if( f_ret < 0 ) {
05401 ADF_sys_err = errno;
05402 if (errno == EMFILE)
05403 *error_return = TOO_MANY_ADF_FILES_OPENED;
05404 else
05405 *error_return = FILE_OPEN_ERROR ;
05406 goto Error_Exit ;
05407 }
05408
05409 #endif
05410
05411 ADF_file[index].file = f_ret ;
05412 *file_index = index ;
05413 strcpy( ADF_file[index].open_mode, status);
05414 if( ADFI_stridx_c( status, "SCRATCH" ) ) {
05415 ADF_file[index].file_name = (char *) malloc (strlen(file) + 1);
05416 if (ADF_file[index].file_name == NULL) {
05417 *error_return = MEMORY_ALLOCATION_FAILED;
05418 goto Error_Exit;
05419 }
05420 strcpy( ADF_file[index].file_name, file ) ;
05421 }
05422 return ;
05423
05424 Error_Exit:
05426 #ifdef USE_STREAM_IO
05427 if( ADF_file[index].file != 0 ) {
05428 if( fclose( ADF_file[index].file ) != 0 )
05429 *error_return = FILE_CLOSE_ERROR ;
05430 }
05431 ADF_file[index].file = NULL ;
05432 #else
05433 if( ADF_file[index].file >= 0 ) {
05434 if( close( ADF_file[index].file ) < 0 ) {
05435 ADF_sys_err = errno;
05436 *error_return = FILE_CLOSE_ERROR ;
05437 }
05438 }
05439 ADF_file[index].file = -1 ;
05440 #endif
05441 ADF_file[index].in_use = 0 ;
05442 if (ADF_file[index].file_name != NULL) {
05443 free (ADF_file[index].file_name);
05444 ADF_file[index].file_name = NULL;
05445 }
05446
05447 }
05448
05449
05450
05451
05452
05453
05454
05455
05456
05457
05458
05459
05460
05461
05462
05463
05464
05465
05466
05467
05468
05469
05470
05471
05472
05473
05474 void ADFI_read_chunk_length(
05475 const unsigned int file_index,
05476 const struct DISK_POINTER *block_offset,
05477 char tag[TAG_SIZE+1],
05478 struct DISK_POINTER *end_of_chunk_tag,
05479 int *error_return )
05480 {
05481 char info[ TAG_SIZE + DISK_POINTER_SIZE ] ;
05482 struct DISK_POINTER current_block_offset ;
05483 unsigned long count ;
05484
05485 if( (block_offset == NULL) || (end_of_chunk_tag == NULL) ) {
05486 *error_return = NULL_POINTER ;
05487 return ;
05488 }
05489
05490 if( tag == NULL ) {
05491 *error_return = NULL_STRING_POINTER ;
05492 return ;
05493 }
05494
05495 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
05496 *error_return = ADF_FILE_NOT_OPENED ;
05497 return ;
05498 }
05499
05500 *error_return = NO_ERROR ;
05501
05502 end_of_chunk_tag->block = 0 ;
05503 end_of_chunk_tag->offset = 0 ;
05504
05506 if( (block_offset->block == 0) && (block_offset->offset == 0) ) {
05507
05509 end_of_chunk_tag->offset = FILE_HEADER_SIZE - TAG_SIZE ;
05510 tag[0] = file_header_tags[0][0] ;
05511 tag[1] = file_header_tags[0][1] ;
05512 tag[2] = file_header_tags[0][2] ;
05513 tag[3] = file_header_tags[0][3] ;
05514 }
05515
05517 else if( (block_offset->block == 0) &&
05518 (block_offset->offset == FREE_CHUNKS_OFFSET) ) {
05519
05521 end_of_chunk_tag->offset =
05522 (FREE_CHUNKS_OFFSET + FREE_CHUNK_TABLE_SIZE) - TAG_SIZE ;
05523 tag[0] = free_chunk_table_start_tag[0] ;
05524 tag[1] = free_chunk_table_start_tag[1] ;
05525 tag[2] = free_chunk_table_start_tag[2] ;
05526 tag[3] = free_chunk_table_start_tag[3] ;
05527 }
05528 else {
05529
05533 count = 0 ;
05534 ADFI_read_file( file_index, block_offset->block, block_offset->offset,
05535 1, info, error_return ) ;
05536 if( *error_return != NO_ERROR )
05537 return ;
05538 if( info[0] == 'z' ) {
05539 current_block_offset.block = block_offset->block ;
05540 current_block_offset.offset = block_offset->offset ;
05541 while( info[0] == 'z' ) {
05542 count++ ;
05543 current_block_offset.offset++ ;
05544 ADFI_adjust_disk_pointer( ¤t_block_offset, error_return ) ;
05545 if( *error_return != NO_ERROR )
05546 return ;
05547
05548 info[0] = '\0' ;
05549 ADFI_read_file( file_index, current_block_offset.block,
05550 current_block_offset.offset, 1, info, error_return ) ;
05551 if( (*error_return == FSEEK_ERROR) || (*error_return == FREAD_ERROR)){
05552 break ;
05553 }
05554 if( *error_return != NO_ERROR )
05555 return ;
05556 }
05557 end_of_chunk_tag->block = block_offset->block ;
05558 end_of_chunk_tag->offset = block_offset->offset + count - TAG_SIZE ;
05559 ADFI_adjust_disk_pointer( end_of_chunk_tag, error_return ) ;
05560 tag[0] = tag[1] = tag[2] = tag[3] = 'z' ;
05561 if( *error_return != NO_ERROR )
05562 return ;
05563 }
05564 else {
05566 ADFI_read_file( file_index, block_offset->block, block_offset->offset,
05567 TAG_SIZE + DISK_POINTER_SIZE, info, error_return ) ;
05568 if( *error_return != NO_ERROR )
05569 return ;
05570
05571
05572 tag[0] = info[0] ;
05573 tag[1] = info[1] ;
05574 tag[2] = info[2] ;
05575 tag[3] = info[3] ;
05576 tag[4] = '\0' ;
05577
05579 if( ADFI_stridx_c( tag, node_start_tag ) == 0 ) {
05580 end_of_chunk_tag->block = block_offset->block ;
05581 end_of_chunk_tag->offset = block_offset->offset +
05582 NODE_HEADER_SIZE - TAG_SIZE ;
05583 ADFI_adjust_disk_pointer( end_of_chunk_tag, error_return ) ;
05584 if( *error_return != NO_ERROR )
05585 return ;
05586 }
05587 else {
05588
05590 ADFI_disk_pointer_from_ASCII_Hex( &info[TAG_SIZE],
05591 &info[DISK_POINTER_SIZE], end_of_chunk_tag, error_return ) ;
05592 if( *error_return != NO_ERROR )
05593 return ;
05594 }
05595 }
05596 }
05597
05598 }
05599
05600
05601
05602
05603
05604
05605
05606
05607
05608
05609
05610
05611
05612
05613
05614
05615
05616
05617
05618
05619
05620
05621 void ADFI_read_data_chunk(
05622 const unsigned int file_index,
05623 const struct DISK_POINTER *block_offset,
05624 struct TOKENIZED_DATA_TYPE *tokenized_data_type,
05625 const int data_size,
05626 const long chunk_bytes,
05627 const long start_offset,
05628 const long total_bytes,
05629 char *data,
05630 int *error_return )
05631 {
05632 int format_compare ;
05633 char tag[TAG_SIZE + 1] ;
05634 struct DISK_POINTER data_start, end_of_chunk_tag ;
05635 long chunk_total_bytes ;
05636
05637 if( block_offset == NULL ) {
05638 *error_return = NULL_POINTER ;
05639 return ;
05640 }
05641
05642 if( (tokenized_data_type == NULL) || (data == NULL) ) {
05643 *error_return = NULL_STRING_POINTER ;
05644 return ;
05645 }
05646
05647 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
05648 *error_return = ADF_FILE_NOT_OPENED ;
05649 return ;
05650 }
05651
05652 if( total_bytes+start_offset > chunk_bytes ) {
05653 *error_return = REQUESTED_DATA_TOO_LONG ;
05654 return ;
05655 }
05656
05657 *error_return = NO_ERROR ;
05658
05660 ADFI_read_chunk_length( file_index, block_offset, tag, &end_of_chunk_tag,
05661 error_return ) ;
05662 if( *error_return != NO_ERROR )
05663 return ;
05664 tag[TAG_SIZE] = '\0' ;
05665
05667 if( ADFI_stridx_c( tag, data_chunk_start_tag ) != 0 ) {
05668 *error_return = ADF_DISK_TAG_ERROR ;
05669 return ;
05670 }
05671
05673 ADFI_read_file( file_index, end_of_chunk_tag.block, end_of_chunk_tag.offset,
05674 TAG_SIZE, tag, error_return ) ;
05675 if( *error_return != NO_ERROR )
05676 return ;
05677 tag[TAG_SIZE] = '\0' ;
05678
05679 if( ADFI_stridx_c( tag, data_chunk_end_tag ) != 0 ) {
05680 *error_return = ADF_DISK_TAG_ERROR ;
05681 return ;
05682 }
05683
05685 data_start.block = block_offset->block ;
05686 data_start.offset = block_offset->offset + start_offset +
05687 DISK_POINTER_SIZE + TAG_SIZE ;
05688 ADFI_adjust_disk_pointer( &data_start, error_return ) ;
05689 if( *error_return != NO_ERROR )
05690 return ;
05691
05693 chunk_total_bytes = end_of_chunk_tag.offset - data_start.offset + start_offset
05694 + (end_of_chunk_tag.block - data_start.block) * DISK_BLOCK_SIZE ;
05695 if( chunk_bytes > chunk_total_bytes ) {
05696 *error_return = REQUESTED_DATA_TOO_LONG ;
05697 return ;
05698 }
05699 else {
05700 if( chunk_bytes < chunk_total_bytes )
05701 *error_return = REQUESTED_DATA_TOO_LONG ;
05702
05704 ADFI_file_and_machine_compare( file_index, tokenized_data_type,
05705 &format_compare, error_return );
05706 if( *error_return != NO_ERROR )
05707 return ;
05708 if( format_compare == 1 ) {
05710 ADFI_read_file( file_index, data_start.block, data_start.offset,
05711 total_bytes, data, error_return ) ;
05712 if( *error_return != NO_ERROR )
05713 return ;
05714 }
05715 else {
05716 ADFI_read_data_translated( file_index, data_start.block,
05717 data_start.offset, tokenized_data_type, data_size,
05718 total_bytes, data, error_return ) ;
05719 if( *error_return != NO_ERROR )
05720 return ;
05721 }
05722 }
05723
05724 }
05725
05726
05727
05728
05729
05730
05731
05732
05733
05734
05735
05736
05737
05738
05739
05740 void ADFI_read_data_chunk_table(
05741 const unsigned int file_index,
05742 const struct DISK_POINTER *block_offset,
05743 struct DATA_CHUNK_TABLE_ENTRY data_chunk_table[],
05744 int *error_return )
05745 {
05746 char tag[ TAG_SIZE + 1 ] ;
05747 struct DISK_POINTER end_of_chunk_tag, tmp_block_offset ;
05748 unsigned int i, number_of_bytes_to_read ;
05749
05750 if( (block_offset == NULL) || (data_chunk_table == NULL) ) {
05751 *error_return = NULL_POINTER ;
05752 return ;
05753 }
05754
05755 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
05756 *error_return = ADF_FILE_NOT_OPENED ;
05757 return ;
05758 }
05759
05760 *error_return = NO_ERROR ;
05762 ADFI_read_chunk_length( file_index, block_offset, tag,
05763 &end_of_chunk_tag, error_return ) ;
05764 if( *error_return != NO_ERROR )
05765 return ;
05766 tag[TAG_SIZE] = '\0' ;
05767
05769 if( ADFI_stridx_c( tag, data_chunk_table_start_tag ) != 0 ) {
05770 *error_return = ADF_DISK_TAG_ERROR ;
05771 return ;
05772 }
05773
05774 number_of_bytes_to_read =
05775 (end_of_chunk_tag.block - block_offset->block) * DISK_BLOCK_SIZE +
05776 (end_of_chunk_tag.offset - block_offset->offset) -
05777 (TAG_SIZE + DISK_POINTER_SIZE) ;
05778
05780 tmp_block_offset.block = block_offset->block ;
05781 tmp_block_offset.offset = block_offset->offset + TAG_SIZE ;
05782
05783 for( i=0; i<number_of_bytes_to_read/(2 * DISK_POINTER_SIZE); i++ ) {
05784 tmp_block_offset.offset += DISK_POINTER_SIZE ;
05785 ADFI_adjust_disk_pointer( &tmp_block_offset, error_return ) ;
05786 if( *error_return != NO_ERROR )
05787 return ;
05788 ADFI_read_disk_pointer_from_disk( file_index,
05789 tmp_block_offset.block, tmp_block_offset.offset,
05790 &data_chunk_table[i].start, error_return ) ;
05791 if( *error_return != NO_ERROR )
05792 return ;
05793 tmp_block_offset.offset += DISK_POINTER_SIZE ;
05794 ADFI_adjust_disk_pointer( &tmp_block_offset, error_return ) ;
05795 if( *error_return != NO_ERROR )
05796 return ;
05797 ADFI_read_disk_pointer_from_disk( file_index,
05798 tmp_block_offset.block, tmp_block_offset.offset,
05799 &data_chunk_table[i].end, error_return ) ;
05800 if( *error_return != NO_ERROR )
05801 return ;
05802 }
05803
05804 ADFI_read_file( file_index, end_of_chunk_tag.block,
05805 end_of_chunk_tag.offset, TAG_SIZE, tag, error_return ) ;
05806 if( *error_return != NO_ERROR )
05807 return ;
05808
05810 if( ADFI_stridx_c( tag, data_chunk_table_end_tag ) != 0 ) {
05811 *error_return = ADF_DISK_TAG_ERROR ;
05812 return ;
05813 }
05814
05815 }
05816
05817
05818
05819
05820
05821
05822
05823
05824
05825
05826
05827
05828
05829
05830
05831
05832
05833
05834 void ADFI_read_data_translated(
05835 const unsigned int file_index,
05836 const unsigned long file_block,
05837 const unsigned long block_offset,
05838 const struct TOKENIZED_DATA_TYPE *tokenized_data_type,
05839 const int data_size,
05840 const long total_bytes,
05841 char *data,
05842 int *error_return )
05843 {
05844 struct DISK_POINTER disk_pointer ;
05845 int current_token = -1 ;
05846 int machine_size ;
05847 unsigned char *to_data = (unsigned char *)data ;
05848 unsigned char *from_data = from_to_data ;
05849 unsigned long chunk_size ;
05850 unsigned long number_of_data_elements, number_of_elements_read ;
05851 unsigned long delta_from_bytes, delta_to_bytes ;
05852
05853 if( data_size <= 0 ) {
05854 *error_return = ZERO_LENGTH_VALUE ;
05855 return ;
05856 }
05857
05859 do {
05860 machine_size = tokenized_data_type[ ++current_token ].machine_type_size ;
05861 } while( tokenized_data_type[ current_token ].type[0] != 0 ) ;
05862
05863 disk_pointer.block = file_block ;
05864 disk_pointer.offset = block_offset ;
05865 number_of_data_elements = total_bytes / data_size ;
05866 number_of_elements_read = 0 ;
05867 chunk_size = CONVERSION_BUFF_SIZE / data_size ;
05868 if ( chunk_size < 1 ) {
05869 *error_return = REQUESTED_DATA_TOO_LONG ;
05870 return ;
05871 }
05872 delta_from_bytes = chunk_size * data_size ;
05873 delta_to_bytes = chunk_size * machine_size ;
05874
05875 while( number_of_elements_read < number_of_data_elements ) {
05877 number_of_elements_read += chunk_size ;
05878 if ( number_of_elements_read > number_of_data_elements ) {
05879 chunk_size -= ( number_of_elements_read - number_of_data_elements ) ;
05880 delta_from_bytes = chunk_size * data_size ;
05881 delta_to_bytes = chunk_size * machine_size ;
05882 }
05883 ADFI_read_file( file_index, disk_pointer.block, disk_pointer.offset,
05884 delta_from_bytes, (char *)from_data, error_return ) ;
05885 if( *error_return != NO_ERROR )
05886 return ;
05887 ADFI_convert_number_format(
05888 ADF_file[file_index].format,
05889 ADF_file[file_index].os_size,
05890 ADF_this_machine_format,
05891 ADF_this_machine_os_size,
05892 FROM_FILE_FORMAT,
05893 tokenized_data_type, chunk_size, from_data,
05894 to_data, error_return ) ;
05895 if( *error_return != NO_ERROR )
05896 return ;
05897 to_data += delta_to_bytes ;
05898 disk_pointer.offset += delta_from_bytes ;
05899 if ( disk_pointer.offset > DISK_BLOCK_SIZE ) {
05900 ADFI_adjust_disk_pointer( &disk_pointer, error_return ) ;
05901 if( *error_return != NO_ERROR )
05902 return ;
05903 }
05904 }
05905
05906 }
05907
05908
05909
05910
05911
05912
05913
05914
05915 void ADFI_read_disk_block()
05916 {
05917 fprintf(stderr,"Subroutine ADFI_read_disk_block is not yet implemented...\n" ) ;
05918 }
05919
05920
05921
05922
05923
05924
05925
05926
05927
05928
05929
05930
05931
05932
05933
05934
05935
05936
05937 void ADFI_read_disk_pointer_from_disk(
05938 const unsigned int file_index,
05939 const unsigned long file_block,
05940 const unsigned long block_offset,
05941 struct DISK_POINTER *block_and_offset,
05942 int *error_return )
05943 {
05944 char disk_block_offset[DISK_POINTER_SIZE] ;
05945
05946 if( block_and_offset == NULL ) {
05947 *error_return = NULL_POINTER ;
05948 return ;
05949 }
05950
05951 if( block_offset > DISK_BLOCK_SIZE ) {
05952 *error_return = BLOCK_OFFSET_OUT_OF_RANGE ;
05953 return ;
05954 }
05955
05956
05957 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
05958 *error_return = ADF_FILE_NOT_OPENED ;
05959 return ;
05960 }
05961
05962 *error_return = NO_ERROR ;
05963
05965 #if 0
05966 if ( ADFI_stack_control(file_index, file_block, block_offset,
05967 GET_STK, DISK_PTR_STK,
05968 DISK_POINTER_SIZE, disk_block_offset ) != NO_ERROR ) {
05969 #endif
05970
05972 ADFI_read_file( file_index, file_block, block_offset,
05973 DISK_POINTER_SIZE, disk_block_offset, error_return ) ;
05974 if( *error_return != NO_ERROR )
05975 return ;
05976
05978 #if 0
05979 ADFI_stack_control(file_index, file_block, block_offset,
05980 SET_STK, DISK_PTR_STK,
05981 DISK_POINTER_SIZE, disk_block_offset );
05982 }
05983 #endif
05984
05986 ADFI_disk_pointer_from_ASCII_Hex( &disk_block_offset[0], &disk_block_offset[8],
05987 block_and_offset, error_return ) ;
05988 if( *error_return != NO_ERROR )
05989 return ;
05990
05991 }
05992
05993
05994
05995
05996 #ifndef USE_STREAM_IO
05997
05998 int ADFI_read (
05999 const unsigned int file_index,
06000 const unsigned int data_length,
06001 char *data)
06002 {
06003 char *data_ptr = data;
06004 unsigned bytes_left = data_length;
06005 int nbytes, bytes_read = 0;
06006
06007 ADF_sys_err = 0;
06008 while (bytes_left > 0) {
06009 nbytes = read (ADF_file[file_index].file, data_ptr, bytes_left);
06010 if (0 == nbytes) break;
06011 if (-1 == nbytes) {
06012 if (EINTR != errno) {
06013 ADF_sys_err = errno;
06014 return -1;
06015 }
06016 }
06017 else {
06018 bytes_left -= nbytes;
06019 bytes_read += nbytes;
06020 data_ptr += nbytes;
06021 }
06022 }
06023 return bytes_read;
06024 }
06025
06026 #endif
06027
06028
06029
06030
06031
06032
06033
06034
06035
06036
06037
06038
06039
06040
06041
06042
06043
06044
06045
06046
06047
06048
06049
06050 void ADFI_read_file(
06051 const unsigned int file_index,
06052 const unsigned long file_block,
06053 const unsigned long block_offset,
06054 const unsigned int data_length,
06055 char *data,
06056 int *error_return )
06057 {
06058 int iret ;
06059
06060
06061 if( data == NULL ) {
06062 *error_return = NULL_STRING_POINTER ;
06063 return ;
06064 }
06065
06066 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
06067 *error_return = ADF_FILE_NOT_OPENED ;
06068 return ;
06069 }
06070
06071 *error_return = NO_ERROR ;
06072
06076 if( data_length + block_offset > DISK_BLOCK_SIZE ) {
06077
06079 ADFI_fseek_file( file_index, file_block, block_offset, error_return ) ;
06080 if( *error_return != NO_ERROR ) {
06081 return ;
06082 }
06083
06085 iret = ADFI_read ( file_index, data_length, data ) ;
06086 if( iret != (int)data_length ) {
06087 *error_return = FREAD_ERROR ;
06088 return ;
06089 }
06090
06091 return;
06092 }
06093
06104 if( num_in_rd_block < DISK_BLOCK_SIZE ||
06105 (long int) file_block != last_rd_block ||
06106 (long int) file_index != last_rd_file ) {
06107
06110 if ( (long int) file_block == last_wr_block && (long int) file_index == last_wr_file ) {
06111
06112
06113 memcpy( rd_block_buffer, wr_block_buffer, DISK_BLOCK_SIZE );
06114 iret = DISK_BLOCK_SIZE;
06115 }
06116 else {
06117
06119 ADFI_fseek_file( file_index, file_block, 0, error_return ) ;
06120 if( *error_return != NO_ERROR ) {
06121 return ;
06122 }
06123
06125 iret = ADFI_read( file_index, DISK_BLOCK_SIZE, rd_block_buffer ) ;
06126 if( iret <= 0 ) {
06127 *error_return = FREAD_ERROR ;
06128 return ;
06129 }
06130
06131 }
06132
06134 last_rd_block = file_block ;
06135 last_rd_file = file_index ;
06136 num_in_rd_block = iret ;
06137
06138 }
06139
06140
06141 memcpy( data, &rd_block_buffer[block_offset], data_length );
06142
06143 }
06144
06145
06146
06147
06148
06149
06150
06151
06152
06153
06154
06155
06156
06157
06158
06159 void ADFI_read_file_header(
06160 const unsigned int file_index,
06161 struct FILE_HEADER *file_header,
06162 int *error_return )
06163 {
06164 char disk_header[ FILE_HEADER_SIZE ] ;
06165
06166 if( file_header == NULL ) {
06167 *error_return = NULL_POINTER ;
06168 return ;
06169 }
06170
06171 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
06172 *error_return = ADF_FILE_NOT_OPENED ;
06173 return ;
06174 }
06175
06176 *error_return = NO_ERROR ;
06177
06179 if ( ADFI_stack_control(file_index, 0, 0, GET_STK, FILE_STK,
06180 FILE_HEADER_SIZE, disk_header ) != NO_ERROR ) {
06181
06183 ADFI_read_file( file_index, 0, 0, FILE_HEADER_SIZE, disk_header,
06184 error_return ) ;
06185 if( *error_return != NO_ERROR )
06186 return ;
06187
06189 if( strncmp( &disk_header[32], file_header_tags[0], TAG_SIZE )!= 0 ) {
06190 *error_return = ADF_MEMORY_TAG_ERROR ;
06191 return ;
06192 }
06193
06194 if( strncmp( &disk_header[64], file_header_tags[1], TAG_SIZE )!= 0 ) {
06195 *error_return = ADF_MEMORY_TAG_ERROR ;
06196 return ;
06197 }
06198
06199 if( strncmp( &disk_header[96], file_header_tags[2], TAG_SIZE )!= 0 ) {
06200 *error_return = ADF_MEMORY_TAG_ERROR ;
06201 return ;
06202 }
06203
06204 if( strncmp( &disk_header[102], file_header_tags[3], TAG_SIZE )!= 0 ) {
06205 *error_return = ADF_MEMORY_TAG_ERROR ;
06206 return ;
06207 }
06208
06209 if( strncmp( &disk_header[130], file_header_tags[4], TAG_SIZE )!= 0 ) {
06210 *error_return = ADF_MEMORY_TAG_ERROR ;
06211 return ;
06212 }
06213
06214 if( strncmp( &disk_header[182], file_header_tags[5], TAG_SIZE )!= 0 ) {
06215 *error_return = ADF_MEMORY_TAG_ERROR ;
06216 return ;
06217 }
06218
06220 ADFI_stack_control(file_index, 0, 0, SET_STK, FILE_STK,
06221 FILE_HEADER_SIZE, disk_header );
06222 }
06223
06226 strncpy( (char *)file_header->what, &disk_header[ 0], 32 ) ;
06227 strncpy( (char *)file_header->tag0, &disk_header[ 32], TAG_SIZE ) ;
06228 strncpy( (char *)file_header->creation_date, &disk_header[ 36], DATE_TIME_SIZE);
06229 strncpy( (char *)file_header->tag1, &disk_header[ 64], TAG_SIZE ) ;
06230 strncpy( (char *)file_header->modification_date, &disk_header[ 68],
06231 DATE_TIME_SIZE ) ;
06232 strncpy( (char *)file_header->tag2, &disk_header[ 96], TAG_SIZE ) ;
06233 file_header->numeric_format = disk_header[100] ;
06234 file_header->os_size = disk_header[101] ;
06235 strncpy( (char *)file_header->tag3, &disk_header[102], TAG_SIZE ) ;
06236
06237 ADFI_ASCII_Hex_2_unsigned_int( 0, 255, 2, &disk_header[106],
06238 &file_header->sizeof_char, error_return ) ;
06239 if( *error_return != NO_ERROR )
06240 return ;
06241
06242 ADFI_ASCII_Hex_2_unsigned_int( 0, 255, 2, &disk_header[108],
06243 &file_header->sizeof_short, error_return ) ;
06244 if( *error_return != NO_ERROR )
06245 return ;
06246
06247 ADFI_ASCII_Hex_2_unsigned_int( 0, 255, 2, &disk_header[110],
06248 &file_header->sizeof_int, error_return ) ;
06249 if( *error_return != NO_ERROR )
06250 return ;
06251
06252 ADFI_ASCII_Hex_2_unsigned_int( 0, 255, 2, &disk_header[112],
06253 &file_header->sizeof_long, error_return ) ;
06254 if( *error_return != NO_ERROR )
06255 return ;
06256
06257 ADFI_ASCII_Hex_2_unsigned_int( 0, 255, 2, &disk_header[114],
06258 &file_header->sizeof_float, error_return ) ;
06259 if( *error_return != NO_ERROR )
06260 return ;
06261
06262 ADFI_ASCII_Hex_2_unsigned_int( 0, 255, 2, &disk_header[116],
06263 &file_header->sizeof_double, error_return ) ;
06264 if( *error_return != NO_ERROR )
06265 return ;
06266
06267 ADFI_ASCII_Hex_2_unsigned_int( 0, 255, 2, &disk_header[118],
06268 &file_header->sizeof_char_p, error_return ) ;
06269 if( *error_return != NO_ERROR )
06270 return ;
06271
06272 ADFI_ASCII_Hex_2_unsigned_int( 0, 255, 2, &disk_header[120],
06273 &file_header->sizeof_short_p, error_return ) ;
06274 if( *error_return != NO_ERROR )
06275 return ;
06276
06277 ADFI_ASCII_Hex_2_unsigned_int( 0, 255, 2, &disk_header[122],
06278 &file_header->sizeof_int_p, error_return ) ;
06279 if( *error_return != NO_ERROR )
06280 return ;
06281
06282 ADFI_ASCII_Hex_2_unsigned_int( 0, 255, 2, &disk_header[124],
06283 &file_header->sizeof_long_p, error_return ) ;
06284 if( *error_return != NO_ERROR )
06285 return ;
06286
06287 ADFI_ASCII_Hex_2_unsigned_int( 0, 255, 2, &disk_header[126],
06288 &file_header->sizeof_float_p, error_return ) ;
06289 if( *error_return != NO_ERROR )
06290 return ;
06291
06292 ADFI_ASCII_Hex_2_unsigned_int( 0, 255, 2, &disk_header[128],
06293 &file_header->sizeof_double_p, error_return ) ;
06294 if( *error_return != NO_ERROR )
06295 return ;
06296
06297 strncpy( file_header->tag4, &disk_header[130], TAG_SIZE ) ;
06298
06299 ADFI_disk_pointer_from_ASCII_Hex( &disk_header[134], &disk_header[142],
06300 &file_header->root_node, error_return ) ;
06301 if( *error_return != NO_ERROR )
06302 return ;
06303
06304 ADFI_disk_pointer_from_ASCII_Hex( &disk_header[146], &disk_header[154],
06305 &file_header->end_of_file, error_return ) ;
06306 if( *error_return != NO_ERROR )
06307 return ;
06308
06309 ADFI_disk_pointer_from_ASCII_Hex( &disk_header[158], &disk_header[166],
06310 &file_header->free_chunks, error_return ) ;
06311 if( *error_return != NO_ERROR )
06312 return ;
06313
06314 ADFI_disk_pointer_from_ASCII_Hex( &disk_header[170], &disk_header[178],
06315 &file_header->extra, error_return ) ;
06316 if( *error_return != NO_ERROR )
06317 return ;
06318
06319 strncpy( file_header->tag5, &disk_header[182], TAG_SIZE ) ;
06320
06321
06323 if( strncmp( file_header->tag0, file_header_tags[0], TAG_SIZE )!= 0 ) {
06324 *error_return = ADF_MEMORY_TAG_ERROR ;
06325 return ;
06326 }
06327
06328 if( strncmp( file_header->tag1, file_header_tags[1], TAG_SIZE )!= 0 ) {
06329 *error_return = ADF_MEMORY_TAG_ERROR ;
06330 return ;
06331 }
06332
06333 if( strncmp( file_header->tag2, file_header_tags[2], TAG_SIZE )!= 0 ) {
06334 *error_return = ADF_MEMORY_TAG_ERROR ;
06335 return ;
06336 }
06337
06338 if( strncmp( file_header->tag3, file_header_tags[3], TAG_SIZE )!= 0 ) {
06339 *error_return = ADF_MEMORY_TAG_ERROR ;
06340 return ;
06341 }
06342
06343 if( strncmp( file_header->tag4, file_header_tags[4], TAG_SIZE )!= 0 ) {
06344 *error_return = ADF_MEMORY_TAG_ERROR ;
06345 return ;
06346 }
06347
06348 if( strncmp( file_header->tag5, file_header_tags[5], TAG_SIZE )!= 0 ) {
06349 *error_return = ADF_MEMORY_TAG_ERROR ;
06350 return ;
06351 }
06352
06353 }
06354
06355
06356
06357
06358
06359
06360
06361
06362
06363
06364
06365
06366
06367
06368
06369
06370
06371 void ADFI_read_free_chunk(
06372 const unsigned int file_index,
06373 const struct DISK_POINTER *block_offset,
06374 struct FREE_CHUNK *free_chunk,
06375 int *error_return )
06376 {
06377 char tag[TAG_SIZE + 1] ;
06378 struct DISK_POINTER chunk_block_offset ;
06379
06380 if( (block_offset == NULL) || (free_chunk == NULL) ) {
06381 *error_return = NULL_POINTER ;
06382 return ;
06383 }
06384
06385 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
06386 *error_return = ADF_FILE_NOT_OPENED ;
06387 return ;
06388 }
06389
06390 *error_return = NO_ERROR ;
06391
06393 ADFI_read_chunk_length( file_index, block_offset, tag,
06394 &(free_chunk->end_of_chunk_tag), error_return ) ;
06395 if( *error_return != NO_ERROR )
06396 return ;
06397 tag[TAG_SIZE] = '\0' ;
06398
06400 if( ADFI_stridx_c( tag, free_chunk_start_tag ) != 0 ) {
06401 *error_return = ADF_DISK_TAG_ERROR ;
06402 return ;
06403 }
06404
06407 chunk_block_offset = *block_offset ;
06408 chunk_block_offset.offset += TAG_SIZE + DISK_POINTER_SIZE ;
06409 ADFI_adjust_disk_pointer( &chunk_block_offset, error_return ) ;
06410 if( *error_return != NO_ERROR )
06411 return ;
06412
06415 ADFI_read_disk_pointer_from_disk( file_index, chunk_block_offset.block,
06416 chunk_block_offset.offset, &(free_chunk->next_chunk), error_return ) ;
06417 if( *error_return != NO_ERROR )
06418 return ;
06419
06420 ADFI_read_file( file_index, free_chunk->end_of_chunk_tag.block,
06421 free_chunk->end_of_chunk_tag.offset, TAG_SIZE, tag, error_return ) ;
06422 if( *error_return != NO_ERROR )
06423 return ;
06424
06426 if( ADFI_stridx_c( tag, free_chunk_end_tag ) != 0 ) {
06427 *error_return = ADF_DISK_TAG_ERROR ;
06428 return ;
06429 }
06430
06431 strncpy( free_chunk->start_tag, free_chunk_start_tag, 4 ) ;
06432 strncpy( free_chunk->end_tag, free_chunk_end_tag, 4 ) ;
06433 }
06434
06435
06436
06437
06438
06439
06440
06441
06442
06443
06444
06445
06446
06447
06448
06449
06450 void ADFI_read_free_chunk_table(
06451 const unsigned int file_index,
06452 struct FREE_CHUNK_TABLE *free_chunk_table,
06453 int *error_return )
06454 {
06455 char disk_free_chunk_data[ FREE_CHUNK_TABLE_SIZE ] ;
06456
06457 if( free_chunk_table == NULL ) {
06458 *error_return = NULL_POINTER ;
06459 return ;
06460 }
06461
06462 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
06463 *error_return = ADF_FILE_NOT_OPENED ;
06464 return ;
06465 }
06466
06467 *error_return = NO_ERROR ;
06468
06470 if ( ADFI_stack_control(file_index, FREE_CHUNKS_BLOCK, FREE_CHUNKS_OFFSET,
06471 GET_STK, FREE_CHUNK_STK, FREE_CHUNK_TABLE_SIZE,
06472 disk_free_chunk_data ) != NO_ERROR ) {
06473
06475 ADFI_read_file( file_index, FREE_CHUNKS_BLOCK, FREE_CHUNKS_OFFSET,
06476 FREE_CHUNK_TABLE_SIZE, disk_free_chunk_data, error_return ) ;
06477 if( *error_return != NO_ERROR )
06478 return ;
06479
06481 if( ADFI_stridx_c( &disk_free_chunk_data[0], free_chunk_table_start_tag ) !=
06482 0 ) {
06483 *error_return = ADF_DISK_TAG_ERROR ;
06484 return ;
06485 }
06486
06487 if( ADFI_stridx_c( &disk_free_chunk_data[FREE_CHUNK_TABLE_SIZE - TAG_SIZE],
06488 free_chunk_table_end_tag ) != 0 ) {
06489 *error_return = ADF_DISK_TAG_ERROR ;
06490 return ;
06491 }
06492
06494 ADFI_stack_control(file_index, FREE_CHUNKS_BLOCK, FREE_CHUNKS_OFFSET,
06495 SET_STK, FREE_CHUNK_STK, FREE_CHUNK_TABLE_SIZE,
06496 disk_free_chunk_data );
06497 }
06498
06500 strncpy( (char *)free_chunk_table->start_tag, &disk_free_chunk_data[ 0],
06501 TAG_SIZE ) ;
06502 strncpy( (char *)free_chunk_table->end_tag,
06503 &disk_free_chunk_data[ FREE_CHUNK_TABLE_SIZE - TAG_SIZE ], TAG_SIZE ) ;
06504
06505 ADFI_disk_pointer_from_ASCII_Hex( &disk_free_chunk_data[ TAG_SIZE],
06506 &disk_free_chunk_data[DISK_POINTER_SIZE],
06507 &free_chunk_table->small_first_block, error_return ) ;
06508 if( *error_return != NO_ERROR )
06509 return ;
06510
06511 ADFI_disk_pointer_from_ASCII_Hex( &disk_free_chunk_data[16],
06512 &disk_free_chunk_data[24], &free_chunk_table->small_last_block,
06513 error_return ) ;
06514 if( *error_return != NO_ERROR )
06515 return ;
06516
06517 ADFI_disk_pointer_from_ASCII_Hex( &disk_free_chunk_data[28],
06518 &disk_free_chunk_data[36], &free_chunk_table->medium_first_block,
06519 error_return ) ;
06520 if( *error_return != NO_ERROR )
06521 return ;
06522
06523 ADFI_disk_pointer_from_ASCII_Hex( &disk_free_chunk_data[40],
06524 &disk_free_chunk_data[48], &free_chunk_table->medium_last_block,
06525 error_return ) ;
06526 if( *error_return != NO_ERROR )
06527 return ;
06528
06529 ADFI_disk_pointer_from_ASCII_Hex( &disk_free_chunk_data[52],
06530 &disk_free_chunk_data[60], &free_chunk_table->large_first_block,
06531 error_return ) ;
06532 if( *error_return != NO_ERROR )
06533 return ;
06534
06535 ADFI_disk_pointer_from_ASCII_Hex( &disk_free_chunk_data[64],
06536 &disk_free_chunk_data[72], &free_chunk_table->large_last_block,
06537 error_return ) ;
06538 if( *error_return != NO_ERROR )
06539 return ;
06540
06542 if( ADFI_stridx_c( free_chunk_table->start_tag, free_chunk_table_start_tag )
06543 != 0 ) {
06544 *error_return = ADF_MEMORY_TAG_ERROR ;
06545 return ;
06546 }
06547
06548 if( ADFI_stridx_c( free_chunk_table->end_tag, free_chunk_table_end_tag )
06549 != 0 ) {
06550 *error_return = ADF_MEMORY_TAG_ERROR ;
06551 return ;
06552 }
06553
06554 }
06555
06556
06557
06558
06559
06560
06561
06562
06563
06564
06565
06566
06567
06568
06569
06570
06571
06572 void ADFI_read_node_header(
06573 const unsigned int file_index,
06574 const struct DISK_POINTER *block_offset,
06575 struct NODE_HEADER *node_header,
06576 int *error_return )
06577 {
06578 char disk_node_data[ NODE_HEADER_SIZE ] ;
06579 int i ;
06580
06581 if( (block_offset == NULL) || (node_header == NULL) ) {
06582 *error_return = NULL_POINTER ;
06583 return ;
06584 }
06585
06586 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
06587 *error_return = ADF_FILE_NOT_OPENED ;
06588 return ;
06589 }
06590
06591 *error_return = NO_ERROR ;
06592
06594 if ( ADFI_stack_control(file_index, block_offset->block, block_offset->offset,
06595 GET_STK, NODE_STK, NODE_HEADER_SIZE,
06596 disk_node_data ) != NO_ERROR ) {
06597
06599 ADFI_read_file( file_index, block_offset->block, block_offset->offset,
06600 NODE_HEADER_SIZE, disk_node_data, error_return ) ;
06601 if( *error_return != NO_ERROR )
06602 return ;
06603
06605 if( ADFI_stridx_c( &disk_node_data[0], node_start_tag ) != 0 ) {
06606 *error_return = ADF_DISK_TAG_ERROR ;
06607 return ;
06608 }
06609
06610 if( ADFI_stridx_c( &disk_node_data[ NODE_HEADER_SIZE - TAG_SIZE ],
06611 node_end_tag ) != 0 ) {
06612 *error_return = ADF_DISK_TAG_ERROR ;
06613 return ;
06614 }
06615
06617 ADFI_stack_control(file_index, block_offset->block, block_offset->offset,
06618 SET_STK, NODE_STK, NODE_HEADER_SIZE, disk_node_data );
06619 }
06620
06622 strncpy( (char *)node_header->node_start_tag, &disk_node_data[ 0], TAG_SIZE ) ;
06623 strncpy( (char *)node_header->node_end_tag,
06624 &disk_node_data[ NODE_HEADER_SIZE - TAG_SIZE], TAG_SIZE ) ;
06625
06626 strncpy( (char *)node_header->name, &disk_node_data[ TAG_SIZE],
06627 ADF_NAME_LENGTH ) ;
06628 strncpy( (char *)node_header->label, &disk_node_data[ 36], ADF_LABEL_LENGTH ) ;
06629
06630 ADFI_ASCII_Hex_2_unsigned_int( 0, MAXIMUM_32_BITS, 8, &disk_node_data[ 68],
06631 &node_header->num_sub_nodes, error_return ) ;
06632 if( *error_return != NO_ERROR )
06633 return ;
06634
06635 ADFI_ASCII_Hex_2_unsigned_int( 0, MAXIMUM_32_BITS, 8, &disk_node_data[ 76],
06636 &node_header->entries_for_sub_nodes, error_return ) ;
06637 if( *error_return != NO_ERROR )
06638 return ;
06639
06640 ADFI_disk_pointer_from_ASCII_Hex( &disk_node_data[84], &disk_node_data[92],
06641 &node_header->sub_node_table, error_return ) ;
06642 if( *error_return != NO_ERROR )
06643 return ;
06644
06645 strncpy( (char *)node_header->data_type, &disk_node_data[ 96],
06646 ADF_DATA_TYPE_LENGTH ) ;
06647
06648 ADFI_ASCII_Hex_2_unsigned_int( 0, 12, 2, &disk_node_data[128],
06649 &node_header->number_of_dimensions, error_return ) ;
06650 if( *error_return != NO_ERROR )
06651 return ;
06652
06653 for( i=0; i<ADF_MAX_DIMENSIONS; i++ ) {
06654 ADFI_ASCII_Hex_2_unsigned_int( 0, MAXIMUM_32_BITS, 8,
06655 &disk_node_data[130+(i*8)], &node_header->dimension_values[i],
06656 error_return ) ;
06657 if( *error_return != NO_ERROR )
06658 return ;
06659 }
06660
06661 ADFI_ASCII_Hex_2_unsigned_int( 0, 65535, 4, &disk_node_data[226],
06662 &node_header->number_of_data_chunks, error_return ) ;
06663 if( *error_return != NO_ERROR )
06664 return ;
06665
06666 ADFI_disk_pointer_from_ASCII_Hex( &disk_node_data[230], &disk_node_data[238],
06667 &node_header->data_chunks, error_return ) ;
06668 if( *error_return != NO_ERROR )
06669 return ;
06670
06672 if( ADFI_stridx_c( node_header->node_start_tag, node_start_tag ) != 0 ) {
06673 *error_return = ADF_MEMORY_TAG_ERROR ;
06674 return ;
06675 }
06676
06677 if( ADFI_stridx_c( node_header->node_end_tag, node_end_tag ) != 0 ) {
06678 *error_return = ADF_MEMORY_TAG_ERROR ;
06679 return ;
06680 }
06681
06682 }
06683
06684
06685
06686
06687
06688
06689
06690
06691
06692
06693
06694
06695
06696
06697
06698
06699
06700 void ADFI_read_sub_node_table(
06701 const unsigned int file_index,
06702 const struct DISK_POINTER *block_offset,
06703 struct SUB_NODE_TABLE_ENTRY sub_node_table[],
06704 int *error_return )
06705 {
06706 char tag[TAG_SIZE + 1] ;
06707 struct DISK_POINTER end_of_chunk_tag, current_child ;
06708 unsigned int number_of_children, i ;
06709
06710 if( (block_offset == NULL) || (sub_node_table == NULL) ) {
06711 *error_return = NULL_POINTER ;
06712 return ;
06713 }
06714
06715 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
06716 *error_return = ADF_FILE_NOT_OPENED ;
06717 return ;
06718 }
06719
06720 *error_return = NO_ERROR ;
06721
06723 ADFI_read_chunk_length( file_index, block_offset, tag,
06724 &end_of_chunk_tag, error_return ) ;
06725 if( *error_return != NO_ERROR )
06726 return ;
06727 tag[TAG_SIZE] = '\0' ;
06728
06730 number_of_children = (
06731 (end_of_chunk_tag.block - block_offset->block) * DISK_BLOCK_SIZE +
06732 (end_of_chunk_tag.offset - block_offset->offset) ) /
06733 (DISK_POINTER_SIZE + ADF_NAME_LENGTH) ;
06734
06735 current_child.block = block_offset->block ;
06736 current_child.offset = block_offset->offset + TAG_SIZE + DISK_POINTER_SIZE ;
06737 ADFI_adjust_disk_pointer( ¤t_child, error_return ) ;
06738 if( *error_return != NO_ERROR )
06739 return ;
06740
06742 for( i=0; i<number_of_children; i++ ) {
06743 ADFI_adjust_disk_pointer( ¤t_child, error_return ) ;
06744 if( *error_return != NO_ERROR )
06745 return ;
06746
06747 ADFI_read_file( file_index, current_child.block, current_child.offset,
06748 ADF_NAME_LENGTH, sub_node_table[i].child_name, error_return ) ;
06749 if( *error_return != NO_ERROR )
06750 return ;
06751
06752 current_child.offset += ADF_NAME_LENGTH ;
06753 ADFI_adjust_disk_pointer( ¤t_child, error_return ) ;
06754 if( *error_return != NO_ERROR )
06755 return ;
06756
06757 ADFI_read_disk_pointer_from_disk( file_index, current_child.block,
06758 current_child.offset, &sub_node_table[i].child_location,
06759 error_return ) ;
06760 if( *error_return != NO_ERROR )
06761 return ;
06762
06763 current_child.offset += DISK_POINTER_SIZE ;
06764 }
06765
06766 }
06767
06768
06769
06770
06771
06772
06773
06774
06775
06776
06777
06778
06779
06780
06781
06782
06783
06784
06785 void ADFI_read_sub_node_table_entry(
06786 const unsigned int file_index,
06787 const struct DISK_POINTER *block_offset,
06788 struct SUB_NODE_TABLE_ENTRY *sub_node_table_entry,
06789 int *error_return )
06790 {
06791 char sub_node_entry_disk_data[ ADF_NAME_LENGTH + DISK_POINTER_SIZE ] ;
06792
06793 if( (block_offset == NULL) || (sub_node_table_entry == NULL) ) {
06794 *error_return = NULL_POINTER ;
06795 return ;
06796 }
06797
06798 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
06799 *error_return = ADF_FILE_NOT_OPENED ;
06800 return ;
06801 }
06802
06803 *error_return = NO_ERROR ;
06804
06806 if ( ADFI_stack_control(file_index, block_offset->block, block_offset->offset,
06807 GET_STK, SUBNODE_STK, ADF_NAME_LENGTH + DISK_POINTER_SIZE,
06808 sub_node_entry_disk_data ) != NO_ERROR ) {
06809
06811 ADFI_read_file( file_index, block_offset->block, block_offset->offset,
06812 ADF_NAME_LENGTH + DISK_POINTER_SIZE, sub_node_entry_disk_data,
06813 error_return ) ;
06814 if( *error_return != NO_ERROR )
06815 return ;
06816
06818 ADFI_stack_control(file_index, block_offset->block, block_offset->offset,
06819 SET_STK, SUBNODE_STK, ADF_NAME_LENGTH + DISK_POINTER_SIZE,
06820 sub_node_entry_disk_data );
06821 }
06822
06824 strncpy( sub_node_table_entry->child_name, &sub_node_entry_disk_data[0],
06825 ADF_NAME_LENGTH ) ;
06826
06828 ADFI_disk_pointer_from_ASCII_Hex( &sub_node_entry_disk_data[ ADF_NAME_LENGTH ],
06829 &sub_node_entry_disk_data[ ADF_NAME_LENGTH + 8 ],
06830 &sub_node_table_entry->child_location, error_return ) ;
06831 if( *error_return != NO_ERROR )
06832 return ;
06833 }
06834
06835
06836
06837
06838
06839
06840
06841
06842
06843
06844
06845
06846
06847
06848
06849 void ADFI_remember_file_format(
06850 const int file_index,
06851 const char numeric_format,
06852 const char os_size,
06853 int *error_return )
06854 {
06855 if( (file_index < 0) || (file_index >= maximum_files) ) {
06856 *error_return = FILE_INDEX_OUT_OF_RANGE ;
06857 return ;
06858 }
06859
06860 *error_return = NO_ERROR ;
06861 ADF_file[file_index].format = numeric_format ;
06862 ADF_file[file_index].os_size = os_size ;
06863 }
06864
06865
06866
06867
06868
06869
06870
06871
06872
06873
06874
06875
06876
06877
06878
06879
06880
06881 void ADFI_remember_version_update(
06882 const int file_index,
06883 const char *what_string,
06884 int *error_return )
06885 {
06886
06887 *error_return = NO_ERROR ;
06888
06889 if( (file_index < 0) || (file_index >= maximum_files) ) {
06890 *error_return = FILE_INDEX_OUT_OF_RANGE ;
06891 return ;
06892 }
06893
06894 if( what_string == NULL ) {
06895 *error_return = NULL_STRING_POINTER ;
06896 return;
06897 }
06898
06899 if( what_string[0] == '\0' ) {
06900 *error_return = STRING_LENGTH_ZERO ;
06901 return;
06902 }
06903
06904 if( strlen( what_string ) > WHAT_STRING_SIZE ) {
06905 *error_return = STRING_LENGTH_TOO_BIG ;
06906 return ;
06907 }
06908
06909 strcpy( ADF_file[file_index].version_update, what_string ) ;
06910
06911 }
06912
06913
06914
06915
06916
06917
06918
06919
06920
06921
06922
06923 void ADFI_set_blank_disk_pointer(
06924 struct DISK_POINTER *block_offset )
06925 {
06926 block_offset->block = BLANK_FILE_BLOCK ;
06927 block_offset->offset = BLANK_BLOCK_OFFSET ;
06928 }
06929
06930
06931
06932
06933
06934
06935
06936
06937
06938
06939
06940
06941
06942
06943
06944
06945
06946
06947
06948
06949
06950
06951
06952 int ADFI_stack_control( const unsigned int file_index,
06953 const unsigned long file_block,
06954 const unsigned long block_offset,
06955 const int stack_mode,
06956 const int stack_type,
06957 const unsigned long data_length,
06958 char *stack_data )
06959 {
06960 int i;
06961 int low_priority;
06962 int insert_index = 0;
06963 int found;
06964
06965 if( stack_data == NULL && (stack_mode == GET_STK || stack_mode == SET_STK) ) {
06966 return NULL_STRING_POINTER ;
06967 }
06968
06969 if( (file_index >= maximum_files || ADF_file[file_index].in_use == 0) &&
06970 stack_mode != INIT_STK ) {
06971 return ADF_FILE_NOT_OPENED ;
06972 }
06973
06974
06975
06976 switch( stack_mode ) {
06977 case INIT_STK:
06978 case CLEAR_STK:
06979 case CLEAR_STK_TYPE:
06980
06981
06982 for (i=0; i<MAX_STACK; i++) {
06983 if ( stack_mode == INIT_STK ) PRISTK[i].priority_level = -1;
06984 else if ( (int) file_index != PRISTK[i].file_index &&
06985 file_index != 0 ) continue;
06986 if ( stack_mode == CLEAR_STK_TYPE &&
06987 stack_type != PRISTK[i].stack_type ) continue ;
06988
06989 if ( PRISTK[i].priority_level > 0 ) free(PRISTK[i].stack_data);
06990 PRISTK[i].file_index = -1;
06991 PRISTK[i].file_block = 0;
06992 PRISTK[i].block_offset = 0;
06993 PRISTK[i].stack_type = -1;
06994 PRISTK[i].priority_level = -1;
06995 }
06996
06997 last_link_ID = 0.0;
06998 break ;
06999 case GET_STK:
07000
07001
07002
07003 for (i=0; i<MAX_STACK; i++) {
07004
07005 if ( PRISTK[i].file_index != (int) file_index ||
07006 PRISTK[i].file_block != file_block ||
07007 PRISTK[i].block_offset != block_offset ) continue;
07008 if ( PRISTK[i].stack_type == stack_type ) {
07009
07010 strncpy ( stack_data, PRISTK[i].stack_data, data_length );
07011
07012 PRISTK[i].priority_level = 1;
07013 return NO_ERROR;
07014 }
07015 else {
07016
07017 free(PRISTK[i].stack_data);
07018 PRISTK[i].file_index = -1;
07019 PRISTK[i].file_block = 0;
07020 PRISTK[i].block_offset = 0;
07021 PRISTK[i].stack_type = -1;
07022 PRISTK[i].priority_level = -1;
07023 }
07024 }
07025
07026 return PRISTK_NOT_FOUND;
07027 case DEL_STK_ENTRY:
07029 for (i=0; i<MAX_STACK; i++) {
07030 if ( PRISTK[i].file_index == (int) file_index &&
07031 PRISTK[i].file_block == file_block &&
07032 PRISTK[i].block_offset == block_offset ) {
07033 free(PRISTK[i].stack_data);
07034 PRISTK[i].file_index = -1;
07035 PRISTK[i].file_block = 0;
07036 PRISTK[i].block_offset = 0;
07037 PRISTK[i].stack_type = -1;
07038 PRISTK[i].priority_level = -1;
07039 return NO_ERROR ;
07040 }
07041 }
07042 break ;
07043 case SET_STK:
07046 found = 'f';
07047 low_priority = -1;
07048 for (i=0; i<MAX_STACK; i++) {
07049
07050 if ( PRISTK[i].file_index == (int) file_index &&
07051 PRISTK[i].file_block == file_block &&
07052 PRISTK[i].block_offset == block_offset ) {
07053 found = 't';
07054
07055 PRISTK[i].priority_level = 1;
07056
07057 strncpy( PRISTK[i].stack_data, stack_data, data_length );
07058 }
07059 else if ( PRISTK[i].stack_type >= 0 ) {
07060
07061
07062 if ( PRISTK[i].priority_level > low_priority ) {
07063 low_priority = PRISTK[i].priority_level;
07064 insert_index = i;
07065 }
07066 PRISTK[i].priority_level++;
07067 }
07068 else if ( found == 'f' ) {
07069
07070 low_priority = MAX_STACK * MAX_STACK;
07071 insert_index = i;
07072 found = 'e';
07073 }
07074 }
07075
07076 if ( found == 't' ) return NO_ERROR;
07077
07078 i = insert_index;
07079 if ( PRISTK[i].priority_level > 0 ) free(PRISTK[i].stack_data);
07080 PRISTK[i].stack_data = ( char * ) malloc(data_length*sizeof(char));
07081 if ( PRISTK[i].stack_data == NULL ) {
07082
07083 PRISTK[i].file_index = -1;
07084 PRISTK[i].file_block = 0;
07085 PRISTK[i].block_offset = 0;
07086 PRISTK[i].stack_type = -1;
07087 PRISTK[i].priority_level = -1;
07088 return NO_ERROR;
07089 }
07090 strncpy( PRISTK[i].stack_data, stack_data, data_length );
07091 PRISTK[i].file_index = file_index;
07092 PRISTK[i].file_block = file_block;
07093 PRISTK[i].block_offset = block_offset;
07094 PRISTK[i].stack_type = stack_type;
07095 PRISTK[i].priority_level = 1;
07096 break ;
07097 }
07098
07099 return NO_ERROR;
07100
07101 }
07102
07103
07104
07105
07106
07107
07108
07109
07110
07111
07112
07113
07114
07115
07116
07117
07118
07119 int ADFI_stridx_c(
07120 const char *str,
07121 const char *substr )
07122 {
07123 int i, j, k ;
07124
07125 if( str == NULL || substr == NULL || substr[0] == '\0' ) {
07126 return -1 ;
07127 }
07128
07129 for( i=0; str[i] != '\0'; i++ ) {
07130 for( j=i, k=0; TO_UPPER( str[j] ) == TO_UPPER( substr[k] ); j++ ) {
07131 if( substr[++k] == '\0' )
07132 return i ;
07133 }
07134 }
07135 return -1 ;
07136 }
07137
07138
07139
07140
07141
07142
07143
07144
07145
07146
07147
07148
07149
07150
07151
07152
07153 void ADFI_string_2_C_string(
07154 const char *string,
07155 const int string_length,
07156 char *c_string,
07157 int *error_return )
07158 {
07159 int i, iend ;
07160
07161 if( (string == NULL) || (c_string == NULL) ) {
07162 *error_return = NULL_STRING_POINTER ;
07163 return ;
07164 }
07165
07166 *error_return = NO_ERROR ;
07167
07169 for( iend=string_length-1; iend>=0; iend-- ) {
07170 if( string[ iend ] != ' ' ) {
07171 break ;
07172 }
07173 }
07174
07176 for( i=0; i<=iend; i++ )
07177 c_string[i] = string[i] ;
07178
07180 c_string[i] = '\0' ;
07181 }
07182
07183
07184
07185
07186
07187
07188
07189
07190
07191
07192
07193
07194
07195
07196
07197
07198
07199
07200
07201
07202
07203 void ADFI_unsigned_int_2_ASCII_Hex(
07204 const unsigned int number,
07205 const unsigned int minimum,
07206 const unsigned int maximum,
07207 const unsigned int string_length,
07208 char string[],
07209 int *error_return )
07210 {
07211 unsigned int i,
07212 ir,
07213 j,
07214 num ;
07216 if( string == NULL ) {
07217 *error_return = NULL_STRING_POINTER ;
07218 return ;
07219 }
07220
07221 if( number < minimum ) {
07222 *error_return = NUMBER_LESS_THAN_MINIMUM ;
07223 return ;
07224 }
07225
07226 if( number > maximum ) {
07227 *error_return = NUMBER_GREATER_THAN_MAXIMUM ;
07228 return ;
07229 }
07230
07231 if( string_length == 0 ) {
07232 *error_return = STRING_LENGTH_ZERO ;
07233 return ;
07234 }
07235
07236 if( string_length > 8 ) {
07237 *error_return = STRING_LENGTH_TOO_BIG ;
07238 return ;
07239 }
07240
07241 *error_return = NO_ERROR ;
07242
07244 num = number ;
07245 for( i=0, ir=string_length - 1; i<string_length; i++, ir-- ) {
07246 if( num >= pows[ ir ] ) {
07247 j = num / pows[ ir ] ;
07248 num = num - j * pows[ ir ] ;
07249 }
07250 else
07251 j = 0 ;
07252 string[i] = ASCII_Hex[ j ] ;
07253 }
07254 }
07255
07256
07257
07258
07259
07260
07261
07262
07263
07264
07265
07266
07267
07268
07269
07270
07271
07272
07273
07274
07275
07276 void ADFI_write_data_chunk(
07277 const unsigned int file_index,
07278 const struct DISK_POINTER *block_offset,
07279 const struct TOKENIZED_DATA_TYPE *tokenized_data_type,
07280 const int data_size,
07281 const long chunk_bytes,
07282 const long start_offset,
07283 const long total_bytes,
07284 const char *data,
07285 int *error_return )
07286 {
07287 int format_compare ;
07288 struct DISK_POINTER current_location, end_of_chunk_tag ;
07289
07290 if( block_offset == NULL ) {
07291 *error_return = NULL_POINTER ;
07292 return ;
07293 }
07294
07295 if( tokenized_data_type == NULL ) {
07296 *error_return = NULL_STRING_POINTER ;
07297 return ;
07298 }
07299
07300 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
07301 *error_return = ADF_FILE_NOT_OPENED ;
07302 return ;
07303 }
07304
07305 if( total_bytes+start_offset > chunk_bytes ) {
07306 *error_return = REQUESTED_DATA_TOO_LONG ;
07307 return ;
07308 }
07309
07310 *error_return = NO_ERROR ;
07311
07313 ADFI_write_file( file_index, block_offset->block, block_offset->offset,
07314 TAG_SIZE, data_chunk_start_tag, error_return ) ;
07315 if( *error_return != NO_ERROR )
07316 return ;
07317
07319 end_of_chunk_tag.block = block_offset->block ;
07320 end_of_chunk_tag.offset = block_offset->offset + TAG_SIZE +
07321 DISK_POINTER_SIZE + chunk_bytes ;
07322 ADFI_adjust_disk_pointer( &end_of_chunk_tag, error_return ) ;
07323 if( *error_return != NO_ERROR )
07324 return ;
07325
07327 current_location.block = block_offset->block ;
07328 current_location.offset = block_offset->offset + TAG_SIZE ;
07329 ADFI_adjust_disk_pointer( ¤t_location, error_return ) ;
07330 if( *error_return != NO_ERROR )
07331 return ;
07332
07333 ADFI_write_disk_pointer_2_disk( file_index, current_location.block,
07334 current_location.offset, &end_of_chunk_tag, error_return ) ;
07335
07336 current_location.offset += start_offset + DISK_POINTER_SIZE ;
07337 ADFI_adjust_disk_pointer( ¤t_location, error_return ) ;
07338 if( *error_return != NO_ERROR )
07339 return ;
07340
07342 if( data == NULL ) {
07347 if( block_of_00_initialized == FALSE ) {
07348 int i ;
07349 for( i=0; i<DISK_BLOCK_SIZE; i++ )
07350 block_of_00[ i ] = '\0' ;
07351 block_of_00_initialized = TRUE ;
07352 }
07353
07354 if( total_bytes > DISK_BLOCK_SIZE ) {
07355 long t_bytes = total_bytes ;
07356
07362 ADFI_write_file( file_index, current_location.block,
07363 current_location.offset, DISK_BLOCK_SIZE - current_location.offset + 1,
07364 block_of_00, error_return ) ;
07365 if( *error_return != NO_ERROR )
07366 return ;
07367
07368 current_location.block++ ;
07369 current_location.offset = 0 ;
07370 t_bytes -= (DISK_BLOCK_SIZE - current_location.offset + 1) ;
07371
07373 while( t_bytes > 0 ) {
07374 ADFI_write_file( file_index, current_location.block,
07375 current_location.offset, MIN( DISK_BLOCK_SIZE, t_bytes),
07376 block_of_00, error_return ) ;
07377 if( *error_return != NO_ERROR )
07378 return ;
07379 t_bytes -= (MIN( DISK_BLOCK_SIZE, t_bytes)) ;
07380 }
07381
07382 }
07383 else {
07384
07386 ADFI_write_file( file_index, current_location.block,
07387 current_location.offset, total_bytes, block_of_00, error_return ) ;
07388 if( *error_return != NO_ERROR )
07389 return ;
07390 }
07391 }
07392 else {
07393
07395 ADFI_file_and_machine_compare( file_index, tokenized_data_type,
07396 &format_compare, error_return );
07397 if( *error_return != NO_ERROR )
07398 return ;
07399 if( format_compare == 1 ) {
07401 ADFI_write_file( file_index, current_location.block,
07402 current_location.offset, total_bytes, data, error_return ) ;
07403 if( *error_return != NO_ERROR )
07404 return ;
07405 }
07406 else {
07407 ADFI_write_data_translated( file_index, current_location.block,
07408 current_location.offset, tokenized_data_type, data_size,
07409 total_bytes, data, error_return ) ;
07410 if( *error_return != NO_ERROR )
07411 return ;
07412 }
07413 }
07414
07416 ADFI_write_file( file_index, end_of_chunk_tag.block, end_of_chunk_tag.offset,
07417 TAG_SIZE, data_chunk_end_tag, error_return ) ;
07418 if( *error_return != NO_ERROR )
07419 return ;
07420
07421 }
07422
07423
07424
07425
07426
07427
07428
07429
07430
07431
07432
07433
07434
07435
07436
07437
07438 void ADFI_write_data_chunk_table(
07439 const unsigned int file_index,
07440 const struct DISK_POINTER *block_offset,
07441 const int number_of_data_chunks,
07442 struct DATA_CHUNK_TABLE_ENTRY data_chunk_table[],
07443 int *error_return )
07444 {
07445 struct DISK_POINTER disk_pointer, end_of_chunk_tag ;
07446 int i ;
07447
07448 if( (block_offset == NULL) || (data_chunk_table == NULL) ) {
07449 *error_return = NULL_POINTER ;
07450 return ;
07451 }
07452
07453 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
07454 *error_return = ADF_FILE_NOT_OPENED ;
07455 return ;
07456 }
07457
07458 *error_return = NO_ERROR ;
07459
07461 disk_pointer.block = block_offset->block ;
07462 disk_pointer.offset = block_offset->offset ;
07463 ADFI_write_file( file_index, disk_pointer.block, disk_pointer.offset,
07464 TAG_SIZE, data_chunk_table_start_tag, error_return ) ;
07465 if( *error_return != NO_ERROR )
07466 return ;
07467
07468 disk_pointer.offset += TAG_SIZE ;
07469 ADFI_adjust_disk_pointer( &disk_pointer, error_return ) ;
07470 if( *error_return != NO_ERROR )
07471 return ;
07472
07474 end_of_chunk_tag.block = disk_pointer.block ;
07475 end_of_chunk_tag.offset = disk_pointer.offset + DISK_POINTER_SIZE +
07476 number_of_data_chunks * 2 * DISK_POINTER_SIZE ;
07477 ADFI_adjust_disk_pointer( &end_of_chunk_tag, error_return ) ;
07478 if( *error_return != NO_ERROR )
07479 return ;
07480 ADFI_write_disk_pointer_2_disk( file_index, disk_pointer.block,
07481 disk_pointer.offset, &end_of_chunk_tag, error_return ) ;
07482 if( *error_return != NO_ERROR )
07483 return ;
07484
07486 disk_pointer.offset += DISK_POINTER_SIZE ;
07487 for( i=0; i<number_of_data_chunks; i++ ) {
07488 ADFI_adjust_disk_pointer( &disk_pointer, error_return ) ;
07489 if( *error_return != NO_ERROR )
07490 return ;
07491 ADFI_write_disk_pointer_2_disk( file_index, disk_pointer.block,
07492 disk_pointer.offset, &data_chunk_table[i].start, error_return ) ;
07493 if( *error_return != NO_ERROR )
07494 return ;
07495 disk_pointer.offset += DISK_POINTER_SIZE ;
07496 ADFI_adjust_disk_pointer( &disk_pointer, error_return ) ;
07497 ADFI_write_disk_pointer_2_disk( file_index, disk_pointer.block,
07498 disk_pointer.offset, &data_chunk_table[i].end, error_return ) ;
07499 if( *error_return != NO_ERROR )
07500 return ;
07501 disk_pointer.offset += DISK_POINTER_SIZE ;
07502 }
07503
07505 ADFI_write_file( file_index, end_of_chunk_tag.block, end_of_chunk_tag.offset,
07506 TAG_SIZE, data_chunk_table_end_tag, error_return ) ;
07507 if( *error_return != NO_ERROR )
07508 return ;
07509
07510 }
07511
07512
07513
07514
07515
07516
07517
07518
07519
07520
07521
07522
07523
07524
07525
07526
07527
07528 void ADFI_write_data_translated(
07529 const unsigned int file_index,
07530 const unsigned long file_block,
07531 const unsigned long block_offset,
07532 const struct TOKENIZED_DATA_TYPE *tokenized_data_type,
07533 const int data_size,
07534 const long total_bytes,
07535 const char *data,
07536 int *error_return )
07537 {
07538 struct DISK_POINTER disk_pointer ;
07539 int current_token = -1 ;
07540 int machine_size ;
07541 unsigned char *from_data = (unsigned char *)data ;
07542 unsigned char *to_data = from_to_data ;
07543 unsigned long chunk_size ;
07544 unsigned long number_of_data_elements, number_of_elements_written ;
07545 unsigned long delta_from_bytes, delta_to_bytes ;
07546
07547 if( data_size <= 0 ) {
07548 *error_return = ZERO_LENGTH_VALUE ;
07549 return ;
07550 }
07551
07553 do {
07554 machine_size = tokenized_data_type[ ++current_token ].machine_type_size ;
07555 } while( tokenized_data_type[ current_token ].type[0] != 0 ) ;
07556
07557 disk_pointer.block = file_block ;
07558 disk_pointer.offset = block_offset ;
07559 number_of_data_elements = total_bytes / data_size ;
07560 number_of_elements_written = 0 ;
07561 chunk_size = CONVERSION_BUFF_SIZE / data_size ;
07562 if ( chunk_size < 1 ) {
07563 *error_return = REQUESTED_DATA_TOO_LONG ;
07564 return ;
07565 }
07566 delta_to_bytes = chunk_size * data_size ;
07567 delta_from_bytes = chunk_size * machine_size ;
07568
07569 while( number_of_elements_written < number_of_data_elements ) {
07571 number_of_elements_written += chunk_size ;
07572 if ( number_of_elements_written > number_of_data_elements ) {
07573 chunk_size -= ( number_of_elements_written - number_of_data_elements ) ;
07574 delta_to_bytes = chunk_size * data_size ;
07575 delta_from_bytes = chunk_size * machine_size ;
07576 }
07577 ADFI_convert_number_format(
07578 ADF_this_machine_format,
07579 ADF_this_machine_os_size,
07580 ADF_file[file_index].format,
07581 ADF_file[file_index].os_size,
07582 TO_FILE_FORMAT,
07583 tokenized_data_type, chunk_size, from_data,
07584 to_data, error_return ) ;
07585 if( *error_return != NO_ERROR )
07586 return ;
07587 ADFI_write_file( file_index, disk_pointer.block, disk_pointer.offset,
07588 delta_to_bytes, (char *)to_data, error_return ) ;
07589 if( *error_return != NO_ERROR )
07590 return ;
07591 from_data += delta_from_bytes ;
07592 disk_pointer.offset += delta_to_bytes ;
07593 if ( disk_pointer.offset > DISK_BLOCK_SIZE ) {
07594 ADFI_adjust_disk_pointer( &disk_pointer, error_return ) ;
07595 if( *error_return != NO_ERROR )
07596 return ;
07597 }
07598 }
07599
07600 }
07601
07602
07603
07604
07605
07606 void ADFI_write_disk_block()
07607 {
07608 fprintf(stderr,"Subroutine ADFI_write_disk_block is not yet implemented...\n" ) ;
07609 }
07610
07611
07612
07613
07614
07615
07616
07617
07618
07619
07620
07621
07622
07623
07624
07625
07626
07627
07628 void ADFI_write_disk_pointer_2_disk(
07629 const unsigned int file_index,
07630 const unsigned long file_block,
07631 const unsigned long block_offset,
07632 const struct DISK_POINTER *block_and_offset,
07633 int *error_return )
07634 {
07635 char disk_block_offset[DISK_POINTER_SIZE] ;
07636
07637 if( block_and_offset == NULL ) {
07638 *error_return = NULL_POINTER ;
07639 return ;
07640 }
07641
07642 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
07643 *error_return = ADF_FILE_NOT_OPENED ;
07644 return ;
07645 }
07646
07647 *error_return = NO_ERROR ;
07648
07650 ADFI_disk_pointer_2_ASCII_Hex( block_and_offset, &disk_block_offset[0],
07651 &disk_block_offset[8], error_return ) ;
07652 if( *error_return != NO_ERROR )
07653 return ;
07654
07656 ADFI_write_file( file_index, file_block, block_offset,
07657 DISK_POINTER_SIZE, disk_block_offset, error_return ) ;
07658 if( *error_return != NO_ERROR )
07659 return ;
07660
07662 #if 0
07663 ADFI_stack_control(file_index, file_block, block_offset,
07664 SET_STK, DISK_PTR_STK, DISK_POINTER_SIZE,
07665 disk_block_offset );
07666 #endif
07667
07668 }
07669
07670
07671
07672
07673 #ifndef USE_STREAM_IO
07674
07675 int ADFI_write (
07676 const unsigned int file_index,
07677 const unsigned int data_length,
07678 const char *data)
07679 {
07680 char *data_ptr = (char *)data;
07681 unsigned bytes_left = data_length;
07682 int nbytes, bytes_out = 0;
07683
07684 ADF_sys_err = 0;
07685 while (bytes_left > 0) {
07686 nbytes = write (ADF_file[file_index].file, data_ptr, bytes_left);
07687 if (-1 == nbytes) {
07688 if (EINTR != errno) {
07689 ADF_sys_err = errno;
07690 return -1;
07691 }
07692 }
07693 else {
07694 bytes_left -= nbytes;
07695 bytes_out += nbytes;
07696 data_ptr += nbytes;
07697 }
07698 }
07699 return bytes_out;
07700 }
07701
07702 #endif
07703
07704
07705
07706
07707
07708
07709
07710
07711
07712
07713
07714
07715
07716
07717
07718
07719
07720
07721
07722
07723 void ADFI_write_file(
07724 const unsigned int file_index,
07725 const unsigned long file_block,
07726 const unsigned long block_offset,
07727 const unsigned int data_length,
07728 const char *data,
07729 int *error_return )
07730 {
07731 int iret, end_block ;
07732
07733 if( data == NULL ) {
07734 *error_return = NULL_STRING_POINTER ;
07735 return ;
07736 }
07737
07738 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
07739 *error_return = ADF_FILE_NOT_OPENED ;
07740 return ;
07741 }
07742
07743 *error_return = NO_ERROR ;
07744
07748 end_block = file_block+(block_offset+data_length)/DISK_BLOCK_SIZE+1;
07749 if ( last_rd_file == (long int) file_index && last_rd_block >= (long int) file_block &&
07750 last_rd_block <= (long int) end_block )
07751 last_rd_block = last_rd_file = num_in_rd_block = -1 ;
07752
07759 if( ( (unsigned long int) data_length + block_offset > DISK_BLOCK_SIZE ||
07760 last_wr_block != (long int) file_block || last_wr_file != (long int) file_index ||
07761 data_length == 0 ) && flush_wr_block > 0 ) {
07762
07764 ADFI_fseek_file( last_wr_file, last_wr_block, 0, error_return ) ;
07765 if( *error_return != NO_ERROR ) {
07766 return ;
07767 }
07768
07770 iret= ADFI_write( last_wr_file, DISK_BLOCK_SIZE, wr_block_buffer );
07771 flush_wr_block = -2 ;
07772 if( iret != DISK_BLOCK_SIZE ) {
07773 *error_return = FWRITE_ERROR ;
07774 return ;
07775 }
07776
07779 if ( last_wr_file == (long int) file_index && last_wr_block >= (long int) file_block &&
07780 last_wr_block <= (long int) end_block )
07781 last_wr_block = last_wr_file = -2 ;
07782
07783 }
07784 if ( data_length == 0 ) return;
07789 if( data_length + block_offset > DISK_BLOCK_SIZE ) {
07790
07792 ADFI_fseek_file( file_index, file_block, block_offset, error_return ) ;
07793 if( *error_return != NO_ERROR ) {
07794 return ;
07795 }
07796
07798 iret = ADFI_write( file_index, data_length, data ) ;
07799 if( iret != (int) data_length ) {
07800 *error_return = FWRITE_ERROR ;
07801 return ;
07802 }
07803
07804 return;
07805 }
07806
07818 if( (long int) file_block != last_wr_block ||
07819 (long int) file_index != last_wr_file ) {
07820
07823 if ( (long int) file_block == last_rd_block && (long int) file_index == last_rd_file ) {
07824
07825
07826 memcpy( wr_block_buffer, rd_block_buffer, DISK_BLOCK_SIZE );
07827 iret = num_in_rd_block;
07828 }
07829 else {
07830
07832 ADFI_fseek_file( file_index, file_block, 0, error_return ) ;
07833 if( *error_return != NO_ERROR ) {
07834 return ;
07835 }
07836
07838 iret = ADFI_read( file_index, DISK_BLOCK_SIZE, wr_block_buffer ) ;
07839 if( iret < DISK_BLOCK_SIZE ) {
07840 if ( iret < 0 ) iret = 0;
07841 memset( &wr_block_buffer[iret], (size_t) ' ', DISK_BLOCK_SIZE-iret );
07842 }
07843
07844 }
07845
07847 last_wr_block = file_block ;
07848 last_wr_file = file_index ;
07849
07850 }
07851
07853 memcpy( &wr_block_buffer[block_offset], data, data_length );
07854 flush_wr_block = 1 ;
07855
07856 }
07857
07858
07859
07860
07861
07862
07863
07864
07865
07866
07867
07868
07869
07870
07871
07872
07873
07874 void ADFI_write_file_header(
07875 const int file_index,
07876 const struct FILE_HEADER *file_header,
07877 int *error_return )
07878 {
07879 char disk_header[ FILE_HEADER_SIZE ] ;
07880
07881 if( file_header == NULL ) {
07882 *error_return = NULL_POINTER ;
07883 return ;
07884 }
07885
07886 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
07887 *error_return = ADF_FILE_NOT_OPENED ;
07888 return ;
07889 }
07890
07891 *error_return = NO_ERROR ;
07892
07894 if( strncmp( file_header->tag0, file_header_tags[0], TAG_SIZE )!= 0 ) {
07895 *error_return = ADF_MEMORY_TAG_ERROR ;
07896 return ;
07897 }
07898
07899 if( strncmp( file_header->tag1, file_header_tags[1], TAG_SIZE )!= 0 ) {
07900 *error_return = ADF_MEMORY_TAG_ERROR ;
07901 return ;
07902 }
07903
07904 if( strncmp( file_header->tag2, file_header_tags[2], TAG_SIZE )!= 0 ) {
07905 *error_return = ADF_MEMORY_TAG_ERROR ;
07906 return ;
07907 }
07908
07909 if( strncmp( file_header->tag3, file_header_tags[3], TAG_SIZE )!= 0 ) {
07910 *error_return = ADF_MEMORY_TAG_ERROR ;
07911 return ;
07912 }
07913
07914 if( strncmp( file_header->tag4, file_header_tags[4], TAG_SIZE )!= 0 ) {
07915 *error_return = ADF_MEMORY_TAG_ERROR ;
07916 return ;
07917 }
07918
07919 if( strncmp( file_header->tag5, file_header_tags[5], TAG_SIZE )!= 0 ) {
07920 *error_return = ADF_MEMORY_TAG_ERROR ;
07921 return ;
07922 }
07923
07927 strncpy( &disk_header[ 0], (char *)file_header->what, WHAT_STRING_SIZE ) ;
07928 strncpy( &disk_header[ 32], (char *)file_header->tag0, TAG_SIZE ) ;
07929 strncpy( &disk_header[ 36], (char *)file_header->creation_date, DATE_TIME_SIZE);
07930 strncpy( &disk_header[ 64], (char *)file_header->tag1, TAG_SIZE ) ;
07931 strncpy( &disk_header[ 68], (char *)file_header->modification_date,
07932 DATE_TIME_SIZE ) ;
07933 strncpy( &disk_header[ 96], (char *)file_header->tag2, TAG_SIZE ) ;
07934 disk_header[100] = file_header->numeric_format ;
07935 disk_header[101] = file_header->os_size ;
07936 strncpy( &disk_header[102], (char *)file_header->tag3, TAG_SIZE ) ;
07937
07938 ADFI_unsigned_int_2_ASCII_Hex( file_header->sizeof_char, 0, 255, 2,
07939 &disk_header[106], error_return ) ;
07940 if( *error_return != NO_ERROR )
07941 return ;
07942 ADFI_unsigned_int_2_ASCII_Hex( file_header->sizeof_short, 0, 255, 2,
07943 &disk_header[108], error_return ) ;
07944 if( *error_return != NO_ERROR )
07945 return ;
07946 ADFI_unsigned_int_2_ASCII_Hex( file_header->sizeof_int, 0, 255, 2,
07947 &disk_header[110], error_return ) ;
07948 if( *error_return != NO_ERROR )
07949 return ;
07950 ADFI_unsigned_int_2_ASCII_Hex( file_header->sizeof_long, 0, 255, 2,
07951 &disk_header[112], error_return ) ;
07952 if( *error_return != NO_ERROR )
07953 return ;
07954 ADFI_unsigned_int_2_ASCII_Hex( file_header->sizeof_float, 0, 255, 2,
07955 &disk_header[114], error_return ) ;
07956 if( *error_return != NO_ERROR )
07957 return ;
07958 ADFI_unsigned_int_2_ASCII_Hex( file_header->sizeof_double, 0, 255, 2,
07959 &disk_header[116], error_return ) ;
07960 if( *error_return != NO_ERROR )
07961 return ;
07962 ADFI_unsigned_int_2_ASCII_Hex( file_header->sizeof_char_p, 0, 255, 2,
07963 &disk_header[118], error_return ) ;
07964 if( *error_return != NO_ERROR )
07965 return ;
07966 ADFI_unsigned_int_2_ASCII_Hex( file_header->sizeof_short_p, 0, 255, 2,
07967 &disk_header[120], error_return ) ;
07968 if( *error_return != NO_ERROR )
07969 return ;
07970 ADFI_unsigned_int_2_ASCII_Hex( file_header->sizeof_int_p, 0, 255, 2,
07971 &disk_header[122], error_return ) ;
07972 if( *error_return != NO_ERROR )
07973 return ;
07974 ADFI_unsigned_int_2_ASCII_Hex( file_header->sizeof_long_p, 0, 255, 2,
07975 &disk_header[124], error_return ) ;
07976 if( *error_return != NO_ERROR )
07977 return ;
07978 ADFI_unsigned_int_2_ASCII_Hex( file_header->sizeof_float_p, 0, 255, 2,
07979 &disk_header[126], error_return ) ;
07980 if( *error_return != NO_ERROR )
07981 return ;
07982 ADFI_unsigned_int_2_ASCII_Hex( file_header->sizeof_double_p, 0, 255, 2,
07983 &disk_header[128], error_return ) ;
07984 if( *error_return != NO_ERROR )
07985 return ;
07986
07987 strncpy( &disk_header[130], file_header->tag4, TAG_SIZE ) ;
07988
07989 ADFI_disk_pointer_2_ASCII_Hex( &file_header->root_node, &disk_header[134],
07990 &disk_header[142], error_return ) ;
07991 if( *error_return != NO_ERROR )
07992 return ;
07993
07994 ADFI_disk_pointer_2_ASCII_Hex( &file_header->end_of_file, &disk_header[146],
07995 &disk_header[154], error_return ) ;
07996 if( *error_return != NO_ERROR )
07997 return ;
07998
07999 ADFI_disk_pointer_2_ASCII_Hex( &file_header->free_chunks, &disk_header[158],
08000 &disk_header[166], error_return ) ;
08001 if( *error_return != NO_ERROR )
08002 return ;
08003
08004 ADFI_disk_pointer_2_ASCII_Hex( &file_header->extra, &disk_header[170],
08005 &disk_header[178], error_return ) ;
08006 if( *error_return != NO_ERROR )
08007 return ;
08008
08009 strncpy( &disk_header[182], file_header->tag5, TAG_SIZE ) ;
08010
08012 ADFI_write_file( file_index, 0, 0, FILE_HEADER_SIZE, disk_header,
08013 error_return ) ;
08015 ADFI_stack_control(file_index, 0, 0, SET_STK, FILE_STK,
08016 FILE_HEADER_SIZE, disk_header );
08017 }
08018
08019
08020
08021
08022
08023
08024
08025
08026
08027
08028
08029
08030
08031
08032
08033
08034 void ADFI_write_free_chunk(
08035 const int file_index,
08036 const struct DISK_POINTER *block_offset,
08037 const struct FREE_CHUNK *free_chunk,
08038 int *error_return )
08039 {
08040 unsigned int i ;
08041 struct DISK_POINTER current_location ;
08042
08043 if( (block_offset == NULL) || (free_chunk == NULL) ) {
08044 *error_return = NULL_POINTER ;
08045 return ;
08046 }
08047
08048 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
08049 *error_return = ADF_FILE_NOT_OPENED ;
08050 return ;
08051 }
08052
08053 *error_return = NO_ERROR ;
08054
08056 if( block_of_XX_initialized == FALSE ) {
08057 for( i=0; i<DISK_BLOCK_SIZE; i++ )
08058 block_of_XX[ i ] = 'x' ;
08059 block_of_XX_initialized = TRUE ;
08060 }
08061
08063 if( strncmp( free_chunk->start_tag, free_chunk_start_tag, TAG_SIZE )!= 0 ) {
08064 *error_return = ADF_MEMORY_TAG_ERROR ;
08065 return ;
08066 }
08067
08068 if( strncmp( free_chunk->end_tag, free_chunk_end_tag, TAG_SIZE )!= 0 ) {
08069 *error_return = ADF_MEMORY_TAG_ERROR ;
08070 return ;
08071 }
08072
08074 ADFI_write_file( file_index, block_offset->block, block_offset->offset,
08075 TAG_SIZE, free_chunk->start_tag, error_return ) ;
08076 if( *error_return != NO_ERROR )
08077 return ;
08078
08080 current_location.block = block_offset->block ;
08081 current_location.offset = block_offset->offset + TAG_SIZE ;
08082 ADFI_adjust_disk_pointer( ¤t_location, error_return ) ;
08083 if( *error_return != NO_ERROR )
08084 return ;
08085
08086 ADFI_write_disk_pointer_2_disk( file_index, current_location.block,
08087 current_location.offset,
08088 &free_chunk->end_of_chunk_tag,
08089 error_return ) ;
08090 if( *error_return != NO_ERROR )
08091 return ;
08092
08093 current_location.offset += DISK_POINTER_SIZE ;
08094 ADFI_adjust_disk_pointer( ¤t_location, error_return ) ;
08095 if( *error_return != NO_ERROR )
08096 return ;
08097
08098 ADFI_write_disk_pointer_2_disk( file_index, current_location.block,
08099 current_location.offset,
08100 &free_chunk->next_chunk,
08101 error_return ) ;
08102 if( *error_return != NO_ERROR )
08103 return ;
08104
08106 current_location.offset += DISK_POINTER_SIZE ;
08107 ADFI_adjust_disk_pointer( ¤t_location, error_return ) ;
08108 if( *error_return != NO_ERROR )
08109 return ;
08110
08112 if( (current_location.block != free_chunk->end_of_chunk_tag.block) &&
08113 (current_location.offset != 0 ) ) {
08114 ADFI_write_file( file_index, current_location.block,
08115 current_location.offset, DISK_BLOCK_SIZE - current_location.offset,
08116 block_of_XX, error_return ) ;
08117 if( *error_return != NO_ERROR )
08118 return ;
08119 current_location.block++ ;
08120 current_location.offset = 0 ;
08121 }
08122
08124 while( current_location.block < free_chunk->end_of_chunk_tag.block ) {
08125 ADFI_write_file( file_index, current_location.block,
08126 0, DISK_BLOCK_SIZE, block_of_XX, error_return ) ;
08127 if( *error_return != NO_ERROR )
08128 return ;
08129 current_location.block++ ;
08130 }
08131
08133 if( current_location.offset < free_chunk->end_of_chunk_tag.offset ) {
08134 ADFI_write_file( file_index, current_location.block,
08135 current_location.offset,
08136 free_chunk->end_of_chunk_tag.offset - current_location.offset,
08137 block_of_XX, error_return ) ;
08138 if( *error_return != NO_ERROR )
08139 return ;
08140 }
08141
08143 ADFI_write_file( file_index, current_location.block,
08144 free_chunk->end_of_chunk_tag.offset, TAG_SIZE, free_chunk->end_tag,
08145 error_return ) ;
08146 if( *error_return != NO_ERROR )
08147 return ;
08148
08149 }
08150
08151
08152
08153
08154
08155
08156
08157
08158
08159
08160
08161
08162
08163
08164
08165
08166 void ADFI_write_free_chunk_table(
08167 const int file_index,
08168 const struct FREE_CHUNK_TABLE *free_chunk_table,
08169 int *error_return )
08170 {
08171 char disk_free_chunk_data[ FREE_CHUNK_TABLE_SIZE ] ;
08172
08173 if( free_chunk_table == NULL ) {
08174 *error_return = NULL_POINTER ;
08175 return ;
08176 }
08177
08178 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
08179 *error_return = ADF_FILE_NOT_OPENED ;
08180 return ;
08181 }
08182
08183 *error_return = NO_ERROR ;
08184
08186 if( strncmp( free_chunk_table->start_tag, free_chunk_table_start_tag,
08187 TAG_SIZE ) != 0 ) {
08188 *error_return = ADF_MEMORY_TAG_ERROR ;
08189 return ;
08190 }
08191
08192 if( strncmp( free_chunk_table->end_tag, free_chunk_table_end_tag,
08193 TAG_SIZE ) != 0 ) {
08194 *error_return = ADF_MEMORY_TAG_ERROR ;
08195 return ;
08196 }
08197
08201 strncpy( &disk_free_chunk_data[ 0], (char *)free_chunk_table->start_tag,
08202 TAG_SIZE ) ;
08203
08204 ADFI_disk_pointer_2_ASCII_Hex( &free_chunk_table->small_first_block,
08205 &disk_free_chunk_data[TAG_SIZE],
08206 &disk_free_chunk_data[DISK_POINTER_SIZE], error_return ) ;
08207 if( *error_return != NO_ERROR )
08208 return ;
08209
08210 ADFI_disk_pointer_2_ASCII_Hex( &free_chunk_table->small_last_block,
08211 &disk_free_chunk_data[16], &disk_free_chunk_data[24], error_return ) ;
08212 if( *error_return != NO_ERROR )
08213 return ;
08214
08215 ADFI_disk_pointer_2_ASCII_Hex( &free_chunk_table->medium_first_block,
08216 &disk_free_chunk_data[28], &disk_free_chunk_data[36], error_return ) ;
08217 if( *error_return != NO_ERROR )
08218 return ;
08219
08220 ADFI_disk_pointer_2_ASCII_Hex( &free_chunk_table->medium_last_block,
08221 &disk_free_chunk_data[40], &disk_free_chunk_data[48], error_return ) ;
08222 if( *error_return != NO_ERROR )
08223 return ;
08224
08225 ADFI_disk_pointer_2_ASCII_Hex( &free_chunk_table->large_first_block,
08226 &disk_free_chunk_data[52], &disk_free_chunk_data[60], error_return ) ;
08227 if( *error_return != NO_ERROR )
08228 return ;
08229
08230 ADFI_disk_pointer_2_ASCII_Hex( &free_chunk_table->large_last_block,
08231 &disk_free_chunk_data[64], &disk_free_chunk_data[72], error_return ) ;
08232 if( *error_return != NO_ERROR )
08233 return ;
08234
08235 strncpy( &disk_free_chunk_data[ 76], (char *)free_chunk_table->end_tag,
08236 TAG_SIZE ) ;
08237
08239 ADFI_write_file( file_index, FREE_CHUNKS_BLOCK, FREE_CHUNKS_OFFSET,
08240 FREE_CHUNK_TABLE_SIZE, disk_free_chunk_data, error_return ) ;
08242 ADFI_stack_control(file_index, FREE_CHUNKS_BLOCK, FREE_CHUNKS_OFFSET,
08243 SET_STK, FREE_CHUNK_STK, FREE_CHUNK_TABLE_SIZE,
08244 disk_free_chunk_data );
08245 }
08246
08247
08248
08249
08250
08251
08252
08253
08254
08255
08256
08257
08258
08259
08260
08261
08262
08263
08264
08265 void ADFI_write_modification_date(
08266 const int file_index,
08267 int *error_return )
08268 {
08269 int i_block_offset ;
08270 char mod_date[DATE_TIME_SIZE] ;
08271
08272
08273 *error_return = NO_ERROR ;
08274
08275 ADFI_get_current_date( mod_date ) ;
08276
08279 i_block_offset = WHAT_STRING_SIZE + TAG_SIZE + DATE_TIME_SIZE + TAG_SIZE ;
08280 ADFI_write_file( file_index, 0, i_block_offset, DATE_TIME_SIZE, mod_date,
08281 error_return ) ;
08282 if( *error_return != NO_ERROR ) {
08283 return;
08284 }
08285
08287 ADFI_flush_buffers( file_index, FLUSH, error_return );
08288 if( *error_return != NO_ERROR ) {
08289 return;
08290 }
08291
08292 if( ADF_file[file_index].version_update[0] != '\0' )
08293 {
08294 i_block_offset = 0 ;
08295 ADFI_write_file( file_index, 0, i_block_offset, WHAT_STRING_SIZE,
08296 ADF_file[file_index].version_update, error_return ) ;
08297
08299 ADF_file[file_index].version_update[0] = '\0' ;
08300 if( *error_return != NO_ERROR ) {
08301 return;
08302 }
08303 }
08304
08305 }
08306
08307
08308
08309
08310
08311
08312
08313
08314
08315
08316
08317
08318
08319
08320
08321
08322
08323 void ADFI_write_node_header(
08324 const int file_index,
08325 const struct DISK_POINTER *block_offset,
08326 const struct NODE_HEADER *node_header,
08327 int *error_return )
08328 {
08329 int i ;
08330 char disk_node_data[ NODE_HEADER_SIZE ] ;
08331
08332 if( (block_offset == NULL) || (node_header == NULL) ) {
08333 *error_return = NULL_POINTER ;
08334 return ;
08335 }
08336
08337 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
08338 *error_return = ADF_FILE_NOT_OPENED ;
08339 return ;
08340 }
08341
08342 *error_return = NO_ERROR ;
08343
08345 if( strncmp( node_header->node_start_tag, node_start_tag, TAG_SIZE )!= 0 ) {
08346 *error_return = ADF_MEMORY_TAG_ERROR ;
08347 return ;
08348 }
08349
08350 if( strncmp( node_header->node_end_tag, node_end_tag, TAG_SIZE )!= 0 ) {
08351 *error_return = ADF_MEMORY_TAG_ERROR ;
08352 return ;
08353 }
08354
08358 strncpy( &disk_node_data[ 0], (char *)node_header->node_start_tag, TAG_SIZE ) ;
08359 strncpy( &disk_node_data[ TAG_SIZE], (char *)node_header->name,
08360 ADF_NAME_LENGTH );
08361 strncpy( &disk_node_data[ 36], (char *)node_header->label, ADF_LABEL_LENGTH ) ;
08362
08363 ADFI_unsigned_int_2_ASCII_Hex( node_header->num_sub_nodes, 0,
08364 MAXIMUM_32_BITS, 8, &disk_node_data[ 68], error_return ) ;
08365 if( *error_return != NO_ERROR )
08366 return ;
08367
08368 ADFI_unsigned_int_2_ASCII_Hex( node_header->entries_for_sub_nodes, 0,
08369 MAXIMUM_32_BITS, 8, &disk_node_data[ 76], error_return ) ;
08370 if( *error_return != NO_ERROR )
08371 return ;
08372
08373 ADFI_disk_pointer_2_ASCII_Hex( &node_header->sub_node_table,
08374 &disk_node_data[84], &disk_node_data[92], error_return ) ;
08375 if( *error_return != NO_ERROR )
08376 return ;
08377
08378 strncpy( &disk_node_data[ 96], (char *)node_header->data_type,
08379 ADF_DATA_TYPE_LENGTH ) ;
08380
08381 ADFI_unsigned_int_2_ASCII_Hex( node_header->number_of_dimensions, 0,
08382 12, 2, &disk_node_data[128], error_return ) ;
08383 if( *error_return != NO_ERROR )
08384 return ;
08385
08386 for( i=0; i<ADF_MAX_DIMENSIONS; i++ ) {
08387 ADFI_unsigned_int_2_ASCII_Hex( node_header->dimension_values[i], 0,
08388 MAXIMUM_32_BITS, 8, &disk_node_data[130+(i*8)], error_return ) ;
08389 if( *error_return != NO_ERROR )
08390 return ;
08391 }
08392
08393 ADFI_unsigned_int_2_ASCII_Hex( node_header->number_of_data_chunks, 0,
08394 65535, 4, &disk_node_data[226], error_return ) ;
08395 if( *error_return != NO_ERROR )
08396 return ;
08397
08398 ADFI_disk_pointer_2_ASCII_Hex( &node_header->data_chunks,
08399 &disk_node_data[230], &disk_node_data[238], error_return ) ;
08400 if( *error_return != NO_ERROR )
08401 return ;
08402
08403 strncpy( &disk_node_data[242], (char *)node_header->node_end_tag, TAG_SIZE ) ;
08404
08406 ADFI_write_file( file_index, block_offset->block, block_offset->offset,
08407 NODE_HEADER_SIZE, disk_node_data, error_return ) ;
08409 ADFI_stack_control(file_index, block_offset->block, block_offset->offset,
08410 SET_STK, NODE_STK, NODE_HEADER_SIZE, disk_node_data );
08411 }
08412
08413
08414
08415
08416
08417
08418
08419
08420
08421
08422
08423
08424
08425
08426
08427
08428 void ADFI_write_sub_node_table(
08429 const unsigned int file_index,
08430 const struct DISK_POINTER *block_offset,
08431 const int number_of_sub_nodes,
08432 struct SUB_NODE_TABLE_ENTRY sub_node_table[],
08433 int *error_return )
08434 {
08435 int i ;
08436 struct DISK_POINTER end_of_chunk_tag, current_child ;
08437
08438 if( (block_offset == NULL) || (sub_node_table == NULL) ) {
08439 *error_return = NULL_POINTER ;
08440 return ;
08441 }
08442
08443 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
08444 *error_return = ADF_FILE_NOT_OPENED ;
08445 return ;
08446 }
08447
08448 *error_return = NO_ERROR ;
08449
08451 end_of_chunk_tag.block = block_offset->block ;
08452 end_of_chunk_tag.offset = block_offset->offset + TAG_SIZE + DISK_POINTER_SIZE +
08453 number_of_sub_nodes * (ADF_NAME_LENGTH + DISK_POINTER_SIZE) ;
08454 ADFI_adjust_disk_pointer( &end_of_chunk_tag, error_return ) ;
08455 if( *error_return != NO_ERROR )
08456 return ;
08457
08459 ADFI_write_file( file_index, block_offset->block, block_offset->offset,
08460 TAG_SIZE, sub_node_start_tag, error_return ) ;
08461 if( *error_return != NO_ERROR )
08462 return ;
08463
08465 current_child.block = block_offset->block ;
08466 current_child.offset = block_offset->offset + TAG_SIZE ;
08467 ADFI_adjust_disk_pointer( ¤t_child, error_return ) ;
08468 if( *error_return != NO_ERROR )
08469 return ;
08470
08471 ADFI_write_disk_pointer_2_disk( file_index, current_child.block,
08472 current_child.offset, &end_of_chunk_tag,
08473 error_return ) ;
08474 if( *error_return != NO_ERROR )
08475 return ;
08476
08478 current_child.offset += DISK_POINTER_SIZE ;
08479 for( i=0; i<number_of_sub_nodes; i++ ) {
08480 ADFI_adjust_disk_pointer( ¤t_child, error_return ) ;
08481 if( *error_return != NO_ERROR )
08482 return ;
08483
08484 ADFI_write_file( file_index, current_child.block, current_child.offset,
08485 ADF_NAME_LENGTH, sub_node_table[i].child_name, error_return ) ;
08486 if( *error_return != NO_ERROR )
08487 return ;
08488
08489 current_child.offset += ADF_NAME_LENGTH ;
08490 ADFI_adjust_disk_pointer( ¤t_child, error_return ) ;
08491 if( *error_return != NO_ERROR )
08492 return ;
08493
08494 ADFI_write_disk_pointer_2_disk( file_index, current_child.block,
08495 current_child.offset, &sub_node_table[i].child_location,
08496 error_return ) ;
08497 if( *error_return != NO_ERROR )
08498 return ;
08499
08500 current_child.offset += DISK_POINTER_SIZE ;
08501 }
08502
08504 ADFI_write_file( file_index, end_of_chunk_tag.block, end_of_chunk_tag.offset,
08505 TAG_SIZE, sub_node_end_tag, error_return ) ;
08506 if( *error_return != NO_ERROR )
08507 return ;
08508
08509 }
08510
08511
08512
08513
08514
08515
08516
08517
08518
08519
08520
08521
08522
08523
08524
08525 void ADFI_write_sub_node_table_entry(
08526 const unsigned int file_index,
08527 const struct DISK_POINTER *block_offset,
08528 struct SUB_NODE_TABLE_ENTRY *sub_node_table_entry,
08529 int *error_return )
08530 {
08531 char sub_node_entry_disk_data[ ADF_NAME_LENGTH + DISK_POINTER_SIZE ] ;
08532
08533 if( (block_offset == NULL) || (sub_node_table_entry == NULL) ) {
08534 *error_return = NULL_POINTER ;
08535 return ;
08536 }
08537
08538 if( file_index >= maximum_files || ADF_file[file_index].in_use == 0 ) {
08539 *error_return = ADF_FILE_NOT_OPENED ;
08540 return ;
08541 }
08542
08543 *error_return = NO_ERROR ;
08544
08546 strncpy( &sub_node_entry_disk_data[0], sub_node_table_entry->child_name,
08547 ADF_NAME_LENGTH ) ;
08548 ADFI_disk_pointer_2_ASCII_Hex( &sub_node_table_entry->child_location,
08549 &sub_node_entry_disk_data[ ADF_NAME_LENGTH ],
08550 &sub_node_entry_disk_data[ ADF_NAME_LENGTH + 8 ], error_return ) ;
08551 if( *error_return != NO_ERROR )
08552 return ;
08553
08555 ADFI_write_file( file_index, block_offset->block, block_offset->offset,
08556 ADF_NAME_LENGTH + DISK_POINTER_SIZE,
08557 sub_node_entry_disk_data, error_return ) ;
08558 if( *error_return != NO_ERROR )
08559 return ;
08560
08562 ADFI_stack_control(file_index, block_offset->block, block_offset->offset,
08563 SET_STK, SUBNODE_STK, ADF_NAME_LENGTH + DISK_POINTER_SIZE,
08564 sub_node_entry_disk_data );
08565
08566 }
08567
08568
08569
08570
08571
08572
08573
08574
08575
08576
08577
08578
08579
08580
08581
08582
08583
08584
08585 char *ADFI_strtok(
08586 char *string,
08587 char **string_pos,
08588 char *token )
08589 {
08590 char *tmp_ptr ;
08591 char *sub_string ;
08592 int string_len ;
08593
08594 if ( string_pos == NULL ) return NULL ;
08595 if( token == NULL || string == NULL || *string_pos == NULL ) return NULL ;
08596
08597
08598
08599 string_len = strlen ( *string_pos ) ;
08600 if ( string_len == 0 ) return NULL ;
08601
08602
08603 tmp_ptr = *string_pos ;
08604 while ( string_len > 0 ) {
08605 if ( tmp_ptr[0] == token[0] ) {
08606 tmp_ptr++ ;
08607 string_len-- ;
08608 }
08609 else {
08610 break ;
08611 }
08612 }
08613 if ( string_len == 0 ) return NULL ;
08614
08615
08616 sub_string = tmp_ptr ;
08617
08618
08619
08620 while ( string_len > 0 ) {
08621 if ( tmp_ptr[0] != token[0] ) {
08622 tmp_ptr++ ;
08623 string_len-- ;
08624 }
08625 else {
08626 tmp_ptr[0] = '\0' ;
08627 break ;
08628 }
08629 }
08630
08631
08632
08633 if ( string_len > 0 )
08634 *string_pos = &tmp_ptr[1] ;
08635 else
08636 *string_pos = NULL ;
08637
08638 return sub_string ;
08639
08640 }
08641
08642