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

Solved [HELP] Replace a line from .ini


Post New Thread Reply   
 
Thread Tools Display Modes
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-18-2018 , 20:02   Re: [HELP] Replace a line from .ini
Reply With Quote #11

You can also use contain()/containi() > -1 instead of equal() to search for lines containing your search string.
__________________
Bugsy is offline
CrAzY MaN
Senior Member
Join Date: Mar 2017
Location: India
Old 04-19-2018 , 04:52   Re: [HELP] Replace a line from .ini
Reply With Quote #12

Quote:
Originally Posted by HamletEagle View Post
You don't need an array, it's totally pointless.

PHP Code:
stock RemoveLine(const FileName[], const OldLine[], const NewLine[])
{
    new const 
TempFileName[] = "tempfile.ini"

    
new ConfigDirPath[128]; get_configsdir(ConfigDirPathcharsmax(ConfigDirPath))
    new 
FullPath[256]; formatex(FullPathcharsmax(FullPath), "%s/%s"ConfigDirPathFileName)

    new 
FilePointer fopen(FullPath"rt")
    if(
FilePointer)
    {
        new 
TempFilePath[256]; formatex(TempFilePathcharsmax(TempFilePath), "%s/%s"ConfigDirPathTempFileName)
        
        new 
InputFilePointer fopen(TempFilePath"wt")
        if(
InputFilePointer)
        {
            new 
FileData[128]
            while(!
feof(FilePointer))
            {
                
fgets(FilePointerFileDatacharsmax(FileData))
                
trim(FileData)
                
                if(
equal(FileDataOldLine))
                {
                    continue
                }
                
fprintf(InputFilePointer"%s^n"FileData)
            }
            
            
fclose(InputFilePointer)
            
fclose(FilePointer)

            
delete_file(FullPath)
            
rename_file(TempFilePathFullPath1)
            
            return 
1
        
}
    }
    return 
0

This is a function that I wrote in the past to remove a line from file. To replace instead of remove put a fprintf before continue with the new string.
Is return 0 compulsory?
And where did you removed line in this code?
Where did you use NewLine[]?
__________________

Last edited by CrAzY MaN; 04-19-2018 at 05:25.
CrAzY MaN is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 04-19-2018 , 06:15   Re: [HELP] Replace a line from .ini
Reply With Quote #13

Read what I said after the code. Add a fprintf with NewLine before "continue".

return is there so you know if the function succeded or not.
__________________

Last edited by HamletEagle; 04-19-2018 at 06:16.
HamletEagle is offline
CrAzY MaN
Senior Member
Join Date: Mar 2017
Location: India
Old 04-19-2018 , 14:17   Re: [HELP] Replace a line from .ini
Reply With Quote #14

Sorry, but i still didnt get it

PHP Code:
#include <amxmodx>
#include <colorchat>

#define PLUGIN "Whatsapp_Group_Request"
#define VERSION "1.0"
#define AUTHOR "CrAzY MaN"

#define xPrefix "Whatsapp Group"

#define ADMIN_FLAG ADMIN_BAN

new iRequestsFile[64];

public 
plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
register_cvar(PLUGINVERSIONFCVAR_SERVER|FCVAR_SPONLY);
    
    
register_clcmd("say /whatsapp""MainMenu"_"Brings Up The Menu");
    
register_clcmd("say whatsapp""MainMenu"_"Brings Up The Menu");
    
    
register_concmd("Type_Your_Whatsapp_Number""Request_To_Add"); //REQUESTING TO ADD
    
register_concmd("amx_viewrequests""View_Requests"); //VIEW REQUESTS TO ADMIN
    
    //YOU CAN SEE REQUESTS IN THIS FILE
    
formatex(iRequestsFilecharsmax(iRequestsFile), "addons/amxmodx/configs/whatsapp_group_request.ini");
}

public 
MainMenu(id)
{
    new 
menu menu_create("\wWant to \rjoin \wour \yWhatsapp Group\y?""main_menu_handler");

    
menu_additem(menu"Yes"""0); // case 0
    
menu_additem(menu"No"""0); // case 1

    
menu_setprop(menuMPROP_EXITMEXIT_NEVER); // REMOVES EXIT BUTTON FROM MENU
    
    
menu_display(idmenu0);

    return 
PLUGIN_HANDLED;
}

public 
main_menu_handler(idmenuitem)    
{
    new 
data[6], accesscallback;
    
menu_item_getinfo(menuitemaccessdata,charsmax(data), _,_callback);

    switch(
item)
    {
        
//YES
        
case : {
                
client_cmd(id"messagemode Type_Your_Whatsapp_Number");
                
ColorChat(idTEAM_COLOR"^4[%s] ^1Enter Your ^3Whatsapp Number."xPrefix);
                
menu_destroy(menu);
            }
        
//NO
        
case : {
                
                
ColorChat(idTEAM_COLOR"^4[%s] ^1You ^3denied ^1joining our ^3Whatsapp Group!"xPrefix);
                
menu_destroy(menu);
            }
    }
    
    
menu_destroy(menu)
    
MainMenu(id);
    return 
PLUGIN_HANDLED;
}
    
