Raised This Month: $32 Target: $400
 8% 

[STOCK] File copy/cut


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ot_207
Veteran Member
Join Date: Jan 2008
Location: Romania The Love Country
Old 06-21-2010 , 17:45   [STOCK] File copy/cut
Reply With Quote #1

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 

__________________
My approved plug-ins | Good for newbies! | Problems?

Back, will come around when I have time.

Last edited by ot_207; 06-22-2010 at 19:17.
ot_207 is offline
Alka
AMX Mod X Plugin Approver
Join Date: Dec 2006
Location: malloc(null)
Old 06-21-2010 , 18:18   Re: [STOCK] File copy/cut
Reply With Quote #2

You can make only just one stock, just add a bool: arg "bool:bCut", then check for it and delete the file :-D
__________________
Still...lovin' . Connor noob! Hello
Alka is offline
Alucard^
AMXX Moderator: Others
Join Date: Sep 2007
Location: Street
Old 06-21-2010 , 20:43   Re: [STOCK] File copy/cut
Reply With Quote #3

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; }
__________________
Approved Plugins - Steam Profile

Public non-terminated projects:
All Admins Menu, HLTV parameters, Subnick,
Second Password (cool style), InfoZone,
Binary C4 plant/defuse, and more...

Private projects:
NoSpec (+menu), NV Surf Management,
PM Adanved System, KZ longjump2, and more...

Last edited by Alucard^; 06-22-2010 at 00:25.
Alucard^ is offline
Send a message via Skype™ to Alucard^
ot_207
Veteran Member
Join Date: Jan 2008
Location: Romania The Love Country
Old 06-22-2010 , 02:55   Re: [STOCK] File copy/cut
Reply With Quote #4

Quote:
Originally Posted by Alka View Post
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.
__________________
My approved plug-ins | Good for newbies! | Problems?

Back, will come around when I have time.
ot_207 is offline
Alucard^
AMXX Moderator: Others
Join Date: Sep 2007
Location: Street
Old 06-22-2010 , 03:11   Re: [STOCK] File copy/cut
Reply With Quote #5

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?
__________________
Approved Plugins - Steam Profile

Public non-terminated projects:
All Admins Menu, HLTV parameters, Subnick,
Second Password (cool style), InfoZone,
Binary C4 plant/defuse, and more...

Private projects:
NoSpec (+menu), NV Surf Management,
PM Adanved System, KZ longjump2, and more...

Last edited by Alucard^; 06-22-2010 at 03:13.
Alucard^ is offline
Send a message via Skype™ to Alucard^
ot_207
Veteran Member
Join Date: Jan 2008
Location: Romania The Love Country
Old 06-22-2010 , 06:03   Re: [STOCK] File copy/cut
Reply With Quote #6

Quote:
Originally Posted by Alucard^ View Post
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.
__________________
My approved plug-ins | Good for newbies! | Problems?

Back, will come around when I have time.
ot_207 is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 06-22-2010 , 07:58   Re: [STOCK] File copy/cut
Reply With Quote #7

Any specific reason to use 3 lines to know the file size instead of using filesize() ?
__________________
Arkshine is offline
ot_207
Veteran Member
Join Date: Jan 2008
Location: Romania The Love Country
Old 06-22-2010 , 08:00   Re: [STOCK] File copy/cut
Reply With Quote #8

Quote:
Originally Posted by Arkshine View Post
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).
__________________
My approved plug-ins | Good for newbies! | Problems?

Back, will come around when I have time.
ot_207 is offline
Arkshine
AMX Mod X Plugin Approver
Join Date: Oct 2005
Old 06-22-2010 , 08:05   Re: [STOCK] File copy/cut
Reply With Quote #9

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.
__________________

Last edited by Arkshine; 06-22-2010 at 08:08.
Arkshine is offline
joropito
AlliedModders Donor
Join Date: Mar 2009
Location: pfnAddToFullPack
Old 06-22-2010 , 08:52   Re: [STOCK] File copy/cut
Reply With Quote #10

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.
__________________

Divide et vinces
approved plugins | steam account

I don't accept PM for support. Just ask on forums.
If you're looking for private work, PM me.
joropito is offline
Send a message via MSN to joropito
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 05:51.


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