AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   New file natives... (https://forums.alliedmods.net/showthread.php?t=55756)

SweatyBanana 05-28-2007 23:38

New file natives...
 
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;
}


pRED* 05-28-2007 23:42

Re: New file natives...
 
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?

scrtxxcaz 05-29-2007 00:02

Re: New file natives...
 
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

SweatyBanana 05-29-2007 00:35

Re: New file natives...
 
Quote:

Originally Posted by scrtxxcaz (Post 483189)
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.

slmclarengt 05-29-2007 01:19

Re: New file natives...
 
Quote:

Originally Posted by SweatyBanana (Post 483194)
because that's not optimized.

But it's a temporary work-around until you FIND an optimized solution :-)

Slmclarengt

pRED* 05-29-2007 02:21

Re: New file natives...
 
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.

_Master_ 05-29-2007 03:11

Re: New file natives...
 
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.


All times are GMT -4. The time now is 10:38.

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