AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   #define string in string (https://forums.alliedmods.net/showthread.php?t=117078)

Greyscale 01-28-2010 20:36

#define string in string
 
Code:

#define PROJECT_AUTHOR      "Greyscale"
#define PROJECT_COPYRIGHT "Copyright (C) 2010  PROJECT_AUTHOR"


Possible? I know in c++ you would use + to concatenate, but not in pawn.

And the above doesn't work. It prints PROJECT_AUTHOR (literally), as I expected.

Kevin_b_er 01-28-2010 23:11

Re: #define string in string
 
In C,
#define "String1""String2"
works.

Don't know if it will in sourcepawn.

However, for everyone who's reading this, the method strictly would only work when creating a literal. It will not concatenate at runtime.

Frus 01-28-2010 23:44

Re: #define string in string
 
Code:

#define PROJECT_AUTHOR      "Greyscale"
#define PROJECT_COPYRIGHT_FORMAT(%1) "Copyright (C) 2010  %1"
#define PROJECT_COPYRIGHT PROJECT_COPYRIGHT_FORMAT(PROJECT_AUTHOR)

I think that would work.

Otherwise you may be forced to stick a strcat() or Format() in your code to format the author string into the copyright string.

Greyscale 01-29-2010 00:42

Re: #define string in string
 
Frus: I tried that, didn't work :/

I have no idea why, hopefully I did something wrong. I'll try again.

Kevin: Doesn't work in SourcePawn.


EDIT: Didn't work, Frus.

Frus 01-29-2010 01:09

Re: #define string in string
 
Since I assume by you saying it twice you tried both methods, I don't see why

Code:

#define PROJECT_AUTHOR      "Greyscale"
#define PROJECT_COPYRIGHT "Copyright (C) 2010"
decl String:copyrightNotice[128];
Format("%s  %s", copyrightNotice, sizeof(copyrightNotice), PROJECT_COPYRIGHT, PROJECT_AUTHOR);

wouldn't work.

main is down so I'm going off the syntax by memory, forgive me if the params are in the wrong order.

Greyscale 01-29-2010 02:06

Re: #define string in string
 
Well, yes I'm sure that method works. If that's the only way that really blows.

I was hoping the compiler would be able to compile it into the code as opposed to making a global string constant to do it.

Fyren 01-29-2010 08:58

Re: #define string in string
 
In C++ you can only use + to concatentate String objects because there is an operator overload for it. You can't do "foo" + "bar".

Our version of the Pawn compiler does not have either a "stringize" operator or a literal string concatenate operator (# and ##, respectively, in the C preprocessor).

rhelgeby 01-29-2010 16:41

Re: #define string in string
 
Can't we overload the + operator for strings ourself then?

Like it's done with floats.
Code:

stock Float:operator+(Float:oper1, oper2)
{
    return FloatAdd(oper1, float(oper2));                        /* "+" is commutative */
}

Maybe it isn't that easy to do with arrays.

berni 01-29-2010 17:37

Re: #define string in string
 
Quote:

Originally Posted by Greyscale (Post 1071232)
Code:

#define PROJECT_AUTHOR      "Greyscale"
#define PROJECT_COPYRIGHT "Copyright (C) 2010  PROJECT_AUTHOR"

Possible? I know in c++ you would use + to concatenate, but not in pawn.

And the above doesn't work. It prints PROJECT_AUTHOR (literally), as I expected.

I already filed a bug report 1 year ago, when the old bug system was still on, nothing happened since then.

Greyscale 01-29-2010 18:11

Re: #define string in string
 
Quote:

Originally Posted by rhelgeby (Post 1072070)
Can't we overload the + operator for strings ourself then?

Like it's done with floats.
Code:

stock Float:operator+(Float:oper1, oper2)
{
    return FloatAdd(oper1, float(oper2));                        /* "+" is commutative */
}

Maybe it isn't that easy to do with arrays.

I had no idea overloading was possible in Pawn.

But the problem is it takes the return value. Pawn can't return strings.

berni: Did you end up using Format() to work around this?


All times are GMT -4. The time now is 20:04.

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