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

[STOCK] delete_file_lines


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
eyal282
Veteran Member
Join Date: Aug 2011
Old 06-30-2017 , 14:34   [STOCK] delete_file_lines
Reply With Quote #1

So I don't like the fact that you can't delete lines from a file so I made this stock and plugin to fix it.

Example usage:

PHP Code:
new File[150];
get_localinfo("amxx_datadir"Filecharsmax(File)); // This is equal to get_datadir without include.

formatex(Filecharsmax(File), "%s/Test"File);

if(!
dir_exists(File))
    
mkdir(File); // Creates the directory.

formatex(Filecharsmax(File), "%s/Test.txt"File);

// Use this to delete all empty lines.
delete_file_lines(File);

// Use this to delete lines 1, 3, 5.
delete_file_lines(File, {135})

// Use this to delete lines from array.
new Lines[5] = {15742};
delete_file_lines(FileLines); 
Explanation of arguments:

PHP Code:
// File[150] = File to delete lines from.
// Lines[] = Array with all lines to delete. Leave empty to remove empty lines.

// return -1 if deleting has failed, otherwise returns amount of lines deleted ( or zero lines ) 
Give thoughts on the snippet and lots of hating criticism.

Notes:

1. Because of AmxModX's limits, do not use files with lines with over 500 letters in them.
2. Do not use often as this stock is quite large in CPU usage.
3. I could not properly check the plugin, so if you find a bug notify me.

The stock:
Spoiler

Last edited by eyal282; 07-01-2017 at 06:36.
eyal282 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-30-2017 , 15:38   Re: [STOCK/PLUGIN] delete_file_lines
Reply With Quote #2

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

    
static ConfigDirPath[128]; 
    if(
ConfigDirPath[0] == EOS)
    {
        
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(FileDataLine))
                {
                    continue
                }
                
                
fprintf(InputFilePointer"%s^n"FileData)
            }
            
            
fclose(InputFilePointer)
            
fclose(FilePointer)

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

I did something like this long time ago. Maybe you want to take a look at it. Right now it removes a specific line from file, but it can be edited to remove multiple lines by using a bi dimensional array.
I did not read your code, just rapidly looked at it and there may be things can be done a lot simpler.
__________________
HamletEagle is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 06-30-2017 , 16:41   Re: [STOCK/PLUGIN] delete_file_lines
Reply With Quote #3

Quote:
Originally Posted by HamletEagle View Post
PHP Code:
stock RemoveLine(const FileName[], const Line[])
{
    new const 
TempFileName[] = "tempfile.ini"

    
static ConfigDirPath[128]; 
    if(
ConfigDirPath[0] == EOS)
    {
        
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(FileDataLine))
                {
                    continue
                }
                
                
fprintf(InputFilePointer"%s^n"FileData)
            }
            
            
fclose(InputFilePointer)
            
fclose(FilePointer)

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

I did something like this long time ago. Maybe you want to take a look at it. Right now it removes a specific line from file, but it can be edited to remove multiple lines by using a bi dimensional array.
I did not read your code, just rapidly looked at it and there may be things can be done a lot simpler.
If your temp file already exists, from my very low knowledge of new file edit system, something bad would happen. My code checks 10000 temp files for at least one that doesn't exist.

Isn't the end of file check already made in fgets?

Last edited by eyal282; 06-30-2017 at 17:36.
eyal282 is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 06-30-2017 , 19:46   Re: [STOCK/PLUGIN] delete_file_lines
Reply With Quote #4

Oh it's gonna be a useful stock. Can't wait to use it, gj.
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo
EFFx is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-01-2017 , 01:02   Re: [STOCK/PLUGIN] delete_file_lines
Reply With Quote #5

I don't think that this one function merits a whole plugin with a single native. A stock would probably make more sense.

Also, you are corrupting the file's contents by using trim() on each line. When you are simply deleting a line, you should not modify any other data in that file.
__________________

Last edited by fysiks; 07-01-2017 at 01:04. Reason: typo
fysiks is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 07-01-2017 , 04:59   Re: [STOCK/PLUGIN] delete_file_lines
Reply With Quote #6

Quote:
Originally Posted by fysiks View Post
I don't think that this one function merits a whole plugin with a single native. A stock would probably make more sense.

Also, you are corrupting the file's contents by using trim() on each line. When you are simply deleting a line, you should not modify any other data in that file.
Without trim, every line is empty only if Text[1] == EOS which created problems for me.
eyal282 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-01-2017 , 05:39   Re: [STOCK/PLUGIN] delete_file_lines
Reply With Quote #7

You can do whatever with a copy of the string, but don't change the original.
klippy is offline
eyal282
Veteran Member
Join Date: Aug 2011
Old 07-01-2017 , 05:43   Re: [STOCK/PLUGIN] delete_file_lines
Reply With Quote #8

Quote:
Originally Posted by KliPPy View Post
You can do whatever with a copy of the string, but don't change the original.
Done.
eyal282 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-01-2017 , 11:37   Re: [STOCK] delete_file_lines
Reply With Quote #9

formatex() requires that you don't use the output variable as an input. If you do use the same variable in both places, you must use format().
__________________
fysiks is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 07-01-2017 , 12:10   Re: [STOCK] delete_file_lines
Reply With Quote #10

Quote:
If your temp file already exists, from my very low knowledge of new file edit system, something bad would happen. My code checks 10000 temp files for at least one that doesn't exist.
You are over thinking. But if you are so concerned about that, give it a more random name.

Quote:
Isn't the end of file check already made in fgets?
I prefer to use feof, but it will work with fgets too.
__________________
HamletEagle 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 07:27.


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