Code: Select all
Procedure SaveToFile(*buf, buflen)
DefFile$ = "Untitled.txt"
Pattern$ = "All files (*.*)|*.*": Pattern = 0
Repeat
File$ = SaveFileRequester("Save File As...", DefFile$, Pattern$, Pattern)
If File$
CompilerIf #PB_Compiler_OS <> #PB_OS_MacOS
Select FileSize(File$)
Case -1: ;Not found
Case -2: MessageRequester("Error", "Name already exists as a directory"): Continue ;re-ask
Default:
Select MessageRequester("File Exists","Overwrite?", #PB_MessageRequester_YesNoCancel)
Case #PB_MessageRequester_No: Continue ;re-ask
Case #PB_MessageRequester_Cancel: Break
EndSelect
EndSelect
CompilerEndIf
;// At this stage it's safe to create and/or overwrite the file
hFile = CreateFile(#PB_Any, File$)
If hFile
If *buf: WriteData(hFile, *buf, buflen): EndIf
CloseFile(hFile)
MessageRequester("Saved", "Saved to "+ File$)
Else
MessageRequester("Error", "Couldn't create file")
Continue ;re-ask
EndIf
EndIf
Break
ForEver
EndProcedure
*buf=AllocateMemory(1)
SaveToFile(*buf,1) ;write 1 byte
;--------------------
Another version, this one simply returns a filename if it's ok to create and/or overwrite, so basically just the SaveFileRequester on its own but with non-Mac handler:
Code: Select all
Procedure.s SaveFilename(DefFile$, Pattern$, PatIndex)
Repeat
File$ = SaveFileRequester("Save File As...", DefFile$, Pattern$, PatIndex)
If File$
CompilerIf #PB_Compiler_OS <> #PB_OS_MacOS
Select FileSize(File$)
Case -1: ;Not found
Case -2: MessageRequester("Error", "Name already exists as a directory"): Continue ;re-ask
Default:
Select MessageRequester("File Exists","Overwrite?", #PB_MessageRequester_YesNoCancel)
Case #PB_MessageRequester_No: Continue ;re-ask
Case #PB_MessageRequester_Cancel: Break
EndSelect
EndSelect
CompilerEndIf
ProcedureReturn File$
EndIf
Break
ForEver
EndProcedure
TryAgain:
File$ = SaveFilename("Untitled.txt", "All files (*.*)|*.*", 0)
If File$
;// At this stage it's safe to create and/or overwrite the file
hFile = CreateFile(#PB_Any, File$)
If hFile
If *buf: WriteData(hFile, *buf, buflen): EndIf
CloseFile(hFile)
MessageRequester("Saved", "Saved to "+ File$)
Else
MessageRequester("Error", "Couldn't create file")
Goto TryAgain
EndIf
EndIf
But I prefer this version, which also takes care of checking if the file can actually be created, returning both the filename and handle:
Code: Select all
Procedure.i SaveFilename(DefFile$, Pattern$, PatIndex, *pname)
Repeat
File$ = SaveFileRequester("Save File As...", DefFile$, Pattern$, PatIndex)
If File$
CompilerIf #PB_Compiler_OS <> #PB_OS_MacOS
Select FileSize(File$)
Case -1: ;Not found
Case -2: MessageRequester("Error", "Name already exists as a directory"): Continue ;re-ask
Default:
Select MessageRequester("File Exists","Overwrite?", #PB_MessageRequester_YesNoCancel)
Case #PB_MessageRequester_No: Continue ;re-ask
Case #PB_MessageRequester_Cancel: Break
EndSelect
EndSelect
CompilerEndIf
hFile = CreateFile(#PB_Any, File$)
If Not hFile: Continue: EndIf
*Outname.String = @*pname
*Outname\s = File$
ProcedureReturn hFile
EndIf
Break
ForEver
EndProcedure
Define Fname$=Space(8192) ;must prealloc
hFile = SaveFilename("Untitled.txt", "All files (*.*)|*.*", 0, @Fname$)
If hFile
;WriteData(hFile...)
CloseFile(hFile)
MessageRequester("Saved","Saved to " + Fname$)
EndIf