Author Topic: file i/o & escape character butt  (Read 2123 times)

i have torqskript woes help me forums

say i have a file with this line:

Code: [Select]
\c6whoa
and i'm shoving that into some variable from a fileobject then farting it out to a client

problem: the \c6 here is not actually read and stored as "\c6", it's "\\c6", so the client doesn't get pretty coloured text; they just get a bunch of red stuff

is there a simple way to ''''un-escape'''' this and salvage control char functionality stuff or should i just use TML instead

Have you tried something like strReplace(%blah,"\\\\c","\\c");?

Have you tried something like strReplace(%blah,"\\\\c","\\c");?
That doesn't do what he needs

I'm not sure there is an easy way. You could do it for each of them seperately:

%line = strReplace(%line, "\\c0", "\c0");
%line = strReplace(%line, "\\c1", "\c1");
%line = strReplace(%line, "\\c2", "\c2");


Alternatively write a string that contains \c6 to a file to see what byte that results in and then use it in the files you want to read, reading them should then give you a string that contains \c6

I personally prefer just using TML when doing any text formatting. You don't get theses kinds of issues from doing <color:rrggbb>


I believe this is due to the way that TorqueScript implements colors.
When TS does this, it actually replaces ASCII codes with the color codes themself:
  • echo("\c1\c2\c3\c4\c5\c6\c7\c8\cr\cp\co $= "\x0\x1\x2\x3\x4\x5\x6\x7\x0B\x0F\x10\x11);
  • 1
You have to replace them with their corresponding metacharacter, and work from there:
%colors = "\c1\c2\c3\c4\c5\c6\c7\c8";
for(%i = 0; %i < 9; %i++)
    %text = strreplace("\c" @ %i, getSubStr(%colors, %i, 1));

   
   


You have to replace them with their corresponding metacharacter, and work from there:

... no.

collapseEscape(...)

collapseEscape("\\c6") => \c6



Wait, what are these?

\*The given character
\xHHASCII character HH (two hexadecimal digits)
\rCarriage return (0x0D)
\nLine feed (0x0A)
\tHorizontal tab (0x09)
\cDBegin color #D (decimal)
\crReset color
\cpPush color
\coRestore color
« Last Edit: August 25, 2014, 01:47:23 AM by portify »

echo("\c1\c2\c3\c4\c5\c6\c7\c8\cr\cp\co $= "\x0\x1\x2\x3\x4\x5\x6\x7\x0B\x0F\x10\x11);
... no.


... no.

collapseEscape("\\c6") => \c6



Quote from: Xalos
Also, the way color codes are handled behind-the-scenes should make any sensible programmer cry in pain.

==>echo("\c1\c2\c3\c4\c5\c6\c7\c8\cr\cp\co" $= "\x02\x03\x04\x05\x06\x07\x0B\x0C\x0F\x10\x11");
1

Yes, you read that right. The color codes OVERWRITE the following ASCII codes: 0x02 - 0x07, 0x0B, 0x0C, and 0x0F - 0x11.

You can't compare against \c0 like this, so I can only assume it's 0x01, but that's how the rest are represented.


"But it's just breaking archaic control characters no one —"
NO. If you're going to implement ASCII, you implement loving ASCII.

If you want to have control characters:
have it stored in text form,1
implement Unicode and use the Private Use Area as your control character,
or make your own character encoding and use that instead.


1echo(getSubStr("<color:FF800080>", 1, 5));
Maybe I restated it incorrectly, I don't know. I'm going off of what has been said before - is Xalos wrong or something?
None of these methods are going to work. If you input a \c# color code into a text field as its component characters, 0x92 0x99 0x50 (for \c2), then it's never going to turn into the really iffy method by which TorqueScript represents its color codes.

You have to actually replace each of the color codes' string representation, such as 0x92 0x99 0x50, with its equivalent metacharacter representation.

%colorCodes = "\c0\c1\c2\c3\c4\c5\c6\c7\c8";
for(%i=0;%i<9;%i++)
    %text = strReplace("\c"@%i, getSubStr(%colorCodes, %i, 1));


Note that this would miss all the non-numeric color codes, such as \cr, \cp, and \co.


echo("\c1\c2\c3\c4\c5\c6\c7\c8\cr\cp\co" $= "\x02\x03\x04\x05\x06\x07\x0B\x0C\x0F\x10\x11");

echo("\c1\c2\c3\c4\c5\c6\c7\c8\cr\cp\co $= "\x0\x1\x2\x3\x4\x5\x6\x7\x0B\x0F\x10\x11);

In any case, just use collapseEscape.

thx port i knew there had to be a function

I'm going off of what has been said before - is Xalos wrong or something?

No one had stated collapseEscape, and I didn't know of it at the time. I was correct within my subset of all knowledge.

Nevertheless, it still should make any sensible programmer cry in pain.