home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / MiscKit1.2.6 / Source / MiscStringSybase.m < prev    next >
Encoding:
Text File  |  1994-05-19  |  2.7 KB  |  102 lines

  1. //
  2. //    MiscStringSybase.m
  3. //        Written by Don Yacktman (c) 1993 by Don Yacktman.
  4. //                Version 1.95  All rights reserved.
  5. //        This notice may not be removed from this source code.
  6. //
  7. //    This object is included in the MiscKit by permission from the author
  8. //    and its use is governed by the MiscKit license, found in the file
  9. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  10. //    for a list of all applicable permissions and restrictions.
  11. //    
  12.  
  13. #import <misckit/MiscString.h>
  14.  
  15. @implementation MiscString(Sybase)
  16.  
  17. // This category includes methods derived from the MOSybaseString class
  18. // which are useful when dealing with Sybase databases.  I prefer this
  19. // be a category, and not a subclass.
  20.  
  21. - convertUnixWildcardsToSybase
  22. // Converts all occurences of "*" to "%" and all occurences of "?" to "_".
  23. {
  24.     [self replaceEveryOccurrenceOfChar:'*' withChar:'%' caseSensitive:YES];
  25.     [self replaceEveryOccurrenceOfChar:'?' withChar:'_' caseSensitive:YES];
  26.     return self;
  27. }
  28.  
  29. - convertToCaseInsensitiveSearchString
  30. // Modifies a MiscString by replacing all alphabetic
  31. // characters with a case-insensitive sql expression.
  32. // (ie "a" or "A" gets converted to "[aA]".
  33. {
  34.     NXStream *stream = NULL;
  35.     long l;
  36.     char *buf;
  37.     const char *s;
  38.     int i;
  39.     
  40.     if (!length) return self;
  41.     
  42.     // Convert to lower case
  43.     [self toLower];
  44.     s = [self stringValue];
  45.     
  46.     // Now create a stream to write the new version into
  47.     stream = NXOpenMemory(NULL, 0, NX_READWRITE);
  48.     
  49.     // Write the new version
  50.     for (i=0; i<length; i++)  {
  51.         if (NXIsAlpha(s[i]))  {
  52.             BOOL skip = NO;
  53.             if ((i > 0) && (i < length - 2)) {
  54.                 if ((s[i-1] == '[') && (s[i+2] == ']')) {
  55.                     if (s[i] == s[i+1]) skip = YES;
  56.                 }
  57.             }
  58.             if (skip) {
  59.                 NXPutc(stream, s[i++]);
  60.                 NXPutc(stream, NXToUpper(s[i]));
  61.             } else {
  62.                 NXPutc(stream, '[');
  63.                 NXPutc(stream, s[i]);
  64.                 NXPutc(stream, NXToUpper(s[i]));
  65.                 NXPutc(stream, ']');
  66.             }
  67.         }  else  {
  68.             NXPutc(stream, s[i]);
  69.         }
  70.     }
  71.     
  72.     // Flush and get the position of the stream (ie the length)
  73.     NXFlush(stream);
  74.     l = NXTell(stream);
  75.     // Go back to the beginning, malloc enough space, and read the stream
  76.     // back out.
  77.     NXSeek(stream, 0, NX_FROMSTART);
  78.     NX_MALLOC(buf, char, l+1);
  79.     NXRead(stream, buf, l);
  80.     // Cap the buffer
  81.     buf[l]='\0';
  82.     // Free the stream
  83.     NXCloseMemory(stream, NX_FREEBUFFER);
  84.     
  85.     // Set the stringValue of the new string to the case insensitive string
  86.     // in the buffer.
  87.     [self setStringValue:buf];
  88.     NX_FREE(buf);
  89.     return self;
  90. }
  91.  
  92. - makeCaseInsensitiveSearchString
  93. // Creates a new MiscString, replacing all alphabetic
  94. // characters with a case-insensitive sql expression.
  95. // (ie "a" or "A" gets converted to "[aA]".
  96. {
  97.     id ciss = [self copy];
  98.     return [ciss convertToCaseInsensitiveSearchString];
  99. }
  100.  
  101. @end
  102.