02 Aug, 2009, Chris Bailey wrote in the 21st comment:
Votes: 0
I would be more than happy to write a ruby solution in a C like style, if you guys want to port it.
02 Aug, 2009, Guest wrote in the 22nd comment:
Votes: 0
Yeah, that's all we had it for on Alsherok too. It was only ever used by an amulet we had that became a regular prank among the imms to pass around unsuspecting. Never bothered to try stripping color codes off of it.
02 Aug, 2009, Davion wrote in the 23rd comment:
Votes: 0
Samson said:
Yeah, that's much easier to read, and makes more sense. You've got a bug in the rewrite though. Where you have
arg2[i] = '\0'
shouldn't that be an i++ and the terminating null be added after the while loop?


Should be a ++i, or else it's setting everything to '\0'. Which is not what you want ;)
02 Aug, 2009, David Haley wrote in the 24th comment:
Votes: 0
void invert(const char* src, char* dst)
{
int len = strlen(src);
for (int i = 0; i < len; i++) {
dst[i] = src[len - i - 1];
}
dst[len] = '\0';
}


If you're compiling with ANSI C, stick the declaration of 'i' outside the for loop.
02 Aug, 2009, Tonitrus wrote in the 25th comment:
Votes: 0
Samson said:
You've got a bug in the rewrite though.

Sorry, wrote it as an example, not intending it as actual code, so I didn't really read it very closely.

Should be something like:

void invert( char *arg1, char *arg2 )
{
int i = 0;
int len = strlen( arg1 ) - 1;
while( i <= len )
{
arg2[i] = arg1[len - i];
i++;
}
arg2[i] = '\0';
}


Left out the incrementation and put the '\0' before the bracer by mistake.
02 Aug, 2009, Runter wrote in the 26th comment:
Votes: 0
Chris Bailey said:
Ported to Ruby, incase anyone needs it.

irb(main):018:0> "Hello World In Reverse!".reverse
=> "!esreveR nI dlroW olleH"


Rofl. Just thought I'd mention that the Ruby string library functionality is available in C. (Including a reverse c-string function.) Although it may be more trouble than it is worth to get installed in existing projects. Not sure. :P
02 Aug, 2009, boblinski wrote in the 27th comment:
Votes: 0
So once I figure out how to actually reverse the string in a ROM environment.. I could then just add it into printf_to_char and send_to_char and send_to_desc..? with a check like.. IS_AFFECTED (ch, AFF_PERPLEX) ?
02 Aug, 2009, Davion wrote in the 28th comment:
Votes: 0
printf_to_char eventually passes off to send_to_char. send_to_desc is at the descriptor level and contains no character data. You could probably put it in send_to_char and it'd do the rest for you.
03 Aug, 2009, boblinski wrote in the 29th comment:
Votes: 0
What is to be added,, and will I also need it in act() ?
03 Aug, 2009, Davion wrote in the 30th comment:
Votes: 0
Just add something like

if(IS_AFFECT(ch, AFF_PERPLEXED ) )
{ char rev[MSL];
invert(txt, rev);
write_to_buffer(ch->desc, rev, 0);
return;
}
03 Aug, 2009, Chris Bailey wrote in the 31st comment:
Votes: 0
I'm still working out the kinks on a way to reverse it with the color codes properly reversed as well. Running into a few kinks. :P
04 Aug, 2009, Chris Bailey wrote in the 32nd comment:
Votes: 0
Ok, so I came up with a way to make it work, with color codes. I wrote it in the ugliest Ruby I could imagine (hoping it would translate to c++ and then C). Here you guys go.

class Inverser 

def initialize(str)
@original = str
@new = String.new
@colors = Array.new
@color_str = '#'
end

def extract_color
i = 0
get_color = false
@original.each_char do |c|
if get_color == true
@colors[i-1] = (@color_str + c)
get_color = false
elsif c == @color_str
get_color = true
else
@new << c
end
i += 1
end
end

def insert_color
i = 0
arr = Array.new
@new.each_char do |c|
if @colors[i].nil?
arr << c
else
arr << @colors[i]
arr << c
end
i += 1
end
@new = String.new
arr.each do |c|
@new << c
end
end

