home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / c-kermit / cklxtr.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  4KB  |  174 lines

  1. #include <system_io_constants.h>
  2.  
  3. #define CV char_varying
  4.  
  5. extern void s$error (short *status, CV(32) *caller, CV(256) *msg);
  6.  
  7. extern void s$attach_port (CV(32) *port_name, CV(256)* path,
  8.     short *hold, short *port_id, short *status);
  9.  
  10. extern void s$open (short *port_id, short *organization,
  11.     short *max_rec_len, short *io_type, short *locking_type,
  12.     short *access_mode, CV(32) *index_name, short *status);
  13.  
  14. extern void s$detach_port (short *port_id, short *status);
  15.  
  16. extern void s$close (short *port_id, short *status);
  17.  
  18. extern void s$seq_read (short *port_id, short *buff_size,
  19.     short *rec_len, void *buffer, short *status);
  20.  
  21. extern void s$seq_write (short *port_id, short *rec_length,
  22.     void *buffer, short *status);
  23.  
  24. extern void s$parse_command (CV(32) *caller, short *status, ... );
  25.  
  26. static CV(32) caller = "ckltxt";
  27. static CV(40) in_def = "text_file:pathname,required";
  28. static CV(40) out_def = "binary_file:pathname.pm,required";
  29. static CV(3)  end_def = "end";
  30. static CV(32) in_port_name = "in_port";
  31. static CV(32) out_port_name = "out_port";
  32. static CV(32) index_name = "";
  33.  
  34. void cklxtr (void)
  35. {
  36.   short status;
  37.   short buff_len;
  38.   short rec_len;
  39.   char  buff[4096];
  40.   char  txt_buff[80];
  41.   int   buff_ndx;
  42.   int   txt_ndx;
  43.   CV(256) in_path;
  44.   CV(256) out_path;
  45.   short   in_port;
  46.   short   out_port;
  47.   short   flag;
  48.   short   io_type;
  49.   short   lock_type;
  50.   short   access;
  51.   short   org;
  52.   unsigned byte;
  53.   int      nybble;
  54.   int      null_cnt;
  55.  
  56.   s$parse_command (&caller, &status,
  57.                    &in_def, &in_path,
  58.                    &out_def, &out_path,
  59.                    &end_def);
  60.  
  61.   if (status)
  62.     return;
  63.  
  64.   flag = 0;
  65.   s$attach_port (&in_port_name, &in_path, &flag, &in_port, &status);
  66.   if (status)
  67.   {
  68.     s$error (&status, &caller, &in_path);
  69.     return;
  70.   }
  71.  
  72.   org = STREAM_FILE;
  73.   buff_len = sizeof buff;
  74.   io_type = INPUT_TYPE;
  75.   lock_type = READ_LOCK;
  76.   access = SEQUENTIAL_MODE;
  77.   s$open (&in_port, &org, &buff_len, &io_type, &lock_type, &access,
  78.           &index_name, &status);
  79.   if (status)
  80.   {
  81.     s$error (&status, &caller, &in_path);
  82.     return;
  83.   }
  84.  
  85.   flag = 0;
  86.   s$attach_port (&out_port_name, &out_path, &flag, &out_port, &status);
  87.   if (status)
  88.   {
  89.     s$error (&status, &caller, &out_path);
  90.     return;
  91.   }
  92.  
  93.   org = FIXED_FILE;
  94.   buff_len = sizeof buff;
  95.   io_type = OUTPUT_TYPE;
  96.   lock_type = WRITE_LOCK;
  97.   access = SEQUENTIAL_MODE;
  98.   s$open (&out_port, &org, &buff_len, &io_type, &lock_type, &access,
  99.           &index_name, &status);
  100.   if (status)
  101.   {
  102.     s$error (&status, &caller, &out_path);
  103.     return;
  104.   }
  105.  
  106.   buff_ndx = 0;
  107.   while (1)
  108.   {
  109.     buff_len = sizeof txt_buff;
  110.     s$seq_read (&in_port, &buff_len, &rec_len, &txt_buff, &status);
  111.     if (status)
  112.       break;
  113.  
  114.     if (txt_buff[0] == 'Z') /* it's a null record */
  115.     {
  116.       nybble = txt_buff[1] - '0';
  117.       if (nybble > 9)
  118.         nybble = nybble - ('9' - 'A' - 1);
  119.       byte = nybble;
  120.       nybble = txt_buff[2] - '0';
  121.       if (nybble > 9)
  122.         nybble = nybble - ('A' - '9' - 1);
  123.       byte = ((16 * byte) + nybble);
  124.       for (null_cnt = 0; null_cnt < byte * 32; null_cnt ++)
  125.       {
  126.         buff[buff_ndx] = 0;
  127.         buff_ndx ++;
  128.       }
  129.     }
  130.     else /* it is a data record */
  131.     {
  132.       for (txt_ndx = 0; txt_ndx < 64; txt_ndx += 2)
  133.       {
  134.     nybble = txt_buff[txt_ndx] - '0';
  135.     if (nybble > 9)
  136.       nybble = nybble - ('9' - 'A' - 1);
  137.     byte = nybble;
  138.     nybble = txt_buff[txt_ndx + 1] - '0';
  139.     if (nybble > 9)
  140.       nybble = nybble - ('A' - '9' - 1);
  141.     byte = ((16 * byte) + nybble);
  142.     buff[buff_ndx] = byte;
  143.         buff_ndx ++;
  144.       }
  145.     }
  146.  
  147.     if (buff_ndx == sizeof buff) /* one block is finished */
  148.     {
  149.       rec_len = buff_ndx;
  150.       buff_ndx = 0;
  151.       s$seq_write (&out_port, &rec_len, &buff, &status);
  152.       if (status)
  153.       {
  154.         s$error (&status, &caller, &out_path);
  155.  
  156.         s$close (&in_port, &status);
  157.         s$detach_port (&in_port, &status);
  158.  
  159.         s$close (&out_port, &status);
  160.         s$detach_port (&out_port, &status);
  161.  
  162.         return;
  163.       } /* if status != 0 */
  164.     } /* if buff_ndx is sizeof buff */
  165.   } /* while (1) */
  166.  
  167.   s$close (&in_port, &status);
  168.   s$detach_port (&in_port, &status);
  169.  
  170.   s$close (&out_port, &status);
  171.   s$detach_port (&out_port, &status);
  172.  
  173. }
  174.