[STOCK] File copy/cut

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1267
Gender:
United States of America

[STOCK] File copy/cut

Post by Archive-Poster »

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.

Code: Select all

#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 0 
    } 

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

    // 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_read, 0, SEEK_END);
    new fsize = ftell(fp_read);
    fseek(fp_read, 0, SEEK_SET);
  
    // Here we copy the info 
    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) 
  
    // Can delete source? 
    if (flags & FW_DELETESOURCE) 
        delete_file(read_path) 
  
    // Success 
    return 1 
} 
User avatar
Archive-Poster
Posts: 1267
Gender:
United States of America

Re: [STOCK] File copy/cut

Post by Archive-Poster »

Here a simple example using the stock:

Code: Select all

#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, ""The file you want to copy doesn't exist"");
        return PLUGIN_HANDLED;
    }
    
    CopyFile(filePath, destPath, 1);
    
    return PLUGIN_HANDLED;
}
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest