home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / clipper / cacode.zip / CADECODE.PRG < prev    next >
Text File  |  1993-07-14  |  6KB  |  234 lines

  1. /**************************************************************************
  2.   CA-Clipper Encode - File Encoder, ala uuencode.exe
  3.   By Kirby L. Wallace, Wallace Information Systems Engineering
  4.  
  5.    FUNCTION: CADecode(cFileName)
  6.  
  7.  PARAMETERS: cFileName : (from command line) Name of DOS File to
  8.                          convert to encoded file.
  9.  
  10. DESCRIPTION: CADecode() decodes a file created by CAEncode(), restoring
  11.              it to it's original state.
  12.  
  13.              Note that it is not necessary to specify the output
  14.              file name.  It is stored in the encoded file.
  15.  
  16.              See .DOC file for details
  17.  
  18.  
  19. ***************************************************************************/
  20.  
  21. #include "fileio.ch"
  22.  
  23. #define  MAXCHARS 1024
  24. #define  LF       CHR(13)+CHR(10)
  25. #define  TRUE     .T.
  26. #define  FALSE    .F.
  27.  
  28. /*************************************************************************/
  29. FUNCTION CADecode(cInFile)
  30. LOCAL    cOutFile  := ''
  31. LOCAL    i, j, k
  32. LOCAL    cBytesIn  := space(8)
  33. LOCAL    cBytesOut := ''
  34. LOCAL    cThisLine
  35. LOCAL    nIHandle, nOHandle
  36. LOCAL    cBits
  37. LOCAL    lHelp := FALSE
  38.  
  39. ? 'CADecode() - File Decoder - Kirby L. Wallace - March 1993'
  40. ? 'Version 2.0'
  41. ?
  42. ? 'Take only as directed - Beware of dogs! - Freeware! - Yow!'
  43. ?
  44. ? cInFile
  45.  
  46.  
  47. BEGIN SEQUENCE
  48.  
  49.  
  50. if cInFile = NIL
  51.     lHelp = TRUE
  52. else
  53.  
  54.     if cInFile $ '?' .or. cInFile = '/h'
  55.         lHelp = TRUE
  56.     endif
  57.  
  58. endif
  59.  
  60. if lHelp
  61.  
  62.     ?
  63.     ? 'Usage: CADECODE filename.ext'
  64.     ?
  65.     ? 'Where filename.ext = name of file created by CAENCODE.EXE'
  66.     ?
  67.  
  68.     BREAK
  69.  
  70. endif
  71.  
  72.  
  73. if file(cInFile)
  74.  
  75.     if ( nIHandle := fopen(cInFile) ) != -1
  76.  
  77.         cThisLine = ReadLine(nIHandle)
  78.  
  79.         if substr(cThisLine,1,10) != '+++CAFile:'
  80.             ? LF+'Input File not a valid CAEncode File'+LF
  81.             BREAK
  82.         else
  83.             cOutFile = substr(trim(cThisLine), 12)
  84.  
  85.             if ( nOHandle := fcreate(cOutFile) ) = -1
  86.                 ? LF+'Output File Create Error'+LF
  87.                 BREAK
  88.             else
  89.                 // if there are no error messages, this will append to the
  90.                 // first line with the input file name (infile --> outfile)
  91.                 ?? ' -->', cOutFile
  92.             endif
  93.  
  94.         endif
  95.  
  96.         while TRUE
  97.  
  98.             cThisLine = ReadLine(nIHandle)
  99.  
  100.             if substr(cThisLine,1,13) = '+++CABegin+++'
  101.                 exit
  102.             endif
  103.  
  104.         end
  105.  
  106.         while TRUE
  107.  
  108.             cThisLine = ReadLine(nIHandle)
  109.  
  110.             if substr(cThisLine,1,11) = '+++CAEnd+++'
  111.  
  112.                 fclose(nIHandle, nOHandle)
  113.  
  114.                 EXIT
  115.  
  116.             endif
  117.  
  118.             ?? '.' // an incredibly cutting-edge-of-technology, high-tech progress indicator
  119.  
  120.             for i = 1 to len(cThisLine) STEP 4
  121.  
  122.                 cBytesOut = ''
  123.                 cBytesIn = substr(cThisLine,i,4)
  124.                 cBits = ''
  125.  
  126.                 if len(cBytesIn) != 4
  127.  
  128.                     if len(cBytesIn) = 2
  129.  
  130.                         // convert two byte value remainder
  131.  
  132.                         cbits = _int2b(asc(substr(cBytesIn,1,1))-48,2) + ;
  133.                                 _int2b(asc(substr(cBytesIn,2,1))-48,6)
  134.  
  135.                         cBytesOut = chr(_b2int(cBits))
  136.  
  137.                         fwrite(nOHandle,cBytesOut)
  138.  
  139.                         EXIT
  140.  
  141.                     else
  142.  
  143.                         // convert three byte remainder
  144.  
  145.                         if len(cBytesIn) = 3
  146.  
  147.                             cBits = _int2b(asc(substr(cBytesIn,1,1))-48,6)+;
  148.                                     _int2b(asc(substr(cBytesIn,2,1))-48,6)+;
  149.                                     _int2b(asc(substr(cBytesIn,3,1))-48,6)
  150.  
  151.                             cBits = substr(cBits,3)
  152.  
  153.                             cBytesOut = chr(_b2int(substr(cBits,1,8)))+;
  154.                                         chr(_b2int(substr(cBits,9,8)))
  155.  
  156.                             fwrite(nOHandle,cBytesOut)
  157.  
  158.                             EXIT
  159.  
  160.                         endif
  161.  
  162.                     endif
  163.                 else
  164.  
  165.                     for j = 1 to len(cBytesIn)
  166.                         cBits += _int2b(asc(substr(cBytesIn,j,1))-48,6)
  167.                     next
  168.  
  169.                     for j = 1 to len(cBits) STEP 8
  170.                         cBytesOut += chr(_b2int(substr(cBits,j,8)))
  171.                     next
  172.  
  173.                     fwrite(nOHandle,cBytesOut)
  174.  
  175.                 endif
  176.  
  177.             next
  178.  
  179.         end
  180.     else
  181.         ? LF+'Input File Open Error'+LF
  182.         BREAK
  183.     endif
  184. else
  185.  
  186.     ? LF+'Input File Not Found'+LF
  187.  
  188. endif
  189.  
  190. END SEQUENCE
  191.  
  192.  
  193. RETURN(NIL)
  194.  
  195.  
  196. /*************************************************************************/
  197. FUNCTION ReadLine(nHandle,nLength)
  198. LOCAL    cRet_Val := ''
  199. LOCAL    cCharBuffer
  200. LOCAL    nPointer
  201. LOCAL    nChars_Read
  202. LOCAL    nLineFeedAt
  203.  
  204. cCharBuffer = space(iif(nLength = NIL,MAXCHARS,nLength))
  205. nLength     = iif(nLength = NIL,MAXCHARS,nLength)
  206.  
  207. BEGIN SEQUENCE
  208.  
  209.     // note current position
  210.     nPointer = fseek(nHandle,0,FS_RELATIVE)
  211.  
  212.     (nChars_Read := fread(nHandle,@cCharBuffer,nLength))
  213.  
  214.     if ( nLineFeedAt := at(LF,cCharBuffer) ) > 0
  215.  
  216.         // got a line with Line Feed, ELSE return all of what we read
  217.  
  218.         cRet_Val = substr(cCharBuffer,1,nLineFeedAt-1)
  219.  
  220.         // reset pointer to next line
  221.         fseek(nHandle,nPointer,FS_SET)
  222.         fseek(nHandle,nPointer+nLineFeedAt+1,FS_SET) // 2 = crlf pair
  223.  
  224.     else
  225.  
  226.         cRet_Val = substr(cCharBuffer,1,nLength)
  227.  
  228.     endif
  229.  
  230. END SEQUENCE
  231.  
  232. RETURN(trim(cRet_Val))
  233.  
  234.