AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [STOCK] File copy/cut (https://forums.alliedmods.net/showthread.php?t=130243)

ot_207 06-21-2010 17:45

[STOCK] File copy/cut
 
Here are some functions that can be useful to us all. You can copy any files with these utilities! (.wav, .mp3, .etcetera)
If you want to use this native on big files it is a possiblity that it will block the HLDS server! So watch out!

I will also add the copy folder stocks after I will finish with block wallhack.

PHP Code:

#define BUFFERSIZE    256

enum FWrite
{
    
FW_NONE 0,
    
FW_DELETESOURCE = (1<<0),
    
FW_CANOVERRIDE = (1<<1)
}
 
stock fcopy(read_path[300], dest_path[300], FWrite:flags FW_NONE

    
// Prepare for read  
    
new fp_read fopen(read_path"rb"

    
// No file to read, errors! 
    
if (!fp_read
    { 
        
fclose(fp_read
        return 

    


    
// If the native cannot override 
    
if (file_exists(dest_path) && !(flags FW_CANOVERRIDE)) 
    {
        return 

    


    
// Prepare for write  
    
new fp_write fopen(dest_path"wb"

    
// Used for copying 
    
static buffer[BUFFERSIZE
    static 
readsize 
  
    
// Find the size of the files
    
fseek(fp_read0SEEK_END);
    new 
fsize ftell(fp_read);
    
fseek(fp_read0SEEK_SET);
  
    
// Here we copy the info 
    
for (new 0fsize+= BUFFERSIZE
    { 
        
readsize fread_blocks(fp_readbufferBUFFERSIZEBLOCK_CHAR); 
        
fwrite_blocks(fp_writebufferreadsizeBLOCK_CHAR); 
    } 
  
    
// Close the files 
    
fclose(fp_read
    
fclose(fp_write
  
    
// Can delete source? 
    
if (flags FW_DELETESOURCE
        
delete_file(read_path
  
    
// Success 
    
return 



Alka 06-21-2010 18:18

Re: [STOCK] File copy/cut
 
You can make only just one stock, just add a bool: arg "bool:bCut", then check for it and delete the file :-D

Alucard^ 06-21-2010 20:43

Re: [STOCK] File copy/cut
 
Oh!! never thinked that this can be possible =o, cool!

With only one stock is something like this right?

Code:
stock fcopy(const read_path[], const dest_path[], cut=0) {     // Prepare for read and write     static buffer[BUFFERSIZE]     static readsize     new fp_read = fopen(read_path, "rb")     new fp_write = fopen(dest_path, "wb")           // No handles, errors!     if (!fp_read)         return 0           // Find the size of the files     fseek(fp_read, 0, SEEK_END);     new fsize = ftell(fp_read);     fseek(fp_read, 0, SEEK_SET);           // Here we copy the files from xxx.wav to our wave     for (new j = 0; j < fsize; j += BUFFERSIZE)     {        readsize = fread_blocks(fp_read, buffer, BUFFERSIZE, BLOCK_CHAR);        fwrite_blocks(fp_write, buffer, readsize, BLOCK_CHAR);     }           // Close the files     fclose(fp_read)     fclose(fp_write)         if(cut) delete_file(read_path);     // Success     return 1 }

And i have some doubts about this:

Code:
j += BUFFERSIZE

Why this? i understand that is j + 256 every time when is looping, but why? is becouse fread_blocks and fwrite_blocks read and write 256 characters at the same time?

Sry for my english.

Good Job!

EDIT: Here a simple example using the sock:

Code:
#include <amxmodx> #include <amxmisc> #define PLUGIN  "Copy/Cut Example" #define AUTHOR  "Alucard" #define VERSION "0.0.1" #define BUFFERSIZE    256 public plugin_init() {     register_plugin(PLUGIN, VERSION, AUTHOR);         register_concmd("amx_copyfile", "HookCmdCopyFile", ADMIN_RCON); } public HookCmdCopyFile(id, level, cid) {     if(!cmd_access(id, level, cid, 3) )         return PLUGIN_HANDLED;         new szFile[32], szDest[128];     read_argv(1, szFile, 31);     read_argv(2, szDest, 127);         static filePath[256], destPath[256];     get_configsdir(filePath, 255);         formatex(destPath, 255, "%s/%s", filePath, szDest);     format(filePath, 255, "%s/%s", filePath, szFile);         if(!file_exists(filePath) )     {         client_print(id, print_console, "El archivo que queres copiar no existe");         return PLUGIN_HANDLED;     }         CopyFile(filePath, destPath, 1);         return PLUGIN_HANDLED; }

ot_207 06-22-2010 02:55

Re: [STOCK] File copy/cut
 
Quote:

Originally Posted by Alka (Post 1215792)
You can make only just one stock, just add a bool: arg "bool:bCut", then check for it and delete the file :-D

Yup. Done!

@Alucard:
It is because that string must be loaded fully so we do not store into it the character "^0" which marks the end of the string. That is why the BUFFERSIZE is equal in all of the situations.
And the files that we are reading and writing can have sizes over 256. That is why we need to loop.

Alucard^ 06-22-2010 03:11

Re: [STOCK] File copy/cut
 
Yes yes i understand why we need the loop, but didn't understand the j += BUFFERSIZE part, but now is clear for me. =p

Now i have another question...

whats the diference betwen using bool: and not using it in this case? without bool: can't work?

ot_207 06-22-2010 06:03

Re: [STOCK] File copy/cut
 
Quote:

Originally Posted by Alucard^ (Post 1216068)
Yes yes i understand why we need the loop, but didn't understand the j += BUFFERSIZE part, but now is clear for me. =p

Now i have another question...

whats the diference betwen using bool: and not using it in this case? without bool: can't work?

It is basically the same thing.
I did it with bool just to look nicer.

Arkshine 06-22-2010 07:58

Re: [STOCK] File copy/cut
 
Any specific reason to use 3 lines to know the file size instead of using filesize() ?

ot_207 06-22-2010 08:00

Re: [STOCK] File copy/cut
 
Quote:

Originally Posted by Arkshine (Post 1216147)
Any specific reason to use 3 lines to know the file size instead of using filesize() ?

Just an example of how to determine filesize in a different way (with fseek).

Arkshine 06-22-2010 08:05

Re: [STOCK] File copy/cut
 
Ok. Though the tutorial is not about how to determine the size and it should use the existing native for the simplicity. Just my opinion, don't kill me. ^^

By the way, you should open fp_write after the check or adding fclose() into, because if fp_read is null, fp_write won't be closed.

joropito 06-22-2010 08:52

Re: [STOCK] File copy/cut
 
This is very useful. Another flag to avoid destination deletion will be nice.

I suggest to use this only at plugin_precache / init / cfg but not in-game because is a blocking function and with big files may cause problems.


All times are GMT -4. The time now is 15:02.

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