//-------------------------------------- //--- 010 Editor v3.1.2 Script File // // File: Interleave.1sc // Author: SweetScape Software // Revision: 1.0 // Purpose: When script is run on two files, // one of the form 'AAAAA...' and the other // of the form 'BBBBB...', the script creates // a new interleaved file in the form // 'ABABABABAB...'. The size of each element // can be specified. See the Uninterleave.1sc // script to separate the joined file. //-------------------------------------- // Define local variables const int DEFAULT_ELEMENT_SIZE = 1; const int BLOCK_SIZE = 16384; const char SCRIPT_NAME[] = "Interleave"; uchar targetBuffer[BLOCK_SIZE], buf1[BLOCK_SIZE], buf2[BLOCK_SIZE]; char filename1[512], filename2[512], str[128]; int pos, pos1, pos2, i, i1, i2, blocksize1, blocksize2, size, size1, size2, which, len, elementpos, elementsize; int targetFileNum, file1Num, file2Num; // Get the filename of the first input file filename1 = InputOpenFileName( "Choose First Input File Name", "All files (*.*)|*.*" ); len = Strlen( filename1 ); if( len == 0 ) return -1; // Get the filename of the second output file filename2 = InputOpenFileName( "Choose Second Input File Name", "All files (*.*)|*.*" ); len = Strlen( filename2 ); if( len == 0 ) return -1; // Input size of each file SPrintf( str, "%d", DEFAULT_ELEMENT_SIZE ); elementsize = InputNumber( SCRIPT_NAME, "Enter the size of each element in the file:", str ); if( elementsize == BAD_VALUE ) return -1; // Check that size is valid if( (elementsize <= 0) || (elementsize > 65535) ) { MessageBox( idOk, SCRIPT_NAME, "Invalid element size." ); return -1; } // Check that file names are different if( filename1 == filename2 ) { MessageBox( idOk, SCRIPT_NAME, "Input files must have different names." ); return -1; } // Open the files if( FileOpen( filename1 ) < 0 ) { MessageBox( idOk, SCRIPT_NAME, "File '%s' could not be opened.", filename1 ); return -1; } file1Num = GetFileNum(); if( FileOpen( filename2 ) < 0 ) { MessageBox( idOk, SCRIPT_NAME, "File '%s' could not be opened.", filename2 ); return -1; } file2Num = GetFileNum(); // Separate file into a two parts pos = 0; FileSelect( file1Num ); size1 = FileSize(); FileSelect( file2Num ); size2 = FileSize(); targetFileNum = FileNew(); which = 0; elementpos = elementsize; pos1 = pos2 = blocksize1 = blocksize2 = 0; size = size1 + size2; i = 0; while( size > 0 ) { if( which == 0 ) { // Read data from the first file if( i1 >= blocksize1 ) { FileSelect( file1Num ); blocksize1 = Min( BLOCK_SIZE, size1 ); ReadBytes( buf1, pos1, blocksize1 ); i1 = 0; pos1 += blocksize1; size1 -= blocksize1; } // Copy a single byte out if( i1 < blocksize1 ) { targetBuffer[i++] = buf1[i1++]; size--; } } else { // Read data from the second file if( i2 >= blocksize2 ) { FileSelect( file2Num ); blocksize2 = Min( BLOCK_SIZE, size2 ); ReadBytes( buf2, pos2, blocksize2 ); i2 = 0; pos2 += blocksize2; size2 -= blocksize2; } // Copy a single byte out if( i2 < blocksize2 ) { targetBuffer[i++] = buf2[i2++]; size--; } } // Switch to other file if we've copied out a full element if( --elementpos == 0 ) { which = !which; elementpos = elementsize; } // Write the array out if necessary if( i >= BLOCK_SIZE ) { FileSelect( targetFileNum ); WriteBytes( targetBuffer, pos, i ); pos += i; i = 0; } } // Write out any left-over data if( i > 0 ) { FileSelect( targetFileNum ); WriteBytes( targetBuffer, pos, i ); }