home *** CD-ROM | disk | FTP | other *** search
-
-
- /* Code Base to Code Base 4 conversion program */
-
- #include "d4base.h"
- #include "w4.h"
- #include "g4char.h"
-
- #include <string.h>
- #ifndef UNIX
- #include <io.h>
- #endif
- #include <stdio.h>
-
- #define MAX_MEMORY 0xFE00
-
- char *file_data ;
- char *convert_data ;
- char *to_data ;
-
- typedef struct
- {
- char title[50] ;
- char prompt[25] ;
- char data[20] ;
- int prompt_len ;
- int data_len ;
- char *ptr ;
- int window_ref ;
- } ITEM_DATA ;
-
- ITEM_DATA to_directory =
- {
- " Enter Destination Directory Name: ",
- " Destination Directory: ",
- "\\C4\\ ",
- 23,
- 14
- } ;
-
- ITEM_DATA file_pattern =
- {
- " Enter new File Pattern ",
- " File Pattern: ",
- "*.C ",
- 14,
- 14
- } ;
-
- ITEM_DATA convert_db =
- {
- " Enter Conversion Database Name: ",
- "Change Convert Database: ",
- "C4CONVER.DBF ",
- 25,
- 14
- } ;
-
- int get_info( ITEM_DATA *), help(void), look_at_file_list( char *),
- start_conversion(void), do_conversion(int) ;
- void calc_item_data( ITEM_DATA *, int ) ;
-
- get_info( item_data )
- ITEM_DATA *item_data ;
- {
- int len, w_ref, half_len ;
-
- len = strlen( item_data->title ) ;
- if ( item_data->data_len > len ) len = item_data->data_len ;
- half_len = len/2 + 3 ;
- w_ref = w4define( 10,40-half_len, 14,40+half_len ) ;
- w4popup() ;
- w4title( 0,2, item_data->title, F_WHITE ) ;
- w4border( DOUBLE, F_WHITE ) ;
-
- g4( 1,3, item_data->data ) ;
- g4upper() ;
-
- g4read() ; /* Will Activate Window */
-
- n4refresh( item_data->window_ref ) ;
-
- w4deactivate( w_ref ) ;
- w4close( w_ref ) ;
-
- return 0 ;
- }
-
- void calc_item_data( item_data, window_ref )
- ITEM_DATA *item_data ;
- int window_ref ;
- {
- item_data->window_ref = window_ref ;
- item_data->ptr = item_data->prompt ;
- while ( *item_data->ptr == ' ' ) item_data->ptr++ ;
- }
-
- int help()
- {
- w4display( " Conversion Help ",
-
- " This conversion program searches a number of files for",
- "character sequences. If the character sequence is located",
- "it is changed into another sequence.",
- "",
- " The defaults are setup to convert \'C\' source files from",
- "the Code Base Nov/88 release to Code Base 4.",
- "",
- " All translated files are placed on a new directory. This",
- "utility will not write over any existing file.",
- (char *) 0
- ) ;
- return 0 ;
- }
-
-
- look_at_file_list( pattern )
- char *pattern ;
- {
- int w_ref, n_data, i, rc ;
- char count_data[14], *data ;
-
- for( rc = u4file_first(pattern,count_data), n_data= 0; rc == 0; n_data +=14)
- rc = u4file_next( count_data ) ;
-
- if ( n_data == 0 )
- {
- w4display( "No Files for Pattern", pattern, (char *) 0 ) ;
- return 0 ;
- }
-
- w_ref = w4define( -1,-1,-1,-1 ) ;
- w4title( 0,-1, " File List ", F_WHITE ) ;
-
- /* Allocate Enough File Memory */
- data = (char *) h4alloc( n_data ) ;
-
- rc = u4file_first(pattern, data ) ;
- n4( data ) ;
-
- for ( i= 14; i< n_data ; i+= 14 )
- {
- if ( u4file_next( data+i ) != 0 ) break ;
- n4( data+i ) ;
- }
-
- n4calc( w_ref, 2,79 ) ; /* Put in upper right hand corner */
- n4key_set( 2,1 ) ; /* ** Searching On ** */
- w4border( DOUBLE, F_WHITE ) ;
- rc = n4activate( &w_ref ) ;
- w4close( w_ref ) ;
- h4free_memory( data ) ;
-
- return rc ;
- }
-
-
- main()
- {
- int rc, main_menu, convert_menu, files ;
-
- d4init() ;
- w4cursor(-1,-1) ;
- file_data = h4alloc( (int) MAX_MEMORY ) ;
- convert_data = h4alloc( (int) MAX_MEMORY ) ;
- to_data = h4alloc( (int) MAX_MEMORY ) ;
-
- /* Make the Error Window Just Defined by 'd4init' a Popup Window */
- w4popup() ;
- w4clear( -1 ) ;
-
- main_menu = w4define(-1,-1,-1,-1) ;
- n4( "Character Sequence Changes") ; n4reaction( n4activate, &convert_menu) ;
- n4key( ALT_C, 0, 0 ) ;
- n4( "Files to Change" ) ; n4reaction( n4activate, &files ) ;
- n4key( ALT_F, 0, 0 ) ;
- n4( "Start Conversion" ) ; n4action( start_conversion ) ;
- n4key( ALT_S, 0, 0 ) ;
- n4( "Help" ) ; n4action( help ) ;
- n4key( ALT_H, 0, 0 ) ;
- n4( "Exit" ) ; n4action( (ACTION *) 0, -1 ) ;
- n4key( ALT_E, 0, 0 ) ;
-
- n4key_special( ESC, CTRL_C, ALT_Q, ALT_M ) ;
-
- convert_menu = w4define(-1,-1,-1,-1) ;
- calc_item_data( &convert_db, convert_menu ) ;
- n4( convert_db.ptr ) ;
- n4action( get_info, &convert_db ) ;
-
- /*
- n4( "Edit Convert Database" ) ;
- n4action( edit_convert_db ) ;
- */
-
- files = w4define(-1,-1,-1,-1) ;
- calc_item_data( &to_directory, files ) ;
- n4( to_directory.ptr ) ;
- n4action( get_info, &to_directory ) ;
-
- file_pattern.window_ref = files ;
- calc_item_data( &file_pattern, files ) ;
- n4( file_pattern.ptr ) ;
- n4action( get_info, &file_pattern ) ;
-
- n4( "Look at File List" ) ;
- n4action( look_at_file_list, file_pattern.data ) ;
-
- n4pulldown( main_menu ) ;
- n4activate( &main_menu ) ;
-
- d4close_all() ;
-
- w4exit(0) ;
- }
-
-
-
- int start_conversion()
- {
- int w_ref ;
-
- w_ref = w4define( 2,45, 7,78 ) ;
- w4border( DOUBLE, F_WHITE ) ;
- w4title( 0,-1, " Status Window ", B_WHITE ) ;
-
- do_conversion( w_ref ) ;
-
- w4deactivate( w_ref ) ;
- w4close( w_ref ) ;
-
- return 0 ;
- }
-
-
- int do_conversion( w_ref )
- int w_ref ;
- {
- long from_ref, to_ref, num_records, tot_memory, i_rec, to_count ;
- int from_width, to_width, tot_width, file_handle,
- i, len, rc, match_done, j, file_col, file_no_col, i_file ;
- char *ptr, *from_ptr, *to_ptr, file_name[14], f_ptr, to_file[80] ;
- unsigned int bytes_read, bytes_written ;
-
- /* Open the Conversion File */
- if ( d4use( convert_db.data ) < 0 ) return 0 ;
-
- /* Verify the Fields */
- from_ref = f4ref( "FROM" ) ;
- to_ref = f4ref( "TO" ) ;
-
- if ( from_ref < 0 || to_ref < 0 ||
- f4type(from_ref) != 'C' ||
- f4type(to_ref) != 'C' )
- {
- w4display( " Illegal Conversion File ",
- "The conversion file must have two fields",
- "names \'FROM\' and \'TO\'. They both",
- "must be of type \'C\'.",
- (char *) 0) ;
- return 0 ;
- }
-
- from_width = f4width( from_ref )+1 ;
- to_width = f4width( to_ref )+1 ;
- tot_width = from_width + to_width ;
- num_records= d4reccount() ;
- from_ptr = f4ptr(from_ref) ;
- to_ptr = f4ptr(to_ref) ;
-
- tot_memory = tot_width * num_records ;
-
- if ( tot_memory > MAX_MEMORY )
- {
- w4display( "", "The Conversion File is too Large",
- "", "Press a key", (char *) 0 ) ;
- return 0 ;
- }
-
- rc = w4display( " Converting Files ",
- " It is a good idea to do your regular backup before",
- "doing your file conversions.",
- "",
- "Pattern:",
- file_pattern.data,
- "",
- "Destination Directory:",
- to_directory.data,
- "",
- "Press \'Y\' to continue and any other character to abort.",
- (char *) 0 ) ;
-
- if ( rc != 'Y' && rc != 'y' )
- {
- w4display( "", "Aborting Conversion on User Request", (char *) 0) ;
- return 0 ;
- }
-
- /* Read the Conversion File into Memory */
- for ( i_rec = 1, ptr = convert_data; i_rec <= num_records; i_rec++ )
- {
- if ( d4go( i_rec ) < 0 ) return 0 ;
- memcpy( ptr, from_ptr, from_width-1 ) ;
- c4trim_n( ptr, from_width ) ;
- ptr += from_width ;
-
- memcpy( ptr, to_ptr, to_width-1 ) ;
- c4trim_n( ptr, to_width ) ;
- ptr += to_width ;
- }
-
- i_file = 0 ;
-
- w4activate( w_ref ) ;
- w4( 1,3, "Converting File: " ) ; file_col = w4col() ;
- w4( 2,3, "File Number:" ) ; file_no_col = w4col() ;
-
- /* Do the conversion - One file at a time */
- for ( rc = u4file_first( file_pattern.data, file_name ) ;
- rc == 0;
- rc = u4file_next(file_name) )
- {
- w4( 1,file_col, " " ) ;
- w4( 1,file_col, file_name ) ;
- w4int( 2,file_no_col, ++i_file, 4 ) ;
-
- if ( (file_handle = u4open(file_name,0)) < 0 ) return 0 ;
-
- bytes_read = read( file_handle, file_data, MAX_MEMORY-1 ) ;
- close( file_handle ) ;
- if ( bytes_read == MAX_MEMORY-1 )
- {
- rc = w4display( " File too Large for Conversion: ",
- file_name,
- "",
- "Press <Ctrl C> to abort and any other key to continue.",
- (char *) 0 ) ;
- if ( rc == 3 ) return 0 ;
- continue ;
- }
-
- if ( bytes_read == 0xFFFF )
- {
- w4display( " Error Reading File: ",
- file_data,
- (char *) 0 ) ;
- continue ;
- }
-
- file_data[bytes_read] = '\0' ;
- to_count = 0 ;
-
- for ( i= 0; i< bytes_read; i++ )
- {
- /* See if there is a character match */
- ptr = convert_data ;
- match_done = 0 ;
- for ( i_rec = 1; i_rec <= num_records && match_done == 0; i_rec++ )
- {
- if ( file_data[i] < ' ' ) break ;
-
- /* Check for Character Match */
- if ( *ptr == file_data[i] )
- {
- /* Check for Complete Match */
- for ( j=1; ptr[j] != '\0' && ptr[j] == file_data[j+i]; j++ ) ;
- if ( ptr[j] == '\0' )
- {
- /* Complete match; do translation */
- len = strlen(ptr+from_width) ;
-
- if ( to_count + len <= MAX_MEMORY )
- memcpy( to_data+ (unsigned int) to_count,
- ptr+from_width, len ) ;
-
- to_count += len ;
- i = i+j-1 ;
- match_done = 1 ;
- }
- }
- ptr += tot_width ;
- }
- if ( match_done == 0 )
- {
- if ( to_count >= MAX_MEMORY )
- {
- to_count++ ;
- break ;
- }
- to_data[ (unsigned int) (to_count++) ] = file_data[i] ;
- }
- }
-
- if ( to_count > MAX_MEMORY )
- {
- rc = w4display( " File too Large for Conversion: ",
- file_name,
- "",
- "Press <Ctrl C> to abort and any other key to continue.",
- (char *) 0 ) ;
- if ( rc == 3 ) return 0 ;
- }
- else
- {
- strcpy( to_file, to_directory.data ) ;
- c4trim_n( to_file, strlen(to_file)+1 ) ;
- if ( to_file[ strlen(to_file)-1] != '\\' )
- to_file[ strlen(to_file) ] = '\\' ;
- strcat( to_file, file_name ) ;
- if ( (file_handle = u4open(to_file, 1)) < 0 )
- {
- rc = w4display( " Suggestion for the File Creation Error: ",
- " Make sure that the destination directory exists and",
- "that the file being translatted does not exist within",
- "the destination directory.",
- "",
- "Press a key to continue the translation or press",
- "<Ctrl C> to abort it.",
- (char *) 0 ) ;
- if ( rc == 3 ) return 0 ;
- }
- else
- {
- bytes_written = write(file_handle, to_data, (unsigned int) to_count);
- close( file_handle ) ;
- if ( bytes_written != (unsigned int) to_count )
- {
- w4display( " Error Writing to Output File: ",
- to_file,
- "",
- "Conversion Aborted",
- (char *) 0 ) ;
- return 0 ;
- }
- }
- }
- }
-
- w4display( "", "Conversion Successfully Completed", "", "Press a key ...", (char *) 0 ) ;
-
- return 0 ;
- }