[bb] PrintF() function by Dan [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:39

Previous topic - Next topic

BlitzBot

Title : PrintF() function
Author : Dan
Posted : 1+ years ago

Description : Prints a text to a file. If the file exists, it adds the text to the end of the file.
Every PrintF() call adds Chr$(13)+Chr$(10) to the end of the line (cr/lf).
The file is then openable in the editor (provided that you have used the printable chars ...)

The usage is Simple:

Type writeout
Field Filename$
Field OldFilename$
Field filestreamID
Field open
Field filenameset
End Type
Global pfile.writeout = New writeout

PrintF("c:filename00.txt",1) ; to set the filename
PrintF("text like you would use in a Print command") ;Writes to the same file
PrintF("this is then the new line in the file")      ;Writes to the same file and adds a new line
;...
PrintF("")   ; The last line of the text, equals the CloseFile command

PrintF("and this")                                   ;Writes to the same file and adds a new line

;comment the next line to remove the RunTimeError
PrintF("c:filename01.txt",1)  ;Should cause an RunTimeError because the last command was not  PrintF("")

PrintF("")     ;close the last file (in this case c:filename00.txt)
PrintF("c:filename01.txt",1)  ;Comment The Previous PrintF("c:filename01.txt",1) line and this line will open a new file !
PrintF("Writes to the filename01.txt")
PrintF("")    ;Closes the filename01.txt


After commenting the "PrintF("c:filename01.txt",1) ;Should cause an RunTimeError because the last command was not  PrintF("")" you should find 2 new files in your C:directory
(if you have the sufficient rights to write to it) [/i]

Code :
Code (blitzbasic) Select
;====================================================================
; Project: PrintF function
; Version: 1.0
; Author: Dan
; Email: -.-
; Copyright:  PD
; Description:      
;     Prints Text To a file ! (easy way)
;     every time PrintF is called it writes the text to the end of the last file set with PrintF("c:hey.txt,1) file.
;     Basic error checking is implemented,but you will need to ensure that the filename is valid
;     every PrintF("text") call adds CrLf ($0d and $0a bytes to the end of the end of the txt$)
;     so that the file can be opened with notepad
; Usage:
;       PrintF(Filename,1) to set the filename
;       PrintF("text")     to write to the file above
;       PrintF("")         to close the opened file !!!
;                          so that a new filename can be set
;===============================================================================================


Function PrintF(Txt$="",setf=0)
;Copy next 8 lines to the beginning of your program, and uncomment them (remove ; )
; Type writeout
; Field Filename$
; Field OldFilename$
; Field filestreamID
; Field open
; Field filenameset
; End Type
; Global pfile.writeout = New writeout
; Const loaddebug=0 ;Used for CheckLoad + CheckAnimLoad functions
; If loaddebug=1 Then PrintF("R:filesize.txt",1) ;to make a list with filenames and filesize. useful for releasing the games, to check if the file has been modified (is it only a simple size check)
;usage:
;PrintF("r: est.txt",1) ;to set the filename
;PrintF("test text") ;to write a line of text to the file
;PrintF("") ;To close the file ! Important before using another file to write!
; ;else it writes to the same file again
Select True
    Case setf=0 And pfilefilenameset=1
If Len(Txt$)>0 ;Is Length of the Text$ greater than 0
If pfileopen=0 ;has the file allready been opened ?
If pfileOldFilename$="" Then pfileOldFilename$=pfileFilename$ ;if no, set the oldfilename as filename$

If FileType(pfileFilename$)=0 ;Doesnt Exists, create new one
pfilefilestreamID=WriteFile(pfileFilename$)
ElseIf FileType(pfileFilename$)=2 ;It is a directory, Stop the program
RuntimeError "PrintF: The Filename is a directory, please check your script"
ElseIf FileType(pfileFilename$)=1 ;File Exists, open it to make additions !
pfilefilestreamID=OpenFile(pfileFilename$)
EndIf
If pfilefilestreamID=0 ;Check if the file could be opened
RuntimeError "PrintF: error cannot open "+pfileFilename$
Else ;The file exists, set the writing position to the end of the file !
SeekFile (pfilefilestreamID,FileSize(pfileFilename$))
EndIf
pfileopen=1 ;Global flag to indicate that the file is open !
ElseIf pfileopen=1 ;File has allready been opened, check if the filename is same (to prevent writing data to a wrong file !)
If pfileFilename$<>pfileOldFilename$
If pfilefilestreamID>0 Then CloseFile pfilefilestreamID
RuntimeError ("PrintF: Filename Missmatch "+pfileFilename$+" is not "+pfileOldFilename$)
EndIf
EndIf
;Write text string into the FilestreamId
For x=1 To Len(Txt$)
WriteByte pfilefilestreamID,Asc(Mid$(Txt$,x,1))
Next
;And add cr+lf, so it can be readed in text editor as new line
WriteByte pfilefilestreamID,$0d
WriteByte pfilefilestreamID,$0a
Else ;if length of the text$ is 0 then the file should be closed !
If pfilefilestreamID>0 Then CloseFile pfilefilestreamID
pfileopen=0
pfileOldFilename=""
EndIf
Case setf=1 And pfilefilenameset=0
  If Txt$="" Then RuntimeError "PrintF (txt$,1) is used To set a filename, And txt$ cannot be empty!"
pfileFilename$=Txt$
pfilefilenameset=1
Case setf=0 And pfilefilenameset=0
   RuntimeError "The Filename was not been set, use PrintF(''c:Filename'',1) before calling PrintF(''text'') writing function"
Case setf=1 And pfilefilenameset=1
   If pfileOldFilename="" And Txt$<>""
  pfileFilename=Txt$
Else
  RuntimeError " Close the filehandle with PrintF('''') before setting a new filename !"
EndIf
End Select
End Function


Comments : none...