]> err.no Git - sope/blob - sope-gdl1/FrontBase2/NSString+FB.m
fix for lF
[sope] / sope-gdl1 / FrontBase2 / NSString+FB.m
1 /* 
2    NSString+FB.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 FB 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+FB.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 "NSString+FB.h"
32 #import "common.h"
33
34 @implementation NSString(FBMiscStrings)
35
36 - (NSString *)_sybModelMakeInstanceVarName {
37   if ([self length] == 0)
38     return @"";
39   else {
40     unsigned clen = 0;
41     char     *s   = NULL;
42     unsigned cnt, cnt2;
43
44     clen = [self cStringLength];
45     s = objc_atomic_malloc(clen + 5);
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 AUTORELEASE([[NSString alloc] initWithCStringNoCopy:s length:strlen(s) freeWhenDone:YES]);
66   }
67 }
68
69 - (NSString *)_sybModelMakeClassName {
70   if ([self length] == 0)
71     return @"";
72   else {
73     unsigned clen = 0;
74     char     *s   = NULL;
75     unsigned cnt, cnt2;
76
77     clen = [self cStringLength];
78     s = objc_atomic_malloc(clen + 1);
79
80     [self getCString:s maxLength:clen];
81     
82     for (cnt = cnt2 = 0; cnt < clen; cnt++, cnt2++) {
83       if ((s[cnt] == '_') && (s[cnt + 1] != '\0')) {
84         s[cnt2] = toupper(s[cnt + 1]);
85         cnt++;
86       }
87       else if ((s[cnt] == '2') && (s[cnt + 1] != '\0')) {
88         s[cnt2] = s[cnt];
89         cnt++;
90         cnt2++;
91         s[cnt2] = toupper(s[cnt]);
92       }
93       else
94         s[cnt2] = tolower(s[cnt]);
95     }
96     s[cnt2] = '\0';
97
98     s[0] = toupper(s[0]);
99
100     return AUTORELEASE([[NSString alloc] initWithCStringNoCopy:s length:s?strlen(s):0 freeWhenDone:YES]);
101   }
102 }
103
104 - (NSString *)_sybStringWithCapitalizedFirstChar {
105   NSCharacterSet *upperSet = [NSCharacterSet uppercaseLetterCharacterSet];
106   
107   if ([self length] == 0)
108     return @"";
109   else if ([upperSet characterIsMember:[self characterAtIndex:0]])
110     return AUTORELEASE([self copy]);
111   else {
112     NSMutableString *str = [NSMutableString stringWithCapacity:[self length]];
113
114     [str appendString:[[self substringToIndex:1] uppercaseString]];
115     [str appendString:[self substringFromIndex:1]];
116
117     return AUTORELEASE([str copy]);
118   }
119 }
120
121 - (NSString *)_sybStripEndSpaces {
122   if ([self length] > 0) {
123     NSCharacterSet  *spaceSet = [NSCharacterSet whitespaceCharacterSet];
124     NSMutableString *str      = [NSMutableString stringWithCapacity:[self length]];
125     IMP             charAtIndex;
126     NSRange         range;
127
128     charAtIndex  = [self methodForSelector:@selector(characterAtIndex:)];
129     range.length = 0;
130
131     for (range.location = ([self length] - 1);
132          range.location >= 0;
133          range.location++, range.length++) {
134       unichar c;
135       
136       c = (unichar)(int)charAtIndex(self, @selector(characterAtIndex:),
137                                     range.location);
138       if (![spaceSet characterIsMember:c])
139         break;
140     }
141     
142     if (range.length > 0) {
143       [str appendString:self];
144       [str deleteCharactersInRange:range];
145       return AUTORELEASE([str copy]);
146     }
147   }
148   return AUTORELEASE([self copy]);
149 }
150  
151 @end
152
153 void __link_NSStringFB() {
154   // used to force linking of object file
155   __link_NSStringFB();
156 }