Raised This Month: $51 Target: $400
 12% 

[SourcePawn] Copy File in binary mode


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
javalia
Senior Member
Join Date: May 2009
Location: korea, republic of
Old 06-23-2011 , 06:12   [SourcePawn] Copy File in binary mode
Reply With Quote #1

SP has fully working FILE IO functions that are wrapped C/C++`s IO functions.

now i am writing here a small snippet that let u copy file in binary mode.
u have no need to use OS`s command to copy a file via some extension, and that kind of work is not so recommended.

anyway, here we go, yea, code is SHORT

Code:
new String:path[256];
    BuildPath(Path_SM, path, 256, "01_-_Seikan_Hikou.mp3");//lankachan.
//sourcemod`s IO system is sandboxed to be only work on directory of sm, the sourcemod/ directory. so , fo this test, i putted that mp3 file on sourcemod/ directory.

    new Handle:file = OpenFile(path, "rb");

    if(file != INVALID_HANDLE){
        
        BuildPath(Path_SM, path, 256, "01_-_Seikan_Hikou2.mp3");/lets copy as this
        new Handle:copytarget = OpenFile(path, "wb");//it will overwrite if there is anything already exist

        if(copytarget != INVALID_HANDLE){
            
            new buffer[32];//lets read/write 32 bytes fir time at max. why? just for fun. k
            //remember that we should read byte by byte.
//source pawn`s cell type is 4 byte, so, in this case, at least, 3 * 32 bytes will be waisted at runtime. but that is not so big deal.
//we have no choice about this waist, so keep go on.
            new readcache;//this will remember how much bytes we readed.

            while(!IsEndOfFile(file)){

                readcache = ReadFile(file, buffer, 32, 1);
                WriteFile(copytarget, buffer, readcache, 1);

            }

            CloseHandle(copytarget);

        }

        CloseHandle(file);

    }
now i copied nice mp3 file, and window media player was played that file as well as original one.
__________________

Last edited by javalia; 06-23-2011 at 12:52.
javalia is offline
javalia
Senior Member
Join Date: May 2009
Location: korea, republic of
Old 06-23-2011 , 06:19   Re: [SourcePawn] Copy File in binary mode
Reply With Quote #2

well, getting file`s size is not needed at all, but i am added it while i am coding and lazy to remove it, so, let us thing that as part of nice snippet k
__________________
javalia is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 06-23-2011 , 11:35   Re: [SourcePawn] Copy File in binary mode
Reply With Quote #3

PHP Code:
new String:buffer[32]; 
I'm pretty sure the "String:" here is wrong and would give you tag mismatches.

While we're at it,
PHP Code:
readcache ReadFile(filebuffersizeof(buffer), 1); 
future-proofs this in case you change the size of buffer.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
javalia
Senior Member
Join Date: May 2009
Location: korea, republic of
Old 06-23-2011 , 12:23   Re: [SourcePawn] Copy File in binary mode
Reply With Quote #4

thanks dude, instead of uploading untested snippet, i just tested it myself, and now it works fine.k
__________________
javalia is offline
ProdigySim
SourceMod Plugin Approver
Join Date: Feb 2010
Old 06-23-2011 , 13:28   Re: [SourcePawn] Copy File in binary mode
Reply With Quote #5

It seems like you could avoid the wasted-space by either reading size=4 chunks or using String:buf[];. Not 100% sure of the implications of either, though.

Also note that you can move files with RenameFile().

Edit:
Quote:
Originally Posted by javalia
//sourcemod`s IO system is sandboxed to be only work on directory of sm, the sourcemod/ directory.
This is patently false. BuildPath is used for sourcemod/ directory files to keep compatibility (in case someone chooses a sourcemod path other than addons/sourcemod/), but you can still manipulate files outside of this directory.

Last edited by ProdigySim; 06-23-2011 at 13:35.
ProdigySim is offline
javalia
Senior Member
Join Date: May 2009
Location: korea, republic of
Old 06-23-2011 , 23:32   Re: [SourcePawn] Copy File in binary mode
Reply With Quote #6

well, i saw ReadFile function`s code of SM.
and, ReadFile function was designed to work with cell type buffer.
and, when we are forcing to read 4 bytes from file and there is only 2 bytes left to read on file, the fread of C will make 2 result : read 2 bytes of junk data from outbound of file with file`s left 2 byte or dont read left 2 bytes at all.
both of them are bad result, so that is why i made it to read byte by byte.
this memory waist is not so good but also not so critical.
and, when i tried c:\\somefilename to open, that try was failed.
__________________
javalia is offline
Thrawn2
Veteran Member
Join Date: Apr 2009
Old 06-24-2011 , 01:50   Re: [SourcePawn] Copy File in binary mode
Reply With Quote #7

performance will be better using 4k blocks i guess...
__________________
einmal mit profis arbeiten. einmal.
Thrawn2 is offline
javalia
Senior Member
Join Date: May 2009
Location: korea, republic of
Old 06-24-2011 , 06:44   Re: [SourcePawn] Copy File in binary mode
Reply With Quote #8

that kind of optimize will not work cuz of struct of ReadFile function of SM.
it reads exact size that we specificated as size of each element per time.
so..unless they make those functions to work with String Array....
well, for it, they also should have some String -> cell byte copy function and vice vera?
__________________
javalia is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:59.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode