Getting rid of stlwr and strupr

Want to discuss changes to the UOX3 source code? Got a code-snippet you'd like to post? Anything related to coding/programming goes here!
Post Reply
punt
VIP
Posts: 244
Joined: Wed Mar 24, 2004 7:46 pm
Has thanked: 0
Been thanked: 9 times

Getting rid of stlwr and strupr

Post by punt »

First, they aren't ANSI, and yes, there is an ifdef for linux. It also doesn't seem to build the way it is currently (link errors, which instead of messing with, why not just get rid of them and do it the same way for any os?). But I guess, given you have ustring, why mix and match?

in cScript.cpp on or about 420

Code: Select all

char *lwrSpeech = new char[strlen(speech)+1];
strcpy( lwrSpeech, speech );
strlwr( lwrSpeech );
can be deleted and in its place add

Code: Select all

    UString lwrSpeech = speech ;
    
This line that comes shortly after:

Code: Select all

strSpeech = JS_NewStringCopyZ( targContext,lwrSpeech);
can be replaced with

Code: Select all

strSpeech = JS_NewStringCopyZ( targContext, lwrSpeech.lower().c_str() );
And at the end of the routine remove the

Code: Select all

delete[] lwrSpeech;


On our about line 2227 do something simlar

Code: Select all

	char *lwrSpeech		= new char[strlen(mySpeech)+1];
	strcpy( lwrSpeech, mySpeech );
	strlwr( lwrSpeech );

	strSpeech = JS_NewStringCopyZ( targContext, lwrSpeech );
changed to

Code: Select all

UString lwrSpeech = mySpeech;
strSpeech = JS_NewStringCopyZ(targContext,lwrSpeech.lower().c_str());
and on line 2244 remove

Code: Select all

delete lwrSpeech ;


Now in UOXJSMethods.cpp

line 3715

Code: Select all

	char *mode		= JS_GetStringBytes( JS_ValueToString( cx, argv[1] ) );
	strlwr( mode );
	if( mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a' )
	{
		MethodError( "Open: Invalid mode must be \"read\", \"write\", or \"append\"!" );
		return JS_FALSE;
	}
	if( strstr( filename, ".." ) || strstr( filename, "\\" ) || strstr( filename, "/" ) )
	{
		MethodError( "Open: file names may not contain \"..\", \"\\\", or \"/\"." );
		return JS_FALSE;
	}
	mode[1] = 0;

	mFile->mWrap = fopen( filename, mode );
becomes

Code: Select all

	
UString mode		= JS_GetStringBytes( JS_ValueToString( cx, argv[1] ) );
        if (mode.lower().find_first_of("rwa",0,1) == std::string::npos)
	{
		MethodError( "Open: Invalid mode must be \"read\", \"write\", or \"append\"!" );
		return JS_FALSE;
	}
	if( strstr( filename, ".." ) || strstr( filename, "\\" ) || strstr( filename, "/" ) )
	{
		MethodError( "Open: file names may not contain \"..\", \"\\\", or \"/\"." );
		return JS_FALSE;
	}

	mFile->mWrap = fopen( filename, mode.lower().substr(0,1).c_str() );

Ok, now to get rid of strupr

in speech.cpp

line 47

Code: Select all

	strupr( langCode );	// Convert to uppercase
	UnicodeTypes cLang = s->Language();
	if( LanguageCodes[cLang] != langCode )
	{
		UnicodeTypes newLang = FindLanguage( langCode );
		if( newLang == TOTAL_LANGUAGES )
			Console.Error( 0, "Unknown language type \"%s\".  PLEASE report this on www.sourceforge.net/projects/uox3 in the bugtracker!", langCode );
		else
			s->Language( newLang );
	}

becomes

Code: Select all

        UString ulangCode = langCode ;
        ulangCode = ulangCode.upper();

	UnicodeTypes cLang = s->Language();
	if( LanguageCodes[cLang] != ulangCode.c_str() )
	{
		UnicodeTypes newLang = FindLanguage( ulangCode.c_str() );
		if( newLang == TOTAL_LANGUAGES )
			Console.Error( 0, "Unknown language type \"%s\".  PLEASE report this on www.sourceforge.net/projects/uox3 in the bugtracker!", ulangCode.c_str() );
		else
			s->Language( newLang );
	}

Now in speech.cpp as well around line 28 change this line

Code: Select all

UnicodeTypes FindLanguage( char *lang )
to

Code: Select all

UnicodeTypes FindLanguage(char *lang )
And in network.cpp around line 42

Code: Select all

UnicodeTypes FindLanguage( const char *lang );

to

Code: Select all

UnicodeTypes FindLanguage( const char *lang );


Ok, now in globals.cpp delete the bottom where strlwr and strupr where defined, and in Platform.h whre they where prototyped.
giwo
Developer
Posts: 1780
Joined: Fri Jun 18, 2004 4:17 pm
Location: California
Has thanked: 0
Been thanked: 0

Post by giwo »

Ahh yes, a few remaining remenants of silly string workarounds.

I've been trying to get rid of all of these other methods of string alterations as I do my work, in favor of using UString everywhere, but alas, UOX is full of it. :)

This, however, I will fix this evening, given the time to do so.

Thanks punt
Scott
Post Reply