SA-MP Forums Archive
Length of a defined string - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Length of a defined string (/showthread.php?tid=599187)



Length of a defined string - paulomu300 - 22.01.2016

Hi,

Is is possible to get the length of a defined string using any of the PAWN Pre-Processor directives?

For example, I want to do something like this:

Код:
#define MODE_NAME "LVDM"

#if strlen(MODE_NAME) > 32
    #error "Define a shorter name."
#endif
This is useful because Pawn compiler would alert me whenever I type something bigger than it should be.

Any clue?


Re: Length of a defined string - xTURBOx - 22.01.2016

i dont know, i suggest testing that by making MODE_NAME longer than 32 charc


Re: Length of a defined string - paulomu300 - 22.01.2016

Quote:
Originally Posted by xTURBOx
Посмотреть сообщение
i dont know, i suggest testing that by making MODE_NAME longer than 32 charc
The code I've posted before won't work because strlen() works at the run level, and not at the compile time.

For example, if you do this:

Код:
#define VALUE (6)

#if VALUE > 5
    #error "Value is bigger then five."
#endif
It will show an error at the time you press the F5 key to compile your script, because the #if directive works at the compile time.

Now, if you do this:

Код:
#define VALUE (6)

main()
{
    if(VALUE > 5)
        print("Value is bigger then five.");
}
It will compile your script, but when you run it, it will display a message on your server console window. That works on run-time.

Each one is suitable for different situations.


Re: Length of a defined string - BroZeus - 22.01.2016

You can't get length of defined string during compile time.

#if directive only supports comparison( >, >=, ==..etc) and simple algebric operations(+, -, * ...etc)

The functions like strlen are runtime functions not compile time so they won't work.

The way to do it is to make a string variable format the predefined string and use strlen on it at run-time code, as you have done using that print statement in your above post.


Re: Length of a defined string - paulomu300 - 22.01.2016

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
You can't get length of defined string during compile time.

#if directive only supports comparison( >, >=, ==..etc) and simple algebric operations(+, -, * ...etc)

The functions like strlen are runtime functions not compile time so they won't work.

The way to do it is to make a string variable format the predefined string and use strlen on it at run-time code, as you have done using that print statement in your above post.
That's what I thought, but I was hoping for some way to do that at the compile time.

Thanks! [+rep]


Re: Length of a defined string - AmigaBlizzard - 22.01.2016

To keep you from changing the string to more than 32 characters (for whatever reason), you can add a comment next to it, or above it or whatever.
That's how I do it.
I also explain in that comment why it's not allowed as an extra warning sign.

PHP код:
// NEVER MAKE THIS TEXT LONGER THAN 32 CHARACTERS, THE SERVER MIGHT CRASH
#define MODE_NAME "LVDM"