home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR2 / CACODE.ZIP / CAENCODE.PRG < prev    next >
Text File  |  1993-07-14  |  5KB  |  205 lines

  1. /*****************************************************************************
  2.   CA-Clipper Decode - File Decoder, ala uudecode.exe
  3.   By Kirby L. Wallace, Wallace Information Systems Engineering
  4.  
  5.    FUNCTION: CAEncode(cFileIn, cFileOut)
  6.  
  7.  PARAMETERS: cInFile : (from command line) Name of DOS File to
  8.                        decode.
  9.  
  10. DESCRIPTION: CAEncode() creates an encoded file for decoding by CADecode()
  11.              It is used primarily to transport files of any type across
  12.              networks that may not necessarily support High/Low order
  13.              ASCII characters.
  14.  
  15.              CAEncode() encodes the file to ensure that all characters in
  16.              the original file are mapped into the "printable" character
  17.              set.
  18.  
  19.              See .DOC file for details
  20.  
  21. ******************************************************************************/
  22.  
  23. #include "fileio.ch"
  24. #define  MAXCHARS 1024
  25. #define  LF       CHR(13)+CHR(10)
  26. #define  TRUE     .T.
  27. #define  FALSE    .F.
  28.  
  29. FUNCTION CAEncode(cFileIn, cFileOut)
  30. LOCAL    nIHandle, nOHandle
  31. LOCAL    i, j, k
  32. LOCAL    cBytesIn   := space(3)
  33. LOCAL    nBytes
  34. LOCAL    cBits      := ''
  35. LOCAL    cBytesOut  := ''
  36. LOCAL    lHelp  := FALSE
  37.  
  38. ? 'CAEncode() - File Encoder - Kirby L. Wallace - March 1993'
  39. ? 'Version 2.0'
  40. ?
  41. ? 'Void where prohibited by law - Batteries not included - Freeware! - Yow!'
  42. ?
  43. ? cFileIn, '-->', cFileOut
  44. ?
  45.  
  46. BEGIN SEQUENCE
  47.  
  48. if cFileIn = NIL .or. cFileOut = NIL
  49.     lHelp = TRUE
  50. else
  51.  
  52.     if cFileIn $ '?' .or. cFileIn = '/h'
  53.         lHelp = TRUE
  54.     endif
  55.  
  56. endif
  57.  
  58. if lHelp
  59.  
  60.     ?
  61.     ? 'Usage: CAENCODE InputFile.ext OutputFile.ext'
  62.     ?
  63.     ? 'Where InputFile.ext = Name of file to be encoded and '
  64.     ? '      OutPutFile.ext = name of encoded file to create'
  65.     ?
  66.  
  67.     BREAK
  68.  
  69. endif
  70.  
  71. *--------------------------------------------------------------------------
  72.  
  73. i := j := k := 0
  74.  
  75. if file(cFileIn)
  76.  
  77.     if ( nIHandle := fopen(cFileIn) ) != -1
  78.  
  79.         // input file is open
  80.  
  81.         if ( nOHandle := fcreate(cFileOut) ) != -1
  82.             // output file is created
  83.             fwrite(nOHandle,'+++CAFile: '+cFileIn+LF+'+++CABegin+++'+LF)
  84.         else
  85.  
  86.             ? LF+'Output File Create Error'+LF
  87.  
  88.             BREAK
  89.  
  90.         endif
  91.  
  92.         while TRUE
  93.  
  94.             /////////////////////////////////////////////////
  95.             // change j to change the right hand margin.   //
  96.             // each increment or decrement changes the     //
  97.             // margin by +/-4 columns. 17 = 68 cols        //
  98.             /////////////////////////////////////////////////
  99.  
  100.             ?? '.' // an incredibly cutting-edge-of-technology, high-tech progress indicator
  101.  
  102.             for j = 1 to 17
  103.  
  104.                 cBytesOut := cBits := ''
  105.                 cBytesIn = space(3)
  106.  
  107.                 if ( nBytes := fread(nIHandle,@cBytesIn,3) ) = 3
  108.  
  109.                     // convert three input bytes to a bitmap
  110.                     cBits = Make_cBits(cBytesIn)        
  111.  
  112.                     // convert bitmap to four bytes
  113.                     cBytesOut = Make_cBytesOut(cBits)   
  114.  
  115.                     // write four bytes to disk
  116.                     fwrite(nOHandle,cBytesOut)          
  117.  
  118.                 else
  119.  
  120.  
  121.                     // end of file
  122.  
  123.                     // how many bytes left over?
  124.  
  125.                     // write remaining bytes - decoder will look for either
  126.                     // 2 or three odd digits after  % 4 and decode same way
  127.  
  128.                     if nBytes  = 1
  129.  
  130.                         cBits = '0000'+_int2b(asc(substr(cBytesIn,1,1)))
  131.  
  132.                         cBytesOut = chr(48+_b2int(substr(cBits,1,6)))+ ;
  133.                                     chr(48+_b2int(substr(cBits,7,6)))
  134.  
  135.                         fwrite(nOHandle,cBytesOut)
  136.  
  137.                     else
  138.  
  139.                         if nBytes = 2
  140.  
  141.                             cBits = '00'+_int2b(asc(substr(cBytesIn,1,1)))+;
  142.                                          _int2b(asc(substr(cBytesIn,2,1)))
  143.  
  144.                             cBytesOut = chr(48+_b2int(substr(cBits,1,6)))+ ;
  145.                                         chr(48+_b2int(substr(cBits,7,6)))+ ;
  146.                                         chr(48+_b2int(substr(cBits,13,6)))
  147.  
  148.                             altd()
  149.  
  150.                             fwrite(nOHandle,cBytesOut)
  151.  
  152.                         endif
  153.  
  154.                     endif
  155.  
  156.                     fwrite(nOHandle,LF+'+++CAEnd+++')
  157.  
  158.                     BREAK
  159.  
  160.                 endif
  161.  
  162.             next
  163.  
  164.             fwrite(nOHandle,LF)
  165.  
  166.         end
  167.  
  168.     else
  169.         ? LF+'Input File Open Error'+LF
  170.  
  171.     endif
  172.  
  173. else
  174.  
  175.     ? LF+'Input File not Found'+LF
  176.  
  177. endif
  178.  
  179. END SEQUENCE
  180.  
  181. fclose(nIHandle); fclose(nOHandle)
  182.  
  183.  
  184. RETURN(NIL)
  185.  
  186. *************************************************************************
  187.  
  188. FUNCTION Make_cBits(cBytesIn)
  189.  
  190. LOCAL cBits := ''
  191. LOCAL i
  192.  
  193. // this method is slightly faster than a for loop with
  194. // a var counter for 1,2,3
  195.  
  196. cBits += _int2b(asc(substr(cBytesIn,1,1)))
  197. cBits += _int2b(asc(substr(cBytesIn,2,1)))
  198. cBits += _int2b(asc(substr(cBytesIn,3,1)))
  199.  
  200.  
  201. RETURN(cBits)
  202.  
  203. *************************************************************************
  204.  
  205.