Raised This Month: $51 Target: $400
 12% 

Load from ini-file


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Drimacus
Member
Join Date: Jun 2011
Old 02-16-2024 , 07:34   Load from ini-file
Reply With Quote #1

Hello!
Please tell me how to load strings from ini-file into a dynamic array and go through the array by FOR loop.

Contents of the ini file:
Code:
models/player/zh/zh_charple001.mdl
models/player/zh/zh_charple002.mdl
models/player/zh/zh_charple003.mdl
Current implementation via While and with limited number of lines (20 lines)
PHP Code:
#define MAX_BUFFER_SIZE 200
#define MAX_MODELS 20
#define MAX_PATH_LENGTH 128
#define MAX_STRING_MODELS_LENGTH 640

new String:g_Models[MAX_MODELS][MAX_STRING_MODELS_LENGTH];
new 
String:g_Models_Count;

public 
OnMapStart()
{
    
g_Models_Count 0;
    
g_Models_Count LoadModels(g_Models"configs/models.ini");

    if (
g_Models_Count == 0SetFailState("No models found by plugin");

    
PrintToServer("%d models precached."g_Models_Count);
}

stock LoadModels(String:models[][], String:ini_file[])
{
    
decl String:buffer[MAX_BUFFER_SIZE];
    
decl String:file[MAX_PATH_LENGTH];
    new 
models_count;

    
BuildPath(Path_SMfileMAX_PATH_LENGTHini_file);
    
    new 
Handle:fileh OpenFile(file"r");
    while (
ReadFileLine(filehbufferMAX_PATH_LENGTH))
    {
        
TrimString(buffer);
        if (
FileExists(buffer))
        {
            
AddFileToDownloadsTable(buffer);
            if (
StrEqual(buffer[strlen(buffer) - 4], ".mdl"false) && (models_count MAX_MODELS))
            {
                
strcopy(models[models_count++], strlen(buffer) + 1buffer);
            }
        }
    }
    return 
models_count;

Drimacus is offline
little_froy
Senior Member
Join Date: May 2021
Old 02-16-2024 , 13:48   Re: Load from ini-file
Reply With Quote #2

use ArrayList
little_froy is offline
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 02-16-2024 , 16:02   Re: Load from ini-file
Reply With Quote #3

You can check this plugin https://forums.alliedmods.net/showthread.php?p=1471736
Does a similar thing about what you are trying to do.
__________________
Marttt is offline
Grey83
Veteran Member
Join Date: Dec 2014
Location: Ukraine
Old 02-19-2024 , 07:50   Re: Load from ini-file
Reply With Quote #4

PHP Code:
#pragma semicolon 1
#pragma newdecls required

ArrayList hModels;

public 
void OnPluginStart()
{
    
hModels = new ArrayList(ByteCountToCells(PLATFORM_MAX_PATH));
}

public 
void OnMapStart()
{
    if(
hModels.LengthhModels.Clear();

    
char buffer[PLATFORM_MAX_PATH];
    
BuildPath(Path_SMbuffersizeof(buffer), "configs/models.ini");

    
File fileh OpenFile(buffer"r");
    if(!
filehThrowError("Config file \"%s\" not found."buffer);

    
int ilen;
    while(
ReadFileLine(filehbuffersizeof(buffer)))
    {
        if(
buffer[len-1] == '\n'buffer[--len] = 0;

        
// ignore lines that are too short
        
if((len TrimString(buffer)) < 5)
            continue;

        
// ignoring comments
        
= -1;
        while(
buffer[++i]) if(buffer[i] == '/' && buffer[i+1] == '/')
        {
            
buffer[i] = 0;
            break;
        }

        
// ignoring too short lines and files missing on the server
        
if(|| (len != && (len TrimString(buffer)) < 5) || !FileExists(buffer))
            continue;

        
AddFileToDownloadsTable(buffer);
        if(!
strcmp(buffer[len 4], ".mdl"false))
        {
            if(
PrecacheModel(buffertrue)) hModels.PushString(buffer);
            else 
LogError("Can't precache model \"%s\""buffer);
        }
    }

    if(!
hModels.LengthLogError("No models found by plugin");
    else 
PrintToServer("%d models precached."hModels.Length);
}

...

void PrintModels()
{
    if(!
hModels.Length)
        return;

    
int num hModels.Length;
    
PrintToServer("Total %i models:"num);
    if(
num)
    {
        
char buffer[PLATFORM_MAX_PATH];
        for(
int inumi++)
        {
            
hModels.GetString(ibuffersizeof(buffer));
            
PrintToServer(" %3i) %s"i+1buffer);
        }
    }

    
PrintToServer(" ");

__________________
Grey83 is offline
Reply



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 14:48.


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