home *** CD-ROM | disk | FTP | other *** search
/ Piper's Pit BBS/FTP: ibm 0010 - 0019 / ibm0010-0019 / ibm0010.tar / ibm0010 / CODE4-4.ZIP / HDR_EXMP.ZIP / EXAMPLES.ZIP / C4CONVER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-06  |  11.5 KB  |  443 lines

  1.  
  2.  
  3. /* Code Base to Code Base 4 conversion program */
  4.  
  5. #include "d4base.h"
  6. #include "w4.h"
  7. #include "g4char.h"
  8.  
  9. #include <string.h>
  10. #ifndef UNIX
  11.    #include <io.h>
  12. #endif
  13. #include <stdio.h>
  14.  
  15. #define  MAX_MEMORY  0xFE00
  16.  
  17. char  *file_data ;
  18. char  *convert_data ;
  19. char  *to_data ;
  20.  
  21. typedef struct
  22. {
  23.    char    title[50] ;
  24.    char    prompt[25] ;
  25.    char    data[20] ;
  26.    int     prompt_len ;
  27.    int     data_len ;
  28.    char   *ptr ;
  29.    int     window_ref ;
  30. }  ITEM_DATA ;
  31.  
  32. ITEM_DATA  to_directory =
  33. {
  34.    " Enter Destination Directory Name: ",
  35.    "  Destination Directory: ",
  36.    "\\C4\\          ",
  37.    23,
  38.    14
  39. } ;
  40.  
  41. ITEM_DATA file_pattern =
  42. {
  43.    " Enter new File Pattern ",
  44.    "           File Pattern: ",
  45.    "*.C           ",
  46.    14,
  47.    14
  48. } ;
  49.  
  50. ITEM_DATA  convert_db =
  51. {
  52.    " Enter Conversion Database Name: ",
  53.    "Change Convert Database: ",
  54.    "C4CONVER.DBF  ",
  55.    25,
  56.    14
  57. } ;
  58.  
  59. int   get_info( ITEM_DATA *), help(void), look_at_file_list( char *),
  60.       start_conversion(void), do_conversion(int) ;
  61. void  calc_item_data( ITEM_DATA *, int ) ;
  62.  
  63. get_info( item_data )
  64. ITEM_DATA  *item_data ;
  65. {
  66.    int  len, w_ref, half_len ;
  67.  
  68.    len =  strlen( item_data->title ) ;
  69.    if ( item_data->data_len > len )  len =  item_data->data_len ;
  70.    half_len =  len/2 + 3 ;
  71.    w_ref =  w4define( 10,40-half_len, 14,40+half_len ) ;
  72.    w4popup() ;
  73.    w4title( 0,2, item_data->title, F_WHITE ) ;
  74.    w4border( DOUBLE, F_WHITE ) ;
  75.  
  76.    g4( 1,3, item_data->data ) ;
  77.    g4upper() ;
  78.  
  79.    g4read() ;  /* Will Activate Window */
  80.  
  81.    n4refresh( item_data->window_ref ) ;
  82.  
  83.    w4deactivate( w_ref ) ;
  84.    w4close( w_ref ) ;
  85.  
  86.    return 0 ;
  87. }
  88.  
  89. void  calc_item_data( item_data, window_ref )
  90. ITEM_DATA  *item_data ;
  91. int  window_ref ;
  92. {
  93.    item_data->window_ref =  window_ref ;
  94.    item_data->ptr =  item_data->prompt ;
  95.    while ( *item_data->ptr == ' ' )  item_data->ptr++ ;
  96. }
  97.  
  98. int  help()
  99. {
  100.    w4display( " Conversion Help ",
  101.  
  102.               "   This conversion program searches a number of files for",
  103.               "character sequences.  If the character sequence is located",
  104.               "it is changed into another sequence.",
  105.               "",
  106.           "   The defaults are setup to convert \'C\' source files from",
  107.           "the Code Base Nov/88 release to Code Base 4.",
  108.               "",
  109.           "   All translated files are placed on a new directory.  This",
  110.           "utility will not write over any existing file.",
  111.           (char *) 0
  112.            ) ;
  113.    return 0 ;
  114. }
  115.  
  116.  
  117. look_at_file_list( pattern )
  118. char *pattern ;
  119. {
  120.    int   w_ref, n_data, i, rc ;
  121.    char  count_data[14], *data ;
  122.  
  123.    for( rc =  u4file_first(pattern,count_data), n_data= 0; rc == 0; n_data +=14)
  124.       rc =  u4file_next( count_data ) ;
  125.  
  126.    if ( n_data == 0 )
  127.    {
  128.       w4display( "No Files for Pattern", pattern, (char *) 0 ) ;
  129.       return 0 ;
  130.    }
  131.  
  132.    w_ref =  w4define( -1,-1,-1,-1 ) ;
  133.    w4title( 0,-1, " File List ", F_WHITE ) ;
  134.  
  135.    /* Allocate Enough File Memory */
  136.    data =  (char *) h4alloc( n_data ) ;
  137.  
  138.    rc =  u4file_first(pattern, data ) ;
  139.    n4( data ) ;
  140.  
  141.    for ( i= 14; i< n_data ; i+= 14 )
  142.    {
  143.       if ( u4file_next( data+i )  != 0 )  break ;
  144.       n4( data+i ) ;
  145.    }
  146.  
  147.    n4calc( w_ref, 2,79 ) ; /* Put in upper right hand corner */
  148.    n4key_set( 2,1 ) ;      /* ** Searching On ** */
  149.    w4border( DOUBLE, F_WHITE ) ;
  150.    rc =  n4activate( &w_ref ) ;
  151.    w4close( w_ref ) ;
  152.    h4free_memory( data ) ;
  153.  
  154.    return  rc ;
  155. }
  156.  
  157.  
  158. main()
  159. {
  160.    int     rc, main_menu, convert_menu, files ;
  161.  
  162.    d4init() ;
  163.    w4cursor(-1,-1) ;
  164.    file_data    =  h4alloc( (int) MAX_MEMORY ) ;
  165.    convert_data =  h4alloc( (int) MAX_MEMORY ) ;
  166.    to_data      =  h4alloc( (int) MAX_MEMORY ) ;
  167.  
  168.    /* Make the Error Window Just Defined by 'd4init' a Popup Window */
  169.    w4popup() ;
  170.    w4clear( -1 ) ;
  171.  
  172.    main_menu =  w4define(-1,-1,-1,-1) ;
  173.    n4( "Character Sequence Changes") ;  n4reaction( n4activate, &convert_menu) ;
  174.    n4key( ALT_C, 0, 0 ) ;
  175.    n4( "Files to Change" ) ;            n4reaction( n4activate, &files ) ;
  176.    n4key( ALT_F, 0, 0 ) ;
  177.    n4( "Start Conversion" ) ;           n4action( start_conversion ) ;
  178.    n4key( ALT_S, 0, 0 ) ;
  179.    n4( "Help" ) ;                       n4action( help ) ;
  180.    n4key( ALT_H, 0, 0 ) ;
  181.    n4( "Exit" ) ;                       n4action( (ACTION *) 0, -1 ) ;
  182.    n4key( ALT_E, 0, 0 ) ;
  183.  
  184.    n4key_special( ESC, CTRL_C, ALT_Q, ALT_M ) ;
  185.  
  186.    convert_menu =  w4define(-1,-1,-1,-1) ;
  187.    calc_item_data( &convert_db, convert_menu ) ;
  188.    n4( convert_db.ptr ) ;
  189.    n4action( get_info, &convert_db ) ;
  190.  
  191. /*
  192.    n4( "Edit Convert Database" ) ;
  193.    n4action( edit_convert_db ) ;
  194. */
  195.  
  196.    files =  w4define(-1,-1,-1,-1) ;
  197.    calc_item_data( &to_directory, files ) ;
  198.    n4( to_directory.ptr ) ;
  199.    n4action( get_info, &to_directory ) ;
  200.  
  201.    file_pattern.window_ref =  files ;
  202.    calc_item_data( &file_pattern, files ) ;
  203.    n4( file_pattern.ptr ) ;
  204.    n4action( get_info, &file_pattern ) ;
  205.  
  206.    n4( "Look at File List" ) ;
  207.    n4action( look_at_file_list, file_pattern.data ) ;
  208.  
  209.    n4pulldown( main_menu ) ;
  210.    n4activate( &main_menu ) ;
  211.  
  212.    d4close_all() ;
  213.  
  214.    w4exit(0) ;
  215. }
  216.  
  217.  
  218.  
  219. int  start_conversion()
  220. {
  221.    int  w_ref ;
  222.  
  223.    w_ref =  w4define( 2,45, 7,78 ) ;
  224.    w4border( DOUBLE, F_WHITE ) ;
  225.    w4title( 0,-1, " Status Window ", B_WHITE ) ;
  226.  
  227.    do_conversion( w_ref ) ;
  228.  
  229.    w4deactivate( w_ref ) ;
  230.    w4close( w_ref ) ;
  231.  
  232.    return 0 ;
  233. }
  234.  
  235.  
  236. int  do_conversion( w_ref )
  237. int  w_ref ;
  238. {
  239.    long      from_ref, to_ref, num_records, tot_memory, i_rec, to_count ;
  240.    int       from_width, to_width, tot_width, file_handle,
  241.          i, len, rc, match_done, j, file_col, file_no_col, i_file ;
  242.    char     *ptr, *from_ptr, *to_ptr, file_name[14], f_ptr, to_file[80] ;
  243.    unsigned int   bytes_read, bytes_written ;
  244.  
  245.    /* Open the Conversion File */
  246.    if ( d4use( convert_db.data )  < 0 )  return 0 ;
  247.  
  248.    /* Verify the Fields */
  249.    from_ref =  f4ref( "FROM" ) ;
  250.    to_ref   =  f4ref( "TO" ) ;
  251.  
  252.    if ( from_ref < 0 || to_ref < 0 ||
  253.         f4type(from_ref) != 'C' ||
  254.         f4type(to_ref) != 'C' )
  255.    {
  256.       w4display( " Illegal Conversion File ",
  257.                  "The conversion file must have two fields",
  258.                  "names \'FROM\' and \'TO\'.  They both",
  259.                  "must be of type \'C\'.",
  260.                  (char *) 0) ;
  261.       return 0 ;
  262.    }
  263.  
  264.    from_width =  f4width( from_ref )+1 ;
  265.    to_width   =  f4width( to_ref )+1 ;
  266.    tot_width  =  from_width + to_width ;
  267.    num_records=  d4reccount() ;
  268.    from_ptr   =  f4ptr(from_ref) ;
  269.    to_ptr     =  f4ptr(to_ref) ;
  270.  
  271.    tot_memory =  tot_width * num_records ;
  272.  
  273.    if ( tot_memory > MAX_MEMORY )
  274.    {
  275.       w4display( "", "The Conversion File is too Large",
  276.                  "", "Press a key", (char *) 0 ) ;
  277.       return 0 ;
  278.    }
  279.  
  280.    rc =  w4display( " Converting Files ",
  281.           "   It is a good idea to do your regular backup before",
  282.           "doing your file conversions.",
  283.           "",
  284.           "Pattern:",
  285.           file_pattern.data,
  286.           "",
  287.           "Destination Directory:",
  288.           to_directory.data,
  289.           "",
  290.           "Press \'Y\' to continue and any other character to abort.",
  291.           (char *) 0 ) ;
  292.  
  293.    if ( rc != 'Y' && rc != 'y' )
  294.    {
  295.       w4display( "", "Aborting Conversion on User Request", (char *) 0) ;
  296.       return 0 ;
  297.    }
  298.  
  299.    /* Read the Conversion File into Memory */
  300.    for ( i_rec = 1, ptr = convert_data; i_rec <= num_records; i_rec++ )
  301.    {
  302.       if ( d4go( i_rec )  < 0 )  return 0 ;
  303.       memcpy( ptr, from_ptr, from_width-1 ) ;
  304.       c4trim_n( ptr, from_width ) ;
  305.       ptr +=  from_width ;
  306.  
  307.       memcpy( ptr, to_ptr, to_width-1 ) ;
  308.       c4trim_n( ptr, to_width ) ;
  309.       ptr +=  to_width ;
  310.    }
  311.  
  312.    i_file =  0 ;
  313.  
  314.    w4activate( w_ref ) ;
  315.    w4( 1,3, "Converting File: " ) ;  file_col    =  w4col() ;
  316.    w4( 2,3, "File Number:" ) ;       file_no_col =  w4col() ;
  317.  
  318.    /* Do the conversion - One file at a time */
  319.    for ( rc =  u4file_first( file_pattern.data, file_name ) ;
  320.          rc == 0;
  321.          rc = u4file_next(file_name)  )
  322.    {
  323.       w4( 1,file_col, "            " ) ;
  324.       w4( 1,file_col, file_name ) ;
  325.       w4int( 2,file_no_col, ++i_file, 4 ) ;
  326.  
  327.       if ( (file_handle = u4open(file_name,0)) < 0 )  return 0 ;
  328.  
  329.       bytes_read =  read( file_handle, file_data, MAX_MEMORY-1 ) ;
  330.       close( file_handle ) ;
  331.       if ( bytes_read == MAX_MEMORY-1 )
  332.       {
  333.      rc =  w4display( " File too Large for Conversion: ",
  334.               file_name,
  335.               "",
  336.               "Press <Ctrl C> to abort and any other key to continue.",
  337.               (char *) 0 ) ;
  338.      if ( rc == 3 )  return 0 ;
  339.          continue ;
  340.       }
  341.  
  342.       if ( bytes_read == 0xFFFF )
  343.       {
  344.          w4display( " Error Reading File: ",
  345.                     file_data,
  346.                     (char *) 0 ) ;
  347.          continue ;
  348.       }
  349.  
  350.       file_data[bytes_read] =  '\0' ;
  351.       to_count =  0 ;
  352.  
  353.       for ( i= 0; i< bytes_read; i++ )
  354.       {
  355.          /* See if there is a character match */
  356.          ptr = convert_data ;
  357.          match_done = 0 ;
  358.      for ( i_rec = 1; i_rec <= num_records && match_done == 0; i_rec++ )
  359.      {
  360.         if ( file_data[i] < ' ' )  break ;
  361.  
  362.             /* Check for Character Match */
  363.         if ( *ptr == file_data[i] )
  364.             {
  365.                /* Check for Complete Match */
  366.            for ( j=1; ptr[j] != '\0' && ptr[j] == file_data[j+i]; j++ ) ;
  367.                if ( ptr[j] == '\0' )
  368.                {
  369.                   /* Complete match; do translation */
  370.                   len = strlen(ptr+from_width) ;
  371.  
  372.                   if ( to_count + len <= MAX_MEMORY )
  373.                      memcpy( to_data+ (unsigned int) to_count,
  374.                              ptr+from_width, len ) ;
  375.  
  376.           to_count += len ;
  377.           i = i+j-1 ;
  378.                   match_done =  1 ;
  379.                }
  380.             }
  381.             ptr +=  tot_width ;
  382.          }
  383.          if ( match_done == 0 )
  384.          {
  385.             if ( to_count >= MAX_MEMORY )
  386.             {
  387.                to_count++ ;
  388.                break ;
  389.             }
  390.         to_data[ (unsigned int) (to_count++) ] =  file_data[i] ;
  391.          }
  392.       }
  393.  
  394.       if ( to_count > MAX_MEMORY )
  395.       {
  396.           rc =  w4display( " File too Large for Conversion: ",
  397.                file_name,
  398.                        "",
  399.                "Press <Ctrl C> to abort and any other key to continue.",
  400.                (char *) 0 ) ;
  401.        if ( rc == 3 )  return 0 ;
  402.       }
  403.       else
  404.       {
  405.          strcpy( to_file, to_directory.data ) ;
  406.      c4trim_n( to_file, strlen(to_file)+1 ) ;
  407.      if ( to_file[ strlen(to_file)-1] != '\\' )
  408.         to_file[ strlen(to_file) ] =  '\\' ;
  409.          strcat( to_file, file_name ) ;
  410.      if ( (file_handle =  u4open(to_file, 1)) < 0 )
  411.      {
  412.         rc =  w4display( " Suggestion for the File Creation Error: ",
  413.                "   Make sure that the destination directory exists and",
  414.                "that the file being translatted does not exist within",
  415.                "the destination directory.",
  416.                "",
  417.                "Press a key to continue the translation or press",
  418.                "<Ctrl C> to abort it.",
  419.                (char *) 0 ) ;
  420.         if ( rc == 3 )  return 0 ;
  421.      }
  422.      else
  423.      {
  424.         bytes_written = write(file_handle, to_data, (unsigned int) to_count);
  425.         close( file_handle ) ;
  426.         if ( bytes_written != (unsigned int) to_count )
  427.         {
  428.            w4display( " Error Writing to Output File: ",
  429.               to_file,
  430.               "",
  431.               "Conversion Aborted",
  432.               (char *) 0 ) ;
  433.            return 0 ;
  434.         }
  435.      }
  436.       }
  437.    }
  438.  
  439.    w4display( "", "Conversion Successfully Completed", "", "Press a key ...", (char *) 0 ) ;
  440.  
  441.    return 0 ;
  442. }
  443.