def reverse_string
arr = Array.new
@new.each_char do |c|
arr << c
end
@new = String.new
arr.size.times do
@new << arr.pop
end
end

def inverse
extract_color
reverse_string
insert_color
return @new
end

end
04 Aug, 2009, Chris Bailey wrote in the 33rd comment:
Votes: 0
Er oops. All of those methods are private except inverse. :P


EDIT: You can test it with "foobar = Inverser.new("#rblah#nfoo#pyaya");foobar.inverse" It isn't meant to be a plug n play, but the code is there and ready to be adapted to whatever system.
04 Aug, 2009, JohnnyStarr wrote in the 34th comment:
Votes: 0
Boblinski, if you are using stock QuickMud it dosn't compile in g++, that is if you haven't cleaned up the
code yet. I'm just about done cleaning up a QuikMud++ (needed extra time to get all the gcc 4.* quirks worked out)
If you are interested in using it as a reference I'll be uploading it soon. :tongue:
04 Aug, 2009, Davion wrote in the 35th comment:
Votes: 0
#include <stdio.h>
#include <string.h>

void invert(const char* src, char* dst)
{ int i, len = strlen(src);

for ( i = 0; i < len; i++)
{ int pos = len - i - 1;
if(src[pos] == '{' && i > 0 && pos < len-1 )
{ dst[i-1] = src[pos];
dst[i] = src[pos+1];
}
else
dst[i] = src[pos];
}
dst[len] = '\0';
}


int main (void)
{ const char *str = "This is my first and only test. Col{rour is a{broun no doubt{";
char inv[200];

invert(str, inv);

printf("%s\n%s\n",str, inv);
return 1;
}


Snagged the one from David Haley and hacked at it. Should maintain proper colourcode, but definitely not in the right order (but I doubt you really care in this case :P)
04 Aug, 2009, flumpy wrote in the 36th comment:
Votes: 0
Chris Bailey said:
Ok, so I came up with a way to make it work, with color codes. I wrote it in the ugliest Ruby I could imagine (hoping it would translate to c++ and then C). Here you guys go.

FAIL


:tongue:
04 Aug, 2009, flumpy wrote in the 37th comment:
Votes: 0
Davion said:
#include <stdio.h>
#include <string.h>

void invert(const char* src, char* dst)
{
… SNIPPY SNIP….
}


int main (void)
{ const char *str = "This is my first and only test. Col{rour is a{broun no doubt{";
char inv[200];

invert(str, inv);

printf("%s\n%s\n",str, inv);
return 1;
}


Snagged the one from David Haley and hacked at it. Should maintain proper colourcode, but definitely not in the right order (but I doubt you really care in this case :P)


Not to be a gripe monkey, but technically this is "reverse" not "invert". Invert tends to imply you are turning it upside down, back to front and, if you remember the spectrum days of old, inverting the colours too.

Call me picky but I think "reverse" is a better description than "invert" :wink:
04 Aug, 2009, Guest wrote in the 38th comment:
Votes: 0
Yeah, but epic laziness on our part back in the day caused us to just copy what we saw and the name never got changed :)
04 Aug, 2009, Chris Bailey wrote in the 39th comment:
Votes: 0
@Flumpy about my failure

No joke. I don't really know any language but Ruby so writing something that would translate is ridiculously hard for me. Runter pointed out to me how crappy it was though, and so did you…so it must be. :P More than willing to supply a real Ruby solution if you guys would like, but I would imagine it would be even harder to port to C++ =)
04 Aug, 2009, flumpy wrote in the 40th comment:
Votes: 0
Chris Bailey said:
@Flumpy about my failure

No joke. I don't really know any language but Ruby so writing something that would translate is ridiculously hard for me. Runter pointed out to me how crappy it was though, and so did you…so it must be. :P More than willing to supply a real Ruby solution if you guys would like, but I would imagine it would be even harder to port to C++ =)


Bah I wrote worse code in my day. Stuff that would make you hair stand on end. I never had a Runter to point stuff out to me, well, not up until a few years ago anyway.

We all learn from how bad things can be. Just take the lessons and do what you can, it's all anyone can do :D
20.0/89