ANSI escape code, from Argument

I am trying to write a program that takes a string from the user and formats the CLI text using an ANSI escape code. Below is the code I have got closest to work, as you can see I have broken the ANSI code into single characters to try and input the different color codes.

The escape code is currently appearing as a string through cout, I think it is because the integer which choses the colour is not the same variable type as the rest of the character array.

Further, I am getting a segmentation fault that I can not source.

Any help is greatly appreciated, I would like someone to simply point me in the right direction to give me a chance to try and understand why this is not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// TestProgram - test ANSI escape code cli text formatting

#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cstring>

using namespace std;

int main(int nArgs, char* pszArgs[])
{
  // take text formats from arguments (just font colors for now)
  const char *input = pszArgs[1];
  int N = 8;
  char code[N][8];
  int num[N];
  // string array of color options
  const char* style[] = { "black\0",
			  "red\0",
			  "green\0",
			  "yellow\0",
			  "blue\0",
			  "magenta\0",
			  "cyan\0",
			  "white\0"	};
  // check input string for colors and load into int format array
  int j = 0;
  for( int i = 0; i < N; i++ ){
    if(	(const char*) strcasestr((const char*) input, 
	(const char*) style[ i ])){
      code[ j ][0] = 0x1b;
      code[ j ][1] = '[';
      code[ j ][2] = '1';
      code[ j ][3] = ';';
      code[ j ][4] = '3';
      code[ j ][5] = (char) i;
      code[ j ][6] = 'm';
      code[ j ][7] = 0; 
      num[ j ] = i+30;
      j++;
    }
  }
  
  // output colored text according to input
  int k = 0;
  while( num[k] && k < N){
    cout 	<< code[k][0]<<code[k][1]<<code[k][2]
		<< code[k][3]<<code[k][4]<<code[k][5]
		<< code[k][6]<<code[k][7]
		<< style[ num[ k ] - 30 ]
		<< endl;
    k++;
  }

  cin.get();
  return 0;
}
When you write a string literal a null character is automatically added to the end so there is no need to write \0 there unless you want two null characters at the end of the string for some reason.
1
2
3
4
5
6
7
8
  const char* style[] = { "black",
			  "red",
			  "green",
			  "yellow",
			  "blue",
			  "magenta",
			  "cyan",
			  "white" };


You should initialize the num elements to 0 because that is what your code assumes. This is probably the cause of the segfault.

On line 36 you want to convert i into a character. Just using a cast will not work because the character values of the digits '0'-'9' are not the same as the corresponding integer values 0-9. One way to do the conversion is to add the zero character '0' and the number.
code[ j ][5] = '0' + i;
closed account (DSLq5Di1)
If you are running this on Windows.. ANSI is no longer supported, and only available for 16 bit legacy applications.

You'll need to use the Console API:-
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686047
Thank you very much,
I have adapted the script and simplified it considerably.
Initialising the elements of N corrected the segfault.

As for using ANSI - the day I go back to Windows is the day I sell my soul to Microsoft.

I may include an ifdef statement and use the console API to make it fully portable, however, this was just a little project for no practical application other than to develop my string handling in c++ so I may not.
Topic archived. No new replies allowed.