Page 1 of 1
String question
Posted: Thu Aug 19, 2004 6:52 am
by No_Idea_At_All
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.)
Posted: Thu Aug 19, 2004 7:04 am
by aChallenged1
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.
Posted: Thu Aug 19, 2004 7:20 am
by Sparr
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)));
Posted: Thu Aug 19, 2004 9:58 am
by No_Idea_At_All
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);
Posted: Thu Aug 19, 2004 11:44 am
by Caladine
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.
Posted: Thu Aug 19, 2004 2:04 pm
by ksmith
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;
Posted: Fri Aug 20, 2004 4:54 am
by No_Idea_At_All
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);