I am stuck in way with the for loop at the end. The whole idea is to count up to gscreenwidth and then resize the string to make it cut off at 24. We want it to skip the &w or &W anything with an & and 1 character so that it shows the color codes. Let me remind you that this is just a basic program for a much bigger one. The &w or &c etc are part of the bigger program. Also the printf and pad right is also part of the bigger program this will all be inside a frame of characters. The last two cout are just me trying to figure out the integer numbers being output.
#include <iostream>
#include <string>
using namespace std;
typedef string::size_type size_type;
typedef unsigned int u_int;
size_type gScreenWidth = 24;
/**
* Count the number of color codes in the specified string.
* @param s string containing text with mud color codes
* @return length of the actual text characters in the string
*/
size_type ccStringLen( string const &s )
{
u_int colors_count = 0;
for( std::string::const_iterator si = s.begin(); si < s.end(); ++si )
if( '&' == *si )
++colors_count;
return s.size() - colors_count * 2;
}
string ccCenter(string const &str)
{
int const screen_len = 80;
int in_len = ccStringLen(str);
int pad_left = (screen_len - in_len)/2;
int pad_right = screen_len - pad_left - in_len;
return string( pad_left, ' ' ) + str + string( pad_right, ' ' );
}
int main (void)
{
using namespace std;
// 1 2
// 12345678901 23456789012345
string title = "&wDummy Title&G and some desc&W";
size_type title_len = ccStringLen( title );
cout << "Title: " << title << "\nTitle Len:" << title_len<< "\n";
int real_len = 0;
// resize the string to fit inside the screen
if( title_len > gScreenWidth )
{
for( std::string::const_iterator si = title.begin(); si < title.end() || title_len == 0; ++si, ++real_len)
{
// skip color codes
if( '&' == *si )
++si;
else
--title_len;
}
title.resize( real_len );
printf("%s\n", ccCenter(title).c_str() );
cout << title_len;
cout << real_len;
}
return 0;
}