Page 1 of 1

equivalent to SetEndOfFile_()?

Posted: Tue Nov 26, 2019 5:49 pm
by morosh
Hello:

is there any equivalent to the win api SetEndOfFile_()?

thanks

Re: equivalent to SetEndOfFile_()?

Posted: Tue Nov 26, 2019 6:11 pm
by Peter
SetEndOfFile_() of which file?

You surely know that for security reasons you cannot access local files from your browser?

Greetings ... Peter

Re: equivalent to SetEndOfFile_()?

Posted: Wed Nov 27, 2019 6:09 am
by Danilo
morosh wrote:is there any equivalent to the win api SetEndOfFile_()?
The following function truncates the file at the current file pointer location (FileSeek()).
You can truncate a file (make it smaller), but you can't extend the file size (make it larger).

Code: Select all

Procedure TruncateFile(file)
    !var f;
    !if (f = spider.file.objects.Get(v_file)) f.size = spider_Loc(v_file);
EndProcedure

file = CreateFile(#PB_Any,"",0)
If file
    WriteString(file,"0123456789",#PB_Unicode)
    
    Debug "Length of file:       " + Lof(file)
    Debug "Current file pointer: " + Loc(file)
    
    FileSeek(file,8)
    TruncateFile(file)
    
    ;WriteString(file,"Hello!",#PB_Unicode)
    ;WriteWord(file,0)
    
    Debug "Length of file:       " + Lof(file)
    Debug "Current file pointer: " + Loc(file)
    
    export = ExportFileMemory(file)
    If export
        Debug "-----"
        Debug "Length of memory: " + MemorySize(export)
        Debug PeekS(export,0,MemorySize(export),#PB_Unicode)
    EndIf
    CloseFile(file)
EndIf

Re: equivalent to SetEndOfFile_()?

Posted: Wed Nov 27, 2019 8:10 pm
by Danilo
Renamed SetEndOfFile() to TruncateFile(), so it is compatible with PureBasic. ;)

Re: equivalent to SetEndOfFile_()?

Posted: Thu Nov 28, 2019 5:33 pm
by morosh
Thank you Danilo
Perfect, exactly what I'm searching for.
By the way, I need also to CopyFile which is simple to implement (copying line by line), but is there something shorter with JS.
Also later I need to delete temporary files, no delete actually available

Regards

Re: equivalent to SetEndOfFile_()?

Posted: Fri Nov 29, 2019 5:36 am
by Danilo
What type of files do you work with, morosh? #PB_LocalStorage files?

Looks like DeleteFile() is missing from SB. For now you could overwrite it using CreateFile():CloseFile() to create a new file with size 0.

EDIT:

Code: Select all

;
; SpiderBasic 2.30 - Delete a file that was created using '#PB_LocalStorage' flag.
;
; ref: https://localforage.github.io/localForage/
;
Procedure DeleteFile(filename.s, flag.i = #PB_LocalStorage)
    If flag = #PB_LocalStorage
        !LocalForage.removeItem("sbfs_" + v_filename);
    EndIf
EndProcedure

Re: equivalent to SetEndOfFile_()?

Posted: Fri Nov 29, 2019 12:04 pm
by morosh
thank you Danilo
my files are on the server side, sometimes I need to do some processing, so I need to create a temporary file, fill it correctly, then copy it to the main file, then delete it (always server side).

Regards