package MISC;
require Exporter;
@ISA = qw/Exporter/;
@EXPORT = qw/parse_tocclientmsg/;
print("hello");
# test_unencode();

# test_parse_toc ();
# just a function to test our our parse_tocclientmsg function.
sub test_parse_toc {
my $teststring = 'toc_cmd argument0 "argument1\" test" argument1.5 "argument2" argument3';

my($argument1, $argument2, $argument3, $argument4, $argument5, $argument6, $argument7) = parse_tocclientmsg($teststring);
# print $output[0];
# print $output[1];
# print $output[2];
print ("\n\n\n");
print $argument2;
print("\n");
print $argument3;
print ("\n");
print $argument4;
print ("\n");
print $argument5;
print ("\n");
print $argument6;
print ("\n");
print $argument7;
print ("\n");
}
sub test_unencode {

my($unencodeteststr) = "argument";
my($unencoderesult) = unencode($unencodeteststr);
print($unencoderesult);

}

sub parse_tocclientmsg {

# A function to split a toc client -> server message command and arguments 
# each into a seperate variable.
# We first split the toc command from the rest of the message, and store it 
# to masterarray. Then we have a loop, in the loop we have an if conditionals,
# which test if the character we are pulling from the front of the string are
# either a quote, a space, or a any other character. If its a quote, a
# second loop is entered with additional conditionals, one that tests for
# backslash, in which case the backslash is deleted and the next char moved to
# the toappend string, another that tests for any character except quote in
# which case the character is moved to $toappend, and another that tests for a
# quote in which case the loop exits, and $toappend is added as a new element on
# $masterarray. Every time a character is processed it is removed from the
# string, and srcount is incremented.

# If the first layer of conditionals finds a space, the space is deleted,
# since it is between the arguments

# If the first layer if conditionals find another character besides quote, a
# split is used to split up to the next space, the resulting string is run
# through unencode function, and the splitted part is added
# to masterarray.

# An else is included in the first if conditional layer, so if no more
# characters are pulled from the string, we have reached the end, and
# masterarray is returned.
my($instring) = @_;

my $quote = "\"";
my $backslash = "\\";
my $shouldwedebug = 0;
my($strlen) = length($instring);
my($strcount) = 0;
# my $instring = "toc_cmd hello hello" 
# my (@outstring) = split(" ", $instring, 2);
# my $instring = "toc_cmd "\"Hello there\", I said";
# split the toc command for the rest of the message
@splitarray = split(" ", $instring, 2); 
my($arraycount) = 2;
my(@masterarray);
$masterarray[0] = $arraycount;
$masterarray[1] = $splitarray[0];
if ($shouldwedebug == 1) {
print("\n1\n");
print($splitarray[0]);
print("\n2\n");
print($splitarray[1]);
print("\n\n\n");
}
# While 1
Masterparent1:  while ($strcount<$strlen) { # 2
my $testchar = substr($splitarray[1], 0, 1);

# If 2
Parent0: if($strlen > $strcount) { # 3

if (my $testchar = substr($splitarray[1], 0, 1)) { # 3.5

#If 
if ($shouldwedebug == 1){
print("\ntestchar:\n");
print($testchar);
print("\n");
}
# test for quote
Parent1:  if($testchar eq $quote) { # 4
	substr($splitarray[1], 0, 1) = "";
	if ($shouldwedebug == 1) {
	print("2:");
	print($splitarray[1]);
	}
 	++$strcount;
	Parent2: while (1) { # 5
		print ("\nParent2\n") if ($shouldwedebug == 1);
		my $inttestchar = substr($splitarray[1], 0, 1);
		if($strcount<$strlen) { # 6
		# Append toappend to another string, that string should
		# be appended to the end of masterarray when we
		# have reached the end of this argument.
		# V backslash test
			# if(substr($splitarray[1], 0, 1) eq '\') {
			if($inttestchar eq $backslash) { # 7
			print ("\nquoted1\n") if ($shouldwedebug == 1);
			substr($splitarray[1], 0, 1) = "";
			++$strcount;
			$toappend = substr($splitarray[1], 0, 1);
			substr($splitarray[1], 0, 1) = "";
			++$strcount;
			#$toarray = join($toarray, $toappend);
			$toarray = $toarray . $toappend;
			# last Parent2;
		 } # e 7		

		# test for quote qq{"}
		elsif (substr($splitarray[1], 0, 1) eq $quote) { # 7
			print ("\nquoted2\n") if ($shouldwedebug == 1);
			substr($splitarray[1], 0, 1) = "";
			++$strcount;
			push(@masterarray, $toarray);
			$toarray = "";
			last Parent2;
		} # e 7
		# test for quote qq{"} | "\" and backslash
		elsif (substr($splitarray[1], 0, 1) ne $quote | $backslash) { # 7
			print ("\nquoted3\n") if ($shouldwedebug == 1);
			$toappend = substr($splitarray[1], 0, 1);
			substr($splitarray[1], 0, 1) = "";
			++$strcount;
			$toarray = $toarray . $toappend;
			# last Parent2;
		} # e 7
		
		else {  # 7
			push(@masterarray, $toarray);
			++$arraycount;
			print ("hmmmmm");
			return(@masterarray);
						
		} # e 7
	} # e 6
	} # e 5
# ++arraycount;
} # e 4

 # test for space
if ($testchar eq " ") { # 5
       substr($splitarray[1], 0, 1) = "";
       ++$strcount;
       # chomp($splitarray[1]);
       # last Parent0;
 } # e 6


# test for quote qq{"}
elsif ($testchar ne $quote) { # 4
 
 # test for space	
if ($testchar eq " ") { # 5
	print ("\nshouldnt be here\n");
	substr($splitarray[1], 0, 1) = "";
	++$strcount;
	# chomp($splitarray[1]);
	last Parent0;
 } # e 6
	@temparray = split(" ",$splitarray[1], 2); 
	# $toappend = substr($splitarray[2], 0, 1);
	# chomp($splitarray[2]);
	$toappend = $temparray[0];
	my $talen = length($toappend);
	$strcount = $talen + $strcount;
	# my($toappend1) = unencode($toappend);
	push(@masterarray, $toappend);
	# my $masterarray[($arraycount)] = $toappend;
	++$arraycount;
	$splitarray[1] = $temparray[1];
	$toappend = "";
} # e 5

} # e 4
else { # 4
return (@masterarray);
} # e 4
} # e 3.5
else {
return (@masterarray);
}

} # e 3
if ($shouldwedebug == 1) {
print $splitarray[1];
print $splitarray[2]; 
}
return (@masterarray);

 } # e 2


