// ////////////////////////////////////////////////////////
// Copyright (1994) Columbia University
// Department of Biochemistry and Molecular Biophysics
//
// Author: Weider Chang, Ph.D
// ///////////////////////////////////////////////////////

#include "wstring.h"

@implementation WDCString


- setAppSpecObject:anObject
{
  _appSpecObject=anObject;
  return self;
}

- getAppSpecObject
{
  return _appSpecObject;
}

- free
{
  // [_appSpecObject free];
  free(_contents_chars);
  [super free];
}

- deepCopy
{
  return [[WDCString alloc] initFromString:self];
}

- shallowCopy
{
  return [self shouldNotImplement:_cmd];
}

- selectedDeepCopy
{
  return [self deepCopy];
}

- initFromString:(String*)str
{
  return [self initFromCString: (char *)[str cStringValue]];
}

- copyCString: (const char *)charptr
{
  [[self cStringValue]?[self empty]:self initFromCString: (char*) charptr];
  return self;
}

- copyStringValue:(String*)string;
{
  return [self copyCString: (char*)[string cStringValue]];
}

// need to collect the new WDCString. 

- (int)compareCString:(const char *)charptr ;
{
  return [self compareCString:charptr ignoreCase:NO];
}


- (int)compareCString:(const char *)charptr ignoreCase:(BOOL)flag;
{
  id string=[[WDCString alloc] initFromCString:(char*)charptr];
  int result=[self compare:string ignoreCase: flag];
  [string free];
  return result;
}

- (int)compare:anObject;
{
  return [self compare:anObject ignoreCase:NO];
}

- (int)compare:anObject ignoreCase:(BOOL)flag;
{
  WDCString *str1, *str2;
  int result=0;
  if ([anObject isKindOf:[self superClass]])
    {
      if (flag)
	{
	  str1=[[[WDCString alloc] initFromString:self] setToLowerCase];
	  str2=[[[WDCString alloc] initFromString:(String*)anObject] 
		setToLowerCase];
	  result=strcmp([str1 cStringValue], [str2 cStringValue]);
	  [str1 free];
	  [str2 free];
	}
      else
	{
	  result=strcmp([self cStringValue], [anObject cStringValue]);
	}
    }
  else 
    {
      [self error:"Can't compare with a non-String object."];
    }
  return result;
}

- (unsigned) hash;
{
  return elt_hash_string((elt)(char*)[self cStringValue]);
}

- (int)intValue;
{
  return atoi([self cStringValue]);
}

- (float)floatValue;
{
  [self notImplemented:_cmd];
  return 0;
}

- (int)getIntValue;
{
  return [self intValue];
}

- (float)getFloatValue;
{
  return [self floatValue];

}

/*
   This is not the right implementation (will not work with 
   setToInitialCapitals method.) It only works in our application.
   */

- setToLowerCase;
{
  char * s=_contents_chars;
  while (*s) *s++=tolower(*s);
  return self;
}

- printForDebugger
{
  printf("(%s %s)\n", [self name], [self cStringValue]);
  return self;
}
  

@end





