AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Manipulate string in file (https://forums.alliedmods.net/showthread.php?t=329259)

iceeedr 12-15-2020 17:07

Manipulate string in file
 
Is there a way to pull a line up without leaving a space? when using "^r" there is an "invisible space on the line.

Example:

PHP Code:

0

if(iFile)
{
    new 
szData[120], szData1[120]

    while(
fgets(iFileszDatacharsmax(szData)))
    {
        
i++
        switch(
szData[0])
        {
            case 
EOS';''/': continue
            default:
            {
                
parse(szDataszData1charsmax(szData1))

                if(
equal(szData1example))
                {
                    
write_file(szConfig"^r"i-1)                        
                    break
                }
            }
        }
    }

    
fclose(iFile)



Bugsy 12-15-2020 21:35

Re: Manipulate string in file
 
Provide the sample output so I don't need to create a text file, compile your plugin, and test it myself.

Easiest may be just to set the last character to EOS.

iceeedr 12-15-2020 21:43

Re: Manipulate string in file
 
The result looks like the example below:
PHP Code:

[Sector 1]
blablabla

[Sector2

So, in the plugin I detect the line that contains "blablabla" and use "^r"

So the text looks like this:
PHP Code:

[Sector 1]
"invisible space"

[Sector2

The line is not "empty" it is as if I gave a space and skipped the line.

Bugsy 12-15-2020 22:06

Re: Manipulate string in file
 
I am having no issue. Though, I am working on very little.. you have not said what exactly you are trying to do or what the source file contents look like. If you need help, provide the problem at hand, not your troubleshooting.

PHP Code:

public a()
{
    new 
iFile fopen"abc123.txt""rt" );
    
    new 
0
    
    
if(iFile)
    {
        new 
szData[120], szData1[120]
    
        while(
fgets(iFileszDatacharsmax(szData)))
        {
            
i++
            switch(
szData[0])
            {
                case 
EOS';''/': continue
                default:
                {
                    new 
iz parse(szDataszData1charsmax(szData1))
    
                    
server_print("[%s]" szData1)                        
                        
                
                }
            }
        }
    
        
fclose(iFile)
    }


Source file
Code:

Test line1
Test line2
Test line3
Test line4
Test line5
Test line6
Test line7
Test line8
T1119
T1110
T1111
T1112

Output
PHP Code:

[Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
T1119]
[
T1110]
[
T1111]
[
T1112


iceeedr 12-15-2020 22:26

Re: Manipulate string in file
 
2 Attachment(s)
Quote:

Originally Posted by Bugsy (Post 2728895)
I am having no issue. Though, I am working on very little.. you have not said what exactly you are trying to do or what the source file contents look like. If you need help, provide the problem at hand, not your troubleshooting.

PHP Code:

public a()
{
    new 
iFile fopen"abc123.txt""rt" );
    
    new 
0
    
    
if(iFile)
    {
        new 
szData[120], szData1[120]
    
        while(
fgets(iFileszDatacharsmax(szData)))
        {
            
i++
            switch(
szData[0])
            {
                case 
EOS';''/': continue
                default:
                {
                    new 
iz parse(szDataszData1charsmax(szData1))
    
                    
server_print("[%s]" szData1)                        
                        
                
                }
            }
        }
    
        
fclose(iFile)
    }


Source file
Code:

Test line1
Test line2
Test line3
Test line4
Test line5
Test line6
Test line7
Test line8
T1119
T1110
T1111
T1112

Output
PHP Code:

[Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
Test]
[
T1119]
[
T1110]
[
T1111]
[
T1112


I'll be as clear as possible, because I don't think you understand.

Let's use the function below as a test to remove an admin for example, after finding the line with steamid I remove it ok?

PHP Code:

public RemoveAdminHandler(id)
{
    
get_user_authid(g_AdminTarget[id], playerinfocharsmax(playerinfo));

    new 
szConfig[MAX_FMT_LENGTH]
    
get_configsdir(szConfigcharsmax(szConfig))
    
add(szConfigcharsmax(szConfig), "/users.ini")

    new 
iFile fopen(szConfig"rt")

    
0

    
if(iFile)
    {
        new 
szData[120], szData1[120]

        while(
fgets(iFileszDatacharsmax(szData)))
        {
            
i++
            switch(
szData[0])
            {
                case 
EOS';''/''[': continue
    
                default:
                {
                    
parse(szDataszData1charsmax(szData1))

                    if(
equal(szData1playerinfo))
                    {
                        
write_file(szConfig"^r"i-1)
                        
server_cmd("amx_reloadadmins")
                        break
                    }
                }
            }
        }

        
fclose(iFile)
    }

    return 
PLUGIN_HANDLED


In the attachment I put user.ini (which is the test before deletion) and users - after.ini (which is after removing the line), notice that in the 1st line of the "after" file there is an "invisible" space.

Bugsy 12-15-2020 23:09

Re: Manipulate string in file
 
I would change your approach. Create an entirely new temp file with lines that should be there and when done, delete the original and do a rename on the temp. Backup your users.ini beforehand.
PHP Code:

public RemoveAdminHandlerid 
{
    
get_user_authid(g_AdminTarget[id], playerinfocharsmax(playerinfo));

    new 
szConfigTemp[MAX_FMT_LENGTH];
    new 
szConfig[MAX_FMT_LENGTH];
    
get_configsdir(szConfigcharsmax(szConfig))
    
add(szConfigcharsmax(szConfig), "/users.ini")
    
add(szConfigTempcharsmax(szConfigTemp), "/usersTemp.ini");
    
    new 
iFile fopen(szConfig"rt")
    
    if(
iFile)
    {
        new 
szData[120], szData1[120]

        while(
fgets(iFileszDatacharsmax(szData)))
        {
            switch(
szData[0])
            {
                case 
EOS';''/''['write_file(szConfigTempszData)
                default:
                {
                    
parse(szDataszData1charsmax(szData1))
                    
                    if(!
equal(szData1playerinfo))
                    {
                        
write_file(szConfigTempszData1)
                    }
                }
            }
        }

        
fclose(iFile)
    }
    

    
delete_fileszConfig );
    
rename_fileszConfigTemp szConfig );

    
server_cmd("amx_reloadadmins")

    return 
PLUGIN_HANDLED



fysiks 12-15-2020 23:19

Re: Manipulate string in file
 
Calling it an "invisible" space is redundant because it's just the regular space character (0x20), what you get when you press your space bar.

When I use:

PHP Code:

write_file("test.txt""^r"2

on this file:
Code:

hello
world
good
bye

I get:
Code:

hello
world

bye


Also, the fact that you're trying to edit a file with write_file() while that same file is open with fopen() is a problem in general (I'm not sure if AMX Mod X has any safe guards in place to prevent bad behavior when you do this).

iceeedr 12-16-2020 10:58

Re: Manipulate string in file
 
Our worst enemy is fatigue, taking into account all the help and even what was punctuated by the fysiks, we arrived at the code below which is apparently functional, thanks.

PHP Code:

new szConfigTemp[MAX_FMT_LENGTH];
new 
szConfig[MAX_FMT_LENGTH];
get_configsdir(szConfigcharsmax(szConfig))
add(szConfigcharsmax(szConfig), "/users.ini")
add(szConfigTempcharsmax(szConfigTemp), "/usersTemp.ini");

new 
iFile fopen(szConfig"r")
new 
iFile2 fopen(szConfigTemp"w")
if(
iFile && iFile2)
{
    new 
szData[120], szData1[120]

    while(
fgets(iFileszDatacharsmax(szData)))
    {
            switch(
szData[0])
            {
                case 
EOS';''/''['fputs(iFile2szData)
                default:
                {
                    
parse(szDataszData1charsmax(szData1))

                    if(!
equal(szData1playerinfo))
                    {
                        
fputs(iFile2szData)
                    }
                }
            }
    }

    
fclose(iFile)
    
fclose(iFile2)
}

delete_fileszConfig );
rename_fileszConfigTemp szConfig ); 


Bugsy 12-16-2020 17:05

Re: Manipulate string in file
 
Cool, pretty much the proper way to code what I provided.

iceeedr 12-16-2020 17:08

Re: Manipulate string in file
 
Quote:

Originally Posted by Bugsy (Post 2728973)
Cool, pretty much the proper way to code what I provided.

I am sorry because even though I am able to write and read English fluently, sometimes I do not understand when the phrase is sarcasm or real, so I will understand it as a compliment. If not, I apologize because the credit is yours and the fysiks for the help, thank you.


All times are GMT -4. The time now is 14:13.

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