home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / CLIPPER / FTOOL / FTOOL.PRG
Text File  |  1993-12-05  |  6KB  |  218 lines

  1.  /*
  2.   * File......: FTOOL.PRG
  3.   * Author....: Torsten Radtke
  4.   * CIS ID....: 100113,717
  5.   * Date......: $Date$ 11/24/1993
  6.   * Revision..: $Revision$ 1.0
  7.   * Log file..: $Logfile$
  8.   * 
  9.   * This is an original work by Torsten Radtke and is placed in the
  10.   * public domain.
  11.   *
  12.   * Modification history:
  13.   * ---------------------
  14.   *
  15.   * $Log$
  16.   *
  17.   *  $DOC$
  18.   *
  19.   *  $FUNCNAME$
  20.   *     FT_STRFILE()
  21.   *
  22.   *  $ONELINER$
  23.   *     Write a string to a file
  24.   *
  25.   *  $SYNTAX$
  26.   *     FT_STRFILE([<cString>],<cFile>[,<lOverwrite>]
  27.   *     [,<nOffset>][,<lTruncate>]) -> nByteWritten
  28.   *
  29.   *  $ARGUMENTS$
  30.   *  <cString> is the string to be written to a file.
  31.   *
  32.   *  <cFile> specifies a filename. Drive and path can be included
  33.   *  (no Wildcards!).
  34.   *
  35.   *  <lOverwrite> not specified or .F. means the file will be created new
  36.   *  in any case. .T. will write to an existing file.
  37.   *  Default: Create new (.F.)
  38.   *
  39.   *  <nOffset> This option allows a position to be specified, from where
  40.   *  the String will be written into the file.
  41.   *  Default: End of file
  42.   *
  43.   *  <lTruncate> If specified as .T. this option will truncate the
  44.   *  remainder of the file, if the written data ends before the last byte
  45.   *  of the file.
  46.   *  Default: Don't truncate (.F.)
  47.   *
  48.   *  $RETURNS$
  49.   *  nByteWritten
  50.   *  Returns the number of bytes that have been written to the file,
  51.   *  or 0 if an error occured.
  52.   *
  53.   *  $DESCRIPTION$
  54.   *  This function is another possibility to write the content of a string 
  55.   *  into a file. Contrary to the CA-Clipper Fxxxx()-functions only one
  56.   *  function call is needed to write the data. FT_STRFILE() creates the
  57.   *  targetfile in any case. This function was created when the original
  58.   *  CA-Tools III function STRFILE() caused an VM-Error on different PC's
  59.   *
  60.   *  $EXAMPLES$
  61.   *    ? FT_STRFILE("ABCDEFGH", "TEST.TXT", .T.)        // Result: 8
  62.   *
  63.   *  ■ A file with drive- and path specification.
  64.   *
  65.   *    ? FT_STRFILE("0123456789", "C:\DOCS\TEST.TXT")      // Resultat: 10
  66.   *
  67.   *  ■ Data in an existing file will be overwritten with the specified string
  68.   *    from position 20.
  69.   *
  70.   *    ? FT_STRFILE("COMPUTER ASSOCIATES", "TEST.TXT", .T., 20)   // Result: 9
  71.   *
  72.   *  ■ A string with a length of 5 bytes will be written from position 10
  73.   *    of a 20 bytes long file. Because the last argument is once .F.,
  74.   *    once .T. the results are different.
  75.   *
  76.   *    FT_STRFILE(REPLICATE("X", 20), "TEST.TXT")
  77.   *    FT_STRFILE("AAAAA", "TEST.TXT", .T., 10, .F.) // "XXXXXXXXXXAAAAAXXXXX"
  78.   *    FT_STRFILE("AAAAA", "TEST.TXT", .T., 10, .T.) // "XXXXXXXXXXAAAAA"    
  79.   *  $SEEALSO$
  80.   *  $INCLUDE$
  81.   *  $END$
  82.   */
  83.  
  84. function FT_STRFILE(cString,cFile,lOverwrite,nOffset,lTruncate)
  85.  
  86.   PRIVATE nRetVal:=0
  87.  
  88.   IF cString==NIL
  89.     cString:=""
  90.   ENDIF
  91.   
  92.   IF cFile==NIL
  93.     RETURN(nRetVal)
  94.   ENDIF
  95.   
  96.   IF lOverwrite==NIL
  97.     lOverwrite:=.F.
  98.   ENDIF
  99.   
  100.   IF nOffset==NIL
  101.     nOffset:=-1
  102.   ENDIF
  103.   
  104.   IF lTruncate==NIL
  105.     lTruncate:=.f.
  106.   ENDIF
  107.   
  108.   IF lOverwrite
  109.     IF FILE(cFile)
  110.       nHandle:=FOPEN(cFile,18)
  111.       IF FERROR()!=0
  112.         RETURN(nRetVal)
  113.       ENDIF
  114.       IF lTruncate
  115.         IF nOffset=-1
  116.           FSEEK(nHandle,0,2)
  117.         ELSE
  118.           IF nOffset<(32*1024)+1
  119.             cBuffer:=SPACE(nOffset)
  120.             nBYTES:=FREAD(nHandle,@cBuffer,nOffset)
  121.             FCLOSE(nHandle)
  122.             nHandle:=FCREATE(cFile)
  123.             IF FERROR()!=0
  124.               RETURN(nRetVal)
  125.             ELSE
  126.               FWRITE(nHandle,cBuffer)
  127.             ENDIF
  128.           ELSE  /* create temporary file */
  129.             nHandle2:=FCREATE("___ftool.$$$")
  130.             IF FERROR()!=0
  131.               FCLOSE(nHandle)
  132.               RETURN(nRetVal)
  133.             ELSE
  134.               DO WHILE nOffset>(32*1024)
  135.                 cBuffer:=SPACE(32*1024)
  136.                 nBYTES:=FREAD(nHandle,@cBuffer,32*1024)
  137.                 IF FERROR()!=0
  138.                   FCLOSE(nHandle)
  139.                   FCLOSE(nHandle2)
  140.                   DELETE FILE ("___ftool.$$$")
  141.                   RETURN(nRetVal)
  142.                 ELSE
  143.                   nBYTES:=FWRITE(nHandle2,cBuffer)
  144.                   IF nBYTES!=(32*1024)
  145.                     FCLOSE(nHandle)
  146.                     FCLOSE(nHandle2)
  147.                     DELETE FILE ("___ftool.$$$")
  148.                     RETURN(nRetVal)
  149.                   ELSE
  150.                     nOffset:=nOffset-(32*1024)
  151.                   ENDIF
  152.                 ENDIF
  153.               ENDDO
  154.               IF nOffset>0
  155.                 cBuffer:=SPACE(nOffset)
  156.                 nBYTES:=FREAD(nHandle,@cBuffer,nOffset)
  157.                 IF FERROR()!=0
  158.                   FCLOSE(nHandle)
  159.                   FCLOSE(nHandle2)
  160.                   DELETE FILE ("___ftool.$$$")
  161.                   RETURN(nRetVal)
  162.                 ELSE
  163.                   nBYTES:=FWRITE(nHandle2,cBuffer)
  164.                   IF nBYTES!=nOffset
  165.                     FCLOSE(nHandle)
  166.                     FCLOSE(nHandle2)
  167.                     DELETE FILE ("___ftool.$$$")
  168.                     RETURN(nRetVal)
  169.                   ELSE
  170.                     FCLOSE(nHandle)
  171.                     FCLOSE(nHandle2)
  172.                     DELETE FILE &cFile
  173.                     COPY FILE ("___ftool.$$$") TO &cFile
  174.                     DELETE FILE ("___ftool.$$$")
  175.                     nHandle:=FOPEN(cFile,18)
  176.                     IF FERROR()!=0
  177.                       RETURN(nRetVal)
  178.                     ELSE
  179.                       FSEEK(nHandle,0,2)
  180.                     ENDIF
  181.                   ENDIF
  182.                 ENDIF
  183.               ENDIF
  184.             ENDIF
  185.           ENDIF
  186.         ENDIF
  187.       ELSE
  188.         IF nOffset=-1
  189.           FSEEK(nHandle,0,2)
  190.           IF FERROR()!=0
  191.             FCLOSE(nHandle)
  192.             RETURN(nRetVal)
  193.           ENDIF
  194.         ELSE
  195.           FSEEK(nHandle,nOffset,0)
  196.           IF FERROR()!=0
  197.             FCLOSE(nHandle)
  198.             RETURN(nRetVal)
  199.           ENDIF
  200.         ENDIF
  201.       ENDIF
  202.     ELSE
  203.       nHandle:=FCREATE(cFile)
  204.       IF FERROR()!=0
  205.         RETURN(nRetVal)
  206.       ENDIF
  207.     ENDIF
  208.   ELSE
  209.     nHandle:=FCREATE(cFile)
  210.     IF FERROR()!=0
  211.       RETURN(nRetVal)
  212.     ENDIF
  213.   ENDIF
  214.   nRetVal:=FWRITE(nHandle,cString)
  215.   FCLOSE(nHandle)
  216.  RETURN(nRetVal)
  217. * -----------------------------------------------------------
  218.