public 
Request_To_Add(id)
{
    new 
Number[64], g_szName[32], g_szAuthID[32], szData[64], iFIlebool:requested false;
    
    
get_user_name(idg_szNamecharsmax(g_szName));
    
get_user_authid(idg_szAuthIDcharsmax(g_szAuthID));
    
    if(
file_exists(iRequestsFile))
    {
        
iFIle fopen(iRequestsFile"rt");
        
        while(!
feof(iFIle))
        {
            
fgets(iFIleszDatacharsmax(szData));
            
            if(!
szData[0] || szData[0] == ';' || szData[0] == ' ' || ( szData[0] == '/' && szData[1] == '/' ))
                continue;
                
            if(
containi(szDatag_szName) == -1)
                continue;
            
            else
                
requested true;
                
            break;
        }
         
        
fclose(iFIle);
        
        if(
requested)
        {
                
ColorChat(idTEAM_COLOR"^4[%s] ^1You ^3can't ^1request ^3more than 1 ^1number."xPrefix);
                
ColorChat(idTEAM_COLOR"^4[%s] ^1If you ^4typed ^3wrong number, ^1contact any ^4Admin."xPrefix);
                return 
PLUGIN_HANDLED;
        }
    }
    
    
read_argv(1Number63);
    
    
//CHECKS IF NUMBER IS CORRECT OR NOT
    
for(new i<= strlen(Number); i++)
    {
        if((
isalpha(Number[i])) || (strlen(Number) < 10)|| (strlen(Number) > 14) || ((Number[0] != '+')  && (strlen(Number) > 10)) )
        {
                
ColorChat(idTEAM_COLOR"^4[%s] ^1Noob, A ^4Mobile Number ^1is of ^4(+Country Code)^3 10 Digits Only!"xPrefix)
                return 
PLUGIN_HANDLED;
        }
    }
    
    
iFIle fopen(iRequestsFile"at");
    
    
//FORMAT IN THE FILE
    
formatex(szDatacharsmax(szData), "^n[REQUEST] %s(%s) : %s"g_szNameg_szAuthIDNumber)
    
fputs(iFIleszData);
    
fclose(iFIle);
    
ColorChat(idTEAM_COLOR"^4[%s] ^4Thank You! ^1You will be ^4added ^1to our ^3whatsapp group ^4ASAP."xPrefix);
    
ColorChat(0TEAM_COLOR"^4[%s] ^3%s ^1has ^4requested ^1to ^4join ^1our ^3Official Whatsapp Group."xPrefixg_szName);
    
ColorChat(0TEAM_COLOR"^4[%s] ^1Type ^4/whatsapp ^1if you want to ^4Join Group ^1too."xPrefix);
    return 
PLUGIN_CONTINUE;
}

public 
View_Requests(id)
{
    if(!(
get_user_flags(id) & ADMIN_FLAG))
    {
        
client_print(idprint_console"Sorry! We can share this only to ADMINs.");
        return 
PLUGIN_HANDLED;
    }
    
    if(!
file_exists(iRequestsFile))
    {
        
client_print(idprint_console"Whoops! File does not exist.");
        return 
PLUGIN_HANDLED;
    }
    
    new 
szData[512], f;
    
client_print(idprint_console"--------------------------------------------------------------------^n                            WHATSAPP GROUP REQUESTS");
    
fopen(iRequestsFile"rt");
    while(!
feof(f))
    {
        
fgets(fszDatacharsmax(szData))
        
trim(szData);
        
client_print(idprint_consoleszData)
    }
    
fclose(f);
    return 
PLUGIN_HANDLED;

Can you explain me from this plugin?
__________________

Last edited by CrAzY MaN; 04-19-2018 at 14:28.
CrAzY MaN is offline
CrAzY MaN
Senior Member
Join Date: Mar 2017
Location: India
Old 04-26-2018 , 05:50   Re: [HELP] Replace a line from .ini
Reply With Quote #15

Ah..
Can i use it simply like
replace_string , replace etc
__________________
CrAzY MaN is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 04-26-2018 , 06:46   Re: [HELP] Replace a line from .ini
Reply With Quote #16

No, I can not. It's a simple function. Just paste it into your plugin and use it. If you can not do that, then I'm sorry but I can not help.
Of course, if you have a concrete question about the function then I will gladly answer.
__________________

Last edited by HamletEagle; 04-26-2018 at 06:46.
HamletEagle is offline
CrAzY MaN
Senior Member
Join Date: Mar 2017
Location: India
Old 04-26-2018 , 09:04   Re: [HELP] Replace a line from .ini
Reply With Quote #17

I mean is it possible with replace_string etc. Without writing that long stocks;
__________________
CrAzY MaN is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 04-26-2018 , 09:49   Re: [HELP] Replace a line from .ini
Reply With Quote #18

Quote:
Originally Posted by CrAzY MaN View Post
I mean is it possible with replace_string etc. Without writing that long stocks;
No.
__________________
HamletEagle is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-26-2018 , 09:50   Re: [HELP] Replace a line from .ini
Reply With Quote #19

Read the line
Search for what you are looking for - containi()
Call replace/replace_all on that line
Write the edited line to file

If you're asking if you can avoid what HamletEagle posted then possibly. You would need to read all file data into a buffer then call replace/replace_all on that and then write the buffer to a new file, delete old and rename new to old. Keep in mind this would replace everything in the file, not just on a specific line.

With either option, you will be using a function to do the work, there are no 1 line natives to do this.
__________________

Last edited by Bugsy; 04-26-2018 at 11:11.
Bugsy is offline
CrAzY MaN
Senior Member
Join Date: Mar 2017
Location: India
Old 04-26-2018 , 12:44   Re: [HELP] Replace a line from .ini
Reply With Quote #20

Quote:
Originally Posted by Bugsy View Post
Read the line
Search for what you are looking for - containi()
Call replace/replace_all on that line
Write the edited line to file
Thats what exactly i meant.
Thanks BTW.
__________________
CrAzY MaN is offline
Reply


Thread Tools
Display Modes

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 00:23.


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