
global proc string[] djnStringToCharArray(string $string)
{
	int $len = `size $string`;
	int $i;
	string $arr[];
	for ($i = 1; $i <= $len; $i++)
	{
		$arr[`size $arr`] = `substring $string $i $i`;
	}
	return $arr;
}


global proc int djnFindFirstInString(string $char, string $this)
{
	int $len = `size $this`;
	int $i;
	string $c;
	for ($i = 1; $i <= $len; $i++)
	{
		$c = `substring $this $i $i`;
		if ($c == $char)
			return $i;
	}
	return -1;
}

// separate the file name from the path given
global proc string[] djnSeparateFileFromPath(string $pathname)
{
	string $separator = "/";
	string $token[];
	tokenize $pathname $separator $token;
	int $i;
	string $return[];
	for ($i = 0; $i < `size $token` - 1; $i++)
	{
		$return[0] = ($return[0] + $token[$i] + "/");
	}
	$return[1] = $token[$i];
	return $return;
}


// remove the file extension from the end of a pathname
// returns the path minus the extension
global proc string[] djnRemoveFileExtension(string $pathname)
{
	string $token[];
	tokenize $pathname "." $token;
	int $num = `size $token`;
	string $return[2];
	int $i;
	for ($i = 0; $i < $num - 1; $i++)
	{
		// this assumes the possibility that there may be dots in the middle of the rest of the path
		// since windows now allows that
		$return[0] = ($return[0] + $token[$i] + ".");
	}
	$return[0] = `djnTrimString $return[0] 1`;
	$return[1] = $token[$num - 1];
	return $return;
}

// remove the newline characters from the end of a string
global proc string djnRemoveNewline(string $thisString)
{
	$match = `match "[\\\n]$" $thisString`;
	if (`size $match`)
		$thisString = `djnTrimString $thisString 1`;
	$match = `match "[\\\r]$" $thisString`;
	if (`size $match`)
		$thisString = `djnTrimString $thisString 1`;
	return $thisString;
}

// remove quotes from

// count the number of times the given string occurs in the second string
global proc int djnCountStringOccurrences(string $find, string $inThis)
{
	int $len = `size $find` - 1;
	string $sub;
	int $i;
	int $count = 0;
	for ($i = 1; $i <= (`size $inThis` - $len); $i++)
	{
		$sub = `substring $inThis $i ($len + $i)`;
		if ($sub == $find)
			$count++;
	}
	return $count;
}

// remove the given amount of characters from the front of the string
global proc string djnChopString(string $thisString, int $amt)
{
	int $size = `size $thisString`;
	string $newString = `substring $thisString ($amt + 1) $size`;
	return $newString;
}

// remove the given amount of characters from the end of the string
global proc string djnTrimString( string $thisString, int $amt)
{
	int $size = `size $thisString`;
	string $newString = `substring $thisString 1 ($size - $amt)`;
	return $newString;
}

// insert a string at the given position in another string
global proc string djnInsertString(string $insert, string $thisString, int $pos)
{
	int $size = `size $thisString`;
	string $front = `substring $thisString 1 $pos`;
	string $back = `substring $thisString ($pos + 1) $size`;
	$front += ($insert + $back);
	return $front;
}

// chop a string up into a char array
global proc string[] djnString_ToCharArray(string $thisString)
{
	int $len = size($thisString);
	string $array[];
	int $i;
	for ($i = 1; $i <= $len; $i++)
	{
		$array[`size $array`] = substring($thisString, $i, $i);
	}
	return $array;
}



global proc djnFileReplace(string $path, string $add)
{
	string $dir[] = djnSeparateFileFromPath($path);
	string $name[] = djnRemoveFileExtension($dir[1]);
	string $tempName;
	int $rand;
	// create random file names, until one is found that doesn't already exist
	// this really shouldn't have to run more than once...
	do{
		$rand = rand(1000, 10000);
		$tempName = ($dir[0] + "tempFile_" + $rand + ".txt");
	}while (`filetest -r $tempName`);
	
	// once a unique name has been found, open the old file
	int $oldId = `fopen $path "r"`;
	if (!$oldId)
		error ("Could not open file " + $path);
	// open the new file
	int $newId = `fopen $tempName "w"`;
	if (!$newId)
		error ("Could not create temporary file at " + $tempName);
	
	// write the file
	while (!`feof $oldId`)
	{
		string $line = `fgetline $oldId`;
		if (size($line))
			fprint $newId ($line + $add);
	}
	// close the files
	fclose $newId;
	fclose $oldId;

	// create the new name for the old file
	string $oldName = ($dir[0] + $name[0] + "_Old_" + $rand + "." + $name[1]);
	// the odd thing about the sysFile comand, is that when you use the rename,
	// move, or copy flags, you put the new name for the file first, then the old name
	// this is opposite from on the command line, in windows at least.
	if(!`sysFile -rename $oldName $path`)
		error ("could not rename " + $path + " to " + $oldName);
	if(!`sysFile -rename $path $tempName`)
		error ("could not rename " + $tempName + " to " + $path);
}
