Length of a defined string
#1

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?
Reply
#2

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

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.
Reply
#4

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.
Reply
#5

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]
Reply
#6

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" 
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)