Reading ini files
#1

Hi, I have my server's settings saved in a file called "Server.ini". It's the only ini file that I need to read from so I decided to create my own function that reads the stuff inside instead of using a ini file reading include. It works, I printed the values and it's all good, but when it comes to setting the password then this is what happens:
https://gyazo.com/632de81739191f94c6d37ded9eb65ea6
in the server's log it's OK though:
Код:
[21:09:55] Setting server password to: "password"
[21:09:55] [SERVER]: Server is locked with a password of "password".
this didn't happen when I had an include and now when I try to connect with that password then it tells me it's the wrong password.
Here's the function:
Код:
LoadServer()
{
	if (fexist("Server.ini"))
	{
		new File:file = fopen("Server.ini", io_read), buffer[128], row[64], value[64], pos;
		
		while (fread(file, buffer))
		{
			pos = strfind(buffer, "=");
			
			strmid(row, buffer, 0, pos);
			strmid(value, buffer, pos + 1, strlen(buffer) - 1);
			
			if (!strcmp(row, "Password")) strcpy(g_ServerPassword, value, 24);
			else if (!strcmp(row, "Taxes")) g_Taxes = floatstr(value);
			else if (!strcmp(row, "OOCEnabled")) g_OOCEnabled = strval(value);
			else if (!strcmp(row, "ShellsDisabled")) g_ShellsDisabled = strval(value);
			else if (!strcmp(row, "CorpsesDisabled")) g_CorpsesDisabled = strval(value);
		}
		fclose(file);
	}
	if (strcmp(g_ServerPassword, "0"))
	{
	    new rcon[32];

	    format(rcon, sizeof(rcon), "password %s", g_ServerPassword);
	    SendRconCommand(rcon);
	    printf("[SERVER]: Server is locked with a password of \"%s\".", g_ServerPassword);
	}
}
What's up? Please don't tell me to use an include because I don't want to. :P Also when the password is 0 it still says that the server has a password.
Reply
#2

What is wrong with the output?
Reply
#3

Did you look at the picture? Compare the picture and what is in the log.
This is in the picture:
Код:
"etting server password to: "password
".SERVER]: Server is locked with a password of "password
and this is in the log:
Код:
Setting server password to: "password"
[SERVER]: Server is locked with a password of "password".
Reply
#4

Increase string maybe ?
Reply
#5

Quote:
Originally Posted by Scripter18
Посмотреть сообщение
Increase string maybe ?
Nope. 24 cells (size of g_ServerPassword) is enough for "password" and 64 for "value". Also I assume increasing the size has nothing to do with this.

EDIT: I tried printing g_ServerPassword like this:
Код:
printf("Password: |%s|", g_ServerPassword);
and it got messed up as well. This means wherever g_ServerPassword or any other thing I load gets printed it messes it up. Like the last one or two characters get moved to the beginning of the message. This is what it looked like:
Код:
|assword: |0
Reply
#6

You're not setting some variables correctly, so they're still 0/nothing!
Reply
#7

I looked here: https://sampwiki.blast.hk/wiki/How_to_read_from_INI_files and found that I need to exclude \r\n. So I changed
Код:
strmid(value, buffer, pos + 1, strlen(buffer) - 1);
to
Код:
strmid(value, buffer, pos + 1, strlen(buffer) - 2);
and it works now.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)