AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   fgets, format (general messup) (https://forums.alliedmods.net/showthread.php?t=89798)

marky_uk 04-10-2009 16:57

fgets, format (general messup)
 
Hey,
This should have been an easy plugin to make but incoporating a file to retrieve data from has been the hardest thing I've come across yet.

What its suppose to do is:
get mapname,
open mapfile associated with mapname,
read all the lines in the mapfile,
remove all the entities listed in the mapfile

What it does:
Compiles fine :p
Crashes on server start

Im guessing the crash is caused by the malformed find_ent_by_model/remove ent because fgets is not supplying the right data I wanted.

I have yet to find a decent tutorial on file reading, so if you could provide while helping me fix my code.

Code:

#include <amxmodx>
#include <engine>

#define PLUGIN "WepEntityRemover"
#define AUTHOR "Marky_UK"
#define VERSION "1.00"

#define FILEPATH "addons/amxmodx/configs/WepEntityRemover"

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)

    new mapfile[256]
    new mapname[256]
    get_mapname(mapname, 255)
    format(mapfile,255,"%s/%s.txt", FILEPATH, mapname)
    new filepointer = fopen(mapfile,"r")
    if(filepointer == 0)
    {
        return PLUGIN_HANDLED;
    }

    new mapfiledata[681],weapon[19]
   
    while(fgets(filepointer,mapfiledata,680))
    {
        parse(mapfiledata,weapon,18)
        new WeaponEnt[65]
        format(WeaponEnt,64,"find_ent_by_model(-1,'armoury_entity','models/%s')", weapon)
        while((WeaponEnt[0]) != 0)
        {
            remove_entity(WeaponEnt[0])
        }
    }
    return PLUGIN_HANDLED;
}

Thanks, +karma

fysiks 04-10-2009 17:09

Re: fgets, format (general messup)
 
WeaponEnt contains a string. That string happens to be the text that makes up a function. This "function" will never get executed because it is just a string of characters.

Basically:
WeaponEnt[0] = "f"

Also, I don't think fgets returns a value. Here is what I use to take in two parameters (mapname and number):

PHP Code:

    new maproundsfile[64]
    
get_configsdir(maproundsfilesizeof(maproundsfile) - 1)
    
add(maproundsfilesizeof(maproundsfile) - 1"/map_rounds.ini")
    
    if( !
file_exists(maproundsfile) ) return
    
    new 
curmap[64]
    
get_mapname(curmapsizeof(curmap) - 1)
    
    new 
fopen(maproundsfile"rt")
    
    new 
data[128], map[64], rounds[16]
    while( !
feof(f) ) 
    { 
        
fgets(fdatasizeof(data) - 1)
        
// I think trim(data) needs to go here since fgets includes newline and carriage return characters.
        
if( !data[0] || data[0] == ';'
           
|| data[0] == '/' && data[1] == '/' ) continue;
         
        
parse(data
              
mapsizeof(map) - 1,
              
roundssizeof(rounds) - 1
             
)
         
        if( !
strcmp(curmapmap) )
        { 
            
set_pcvar_num(cvar_mp_maxroundsstr_to_num(rounds))
            
mapfound true
            
break
        }
    }
    
    
fclose(f


marky_uk 04-10-2009 17:14

Re: fgets, format (general messup)
 
Is what I thought aswell, I can't think right now because some guy was just talking to me and has completely confused me so I have yet to come to a solution if you could give me a hint please :p

marky_uk 04-10-2009 17:16

Re: fgets, format (general messup)
 
Quote:

Originally Posted by fysiks (Post 802497)
WeaponEnt contains a string. That string happens to be the text that makes up a function. This "function" will never get executed because it is just a string of characters.

Basically:
WeaponEnt[0] = "f"

Also, I don't think fgets returns a value. Here is what I use to take in two parameters (mapname and number):

PHP Code:

    new maproundsfile[64]
    
get_configsdir(maproundsfilesizeof(maproundsfile) - 1)
    
add(maproundsfilesizeof(maproundsfile) - 1"/map_rounds.ini")
    
    if( !
file_exists(maproundsfile) ) return
    
    new 
curmap[64]
    
get_mapname(curmapsizeof(curmap) - 1)
    
    new 
fopen(maproundsfile"rt")
    
    new 
data[128], map[64], rounds[16]
    while( !
feof(f) ) 
    { 
        
fgets(fdatasizeof(data) - 1)
         
        if( !
data[0] || data[0] == ';'
           
|| data[0] == '/' && data[1] == '/' ) continue;
         
        
parse(data
              
mapsizeof(map) - 1,
              
roundssizeof(rounds) - 1
             
)
         
        if( !
strcmp(curmapmap) )
        { 
            
set_pcvar_num(cvar_mp_maxroundsstr_to_num(rounds))
            
mapfound true
            
break
        }
    }
    
    
fclose(f



fgets stores data into the second parameter and returns 0 on error

EDIT: OR DOES IT :p
EDIT2: NO IT DOESNT >.<

fysiks 04-10-2009 17:20

Re: fgets, format (general messup)
 
Quote:

Originally Posted by marky_uk (Post 802503)
fgets stores data into the second parameter and returns 0 on error

EDIT: OR DOES IT :p
EDIT2: NO IT DOESNT >.<

I looked at the funcwiki and in the include files and it doesn't say anything about returning a value. :)

danielkza 04-10-2009 17:23

Re: fgets, format (general messup)
 
Code:

static cell AMX_NATIVE_CALL amx_fgets(AMX *amx, cell *params)
{
    FILE *fp = (FILE *)params[1];

    if (!fp)
        return 0;

    static char buffer[4096];
    buffer[0] = '\0';
    fgets(buffer, sizeof(buffer)-1, fp);
    return set_amxstring(amx, params[2], buffer, params[3]);
}

The code seems right, returns 0 on invalid file handle, and set_amxstring() returns the lenght of the string, so it should be 0 when nothing was read.

fysiks 04-10-2009 17:26

Re: fgets, format (general messup)
 
Quote:

Originally Posted by danielkza (Post 802510)
Code:

static cell AMX_NATIVE_CALL amx_fgets(AMX *amx, cell *params)
{
    FILE *fp = (FILE *)params[1];

    if (!fp)
        return 0;

    static char buffer[4096];
    buffer[0] = '\0';
    fgets(buffer, sizeof(buffer)-1, fp);
    return set_amxstring(amx, params[2], buffer, params[3]);
}

The code seems right, returns 0 on invalid file handle, and set_amxstring() returns the lenght of the string, so it should be 0 when nothing was read.

But it reads \r\n according to the include. So, it will only be zero on eof. Correct me if I'm wrong.

danielkza 04-10-2009 17:31

Re: fgets, format (general messup)
 
Quote:

Originally Posted by fysiks (Post 802515)
But it reads \r\n according to the include. So, it will only be zero on eof. Correct me if I'm wrong.

That's why you should always trim() it before using. This way the string will be empty on an empty line.

fysiks 04-10-2009 17:34

Re: fgets, format (general messup)
 
Now that I think of it, that's why it will work as the condition for the while loop :oops:

marky_uk 04-10-2009 18:00

Re: fgets, format (general messup)
 
Sorry guys, im still very new to scripting and I appreciate the help you have give me but I can't think of a way to fix this.

This, im guessing, is the main problem: WeaponEnt[0] = "f"
Another problem is the data im reading from the file: needs trim() for /n/r
Another problem I have just seen is that im using a static size for the weapon model name, variable weapon, which should be dynamic because not all the names are 18 chars.

Please could I have some more help?
Thanks


All times are GMT -4. The time now is 02:15.

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