String question

Need help running MacroQuest2? Ask your questions about how to get things to work on your computer.

Moderator: MacroQuest Developers

No_Idea_At_All
a lesser mummy
a lesser mummy
Posts: 49
Joined: Sat Aug 07, 2004 11:28 pm

String question

Post by No_Idea_At_All » Thu Aug 19, 2004 6:52 am

I'm not 100% sure what is the best way to do this and I know it would take 5 secs from someone here. even a pointer to the correct funtion to use would be a help.

This is with a plugin BTW
I have a string which contains the long zonename which I print to the hud. What I would like to do is create a string to print underneath which is the same length but filled with "=" characters.

I've tried looking at the String Manipulation examples in .Net and although they all do weird and wonderful stuff none seem to do what I need (either that or they just over complicate it.)

User avatar
aChallenged1
a grimling bloodguard
a grimling bloodguard
Posts: 1804
Joined: Mon Jun 28, 2004 10:12 pm

Post by aChallenged1 » Thu Aug 19, 2004 7:04 am

There should be a way to "count" characters in a string. I've seen it done but don't have the background to tell you how. Sorry. There is a lot of good info to be found with google.
Fuck writing MQ2 macros. Go with IS scripts; IS Rules!

Sparr
a hill giant
a hill giant
Posts: 159
Joined: Mon Jun 24, 2002 5:41 am

Post by Sparr » Thu Aug 19, 2004 7:20 am

forgive syntax errors, im coding this right here off the top of my head. This is proof of concept code, sets up a test case and such, obviously not all required in the solution.

Code: Select all

char ZoneName[MAX_STRING];
strcpy(ZoneName,"Plane of Tranquility");
char Equals[MAX_STRING];
strcpy(Equals,"================================================");
printf("%s\n%s\n",ZoneName,Equals+(strlen(Equals)-strlen(ZoneName)));
[img]http://www.trifocus.net/~sparr/sparr_rotate_sig_16.gif[/img]

No_Idea_At_All
a lesser mummy
a lesser mummy
Posts: 49
Joined: Sat Aug 07, 2004 11:28 pm

Post by No_Idea_At_All » Thu Aug 19, 2004 9:58 am

Thx for the little push along Sparr (hehe the strlen function was hiding from me.)

Very crude way to do it, if someone could think of a "neater" way to do it please let me know.

Code: Select all

		strcpy(szBuffer, ((PZONEINFO)pZoneInfo)->LongName);
		strcat(szBuffer, " (");
		strcat(szBuffer, ((PZONEINFO)pZoneInfo)->ShortName);
		strcat(szBuffer, ")");
		DrawHUDText(szBuffer, X, Y, DisplayColor);
		Y += 8;
		strcpy(szLine, "                                                                         ");
		_strnset(szLine, '=', strlen(szBuffer));
		DrawHUDText(szLine, X, Y, DisplayColor);

Caladine
a hill giant
a hill giant
Posts: 164
Joined: Fri Feb 13, 2004 9:29 pm

Post by Caladine » Thu Aug 19, 2004 11:44 am

That seems to be the best way to do it NIAA, and probably the neatest way to do it without introducing unneeded lines of code. It gets the job done, and it's pretty clear what it is doing.

ksmith
orc pawn
orc pawn
Posts: 20
Joined: Mon Feb 09, 2004 1:15 pm
Contact:

Post by ksmith » Thu Aug 19, 2004 2:04 pm

I'll never understand the Hungarian notation, but here you go:

Code: Select all

// you will probably need to include stdio.h for snprintf()
// assuming X, Y, and DisplayColor are ints
int iBufLen, iPosX, iPosY, iDisplayColor;
char **szBuffer;

// iPosX, iPosY, iDisplayColor get set here

iBufLen = strlen(((PZONEINFO)pZoneInfo)->LongName) +
    strlen(((PZONEINFO)pZoneInfo)->ShortName) + 4;

*szBuffer = new char [iBufLen];

if (*szBuffer == NULL)
{
    // throw an exception, unable to allocate memory
}

snprintf(*szBuffer, iBufLen, "%s (%s)",
    ((PZONEINFO)pZoneInfo)->LongName, ((PZONEINFO)pZoneInfo)->ShortName);

DrawHUDText(*szBuffer, iPosX, iPosY, iDisplayColor);

// replace all text in szBuffer with '='
for (char *i = *szBuffer; *i != '\0'; i++)
    *i = '=';

iPosY += 8;

DrawHUDText(*szBuffer, iPosX, iPosY, iDisplayColor);

delete [] *szBuffer;


No_Idea_At_All
a lesser mummy
a lesser mummy
Posts: 49
Joined: Sat Aug 07, 2004 11:28 pm

Post by No_Idea_At_All » Fri Aug 20, 2004 4:54 am

OK this maybe slightly better. _strnset needs to know the number of characters, whereas _strset changes the whole string. So by copying szBuffer to szLine I have the exact length I need, rather than all the spaces in the previous example.

Thanks for the code guys it really helped me work through the problem.

Code: Select all

		strcpy(szBuffer, ((PZONEINFO)pZoneInfo)->LongName);
		strcat(szBuffer, " (");
		strcat(szBuffer, ((PZONEINFO)pZoneInfo)->ShortName);
		strcat(szBuffer, ")");
		DrawHUDText(szBuffer, X, Y, DisplayColor);
		Y += 8;
		strcpy(szLine, szBuffer);
		_strset(szLine, '=');
		DrawHUDText(szLine, X, Y, DisplayColor);