File: NSString%2BPBXAdditions.m

package info (click to toggle)
gnustep-xcode 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,168 kB
  • sloc: objc: 9,258; sh: 61; makefile: 20
file content (348 lines) | stat: -rw-r--r-- 9,809 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
   Copyright (C) 2018, 2019, 2020, 2021 Free Software Foundation, Inc.

   Written by: Gregory John Casamento <greg.casamento@gmail.com>
   Date: 2022
   
   This file is part of the GNUstep XCode Library

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
   
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110 USA.
*/

#import <Foundation/NSArray.h>
#import <Foundation/NSCharacterSet.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSFileManager.h>

#import "GSXCBuildContext.h"
#import "NSString+PBXAdditions.h"
#import "GSXCBuildContext.h"

#ifdef _MSC_VER
#import <stdint.h>
#import <stdio.h>
#define popen _popen
#else
#import <unistd.h>
#endif

extern char **environ;

static NSString *_cachedRootPath = nil;

@implementation NSString (PBXAdditions)

- (NSString *) firstPathComponent
{
  NSArray *components = [self pathComponents];
  return ([components count] > 0)?[components objectAtIndex: 0]:@"";
}

- (NSString *) stringByReplacingPathExtensionWith: (NSString *)ext
{
  NSString *result = [self stringByDeletingPathExtension];
  result = [result stringByAppendingPathExtension: ext];
  return result;
}

- (NSString *) stringByEscapingSpecialCharacters
{
  NSString *result = nil;

  result = [self stringByReplacingOccurrencesOfString: @" "
						     withString: @"_"];
  result = [self stringByReplacingOccurrencesOfString: @"("
                                           withString: @"\\)"];
  result = [self stringByReplacingOccurrencesOfString: @")"
                                           withString: @"\\)"];
  result = [self stringByReplacingOccurrencesOfString: @"["
                                           withString: @"\\["];
  result = [self stringByReplacingOccurrencesOfString: @"]"
                                           withString: @"\\]"];
  result = [self stringByReplacingOccurrencesOfString: @"{"
                                           withString: @"\\{"];
  result = [self stringByReplacingOccurrencesOfString: @"}"
                                           withString: @"\\}"];

  return result;
}

- (NSString *) stringByEliminatingSpecialCharacters
{
  NSString *cs = @"()[]/\\| ";
  NSString *result = @"";
  NSUInteger l = [self length];
  NSUInteger i = 0;

  for (i = 0; i < l; i++)
    {
      NSString *c = [NSString stringWithFormat: @"%c",[self characterAtIndex: i]];
      if ([cs containsString: c])
        {
          continue;
        }
      result = [result stringByAppendingString: c];
    }

  return result;
}

- (NSString *) stringByCapitalizingFirstCharacter
{
  unichar c = [self characterAtIndex: 0];
  NSRange range = NSMakeRange(0,1);
  NSString *oneChar = [[NSString stringWithFormat:@"%C",c] uppercaseString];
  NSString *name = [self stringByReplacingCharactersInRange: range withString: oneChar];
  
  return name;
}

- (NSString *) stringByDeletingFirstPathComponent
{
  NSArray *components = [self pathComponents];
  NSString *firstComponent = [self firstPathComponent];
  NSString *result = @"";
  NSEnumerator *en = [components objectEnumerator];
  NSString *c = nil;

  while ((c = [en nextObject]) != nil)
    {
      if ([c isEqualToString: firstComponent])
        continue;
      
      result = [result stringByAppendingPathComponent: c];
    }
  
  return result;
}

- (NSString *) stringByReplacingEnvironmentVariablesWithValues
{
  NSString *result = nil;
  NSMutableDictionary *dict = [NSMutableDictionary dictionary];

  result = [NSString stringWithString: self]; // autoreleased copy
  
  // Get env vars...
  char **env = NULL;
  for (env = environ; *env != 0; env++)
    {
      char *thisEnv = *env;
      NSString *envStr = [NSString stringWithCString: thisEnv encoding: NSUTF8StringEncoding];
      NSArray *components = [envStr componentsSeparatedByString: @"="];
      [dict setObject: [components lastObject]
               forKey: [components firstObject]];
    }

  // Replace all variables in the plist with the values...
  NSArray *keys = [dict allKeys];
  NSEnumerator *en = [keys objectEnumerator];
  NSString *k = nil;
  while ((k = [en nextObject]) != nil)
    {
      NSString *v = [dict objectForKey: k];
      result = [result stringByReplacingOccurrencesOfString: [NSString stringWithFormat: @"$(%@)",k]
                                                 withString: v];
      result = [result stringByReplacingOccurrencesOfString: [NSString stringWithFormat: @"$%@",k]
                                                 withString: v];
    }

  return result;
}

- (NSString *) stringByAddingQuotationMarks
{
  return [NSString stringWithFormat: @"'%@'", self];
}

