//
// StringTestUNIX.m -- test out the String class' UNIX category
//		Written by Don Yacktman (c) 1994 by Don Yacktman.
//				Version 1.0.  All rights reserved.
//
//		This notice may not be removed from this source code.
//
//	This program is included in the MiscKit by permission from the author
//	and its use is governed by the MiscKit license, found in the file
//	"LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
//	for a list of all applicable permissions and restrictions.
//	

void printPathAndFileName(id aString)
{
	printf("Given:        \"%s\"\n", [aString stringValue]);
	printf("     Path is:  \"%s\"\n", [[aString pathName] stringValueAndFree]);
	printf(" Filename is:  \"%s\"\n",
			[[aString fileName] stringValueAndFree]);
	printf("Extension is:  \"%s\"\n",
			[[aString fileExtension] stringValueAndFree]);
	printf("Base name is:  \"%s\"\n",
			[[aString fileBasename] stringValueAndFree]);
}

#define USTRINGS 5
void testMiscStringUNIX()
{
	id string[USTRINGS]; int i;
	printf("***** Testing MiscStringUNIX category\n");

// Test MiscStringUNIX.m methods:

	// -fileName, -fileNameFromZone:, -pathName, -pathNameFromZone:
	string[0] = [MiscString newWithString:
	"/Net/darth/Users/don/Projects/daymisckit_proj/daymisckit-1/DAYString.m"];
	printPathAndFileName(string[0]);
	[string[0] setStringValue:"DAYString.m"];
	printPathAndFileName(string[0]);
	[string[0] setStringValue:"README"];
	printPathAndFileName(string[0]);
	[string[0] setStringValue:"/a/README.tar.gz"];
	printPathAndFileName(string[0]);
	[string[0] setStringValue:"~/.cshrc"];
	printPathAndFileName(string[0]);
	[string[0] setStringValue:"~/.newsrc.old"];
	printPathAndFileName(string[0]);
	[string[0] setStringValue:""];
	printPathAndFileName(string[0]);

	// -encrypt:
	[string[0] setStringValue:"Blahbla0"];
	string[1] = [MiscString newWithString:"DY"];
	string[2] = [MiscString newWithString:NULL];
	string[3] = [MiscString newWithString:""];
	string[4] = [MiscString newWithString:"Blahbla0xyzpdq"];
	printf("String \"%s\" with salt \"%s\" encrypts to  \"%s\" .\n",
		[string[0] stringValue], [string[1] stringValue],
		[[string[0] encrypt:string[1]] stringValueAndFree]);
	printf("String \"%s\" with salt \"%s\" encrypts to  \"%s\" .\n",
		[string[4] stringValue], [string[1] stringValue],
		[[string[4] encrypt:string[1]] stringValueAndFree]);
	printf("String \"%s\" with salt \"%s\" encrypts to  \"%s\" .\n",
		[string[2] stringValue], [string[1] stringValue],
		[[string[2] encrypt:string[1]] stringValueAndFree]);
	printf("String \"%s\" with salt \"%s\" encrypts to  \"%s\" .\n",
		[string[0] stringValue], [string[2] stringValue],
		[[string[0] encrypt:string[2]] stringValueAndFree]);
	[string[2] setStringValue:""];
	printf("String \"%s\" with salt \"%s\" encrypts to  \"%s\" .\n",
		[string[3] stringValue], [string[1] stringValue],
		[[string[3] encrypt:string[1]] stringValueAndFree]);
	printf("String \"%s\" with salt \"%s\" encrypts to  \"%s\" .\n",
		[string[0] stringValue], [string[3] stringValue],
		[[string[0] encrypt:string[3]] stringValueAndFree]);

	// -replaceHomeWithTilde, -replaceTildeWithHome
	[string[0] setStringValue:NXHomeDirectory()];
	[string[0] cat:"/aFile.txt"];
	[string[1] setStringValue:"/usr/local/bin/spew"];
	for (i=0; i<4; i++)
		printf("Replace home with tilde: \"%s\" to \"%s\".\n",
				[string[i] stringValue],
				[[[string[i] copy] replaceHomeWithTilde] stringValueAndFree]);
	[string[0] setStringValue:"~/SomeCode.c"];
	for (i=0; i<4; i++)
		printf("Replace tilde with home: \"%s\" to \"%s\".\n",
				[string[i] stringValue],
				[[[string[i] copy] replaceTildeWithHome] stringValueAndFree]);

	// ***** Not yet tested!
	// - addExtensionIfNeeded:(const char *)aString;
	// - (BOOL)isRelativePath;
	// - (BOOL)isAbsolutePath;
	// - (BOOL)doesExistInFileSystem;
	// - (BOOL)isFileOfType:(MiscFileType)fileType;
	// - pathComponentAt:(int)index
	// - (int)numberOfPathComponents
	// - setDirectory:(const char *)dir file:(const char *)file;
	// - initDirectory:(const char *)dir file:(const char *)file;

	for (i=0; i<USTRINGS; i++) [string[i] free];
}

