y_ini - check if entry exists
#1

I have the a file which contains:

Name = Hello
Kills = 0

How to check with Y_INI if the entry "Name" exists in the file?
Reply
#2

@edited
Reply
#3

Quote:
Originally Posted by F1N4L
Посмотреть сообщение
@edited
No, I don't want to check if the file exists. I want to check inside the file if exists "Name" or "Kills" or etc.
Reply
#4

Check for INI_GetEntryName, it should exist in YSI 4 as well.
Reply
#5

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Check for INI_GetEntryName, it should exist in YSI 4 as well.
Can you give me example for INI_GetEntryName how works, because I checked on internet and I can't understand the parameters: INI_GetEntryName(source[], dest[], &i)
Reply
#6

I looked at the code in github and the function has been removed in the YSI 4 version. That's the way it was used in the older version:

PHP код:
bool:INI_GetEntryName(source[], dest[], &i)
{
    new
        
j;
    while (
source[j] && source[j] <= ' 'j++;
    
j;
    while (
source[i] > ' ' && source[i] != '='i++;
    if (
== j) return false;
    
-= j;
    
memcpy(destsourcej4MAX_INI_ENTRY_NAME);
    
dest[i] = '\0';
    return 
true;

PHP код:
EntryNameExists(const file_name[], const entry_name[])
{
    new 
Filefopen(file_nameio_read);
    
    if (!
f) return 0;
    
    new 
line[MAX_INI_ENTRY_NAME], stringIdent[MAX_INI_ENTRY_NAME], poscomment;
    
    while (
fread(fline))
    {
        
StripNL(line);
        
        if (!
line[0]) continue;
        
        
comment chrfind(';'line);
        
        if (
comment != -1line[comment] = '\0';
        if (
INI_GetEntryName(linestringIdentpos) && !strcmp(entry_namestringIdent)) return fclose(f), 1;
    }
    return 
fclose(f), 0;

You have to read every line of the file and check if the entry returned is the one you want. An example:
pawn Код:
if (!EntryNameExists("some_file.ini", "some_entry")) print("\"some_entry\" does not exist in the INI file.");
Reply
#7

Why do all that when you can just do this?
Код:
new File: file = fopen("file.ini", io_read);
if (!file) return 1;

new str[128], bool: found;
while (fread(file, str))
{
    if (strcmp(str, "entry =", false, LENGTH OF THE ENTRY + 2 (7 here) ))
        continue;
    found = true;
}
fclose(file);

// Use the found variable to see if it exists or not.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)