- (NSString *) findRootPath
{
  NSString *result = nil;
  //  static NSString *_cachedRootPath = nil;

  if (_cachedRootPath == nil)
    {
      NSString *driveLetters = @"cdefghijklmnopqrstuvwxyz", *b = nil;
      NSArray *base = [NSArray arrayWithObjects: @"/msys64", @"/tools/msys64", nil];
      NSEnumerator *en = [base objectEnumerator];
      NSFileManager *fm = [NSFileManager defaultManager];
      
      while ((b = [en nextObject]) != nil)
	{
	  NSUInteger i = 0;

	  for(i = 0; i < [driveLetters length]; i++)
	    {
	      unichar letter = [driveLetters characterAtIndex: i];
	      
	      result = [NSString stringWithFormat: @"%c:%@", letter, b];
	      if ([fm fileExistsAtPath: result])
		{
		  ASSIGN(_cachedRootPath, result);
		  break;
		}
	    }
	}
    }
  else
    {
      result = _cachedRootPath;
    }
  
  NSDebugLog(@"root path = %@", result);
  
  return result;
}

- (NSString *) execPathForString
{
  NSString *result = nil;
  NSString *cmd = self;
  GSXCBuildContext *ctx = [GSXCBuildContext sharedBuildContext];
  NSDictionary *dict = [ctx config];
  NSString *setupScript = [dict objectForKey: @"setupScript"];
#ifdef _WIN32
  NSString *rootPath = [self findRootPath];

  if (setupScript != nil)
    {
      result = [NSString stringWithFormat: @"%@/usr/bin/bash -c \"%@ > /dev/null  && %@\"", rootPath, setupScript, cmd];
    }
  else
    {      
      result = [NSString stringWithFormat: @"%@/usr/bin/bash -c \"%@\"", rootPath, cmd];
    }
#else
  if (setupScript != nil)
    {
      result = [NSString stringWithFormat: @"%@ > /dev/null && %@", setupScript, cmd];
    }
  else
    {      
      result = [cmd copy];
    }
#endif

  NSDebugLog(@"%@", result);
  
  return result;
}

+ (NSString *) stringForCommand: (NSString *)command
{
  NSString *output = nil;
  char string[2048];
  const char *cmd_string;
  FILE *fp;
  NSString *cmd = [command execPathForString];

  cmd_string = [cmd cString];
  
  /* Open the command for reading. */
  fp = popen(cmd_string, "r");
  if (fp != NULL)
    {
      output = @"";
      
      /* Read the output a line at a time - output it. */
      while (fgets(string, sizeof(string) - 1, fp) != NULL)
        {
          int len = strlen(string);
          int i = 0;

          for(i = 0; i < len; i++)
            {
              if(string[i] == '\n')
                {
                  string[i] = '\0';
                }
            }

          output = [output stringByAppendingString: 
                             [NSString stringWithCString: string]];
        }
      
      pclose(fp);
    }

  output = [output stringByTrimmingTrailingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
  
  return output;
}

+ (NSString *) stringForEnvironmentVariable: (char *)envvar
{
  NSString *v = [NSString stringWithCString: envvar];
  return [self stringForEnvironmentVariable: v
                               defaultValue: nil];
}

+ (NSString *) stringForEnvironmentVariable: (NSString *)v
                               defaultValue: (NSString *)d
{
  NSProcessInfo *pi = [NSProcessInfo processInfo];
  NSDictionary *e = [pi environment];
  NSString *r = [e objectForKey: v];

  if (r == nil)
    {
      r = d;
    }

  return r;
}

- (NSString *) stringByTrimmingTrailingCharactersInSet:(NSCharacterSet *)characterSet
{
    NSRange rangeOfLastWantedCharacter = [self rangeOfCharacterFromSet:[characterSet invertedSet]
                                                               options:NSBackwardsSearch];
    if (rangeOfLastWantedCharacter.location == NSNotFound) {
        return @"";
    }
    return [self substringToIndex:rangeOfLastWantedCharacter.location+1]; // non-inclusive
}

- (NSString *) stringByResolvingPath
{
#ifndef __MINGW32__
  NSString *expandedPath = [[self stringByExpandingTildeInPath] stringByStandardizingPath];
  const char *cpath = [expandedPath cStringUsingEncoding: NSUTF8StringEncoding];
  char *resolved = NULL;
  char *returnValue = realpath(cpath, resolved);

  if (returnValue == NULL && resolved != NULL)
    {
      NSLog(@"Error with path %s", resolved);
      return nil;
    }

  return [NSString stringWithCString: returnValue encoding: NSUTF8StringEncoding];
#else
  return self;
#endif
}

- (NSString *) lowercaseFirstCharacter
{
  // Lowercase the first letter of the class to make the element name
  NSString *first = [[self substringToIndex: 1] lowercaseString];
  NSString *rest = [self substringFromIndex: 1];
  NSString *result = [NSString stringWithFormat: @"%@%@", first, rest];
  return result;
}

@end