AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Check if is in file (https://forums.alliedmods.net/showthread.php?t=82554)

anakin_cstrike 12-25-2008 15:15

Check if is in file
 
Hi!
I want to check if a string is in a file.
For example: in the file i have:
Code:

BAILOPAN
Freecode Alka
Exolent

And i want to check if 'Alka' is in that file (space between strings, or new lines)

I tried using this, wich i found is no mine
PHP Code:

stock bool:is_string_in_file(file[], const string[])
{
   new 
iFile fopen(file"rt");

   if(!
iFile)
   return 
false;

   new 
szBuffer[64], szData[32];

   while(!
feof(iFile))
   {
      
fgets(iFileszBuffersizeof szBuffer 1);
      
trim(szBuffer);
 
      if(!
szBuffer[0] || szBuffer[0] == ';')
         continue;
 
      
parse(szBufferszDatasizeof szData 1);
 
      if(
equali(szDatastringstrlen(string)))
      {
         
fclose(iFile);
         return 
true;
      }
   }
   
fclose(iFile);
   return 
false;


but is not working, or using 'str_piece' from Alka's Extra precacher...ofcourse, i the file i've put instead of spaces a token.

Anyway, help would be apreciated.

Exolent[jNr] 12-25-2008 15:42

Re: Check if is in file
 
PHP Code:

      if(equali(szDatastringstrlen(string))) 

:arrow:
PHP Code:

      if(containi(szDatastring) != -1


anakin_cstrike 12-25-2008 16:02

Re: Check if is in file
 
Thanks, but is not working.
I tried like this:

PHP Code:

public client_connectid )
{
    static 
name32 ];
    
get_user_nameidnamesizeof name );
    
    if( 
IsInFile"name_list.txt"name ) )
        
server_cmd"kick #%d"get_user_useridid ) );
    
    return 
PLUGIN_CONTINUE;
}

stock bool:IsInFile(file[], const string[])
{
    new 
iFile fopenfile"rt" );

    if( !
iFile )
        return 
false;

    new 
szBuffer64 ], szData32 ];

    while( !
feofiFile ) )
    {
        
fgetsiFileszBuffersizeof szBuffer );
        
trimszBuffer );
 
        if( !
szBuffer] || szBuffer] == ';' )
            continue;
 
        
parseszBufferszDatasizeof szData );
 
        if( 
containiszDatastring ) != -1)
        {
            
fcloseiFile );
            return 
true;
        }
    }
    
    
fcloseiFile );
    
    return 
false;



danielkza 12-25-2008 16:28

Re: Check if is in file
 
PHP Code:

#include <amxmisc>

new g_szFile[64]
new Array:
g_hNameArray

public plugin_init()
{
    
get_configsdir(g_szFilecharsmax(g_szFile))
    
add(g_szFilecharsmax(g_szFile), "/names.cfg")
    
    
LoadConfig()
    
    
// Register a command that calls LoadConfig so you can reload the file whenever you want.
    // LoadConfig() returns the number of records read.
    
    // register_concmd(...)
}

public 
client_connect(id)
{
    static 
szName[32];
    
get_user_name(idszNamecharsmax(szName));
    
    if(
ArraySearchString(g_hNameArrayszName) != -1)
    
server_cmd("kick #%d"get_user_userid(id));
}

LoadConfig(iFatal false)
{
    if(!
g_hNameArray)
        
g_hNameArray ArrayCreate(32)
    else
        
ArrayClear(g_hNameArray)
        
    new 
hFile fopen(g_szFile"r")
    if(!
hFile)
    {
        
hFile fopen(g_szFile"w")
        if(!
hFile)
        {
            if(
iFatal)
            {
                new 
szFailState[64]
                
formatex(szFailStatecharsmax(szFailState), "Can't open/create file %s"g_szFile)
                
                
set_fail_state(szFailState)
            }
            
            return -
1
        
}
        else
        {
            
fclose(hFile)
            
            return 
0
        
}
    }
    
    static 
szBuffer[128], szSplitBuffer[128], szName[32]
    while(!
feof(hFile) && fgets(hFileszBuffercharsmax(szBuffer)))
    {
        
trim(szBuffer)
        
        if(!
szBuffer[0] || szBuffer[0] == ';' || equal(szBuffer"//"2))
            continue
        
        
copy(szSplitBuffercharsmax(szSplitBuffer), szBuffer)
        
        while(
szSplitBuffer[0])
        {
            
strbreak(szSplitBufferszNamecharsmax(szName), szSplitBuffercharsmax(szSplitBuffer))
            
            if(
szName[0])
                
ArrayPushString(g_hNameArrayszName)
        }
    }
    
fclose(hFile)
    
    return 
ArraySize(g_hNameArray)
}

ArraySearchString(const Array:hArray, const szSearch[], iCaseSensitive false)
{
    static 
iSize
    
if(!hArray || !(iSize ArraySize(hArray)))
        return -
1
    
    
static szTempString[32]
    for(new 
i=0iSizei++)
    {
        
ArrayGetString(hArrayiszTempStringcharsmax(szTempString))
        
        if(
iCaseSensitive equal(szSearchszTempString) : equali(szSearchszTempString))
            return 
i
    
}
    
    return -
1



anakin_cstrike 12-25-2008 16:46

Re: Check if is in file
 
Thanks for that code...is working.


All times are GMT -4. The time now is 09:08.

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