I just came up with a problem I am stuck with. I must remove the color codes from a char array in C. Let's say the message is this:
[15:51:55] [error]: {ffffff}you already have this vehicle!r "police car (lspd)" (modelid: 596, vehicleid: 306)
The color codes are those within the { and } characters. I wrote this function, the first argument is the original message, and the second one is the array to store the original message without the color codes.
void eliminarCodigosColores(char *mensaje, char *destino)
{
for (int i = 0; i < strlen(mensaje); i++)
{
if ((mensaje[i] != '{' && mensaje[i + 7] != '}') || (mensaje[i - 1] != '{' && mensaje[i + 6] != '}') ||
(mensaje[i - 2] != '{' && mensaje[i + 5] != '}') || (mensaje[i - 3] != '{' && mensaje[i + 4] != '}') ||
(mensaje[i - 4] != '{' && mensaje[i + 3] != '}') || (mensaje[i - 5] != '{' && mensaje[i + 2] != '}') ||
(mensaje[i - 6] != '{' && mensaje[i + 1] != '}') || (mensaje[i - 7] != '{' && mensaje[i] != '}'))
{
*destino++ = mensaje[i];
}
}
}
It does not work properly (the color codes are not removed) and I don't know what's wrong. Is there a better or simpler way to do this?
Thanks in advance.
I would simply watch for an opening {
, see if there's a }
7 chars later, and skip the whole bunch:
void eliminarCodigosColores(const char *mensaje, char *destino)
{
int i = 0, j = 0;
while ( i < strlen(mensaje) )
{
if ((i + 7 < strlen(mensaje)) &&
(mensaje[i] == '{') &&
(mensaje[i + 7] == '}'))
{
i += 8;
}
else
{
destino[j++] = mensaje[i++];
}
}
destino[j] = 0;
}