Raised This Month: $ Target: $400
 0% 

New file natives...


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
SweatyBanana
BANNED
Join Date: Sep 2005
Location: LOL
Old 05-28-2007 , 23:38   New file natives...
Reply With Quote #1

Well Emp has gotten me this far with new file natives, but we are having a problem getting the semi-colon to the beginning of the line. Currently it is just added to the end of the line.

Code:
#include <amxmodx>
#include <amxmisc>

#define PLUGIN "Remove admins"
#define VERSION "0.1"
#define AUTHOR "SweatyBanana"

enum{TYPE_STEAM, TYPE_NAME}

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR);
    register_clcmd("amx_removeadmin","remove_cmd",ADMIN_RCON,"amx_removeadmin <steamid or nick>");
}

public remove_cmd(id,level,cid)
{
    if( !(get_user_flags(id)&ADMIN_RCON) )
        return PLUGIN_HANDLED;

    new players[32], inum, i, player;
    new TARGET[32], playerinfo[32];
    new command_type;
    
    get_players(players,inum);
    
    read_argv(1,TARGET,31);
    remove_quotes(TARGET);

    if( equal(TARGET, "STEAM_", 6) )
    {
        command_type = TYPE_STEAM;

        for(i = 0; i < inum; i++)
        {
            player = players[i];
            
            get_user_authid(player, playerinfo, 31);
            
            if( equal(playerinfo, TARGET) )
            {
                remove_user_flags(player);
                break;
            }
        }
    }
    else
    {
        command_type = TYPE_NAME;

        for(i = 0; i < inum; i++)
        {
            player = players[i];
            
            get_user_name(player, playerinfo, 31);
            if( containi(playerinfo,TARGET) != -1 )
            {
                remove_user_flags(player);
                break;
            }
        }
    }
    
    new filename[64], text[51];
    get_configsdir(filename, 63);
    format(filename, 63, "%s/users.ini", filename);

    new file = fopen(filename, "a+");

    while(!feof(file))
    {
        fgets(file, text, 50);

        if( text[0] == ';')
            continue;

        parse( text, playerinfo, 31, text, 1);

        if( ( command_type == TYPE_STEAM && equal(playerinfo, TARGET))
        || ( command_type == TYPE_NAME && containi(playerinfo, TARGET) != -1) )
        {
            //need to rewind to the begining of this line somehow...
            fputs(file,    ";");

            console_print(id,"********************ADMIN ID REMOVAL TOOL**************");
            console_print(id,"");
            console_print(id," The target, %s, was removed from users.ini ",TARGET);
            console_print(id,"");
            console_print(id,"********************ADMIN ID REMOVAL TOOL**************");

            break;
        }
    }

    fclose(file);

    return PLUGIN_HANDLED;
}

Last edited by SweatyBanana; 05-28-2007 at 23:42.
SweatyBanana is offline
Send a message via AIM to SweatyBanana Send a message via Yahoo to SweatyBanana
pRED*
Join Date: Dec 2006
Old 05-28-2007 , 23:42   Re: New file natives...
Reply With Quote #2

Trying using ftell before you read in the line (and cache the result), read the line in, add your ;, then use fseek to go back to the cached position and write there?
pRED* is offline
scrtxxcaz
Senior Member
Join Date: Feb 2007
Location: a place u cant find
Old 05-29-2007 , 00:02   Re: New file natives...
Reply With Quote #3

instead of using fputs which will just add to the line that is already there try this..
Code:
   new msg[256]
   format(msg,255,"; %s",text);
   replace(text, 511, text, msg)
   write_file(file, text, line - 1)
this should replace the line with the same text that was parsed and add the ; in front
__________________
Please leave your name when giving me any karma

Counter Strike Freeze Tag
Status: Testing.... (bugs)

Revised Registration
Status: In Progress
scrtxxcaz is offline
Send a message via AIM to scrtxxcaz
SweatyBanana
BANNED
Join Date: Sep 2005
Location: LOL
Old 05-29-2007 , 00:35   Re: New file natives...
Reply With Quote #4

Quote:
Originally Posted by scrtxxcaz View Post
instead of using fputs which will just add to the line that is already there try this..
Code:
   new msg[256]
   format(msg,255,"; %s",text);
   replace(text, 511, text, msg)
   write_file(file, text, line - 1)
this should replace the line with the same text that was parsed and add the ; in front
because that's not optimized.
SweatyBanana is offline
Send a message via AIM to SweatyBanana Send a message via Yahoo to SweatyBanana
slmclarengt
Veteran Member
Join Date: Jul 2004
Location: The Cookie Jar... or Pul
Old 05-29-2007 , 01:19   Re: New file natives...
Reply With Quote #5

Quote:
Originally Posted by SweatyBanana View Post
because that's not optimized.
But it's a temporary work-around until you FIND an optimized solution :-)

Slmclarengt
__________________
But we don’t beat the Reaper by living longer. We beat the Reaper by living well. -Dr. Randy Pausch, R.I.P.

Come play WC3:FT on BnD Clan Server! You know you want to: Connect to WC3:FT BnD - go ahead click me!
slmclarengt is offline
pRED*
Join Date: Dec 2006
Old 05-29-2007 , 02:21   Re: New file natives...
Reply With Quote #6

AFAIK the old file natives open a file, seek to the file you specified do the read/write and then close the file. Sequentially reading to find the line you want is a slow task..

Try using ftell and fseek to overwrite a line (I've never tried it but I think it might work), but I think i've read somewhere that the only way to do this is to create a backup file and sequentially copy lines from the file into the backup. When you reach the file you want to modify do the modification and copy that across. Once you reach the end of the file, delete it and rename the backup to the original name.
pRED* is offline
_Master_
Senior Member
Join Date: Dec 2006
Old 05-29-2007 , 03:11   Re: New file natives...
Reply With Quote #7

Change this to fit your needs.

Note: If you don't need the line number ther you should use fread_blocks() and fwrite_blocks() instead.

Last edited by _Master_; 05-29-2007 at 03:19.
_Master_ 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 08:11.


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