// MOSybaseString.m
//
// by Mike Ferris
// Part of MOKit
// Copyright 1993, all rights reserved.

// ABOUT MOKit
// by Mike Ferris (mike@lorax.com)
//
// MOKit is a collection of useful and general objects.  Permission is 
// granted by the author to use MOKit in your own programs in any way 
// you see fit.  All other rights pertaining to the kit are reserved by the 
// author including the right to sell these objects as objects,  as part 
// of a LIBRARY, or as SOURCE CODE.  In plain English, I wish to retain 
// rights to these objects as objects, but allow the use of the objects 
// as pieces in a fully functional program.  Permission is also granted to 
// redistribute the source code of MOKit for FREE as long as this copyright 
// notice is left intact and unchanged.  NO WARRANTY is expressed or implied.  
// The author will under no circumstances be held responsible for ANY 
// consequences from the use of these objects.  Since you don't have to pay 
// for them, and full source is provided, I think this is perfectly fair.

#import "MOKit/MOSybaseString.h"
#import <objc/objc-runtime.h>

#define CLASS_VERSION		0
#define CLASS_NAME			"MOSybaseString"

@implementation MOSybaseString

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// =-=-=-=-=-=-=-=-=-=-=-=- Initializing  the class -=-=-=-=-=-=-=-=-=-=-=-=
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

+ initialize
// Set our version.
{
	if (self == objc_lookUpClass(CLASS_NAME))  {
		[self setVersion:CLASS_VERSION];
	}
	return self;
}

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// =-=-=-=-=-=-=-=-=-=-=-=-= Sybase-ish  Utilities =-=-=-=-=-=-=-=-=-=-=-=-=
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

- convertUnixWildcardsToSybase
// Converts all occurrences of "*" to "%" and all occurrences of "?" to "_".
// This does not work on unique strings.
{
	if (isUnique)  {
		return nil;
	}
	[self replaceAllOccurrencesOfChar:'*' with:'%'];
	[self replaceAllOccurrencesOfChar:'?' with:'_'];
	return self;
}

- convertToCaseInsensitiveSearchString
// Modifies a MOSybaseString by replacing all alphabetic
// characters with a case-insensitive sql expression.  (ie "a" or "A" gets
// converted to "[aA]".  This doesn't work for unique strings.
{
	NXStream *stream = NULL;
	long l;
	char *buf;
	const char *s;
	int i;
	
	if (isUnique)  {
		return nil;
	}
	
	if ([self isEmpty])  {
		return self;
	}
	
	// Convert to lower case
	[self convertToLower];
	s = [self stringValue];
	
	// Now create a stream to write the new version into
	stream = NXOpenMemory(NULL, 0, NX_READWRITE);
	
	// Write the new version
	for (i=0; i<len; i++)  {
		if (NXIsAlpha(s[i]))  {
			NXPutc(stream, '[');
			NXPutc(stream, s[i]);
			NXPutc(stream, NXToUpper(s[i]));
			NXPutc(stream, ']');
		}  else  {
			NXPutc(stream, s[i]);
		}
	}
	
	// Flush and get the position of the stream (ie the length)
	NXFlush(stream);
	l = NXTell(stream);
	// Go back to the beginning, malloc enough space, and read the stream
	// back out.
	NXSeek(stream, 0, NX_FROMSTART);
	NX_MALLOC(buf, char, l+1);
	NXRead(stream, buf, l);
	// Cap the buffer
	buf[l]='\0';
	// Free the stream
	NXCloseMemory(stream, NX_FREEBUFFER);
	
	// Set the stringValue of the new string to the case insensitive string
	// in the buffer.
	[self setStringValueNoCopy:buf shouldFree:YES];
	
	return self;
}

- makeCaseInsensitiveSearchString
// Creates a new MOSybaseString from aString by replacing all alphabetic
// characters with a case-insensitive sql expression.  (ie "a" or "A" gets
// converted to "[aA]".
{
	id ciss;
	
	// First make a copy
	ciss = [self deepCopy];
	return [ciss convertToCaseInsensitiveSearchString];
}

@end
