Debian Codegraph @

XCode/NSString+PBXAdditions.m

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 Copyright (C) 2018, 2019, 2020, 2021 Free Software Foundation, Inc.
3
4 Written by: Gregory John Casamento <[email protected]>
5 Date: 2022
6
7 This file is part of the GNUstep XCode Library
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free
21 Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110 USA.
23 */
24
25 #import <Foundation/NSArray.h>
26 #import <Foundation/NSCharacterSet.h>
27 #import <Foundation/NSDebug.h>
28 #import <Foundation/NSDictionary.h>
29 #import <Foundation/NSFileManager.h>
30
31 #import "GSXCBuildContext.h"
32 #import "NSString+PBXAdditions.h"
33 #import "GSXCBuildContext.h"
34
35 #ifdef _MSC_VER
36 #import <stdint.h>
37 #import <stdio.h>
38 #define popen _popen
39 #else
40 #import <unistd.h>
41 #endif
42
43 extern char **environ;
44
45 static NSString *_cachedRootPath = nil;
46
47 @implementation NSString (PBXAdditions)
48
49 - (NSString *) firstPathComponent
50 {
51 NSArray *components = [self pathComponents];
52 return ([components count] > 0)?[components objectAtIndex: 0]:@"";
53 }
54
55 - (NSString *) stringByReplacingPathExtensionWith: (NSString *)ext
56 {
57 NSString *result = [self stringByDeletingPathExtension];
58 result = [result stringByAppendingPathExtension: ext];
59 return result;
60 }
61
62 - (NSString *) stringByEscapingSpecialCharacters
63 {
64 NSString *result = nil;
65
66 result = [self stringByReplacingOccurrencesOfString: @" "
67 withString: @"_"];
68 result = [self stringByReplacingOccurrencesOfString: @"("
69 withString: @"\\)"];
70 result = [self stringByReplacingOccurrencesOfString: @")"
71 withString: @"\\)"];
72 result = [self stringByReplacingOccurrencesOfString: @"["
73 withString: @"\\["];
74 result = [self stringByReplacingOccurrencesOfString: @"]"
75 withString: @"\\]"];
76 result = [self stringByReplacingOccurrencesOfString: @"{"
77 withString: @"\\{"];
78 result = [self stringByReplacingOccurrencesOfString: @"}"
79 withString: @"\\}"];
80
81 return result;
82 }
83
84 - (NSString *) stringByEliminatingSpecialCharacters
85 {
86 NSString *cs = @"()[]/\\| ";
87 NSString *result = @"";
88 NSUInteger l = [self length];
89 NSUInteger i = 0;
90
91 for (i = 0; i < l; i++)
92 {
93 NSString *c = [NSString stringWithFormat: @"%c",[self characterAtIndex: i]];
94 if ([cs containsString: c])
95 {
96 continue;
97 }
98 result = [result stringByAppendingString: c];
99 }
100
101 return result;
102 }
103
104 - (NSString *) stringByCapitalizingFirstCharacter
105 {
106 unichar c = [self characterAtIndex: 0];
107 NSRange range = NSMakeRange(0,1);
108 NSString *oneChar = [[NSString stringWithFormat:@"%C",c] uppercaseString];
109 NSString *name = [self stringByReplacingCharactersInRange: range withString: oneChar];
110
111 return name;
112 }
113
114 - (NSString *) stringByDeletingFirstPathComponent
115 {
116 NSArray *components = [self pathComponents];
117 NSString *firstComponent = [self firstPathComponent];
118 NSString *result = @"";
119 NSEnumerator *en = [components objectEnumerator];
120 NSString *c = nil;
121
122 while ((c = [en nextObject]) != nil)
123 {
124 if ([c isEqualToString: firstComponent])
125 continue;
126
127 result = [result stringByAppendingPathComponent: c];
128 }
129
130 return result;
131 }
132
133 - (NSString *) stringByReplacingEnvironmentVariablesWithValues
134 {
135 NSString *result = nil;
136 NSMutableDictionary *dict = [NSMutableDictionary dictionary];
137
138 result = [NSString stringWithString: self]; // autoreleased copy
139
140 // Get env vars...
141 char **env = NULL;
142 for (env = environ; *env != 0; env++)
143 {
144 char *thisEnv = *env;
145 NSString *envStr = [NSString stringWithCString: thisEnv encoding: NSUTF8StringEncoding];
146 NSArray *components = [envStr componentsSeparatedByString: @"="];
147 [dict setObject: [components lastObject]
148 forKey: [components firstObject]];
149 }
150
151 // Replace all variables in the plist with the values...
152 NSArray *keys = [dict allKeys];
153 NSEnumerator *en = [keys objectEnumerator];
154 NSString *k = nil;
155 while ((k = [en nextObject]) != nil)
156 {
157 NSString *v = [dict objectForKey: k];
158 result = [result stringByReplacingOccurrencesOfString: [NSString stringWithFormat: @"$(%@)",k]
159 withString: v];
160 result = [result stringByReplacingOccurrencesOfString: [NSString stringWithFormat: @"$%@",k]
161 withString: v];
162 }
163
164 return result;
165 }
166
167 - (NSString *) stringByAddingQuotationMarks
168 {
169 return [NSString stringWithFormat: @"'%@'", self];
170 }
171
172 - (NSString *) findRootPath
173 {
174 NSString *result = nil;
175 // static NSString *_cachedRootPath = nil;
176
177 if (_cachedRootPath == nil)
178 {
179 NSString *driveLetters = @"cdefghijklmnopqrstuvwxyz", *b = nil;
180 NSArray *base = [NSArray arrayWithObjects: @"/msys64", @"/tools/msys64", nil];
181 NSEnumerator *en = [base objectEnumerator];
182 NSFileManager *fm = [NSFileManager defaultManager];
183
184 while ((b = [en nextObject]) != nil)
185 {
186 NSUInteger i = 0;
187
188 for(i = 0; i < [driveLetters length]; i++)
189 {
190 unichar letter = [driveLetters characterAtIndex: i];
191
192 result = [NSString stringWithFormat: @"%c:%@", letter, b];
193 if ([fm fileExistsAtPath: result])
194 {
195 ASSIGN(_cachedRootPath, result);
196 break;
197 }
198 }
199 }
200 }
201 else
202 {
203 result = _cachedRootPath;
204 }
205
206 NSDebugLog(@"root path = %@", result);
207
208 return result;
209 }
210
211 - (NSString *) execPathForString
212 {
213 NSString *result = nil;
214 NSString *cmd = self;
215 GSXCBuildContext *ctx = [GSXCBuildContext sharedBuildContext];
216 NSDictionary *dict = [ctx config];
217 NSString *setupScript = [dict objectForKey: @"setupScript"];
218 #ifdef _WIN32
219 NSString *rootPath = [self findRootPath];
220
221 if (setupScript != nil)
222 {
223 result = [NSString stringWithFormat: @"%@/usr/bin/bash -c \"%@ > /dev/null && %@\"", rootPath, setupScript, cmd];
224 }
225 else
226 {
227 result = [NSString stringWithFormat: @"%@/usr/bin/bash -c \"%@\"", rootPath, cmd];
228 }
229 #else
230 if (setupScript != nil)
231 {
232 result = [NSString stringWithFormat: @"%@ > /dev/null && %@", setupScript, cmd];
233 }
234 else
235 {
236 result = [cmd copy];
237 }
238 #endif
239
240 NSDebugLog(@"%@", result);
241
242 return result;
243 }
244
245 + (NSString *) stringForCommand: (NSString *)command
246 {
247 NSString *output = nil;
248 char string[2048];
249 const char *cmd_string;
250 FILE *fp;
251 NSString *cmd = [command execPathForString];
252
253 cmd_string = [cmd cString];
254
255 /* Open the command for reading. */
256 fp = popen(cmd_string, "r");
257 if (fp != NULL)
258 {
259 output = @"";
260
261 /* Read the output a line at a time - output it. */
262 while (fgets(string, sizeof(string) - 1, fp) != NULL)
263 {
264 int len = strlen(string);
265 int i = 0;
266
267 for(i = 0; i < len; i++)
268 {
269 if(string[i] == '\n')
270 {
271 string[i] = '\0';
272 }
273 }
274
275 output = [output stringByAppendingString:
276 [NSString stringWithCString: string]];
277 }
278
279 fclose(fp);
280 }
281
282 output = [output stringByTrimmingTrailingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
283
284 return output;
285 }
286
287 + (NSString *) stringForEnvironmentVariable: (char *)envvar
288 {
289 NSString *v = [NSString stringWithCString: envvar];
290 return [self stringForEnvironmentVariable: v
291 defaultValue: nil];
292 }
293
294 + (NSString *) stringForEnvironmentVariable: (NSString *)v
295 defaultValue: (NSString *)d
296 {
297 NSProcessInfo *pi = [NSProcessInfo processInfo];
298 NSDictionary *e = [pi environment];
299 NSString *r = [e objectForKey: v];
300
301 if (r == nil)
302 {
303 r = d;
304 }
305
306 return r;
307 }
308
309 - (NSString *) stringByTrimmingTrailingCharactersInSet:(NSCharacterSet *)characterSet
310 {
311 NSRange rangeOfLastWantedCharacter = [self rangeOfCharacterFromSet:[characterSet invertedSet]
312 options:NSBackwardsSearch];
313 if (rangeOfLastWantedCharacter.location == NSNotFound) {
314 return @"";
315 }
316 return [self substringToIndex:rangeOfLastWantedCharacter.location+1]; // non-inclusive
317 }
318
319 - (NSString *) stringByResolvingPath
320 {
321 #ifndef __MINGW32__
322 NSString *expandedPath = [[self stringByExpandingTildeInPath] stringByStandardizingPath];
323 const char *cpath = [expandedPath cStringUsingEncoding: NSUTF8StringEncoding];
324 char *resolved = NULL;
325 char *returnValue = realpath(cpath, resolved);
326
327 if (returnValue == NULL && resolved != NULL)
328 {
329 NSLog(@"Error with path %s", resolved);
330 return nil;
331 }
332
333 return [NSString stringWithCString: returnValue encoding: NSUTF8StringEncoding];
334 #else
335 return self;
336 #endif
337 }
338
339 - (NSString *) lowercaseFirstCharacter
340 {
341 // Lowercase the first letter of the class to make the element name
342 NSString *first = [[self substringToIndex: 1] lowercaseString];
343 NSString *rest = [self substringFromIndex: 1];
344 NSString *result = [NSString stringWithFormat: @"%@%@", first, rest];
345 return result;
346 }
347
348 @end