AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [SourcePawn] Copy File in binary mode (https://forums.alliedmods.net/showthread.php?t=159895)

javalia 06-23-2011 06:12

[SourcePawn] Copy File in binary mode
 
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.

javalia 06-23-2011 06:19

Re: [SourcePawn] Copy File in binary mode
 
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

Powerlord 06-23-2011 11:35

Re: [SourcePawn] Copy File in binary mode
 
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.

javalia 06-23-2011 12:23

Re: [SourcePawn] Copy File in binary mode
 
thanks dude, instead of uploading untested snippet, i just tested it myself, and now it works fine.k

ProdigySim 06-23-2011 13:28

Re: [SourcePawn] Copy File in binary mode
 
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.

javalia 06-23-2011 23:32

Re: [SourcePawn] Copy File in binary mode
 
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.

Thrawn2 06-24-2011 01:50

Re: [SourcePawn] Copy File in binary mode
 
performance will be better using 4k blocks i guess...

javalia 06-24-2011 06:44

Re: [SourcePawn] Copy File in binary mode
 
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?


All times are GMT -4. The time now is 21:53.

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