returning a const char*

S

Simon

Hi,

I think this is slightly OT, (I am not certain that Macros are part of the
standard), but I am hopping that someone could help.

I am trying to use a language file in my system.
the format of the file would be something like

[option]
open=Open
close=Close
....
and so on.

Now I want to create some macros that I could use all over the place in all
the functions that would output text to the screen.

something like

SetMenuText( LANG_CLOSE ); or DisplayMessage( LANG_HELLOWORLD ); and so one
....

where the macro would represent...

#define LANG_CLOSE GetLanguagefile( "option", "close",
"Close" );
#define LANG_HELLOWORLD GetLanguagefile( "option", "hellow", "Hello
world..." );

but my problem is the return value, I would like to return a const char* or
even a char* but I don't think it is possible to return them directly.
I could have a global char *, char * g_szReturn = NULL; but is it really
safe to use it that way?

I need to return the function otherwise the macro are not very useful really
because I would have to add 2 or three lines of code every time.
Like

char szText[1024];
if( GetLanguagefile( "option", "close", "Close", szText, 1024 ) ){
SetMenuText( szText );
}

Any ideas how I could safely return a const char* or char *?

Many thanks.

Simon
 
J

John Harrison

Simon said:
Hi,

I think this is slightly OT, (I am not certain that Macros are part of the
standard), but I am hopping that someone could help.

Macros are part of the standard.
I am trying to use a language file in my system.
the format of the file would be something like

[option]
open=Open
close=Close
...
and so on.

Now I want to create some macros that I could use all over the place in all
the functions that would output text to the screen.

something like

SetMenuText( LANG_CLOSE ); or DisplayMessage( LANG_HELLOWORLD ); and so one
...

where the macro would represent...

#define LANG_CLOSE GetLanguagefile( "option", "close",
"Close" );
#define LANG_HELLOWORLD GetLanguagefile( "option", "hellow", "Hello
world..." );

No reason for macros, inline functions would be better.
but my problem is the return value, I would like to return a const char* or
even a char* but I don't think it is possible to return them directly.
I could have a global char *, char * g_szReturn = NULL; but is it really
safe to use it that way?

Not really, its obscure and therefore dangerous.
I need to return the function otherwise the macro are not very useful really
because I would have to add 2 or three lines of code every time.
Like

char szText[1024];
if( GetLanguagefile( "option", "close", "Close", szText, 1024 ) ){
SetMenuText( szText );
}

Any ideas how I could safely return a const char* or char *?

Don't, return a string instead.

#include <string>

inline std::string LANG_CLOSE()
{
return GetLanguagefile( "option", "close", "Close" );
}

SetMenuText(LANG_CLOSE().c_str());

john
 
S

Simon

Thanks for your reply,
Macros are part of the standard.

ah, thanks.
Don't, return a string instead.

#include <string>

inline std::string LANG_CLOSE()
{
return GetLanguagefile( "option", "close", "Close" );
}

SetMenuText(LANG_CLOSE().c_str());

But could i not go a step further and still use my Macro?

#define LANG_CLOSE GetLanguagefile( "option", "close", "Close" ).c_str()


Simon.
 
J

John Harrison

Simon said:
Thanks for your reply,


ah, thanks.


But could i not go a step further and still use my Macro?

#define LANG_CLOSE GetLanguagefile( "option", "close", "Close" ).c_str()

Only if GetLanguageFile returns a std::string. My inline function would work
whether GetLanguageFile returns char*, const char* or std::string.

But why use a macro? They have no advantage in this case.

john
 
S

Sims

Only if GetLanguageFile returns a std::string. My inline function would work
whether GetLanguageFile returns char*, const char* or std::string.

I am not sure I follow, GetLanguagefile(...) will return a string in both
cases.
But why use a macro? They have no advantage in this case.

Just curious really. If I could cut out the '.c_str()' it would be a bonus.

Simon
 
J

John Harrison

Sims said:
I am not sure I follow, GetLanguagefile(...) will return a string in both
cases.

A std::string can be implicitly created from a char* or const char*. This
would happen with my inline function if GetLanguageFile happened to return a
char* or const char*. I just mentioned this because I wasn't sure if
GetLanguageFile was a function you are writing or not. If you are writing it
then you should make it return a std::string.
Just curious really. If I could cut out the '.c_str()' it would be a bonus.

Fair enough.

john
 
S

Sims

A std::string can be implicitly created from a char* or const char*. This
would happen with my inline function if GetLanguageFile happened to return a
char* or const char*. I just mentioned this because I wasn't sure if
GetLanguageFile was a function you are writing or not. If you are writing it
then you should make it return a std::string.

Yes it is my function. So i guess i will make it return std:string
Fair enough.

john


Thanks for your help.

Simon
 
J

JKop

My Suggestion:



struct Language
{
char* pOpen;
char* pClose;
char* pRead;
char* pWrite;
};


const Language English = {

"Open",
"Close",
"Read",
"Write" };



const Language Irish = {

"Oscail",
"Dún",
"Léigh",
"Scríobh" };




const Language* pCurrentLanguage = &English; //Default to English



void SetLanguage(const Language* const NewLanguage)
{
pCurrentLanguage = NewLanguage;
}



int main(void)
{
SetLanguage(&English);

cout << pCurrentLanguage.pOpen;

SetLanguage(&Irish);

cout << pCurrentLanguage.pOpen;
}



You may consider setting "SetLanguage" to take an enum instead of a pointer,
whatever tickles your fancy. I myself like this way!


Hope that helps.


-JKop
 
J

JKop

And once again I have missed the point!!


You can have language file like this:


char* pLoadedLanguage = "Open\0Close\0Read\0Write";


Language MakeLanguageStruct(const char* pLoadedLanguage)
{
Language NewLanguage;

NewLanguage.pOpen = pLoadedLanguage;


while (pLoadLanguage +=1) {}

pLoadLanguage +=1;

NewLanguage.pClose = pLoadedLanguage;



while (pLoadLanguage +=1) {}

pLoadLanguage +=1;

NewLanguage.pRead = pLoadedLanguage;



while (pLoadLanguage +=1) {}

pLoadLanguage +=1;

Language.pWrite = pLoadedLanguage;


return NewLanguage;
}



Or something to that effect!!



-JKop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top