]> err.no Git - sope/blob - sope-gdl1/PostgreSQL/NSString+PostgreSQL72.m
git-svn-id: http://svn.opengroupware.org/SOPE/trunk@20 e4a50df8-12e2-0310-a44c-efbce7...
[sope] / sope-gdl1 / PostgreSQL / NSString+PostgreSQL72.m
1 /* 
2    NSString+PostgreSQL72.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 PostgreSQL72 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 // $Id: NSString+PostgreSQL72.m 1 2004-08-20 10:38:46Z znek $
26
27 #if LIB_FOUNDATION_BOEHM_GC
28 #  include <objc/gc.h>
29 #endif
30
31 #import <objc/objc-api.h>
32 #include "common.h"
33 #import "NSString+PostgreSQL72.h"
34
35 @implementation NSString(PostgreSQL72MiscStrings)
36
37 - (NSString *)_pgModelMakeInstanceVarName {
38   if ([self length] == 0)
39     return @"";
40   else {
41     unsigned clen = 0;
42     char     *s   = NULL;
43     int      cnt, cnt2;
44
45     clen = [self cStringLength];
46     s = malloc(clen + 10);
47
48     [self getCString:s maxLength:clen];
49     
50     for (cnt = cnt2 = 0; cnt < clen; cnt++, cnt2++) {
51       if ((s[cnt] == '_') && (s[cnt + 1] != '\0')) {
52         s[cnt2] = toupper(s[cnt + 1]);
53         cnt++;
54       }
55       else if ((s[cnt] == '2') && (s[cnt + 1] != '\0')) {
56         s[cnt2] = s[cnt];
57         cnt++;
58         cnt2++;
59         s[cnt2] = toupper(s[cnt]);
60       }
61       else
62         s[cnt2] = tolower(s[cnt]);
63     }
64     s[cnt2] = '\0';
65     
66     return [[[NSString alloc] 
67                        initWithCStringNoCopy:s length:strlen(s) freeWhenDone:YES] 
68                        autorelease];
69   }
70 }
71
72 - (NSString *)_pgModelMakeClassName {
73   if ([self length] == 0)
74     return @"";
75   else {
76     unsigned clen = 0;
77     char     *s   = NULL;
78     int      cnt, cnt2;
79
80     clen = [self cStringLength];
81     s = malloc(clen + 10);
82
83     [self getCString:s maxLength:clen];
84     
85     for (cnt = cnt2 = 0; cnt < clen; cnt++, cnt2++) {
86       if ((s[cnt] == '_') && (s[cnt + 1] != '\0')) {
87         s[cnt2] = toupper(s[cnt + 1]);
88         cnt++;
89       }
90       else if ((s[cnt] == '2') && (s[cnt + 1] != '\0')) {
91         s[cnt2] = s[cnt];
92         cnt++;
93         cnt2++;
94         s[cnt2] = toupper(s[cnt]);
95       }
96       else
97         s[cnt2] = tolower(s[cnt]);
98     }
99     s[cnt2] = '\0';
100
101     s[0] = toupper(s[0]);
102
103     return [[[NSString alloc] 
104                        initWithCStringNoCopy:s length:strlen(s)
105                        freeWhenDone:YES]
106                        autorelease];
107   }
108 }
109
110 - (NSString *)_pgStringWithCapitalizedFirstChar {
111   NSCharacterSet *upperSet = [NSCharacterSet uppercaseLetterCharacterSet];
112   
113   if ([self length] == 0)
114     return @"";
115   else if ([upperSet characterIsMember:[self characterAtIndex:0]])
116     return [[self copy] autorelease];
117   else {
118     NSMutableString *str = [NSMutableString stringWithCapacity:[self length]];
119
120     [str appendString:[[self substringToIndex:1] uppercaseString]];
121     [str appendString:[self substringFromIndex:1]];
122
123     return [[str copy] autorelease];
124   }
125 }
126
127 - (NSString *)_pgStripEndSpaces {
128   if ([self length] > 0) {
129     NSCharacterSet  *spaceSet = [NSCharacterSet whitespaceCharacterSet];
130     NSMutableString *str      = [NSMutableString stringWithCapacity:[self length]];
131     IMP             charAtIndex;
132     NSRange         range;
133
134     charAtIndex  = [self methodForSelector:@selector(characterAtIndex:)];
135     range.length = 0;
136
137     for (range.location = ([self length] - 1);
138          range.location >= 0;
139          range.location++, range.length++) {
140       unichar c;
141       
142       c = (unichar)(int)charAtIndex(self, @selector(characterAtIndex:),
143                                     range.location);
144       if (![spaceSet characterIsMember:c])
145         break;
146     }
147     
148     if (range.length > 0) {
149       [str appendString:self];
150       [str deleteCharactersInRange:range];
151       return AUTORELEASE([str copy]);
152     }
153   }
154   return AUTORELEASE([self copy]);
155 }
156  
157 @end
158
159 void __link_NSStringPostgreSQL72() {
160   // used to force linking of object file
161   __link_NSStringPostgreSQL72();
162 }