]> err.no Git - sope/blob - sope-gdl1/MySQL4/NSString+SQLite.m
more cleanups
[sope] / sope-gdl1 / MySQL4 / NSString+SQLite.m
1 /* 
2    NSString+MySQL4.m
3
4    Copyright (C) 1999 MDlink online service center GmbH and Helge Hess
5
6    Author: Helge Hess (helge@mdlink.de)
7
8    This file is part of the MySQL4 Adaptor Library
9
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Library General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Library General Public License for more details.
19
20    You should have received a copy of the GNU Library General Public
21    License along with this library; see the file COPYING.LIB.
22    If not, write to the Free Software Foundation,
23    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 */
25
26 #if LIB_FOUNDATION_BOEHM_GC
27 #  include <objc/gc.h>
28 #endif
29
30 #import <objc/objc-api.h>
31 #include "common.h"
32 #import "NSString+MySQL4.h"
33
34 @implementation NSString(MySQL4MiscStrings)
35
36 - (NSString *)_mySQL4ModelMakeInstanceVarName {
37   unsigned clen = 0;
38   char     *s   = NULL;
39   int      cnt, cnt2;
40   
41   if ([self length] == 0)
42     return @"";
43
44   clen = [self cStringLength];
45   s = malloc(clen + 10);
46
47   [self getCString:s maxLength:clen];
48     
49   for (cnt = cnt2 = 0; cnt < clen; cnt++, cnt2++) {
50     if ((s[cnt] == '_') && (s[cnt + 1] != '\0')) {
51       s[cnt2] = toupper(s[cnt + 1]);
52       cnt++;
53     }
54     else if ((s[cnt] == '2') && (s[cnt + 1] != '\0')) {
55       s[cnt2] = s[cnt];
56       cnt++;
57       cnt2++;
58       s[cnt2] = toupper(s[cnt]);
59     }
60     else
61       s[cnt2] = tolower(s[cnt]);
62   }
63   s[cnt2] = '\0';
64   
65   return [[[NSString alloc] 
66             initWithCStringNoCopy:s length:strlen(s) freeWhenDone:YES] 
67            autorelease];
68 }
69
70 - (NSString *)_mySQL4ModelMakeClassName {
71   unsigned clen = 0;
72   char     *s   = NULL;
73   int      cnt, cnt2;
74   
75   if ([self length] == 0)
76     return @"";
77
78   clen = [self cStringLength];
79   s = malloc(clen + 10);
80
81   [self getCString:s maxLength:clen];
82     
83   for (cnt = cnt2 = 0; cnt < clen; cnt++, cnt2++) {
84       if ((s[cnt] == '_') && (s[cnt + 1] != '\0')) {
85         s[cnt2] = toupper(s[cnt + 1]);
86         cnt++;
87       }
88       else if ((s[cnt] == '2') && (s[cnt + 1] != '\0')) {
89         s[cnt2] = s[cnt];
90         cnt++;
91         cnt2++;
92         s[cnt2] = toupper(s[cnt]);
93       }
94       else
95         s[cnt2] = tolower(s[cnt]);
96   }
97   s[cnt2] = '\0';
98
99   s[0] = toupper(s[0]);
100
101   return [[[NSString alloc] 
102                        initWithCStringNoCopy:s length:strlen(s)
103                        freeWhenDone:YES]
104                        autorelease];
105 }
106
107 - (NSString *)_mySQL4StringWithCapitalizedFirstChar {
108   NSCharacterSet  *upperSet;
109   NSMutableString *str;
110   
111   if ([self length] == 0)
112     return @"";
113   
114   upperSet = [NSCharacterSet uppercaseLetterCharacterSet];
115   if ([upperSet characterIsMember:[self characterAtIndex:0]])
116     return [[self copy] autorelease];
117   
118   str = [NSMutableString stringWithCapacity:[self length]];
119   [str appendString:[[self substringToIndex:1] uppercaseString]];
120   [str appendString:[self substringFromIndex:1]];
121   return [[str copy] autorelease];
122 }
123
124 - (NSString *)_mySQL4StripEndSpaces {
125   NSCharacterSet  *spaceSet;
126   NSMutableString *str;
127   IMP             charAtIndex;
128   NSRange         range;
129   
130   if ([self length] == 0)
131     return [[self copy] autorelease];
132   
133   spaceSet = [NSCharacterSet whitespaceCharacterSet];
134   str      = [NSMutableString stringWithCapacity:[self length]];
135   charAtIndex  = [self methodForSelector:@selector(characterAtIndex:)];
136   range.length = 0;
137
138   for (range.location = ([self length] - 1);
139          range.location >= 0;
140          range.location++, range.length++) {
141       unichar c;
142       
143       c = (unichar)(int)charAtIndex(self, @selector(characterAtIndex:),
144                                     range.location);
145       if (![spaceSet characterIsMember:c])
146         break;
147   }
148     
149   if (range.length > 0) {
150       [str appendString:self];
151       [str deleteCharactersInRange:range];
152       return [[str copy] autorelease];
153   }
154   return [[self copy] autorelease];
155 }
156  
157 @end
158
159 void __link_NSStringMySQL4() {
160   // used to force linking of object file
161   __link_NSStringMySQL4();
162 }