sub unencode { 
# 1
# A function to remove backslashes encoding from a string.

# my($locquote) = "\"";
my($locslash) = "\\";

print("We are here");
my ($locapp);
my ($loctoarray);
my(@loctstr2) = @_;
my(@loctstr1) = $locteststring2[0];
my($locstrlen) = length($locteststring1);
my ($locscount) = 0;
print $locstr1;
# enter while loop. locstrcount is incremented every time a character is
# processed, and locstrlen is the length of the string we are processing.

Whileloop1: while ($locstrcount<$locstrlen) { # 2

if(my ($testchar1) = substr($loctstr1, 0, 1)) { # 3
my($testchar1) = substr($loctstr1, 0, 1);
# If the test is backslash, go ahead and delete the backslash, and move the
# next character to the toappend string, which is returned when the function
# exits. Increment locstrcount for every character removed.

if($testchar1 eq $locslash) { # 4
	# print ("\nquoted1\n") if ($shouldwedebug == 1);
	substr($loctstr1, 0, 1) = "";
	 ++$locscount;
	$locapp = substr($loctstr1, 0, 1);
	substr($loctstr1, 0, 1) = "";
	# ++$locscount;
	# $toarray = join($loctoarray, $loctoappend);
	$loctoarray = $loctoarray . $locapp;
	# last Parent2;
} # e 4
# test for quote qq{"} | "\" and backslash
# If char does not equal backslash, go ahead and copy the character to our
# toappend string which is returned when the function exits

elsif ($testchar1 ne $locslash) { # 4
	# print ("\nquoted3\n") if ($shouldwedebug == 1);
	$locapp = substr($loctstr1, 0, 1);
	substr($teststring1, 0, 1) = "";
	# ++$locscount;
	$loctoarray = $loctoarray . $locapp;
	# last Parent2;
} # e 4
} # e 3
# Set return information and end the while loop
else { # 3
my($finstring);
$finstring =  $loctoarray;
return($finstring);
$loctoarray = "";
last Whileloop1;
} # e 3
} # e 2

} # e 1

1;