home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a075 / 1.img / TOOLKIT1.EXE / SST239.PRG < prev    next >
Encoding:
Text File  |  1989-10-22  |  5.7 KB  |  204 lines

  1. ********************
  2.  
  3. FUNCTION Packdbt
  4.  
  5.    PARAMETERS _tfile
  6.  
  7.    * This function has two anciliary funtions
  8.    * that are part of this main routine.  They
  9.    * are: MAKEDBTHEAD() and COPYDBT()
  10.  
  11.    IF PCOUNT() = 0
  12.       RETURN(.F.)
  13.    ELSE
  14.       IF TYPE("_tfile") != "C"
  15.          RETURN(.F.)
  16.       ELSE
  17.          IF !FILE(_tfile)
  18.             RETURN(.F.)
  19.          ELSE
  20.             IF !ISA_DBF(_tfile)
  21.                RETURN(.F.)
  22.             ENDIF
  23.          ENDIF
  24.       ENDIF
  25.    ENDIF
  26.  
  27.    IF EMPTY(AT(".", _tfile))
  28.       _root = _tfile
  29.       _dbt  = ".DBT"
  30.       _ext  = ".DBF"
  31.    ELSE
  32.       _root = Parsing(@_tfile, ".")
  33.       _dbt  = ".DBT"
  34.       _ext  = "." + _tfile
  35.    ENDIF
  36.  
  37.    USE (_root + _ext)
  38.    _thehead = HEADER()
  39.    _theleng = RECSIZE()
  40.    _thenumb = LASTREC()
  41.    USE
  42.  
  43.    * Now get the number of memos in the file 
  44.    * and their beginning byte position.
  45.    * The 'whereat' variable is a _string which
  46.    * needs to be parsed in order to determine
  47.    * the position of the .DBT.
  48.    _whereat = ""
  49.    _memos = FTYPECNT(_root + _ext , "M", @_whereat)
  50.  
  51.    _fhandle = FOPEN(_root + _ext, 2)    && Original Database  <-- IN READ/WRITE MODE
  52.    _newdbt  = FCREATE("SJSTEMP.$$$")    && New .DBT file
  53.    _olddbt  = FOPEN(_root + _dbt)       && Original .DBT file
  54.    IF !MAKEDBTHEAD(_newdbt, _olddbt)    && Creates the header
  55.       RETURN(.F.)                       && An error has occured
  56.    ENDIF
  57.  
  58.    * if the _dbhandle is o.k., continue, otherwise, quit
  59.    _written = 1
  60.  
  61.    FSEEK(_fhandle, _thehead, 0)
  62.    FOR _qqaz = 1 TO _thenumb
  63.       _string = SPACE(_theleng)
  64.       _justok = FSEEK(_fhandle, 0, 1)      && Gets the file pointer
  65.       FREAD(_fhandle, @_string, _theleng)  && Get the record
  66.       STORE _whereat TO _tempparse         && Store the parse to a temp
  67.       FOR _qqay = 1 TO _memos
  68.          _z = PARSING(@_tempparse)
  69.          _dbtpoint = VAL(SUBSTR(_string, VAL(_z), 10))
  70.          IF !EMPTY(_dbtpoint)
  71.             _writeback = TRANSFORM(_written, "9999999999")
  72.             _string = SUBSTR(_string, 1, VAL(_z) -1) + _writeback + ;
  73.                      SUBSTR(_string, VAL(_z)+10)
  74.             _numblocks = COPYDBT(_olddbt, _newdbt, _dbtpoint, _written)
  75.             IF EMPTY(_numblocks)   && An error has occured.
  76.                RETURN(.F.)
  77.             ENDIF
  78.             _written = _written + _numblocks
  79.          ENDIF
  80.       NEXT
  81.       * Reposition the file pointer to the beginning of the record
  82.       FSEEK(_fhandle, _justok, 0)   
  83.       * Write out the new _string which in turn points to the next record
  84.       FWRITE(_fhandle, _string)      
  85.       IF FERROR() != 0
  86.          RETURN(.F.)
  87.       ENDIF
  88.    NEXT
  89.    FWRITE(_newdbt,  I2BIN(26), 1)
  90.    FCLOSE(_fhandle)
  91.    FCLOSE(_newdbt)
  92.    FCLOSE(_olddbt)
  93.    ERASE &_root.&_dbt.
  94.    RENAME SJSTEMP.$$$ TO &_root.&_dbt.
  95.    RETURN(.T.)
  96.  
  97. *********************
  98.  
  99. FUNCTION Makedbthead
  100.  
  101.    PARAMETERS handle1, handle2
  102.  
  103.    * handle1 is the file handle to the temp DBT
  104.    * handle2 is the file handle to the good DBT file
  105.  
  106.    FSEEK(handle2, 0, 0)
  107.    FWRITE(handle1, I2BIN(3), 1)
  108.    FOR _qqaz = 2 TO 100
  109.       FWRITE(handle1, I2BIN(0), 1)
  110.    NEXT
  111.    FWRITE(handle1, "Packed b_qqay Steve Straley's ToolkiT(tm) ")
  112.    FWRITE(handle1, "Published b_qqay Four Season's Publishing - 212-599-2141")
  113.    IF FERROR() != 0
  114.       RETURN(.F.)
  115.    ENDIF
  116.    DO WHILE FSEEK(handle1, 0, 1) != 512
  117.       FWRITE(handle1, I2BIN(0), 1)
  118.    ENDDO
  119.    RETURN((FERROR() = 0))
  120.  
  121. *****************
  122.  
  123. FUNCTION Copydbt
  124.  
  125.    PARAMETERS _filepoint, _farpoint, _filestart, _farstart
  126.  
  127.    *          old dbt   , new dbt,   old point,  new point
  128.  
  129.    * take the file pointer for the old dbt file '_farpoint'
  130.    * and write out the contents to the new dbt file in the
  131.    * file pointer '_filepoint'.  The position in the old dbt
  132.    * file to start looking at is in '_filestart' and the return
  133.    * value will be the block pointer in the new dbt file
  134.  
  135.    * _seedpoint is the original file position in the old DBT file
  136.  
  137.    _seedpoint = FSEEK(_filepoint, 1, 0)  
  138.  
  139.    * set the file pointer in the NEW dbt to the end of the last
  140.    * block _written to it
  141.    
  142.    bytecount = 0
  143.  
  144.    * go to the beginning block pointer in the old dbt
  145.  
  146.    FSEEK(_filepoint, (512 * _filestart), 0)   
  147.  
  148.    * read the file and find the end of memo counter
  149.    * and if the byte is not 1A, then write out the
  150.    * byte to the new DBT file.
  151.  
  152. *   _www = " "
  153. *   DO WHILE .T.
  154. *      FREAD(_filepoint, @_www, 1)
  155. *      IF BIN2I(_www) != 26
  156. *         bytecount = bytecount + 1
  157. *         FWRITE(_farpoint, _www)
  158. *         IF FERROR() != 0
  159. *            RETURN(0)
  160. *         ENDIF
  161. *      ELSE
  162. *         EXIT
  163. *      ENDIF
  164. *   ENDDO
  165.  
  166.    _notnow = .T.
  167.    DO WHILE _notnow
  168.       _www = SPACE(512)
  169.       FREAD(_filepoint, @_www, 512)
  170.       IF EMPTY(AT(CHR(26),_www))  && No memo marker
  171.          bytecount = bytecount + 512
  172.          FWRITE(_farpoint, _www)
  173.       ELSE
  174.          bytecount = bytecount + ( AT(CHR(26),_www)-1 )
  175.          FWRITE(_farpoint, _www, ( AT(CHR(26),_www)-1 ) )
  176.          _notnow = .F.
  177.       ENDIF
  178.       IF FERROR() != 0
  179.          RETURN(0)
  180.       ENDIF
  181.    ENDDO
  182.        
  183.    
  184.    * find out how man_qqay blocks were _written out 
  185.    * to the new file.
  186.  
  187.    _blckcount = (INT(bytecount / 512)) + 1
  188.  
  189.    * figure out how man_qqay bytes until the end of the 
  190.    * next memo file, reposition the files, and return
  191.    * the number of blocks _written
  192.  
  193.    _remaining = (_blckcount * 512) - bytecount - 1
  194.    FWRITE(_farpoint, I2BIN(26), 1)
  195.    FWRITE(_farpoint, I2BIN(32), _remaining)
  196.    IF FERROR() != 0
  197.       RETURN(0)
  198.    ENDIF
  199.    FSEEK(_filepoint, _seedpoint, 0)            && Original dbt
  200.    RETURN(_blckcount)
  201.  
  202. * End of File
  203.  
  204.