#include "d4all.hpp" void usageOutput(void) { printf("\n\nUSAGE: This executable will transform a xBase database by \n\tincreasing the size of Numerical fields to a minimum length of 2."); printf("\n\nSYNTAX: Transform target_file"); printf("\n\nExample: c:\\>transform example.dbf" ) ; } void errorOutput(char *string, char *string2) { char buff[320]; sprintf(buff,"\n\nERROR: %s \n\n Note: %s\n\n",string,string2); printf("%s",buff); } int main(int argc, char *argv[]) { int rc, i, j; short needTransform = 0 ; char buffer[20] = "transformTemp" ; unsigned long before, countAfter = 0 ; if ( argc == 1 ) { printf("\n\nTransform - This program transforms a xBase compatible database into an\n"); printf("\t OLE DB xBase compatible database\n\n"); usageOutput(); return (0); } if ( !strcmp(argv[1],"/?") || !strcmp(argv[1],"?") ) { printf("\n\nTransform - This program transforms a xBase compatible database into an\n"); printf("\t OLE DB xBase compatible database\n\n"); usageOutput(); return (0); } if ( argc != 2 ) { errorOutput("The wrong number of arguments have been passed to transform","Please consult the Usage below for the proper input"); usageOutput(); exit (-1); } Code4 cb ; cb.accessMode = OPEN4DENY_RW ; Data4 data ; rc = data.open( cb, argv[1] ) ; if (rc != 0) { errorOutput("Unable to open the file","The file does not exist or is in use by another process."); cb.initUndo() ; exit (-1); } before = data.recWidth() ; Field4info fieldInfo( data ) ; FIELD4INFO *fields = fieldInfo.fields() ; Tag4info tagInfo( data ) ; for ( i=0 ; i < fieldInfo.numFields(); i++ ) { if (fields[i].type == r4num ) { if (fields[i].dec == 0 ) if (fields[i].len < 2 ) { fields[i].len = 2 ; needTransform = 1 ; } } countAfter += fields[i].len ; } //add one for deleted marker. countAfter++ ; if ( needTransform ) { if ( before > countAfter ) { errorOutput("Error transfering fields","The record width before is greater than the after count."); cb.initUndo() ; exit (-1); } #ifdef S4FOX cb.compatibility = data.data->dataFile->compatibility ; #endif Data4 data2 ; FIELD4 *temp ; long length ; cb.safety = 0 ; rc = data2.create( cb, buffer, fields, NULL ) ; cb.safety = 1 ; if( rc != r4success ) { errorOutput("Unable to create temporary file","The file, \"transformtemp.dbf\", already exists, has problems with field information, or is in use by another process."); cb.initUndo() ; exit (-1); } // Transfer all data from first database to second database for (rc=data.top(); rc == r4success ; rc = data.skip() ) { data2.appendStart() ; for (j = 1; j <= data2.numFields() ; j++ ) { switch( f4type( d4fieldJ( data.data, j ) ) ) { case r4gen: case r4bin: case r4memo: temp = d4fieldJ(data.data, j) ; length = f4memoLen( temp ) ; f4memoAssignN( d4fieldJ(data2.data, j), f4memoPtr(temp), length ) ; break ; default: f4assignField( d4fieldJ( data2.data, j), d4fieldJ( data.data, j) ) ; } } data2.append() ; } d4replace(data.data, data2.data, 0 ) ; } cb.closeAll() ; cb.safety = 0 ; cb.autoOpen = 0 ; data.open(cb, argv[1] ); Index4 index ; index.create(data, NULL, tagInfo) ; u4nameExt( buffer, sizeof(buffer), INDEX4EXT, 0 ) ; u4remove(buffer) ; data.close() ; cb.initUndo() ; return 0 ; }