home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / clipper / snip0693.zip / FILEIN.PRG < prev    next >
Text File  |  1993-03-13  |  4KB  |  192 lines

  1. /***************************************************************************
  2.  
  3.     FileIn.PRG
  4.  
  5.     EXAMPLE SYNTAX: FILEIN CAPFIRST.HEX CAPFIRST.OBJ
  6.  
  7.     FILEIN Takes a HEX text file and converts it to it's DOS file
  8.     equivelent.  The HEX text file MUST have been created with
  9.     FILEOUT.PRG.
  10.  
  11.     For instance, if CAPFIRST.HEX contains a HEX dump of CAPFIRST.OBJ
  12.     then to create CAPFIRST.OBJ from CAPFIRST.HEX, enter:
  13.  
  14.         FILEIN CAPFIRST.HEX CAPFIRST.OBJ
  15.  
  16.     For this example, cut the HEX dump that follows (in a following
  17.     message) into it's own file (name it what you will).  Cut it exactly
  18.     at the lines it begins and ends on.
  19.  
  20.     You can then use the resulting CAPFIRST.OBJ as a valid Object
  21.     File, just like any other.
  22.  
  23.     Compile with: -m -n
  24.     Link With   : whatever
  25.  
  26.     Kirby L. Wallace
  27.     120 N. Sandusky Ave.
  28.     Tulsa, OK.  74115
  29.  
  30.     RELEASED TO THE PUBLIC DOMAIN: March 1993
  31.  
  32. ***************************************************************************/
  33.  
  34. #include "fileio.ch"
  35. #define  MAXCHARS 1024
  36. #define  LF CHR(13)+CHR(10)
  37.  
  38. ****************************************************************************
  39. FUNCTION FileIn( cInFile, cOutFile )
  40.  
  41. LOCAL   nIHandle, nOHandle
  42. LOCAL   cByte := ' ', i
  43. LOCAL   cThisLine
  44.  
  45. BEGIN SEQUENCE
  46.  
  47.     if !file(cInFile)
  48.         ?
  49.         ? 'Input File Not Found: '+cInFIle
  50.         BREAK
  51.  
  52.     endif
  53.  
  54.     nOHandle = fcreate(cOutFile)
  55.  
  56.     if ferror() != 0
  57.         ?
  58.         ? 'Output File Create Error: '+cOutFile
  59.         BREAK
  60.     endif
  61.  
  62.     nIHandle = fopen(cInFile)
  63.  
  64.     if ferror() != 0
  65.         ?
  66.         ? 'Input File Open Error: '+cInFile
  67.         BREAK
  68.     endif
  69.  
  70.     while !_FEOF(nIHandle)
  71.  
  72.         cThisLine = _ReadLine(nIHandle)
  73.  
  74.         for i = 1 to len(cThisLine) STEP 2
  75.             fwrite(nOHandle,chr(_HToI(substr(cThisLine,i,2))))
  76.         next
  77.  
  78.     end
  79.  
  80.  
  81. END SEQUENCE
  82.  
  83. fclose(nIHandle) ; fclose(nOHandle)
  84.  
  85. RETURN(NIL)
  86.  
  87. ****************************************************************************
  88. FUNCTION _ReadLine(nHandle,nLength)
  89. LOCAL    cRet_Val := ''
  90. LOCAL    cCharBuffer
  91. LOCAL    nPointer
  92. LOCAL    nChars_Read
  93. LOCAL    nLineFeedAt
  94.  
  95. cCharBuffer = space(iif(nLength = NIL,MAXCHARS,nLength))
  96. nLength     = iif(nLength = NIL,MAXCHARS,nLength)
  97.  
  98. BEGIN SEQUENCE
  99.  
  100.     // note current position
  101.     nPointer = fseek(nHandle,0,FS_RELATIVE)
  102.  
  103.     (nChars_Read := fread(nHandle,@cCharBuffer,nLength))
  104.  
  105.     if ( nLineFeedAt := at(LF,cCharBuffer) ) > 0
  106.  
  107.         // got a line with Line Feed, ELSE return all of what we read
  108.  
  109.         cRet_Val = substr(cCharBuffer,1,nLineFeedAt-1)
  110.  
  111.         // reset pointer to next line
  112.         fseek(nHandle,nPointer,FS_SET)
  113.         fseek(nHandle,nPointer+nLineFeedAt+1,FS_SET) // 2 = crlf pair
  114.  
  115.     else
  116.  
  117.         cRet_Val = substr(cCharBuffer,1,nLength)
  118.  
  119.     endif
  120.  
  121. END SEQUENCE
  122.  
  123. RETURN(trim(cRet_Val))
  124.  
  125. ****************************************************************************
  126. FUNCTION _FEOF(nHandle)
  127. LOCAL nCurrentPointer := fseek(nHandle,0,FS_RELATIVE)
  128. LOCAL nEOFPosition := fseek(nHandle,0,FS_END)
  129. LOCAL k := ' '
  130.  
  131. fseek(nHandle,nCurrentPointer,FS_SET) // reset pointer to original position
  132.  
  133. RETURN(iif(nCurrentPointer >= nEOFPosition,.t.,.f.))
  134.  
  135.  
  136.  
  137.  
  138. ****************************************************************************
  139. FUNCTION _HToI(cVar)
  140. LOCAL    nInt := 0
  141. LOCAL    i
  142. LOCAL    nVal
  143.  
  144. for i = 1 to len(cVar)
  145.      if upper(substr(cVar,i,1)) $ 'ABCDEF'
  146.          nVal = 9 + _ap(substr(cVar,i,1))
  147.      else
  148.          nVal = val(substr(cVar,i,1))
  149.      endif
  150.  
  151.      nInt += nVal * _to_power(16,len(cVar)-i)
  152.  
  153. next
  154.  
  155. RETURN(nInt)
  156.  
  157.  
  158. ****************************************************************************
  159. FUNCTION _AP(cAp_Char)
  160. RETURN(iif(cAp_Char=NIL,0,at(lower(cAp_Char),'abcdefghijklmnopqrstuvwxyz')))
  161.  
  162.  
  163. ****************************************************************************
  164. FUNCTION _To_Power(nNum1, nNum2)
  165. LOCAL    nNum3, i
  166.  
  167. do case
  168.  
  169.     case nNum2 == 0
  170.         nNum3 = 1
  171.  
  172.     case nNum2 == 1
  173.         nNum3 = nNum1
  174.  
  175.     case nNum2 > 0
  176.          nNum3 = 1
  177.  
  178.          for i = 1 to nNum2
  179.              nNum3 *= nNum1
  180.          next
  181.  
  182.     case nNum2 < 0
  183.         nNum3 = 0.000000 + nNum1
  184.  
  185.         for i = 0 to nNum2 STEP -1
  186.             nNum3 /= nNum1
  187.         next
  188.  
  189. endcase
  190.  
  191. RETURN(nNum3)
  192.