︙ | | |
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
|
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
|
-
+
+
-
+
+
-
+
+
-
+
+
-
+
-
+
+
+
-
+
+
|
+ (OFString*)currentDirectoryPath
{
OFString *ret;
char *buffer = getcwd(NULL, 0);
@try {
ret = [OFString stringWithCString: buffer];
ret = [OFString stringWithCString: buffer
encoding: OF_STRING_ENCODING_NATIVE];
} @finally {
free(buffer);
}
return ret;
}
+ (BOOL)fileExistsAtPath: (OFString*)path
{
struct stat s;
if (stat([path cString], &s) == -1)
if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
&s) == -1)
return NO;
if (S_ISREG(s.st_mode))
return YES;
return NO;
}
+ (BOOL)directoryExistsAtPath: (OFString*)path
{
struct stat s;
if (stat([path cString], &s) == -1)
if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
&s) == -1)
return NO;
if (S_ISDIR(s.st_mode))
return YES;
return NO;
}
+ (void)createDirectoryAtPath: (OFString*)path
{
#ifndef _WIN32
if (mkdir([path cString], DIR_MODE))
if (mkdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
DIR_MODE))
#else
if (mkdir([path cString]))
if (mkdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#endif
@throw [OFCreateDirectoryFailedException newWithClass: self
path: path];
}
+ (OFArray*)filesInDirectoryAtPath: (OFString*)path
{
OFAutoreleasePool *pool;
OFMutableArray *files = [OFMutableArray array];
#ifndef _WIN32
DIR *dir;
struct dirent *dirent;
if ((dir = opendir([path cString])) == NULL)
if ((dir = opendir([path cStringWithEncoding:
OF_STRING_ENCODING_NATIVE])) == NULL)
@throw [OFOpenFileFailedException newWithClass: self
path: path
mode: @"r"];
@try {
pool = [[OFAutoreleasePool alloc] init];
while ((dirent = readdir(dir)) != NULL) {
OFString *file;
if (!strcmp(dirent->d_name, ".") ||
!strcmp(dirent->d_name, ".."))
continue;
file = [OFString
file = [OFString stringWithCString: dirent->d_name];
stringWithCString: dirent->d_name
encoding: OF_STRING_ENCODING_NATIVE];
[files addObject: file];
[pool releaseObjects];
}
[pool release];
} @finally {
|
︙ | | |
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
|
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
|
-
+
-
+
|
[files makeImmutable];
return files;
}
+ (void)changeToDirectory: (OFString*)path
{
if (chdir([path cString]))
if (chdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
@throw [OFChangeDirectoryFailedException newWithClass: self
path: path];
}
#ifndef _PSP
+ (void)changeModeOfFile: (OFString*)path
toMode: (mode_t)mode
{
# ifndef _WIN32
if (chmod([path cString], mode))
if (chmod([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], mode))
@throw [OFChangeFileModeFailedException newWithClass: self
path: path
mode: mode];
# else
DWORD attributes = GetFileAttributes([path cString]);
if (attributes == INVALID_FILE_ATTRIBUTES)
|
︙ | | |
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
-
+
+
|
}
#endif
+ (OFDate*)modificationDateOfFile: (OFString*)path
{
struct stat s;
if (stat([path cString], &s) == -1)
if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
&s) == -1)
/* FIXME: Maybe use another exception? */
@throw [OFOpenFileFailedException newWithClass: self
path: path
mode: @"r"];
/* FIXME: We could be more precise on some OSes */
return [OFDate dateWithTimeIntervalSince1970: s.st_mtime];
|
︙ | | |
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
|
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
-
+
+
-
+
+
-
+
+
|
[mutex lock];
@try {
# endif
if (owner != nil) {
struct passwd *passwd;
if ((passwd = getpwnam([owner cString])) == NULL)
if ((passwd = getpwnam([owner cStringWithEncoding:
OF_STRING_ENCODING_NATIVE])) == NULL)
@throw [OFChangeFileOwnerFailedException
newWithClass: self
path: path
owner: owner
group: group];
uid = passwd->pw_uid;
}
if (group != nil) {
struct group *group_;
if ((group_ = getgrnam([group cString])) == NULL)
if ((group_ = getgrnam([group cStringWithEncoding:
OF_STRING_ENCODING_NATIVE])) == NULL)
@throw [OFChangeFileOwnerFailedException
newWithClass: self
path: path
owner: owner
group: group];
gid = group_->gr_gid;
}
# ifdef OF_THREADS
} @finally {
[mutex unlock];
}
# endif
if (chown([path cString], uid, gid))
if (chown([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
uid, gid))
@throw [OFChangeFileOwnerFailedException newWithClass: self
path: path
owner: owner
group: group];
}
#endif
|
︙ | | |
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
-
+
+
-
+
+
-
+
-
+
-
+
-
+
+
-
+
+
|
if ([self directoryExistsAtPath: destination]) {
OFString *filename = [source lastPathComponent];
destination = [OFString stringWithPath: destination, filename,
nil];
}
#ifndef _WIN32
if (rename([source cString], [destination cString]))
if (rename([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
[destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#else
if (!MoveFile([source cString], [destination cString]))
if (!MoveFile([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
[destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#endif
@throw [OFRenameFileFailedException newWithClass: self
sourcePath: source
destinationPath: destination];
[pool release];
}
+ (void)deleteFileAtPath: (OFString*)path
{
#ifndef _WIN32
if (unlink([path cString]))
if (unlink([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#else
if (!DeleteFile([path cString]))
if (!DeleteFile([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
#endif
@throw [OFDeleteFileFailedException newWithClass: self
path: path];
}
+ (void)deleteDirectoryAtPath: (OFString*)path
{
if (rmdir([path cString]))
if (rmdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE]))
@throw [OFDeleteDirectoryFailedException newWithClass: self
path: path];
}
#ifndef _WIN32
+ (void)linkFileAtPath: (OFString*)source
toPath: (OFString*)destination
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
if ([self directoryExistsAtPath: destination]) {
OFString *filename = [source lastPathComponent];
destination = [OFString stringWithPath: destination, filename,
nil];
}
if (link([source cString], [destination cString]) != 0)
if (link([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
[destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]) != 0)
@throw [OFLinkFailedException newWithClass: self
sourcePath: source
destinationPath: destination];
[pool release];
}
#endif
#if !defined(_WIN32) && !defined(_PSP)
+ (void)symlinkFileAtPath: (OFString*)source
toPath: (OFString*)destination
{
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
if ([self directoryExistsAtPath: destination]) {
OFString *filename = [source lastPathComponent];
destination = [OFString stringWithPath: destination, filename,
nil];
}
if (symlink([source cString], [destination cString]) != 0)
if (symlink([source cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
[destination cStringWithEncoding: OF_STRING_ENCODING_NATIVE]) != 0)
@throw [OFSymlinkFailedException newWithClass: self
sourcePath: source
destinationPath: destination];
[pool release];
}
#endif
|
︙ | | |
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
|
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
-
+
+
-
-
+
+
|
mode: (OFString*)mode
{
self = [super init];
@try {
int flags;
if ((flags = parse_mode([mode cString])) == -1)
if ((flags = parse_mode([mode cStringWithEncoding:
OF_STRING_ENCODING_NATIVE])) == -1)
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
if ((fileDescriptor = open([path cString], flags,
DEFAULT_MODE)) == -1)
if ((fileDescriptor = open([path cStringWithEncoding:
OF_STRING_ENCODING_NATIVE], flags, DEFAULT_MODE)) == -1)
@throw [OFOpenFileFailedException newWithClass: isa
path: path
mode: mode];
closable = YES;
} @catch (id e) {
[self release];
|
︙ | | |
︙ | | |
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
|
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
|
-
+
-
+
-
-
-
+
+
+
-
+
-
-
+
+
+
-
+
-
-
+
+
-
-
-
+
+
+
-
+
-
-
+
+
+
-
+
+
+
+
+
+
+
+
+
-
+
-
+
-
+
-
+
+
-
+
-
+
|
/*
* Even though cStringLength can change, length cannot, therefore no
* need to change it.
*/
}
- (void)appendCString: (const char*)cString
- (void)appendUTF8String: (const char*)UTF8String
{
size_t cStringLength = strlen(cString);
size_t UTF8StringLength = strlen(UTF8String);
size_t length;
if (cStringLength >= 3 && !memcmp(cString, "\xEF\xBB\xBF", 3)) {
cString += 3;
cStringLength -= 3;
if (UTF8StringLength >= 3 && !memcmp(UTF8String, "\xEF\xBB\xBF", 3)) {
UTF8String += 3;
UTF8StringLength -= 3;
}
switch (of_string_check_utf8(cString, cStringLength, &length)) {
switch (of_string_check_utf8(UTF8String, UTF8StringLength, &length)) {
case 1:
s->isUTF8 = YES;
break;
case -1:
@throw [OFInvalidEncodingException newWithClass: isa];
}
s->cString = [self resizeMemory: s->cString
toSize: s->cStringLength + cStringLength + 1];
memcpy(s->cString + s->cStringLength, cString, cStringLength + 1);
toSize: s->cStringLength +
UTF8StringLength + 1];
memcpy(s->cString + s->cStringLength, UTF8String, UTF8StringLength + 1);
s->cStringLength += cStringLength;
s->cStringLength += UTF8StringLength;
s->length += length;
}
- (void)appendCString: (const char*)cString
withLength: (size_t)cStringLength
- (void)appendUTF8String: (const char*)UTF8String
withLength: (size_t)UTF8StringLength
{
size_t length;
if (cStringLength >= 3 && !memcmp(cString, "\xEF\xBB\xBF", 3)) {
cString += 3;
cStringLength -= 3;
if (UTF8StringLength >= 3 && !memcmp(UTF8String, "\xEF\xBB\xBF", 3)) {
UTF8String += 3;
UTF8StringLength -= 3;
}
switch (of_string_check_utf8(cString, cStringLength, &length)) {
switch (of_string_check_utf8(UTF8String, UTF8StringLength, &length)) {
case 1:
s->isUTF8 = YES;
break;
case -1:
@throw [OFInvalidEncodingException newWithClass: isa];
}
s->cString = [self resizeMemory: s->cString
toSize: s->cStringLength + cStringLength + 1];
memcpy(s->cString + s->cStringLength, cString, cStringLength);
toSize: s->cStringLength +
UTF8StringLength + 1];
memcpy(s->cString + s->cStringLength, UTF8String, UTF8StringLength);
s->cStringLength += cStringLength;
s->cStringLength += UTF8StringLength;
s->length += length;
s->cString[s->cStringLength] = 0;
}
- (void)appendCString: (const char*)cString
withEncoding: (of_string_encoding_t)encoding
{
return [self appendCString: cString
withEncoding: encoding
length: strlen(cString)];
}
- (void)appendCString: (const char*)cString
withEncoding: (of_string_encoding_t)encoding
length: (size_t)cStringLength
{
if (encoding == OF_STRING_ENCODING_UTF_8)
[self appendCString: cString
[self appendUTF8String: cString
withLength: cStringLength];
else {
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
[self appendString:
[OFString stringWithCString: cString
encoding: encoding
length: cStringLength]];
[pool release];
}
}
- (void)appendString: (OFString*)string
{
size_t cStringLength;
size_t UTF8StringLength;
if (string == nil)
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
cStringLength = [string cStringLength];
UTF8StringLength = [string UTF8StringLength];
s->cString = [self resizeMemory: s->cString
toSize: s->cStringLength + cStringLength + 1];
toSize: s->cStringLength +
UTF8StringLength + 1];
memcpy(s->cString + s->cStringLength, string->s->cString,
cStringLength);
UTF8StringLength);
s->cStringLength += cStringLength;
s->cStringLength += UTF8StringLength;
s->length += string->s->length;
s->cString[s->cStringLength] = 0;
if (string->s->isUTF8)
s->isUTF8 = YES;
}
|
︙ | | |
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
|
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
|
-
-
+
+
-
+
-
-
+
+
-
+
|
withArguments: arguments];
va_end(arguments);
}
- (void)appendFormat: (OFConstantString*)format
withArguments: (va_list)arguments
{
char *cString;
int cStringLength;
char *UTF8String;
int UTF8StringLength;
if (format == nil)
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
if ((cStringLength = of_vasprintf(&cString, [format cString],
if ((UTF8StringLength = of_vasprintf(&UTF8String, [format UTF8String],
arguments)) == -1)
@throw [OFInvalidFormatException newWithClass: isa];
@try {
[self appendCString: cString
withLength: cStringLength];
[self appendUTF8String: UTF8String
withLength: UTF8StringLength];
} @finally {
free(cString);
free(UTF8String);
}
}
- (void)prependString: (OFString*)string
{
return [self insertString: string
atIndex: 0];
|
︙ | | |
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
-
+
|
if (index > s->length)
@throw [OFOutOfRangeException newWithClass: isa];
if (s->isUTF8)
index = of_string_index_to_position(s->cString, index,
s->cStringLength);
newCStringLength = s->cStringLength + [string cStringLength];
newCStringLength = s->cStringLength + [string UTF8StringLength];
s->cString = [self resizeMemory: s->cString
toSize: newCStringLength + 1];
memmove(s->cString + index + string->s->cStringLength,
s->cString + index, s->cStringLength - index);
memcpy(s->cString + index, string->s->cString,
string->s->cStringLength);
|
︙ | | |
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
|
-
-
-
-
+
+
+
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
|
s->cStringLength = newCStringLength;
s->length = newLength;
}
- (void)replaceOccurrencesOfString: (OFString*)string
withString: (OFString*)replacement
{
const char *cString = [string cString];
const char *replacementCString = [replacement cString];
size_t cStringLength = string->s->cStringLength;
size_t replacementCStringLength = replacement->s->cStringLength;
const char *UTF8String = [string UTF8String];
const char *replacementUTF8String = [replacement UTF8String];
size_t UTF8StringLength = string->s->cStringLength;
size_t replacementUTF8StringLength = replacement->s->cStringLength;
size_t i, last, newCStringLength, newLength;
char *newCString;
if (cStringLength > s->cStringLength)
if (UTF8StringLength > s->cStringLength)
return;
newCString = NULL;
newCStringLength = 0;
newLength = s->length;
for (i = 0, last = 0; i <= s->cStringLength - cStringLength; i++) {
if (memcmp(s->cString + i, cString, cStringLength))
for (i = 0, last = 0; i <= s->cStringLength - UTF8StringLength; i++) {
if (memcmp(s->cString + i, UTF8String, UTF8StringLength))
continue;
@try {
newCString = [self
resizeMemory: newCString
toSize: newCStringLength + i - last +
replacementCStringLength + 1];
replacementUTF8StringLength + 1];
} @catch (id e) {
[self freeMemory: newCString];
@throw e;
}
memcpy(newCString + newCStringLength, s->cString + last,
i - last);
memcpy(newCString + newCStringLength + i - last,
replacementCString, replacementCStringLength);
replacementUTF8String, replacementUTF8StringLength);
newCStringLength += i - last + replacementCStringLength;
newCStringLength += i - last + replacementUTF8StringLength;
newLength = newLength - string->s->length +
replacement->s->length;
i += cStringLength - 1;
i += UTF8StringLength - 1;
last = i + 1;
}
@try {
newCString = [self
resizeMemory: newCString
toSize: newCStringLength +
|
︙ | | |
︙ | | |
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
|
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
|
-
+
-
-
+
+
-
+
|
return [self readTillDelimiter: delimiter
withEncoding: OF_STRING_ENCODING_UTF_8];
}
- (OFString*)readTillDelimiter: (OFString*)delimiter
withEncoding: (of_string_encoding_t)encoding
{
const char *delimiterCString;
const char *delimiterUTF8String;
size_t i, j, delimiterLength, bufferLength, retLength;
char *retCString, *buffer, *newCache;
OFString *ret;
/* FIXME: Convert delimiter to specified charset */
delimiterCString = [delimiter cString];
delimiterLength = [delimiter cStringLength];
delimiterUTF8String = [delimiter UTF8String];
delimiterLength = [delimiter UTF8StringLength];
j = 0;
if (delimiterLength == 0)
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
/* Look if there's something in our cache */
if (cache != NULL) {
for (i = 0; i < cacheLength; i++) {
if (cache[i] != delimiterCString[j++])
if (cache[i] != delimiterUTF8String[j++])
j = 0;
if (j == delimiterLength || cache[i] == '\0') {
if (cache[i] == '\0')
delimiterLength = 1;
ret = [OFString
|
︙ | | |
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
|
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
|
-
+
|
}
bufferLength = [self _readNBytes: of_pagesize
intoBuffer: buffer];
/* Look if there's the delimiter or \0 */
for (i = 0; i < bufferLength; i++) {
if (buffer[i] != delimiterCString[j++])
if (buffer[i] != delimiterUTF8String[j++])
j = 0;
if (j == delimiterLength || buffer[i] == '\0') {
if (buffer[i] == '\0')
delimiterLength = 1;
retLength = cacheLength + i + 1 -
|
︙ | | |
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
|
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
|
-
+
-
+
-
+
-
+
|
fromBuffer: [dataArray cArray]];
return [dataArray count] * [dataArray itemSize];
}
- (size_t)writeString: (OFString*)string
{
size_t length = [string cStringLength];
size_t length = [string UTF8StringLength];
[self writeNBytes: length
fromBuffer: [string cString]];
fromBuffer: [string UTF8String]];
return length;
}
- (size_t)writeLine: (OFString*)string
{
size_t stringLength = [string cStringLength];
size_t stringLength = [string UTF8StringLength];
char *buffer;
buffer = [self allocMemoryWithSize: stringLength + 1];
@try {
memcpy(buffer, [string cString], stringLength);
memcpy(buffer, [string UTF8String], stringLength);
buffer[stringLength] = '\n';
[self writeNBytes: stringLength + 1
fromBuffer: buffer];
} @finally {
[self freeMemory: buffer];
}
|
︙ | | |
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
|
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
|
-
+
-
+
-
+
-
+
|
return ret;
}
- (size_t)writeFormat: (OFConstantString*)format
withArguments: (va_list)arguments
{
char *cString;
char *UTF8String;
int length;
if (format == nil)
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
if ((length = of_vasprintf(&cString, [format cString],
if ((length = of_vasprintf(&UTF8String, [format UTF8String],
arguments)) == -1)
@throw [OFInvalidFormatException newWithClass: isa];
@try {
[self writeNBytes: length
fromBuffer: cString];
fromBuffer: UTF8String];
} @finally {
free(cString);
free(UTF8String);
}
return length;
}
- (size_t)pendingBytes
{
|
︙ | | |
︙ | | |
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
|
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
|
-
+
-
-
+
+
-
+
+
-
+
+
-
+
+
-
+
+
-
+
+
-
+
-
+
+
|
}
}
if ((i = of_string_unicode_to_utf8(c, buffer)) == 0)
return nil;
buffer[i] = 0;
return [OFString stringWithCString: buffer
return [OFString stringWithUTF8String: buffer
length: i];
}
@implementation OFString (XMLUnescaping)
- (OFString*)stringByXMLUnescaping
{
return [self stringByXMLUnescapingWithDelegate: nil];
}
- (OFString*)stringByXMLUnescapingWithDelegate:
(id <OFStringXMLUnescapingDelegate>)delegate
{
const char *string;
size_t i, last, length;
BOOL inEntity;
OFMutableString *ret;
string = [self cString];
length = [self cStringLength];
string = [self UTF8String];
length = [self UTF8StringLength];
ret = [OFMutableString string];
last = 0;
inEntity = NO;
for (i = 0; i < length; i++) {
if (!inEntity && string[i] == '&') {
[ret appendCString: string + last
[ret appendUTF8String: string + last
withLength: i - last];
last = i + 1;
inEntity = YES;
} else if (inEntity && string[i] == ';') {
const char *entity = string + last;
size_t entityLength = i - last;
if (entityLength == 2 && !memcmp(entity, "lt", 2))
[ret appendCString: "<"
withEncoding: OF_STRING_ENCODING_ASCII
withLength: 1];
length: 1];
else if (entityLength == 2 && !memcmp(entity, "gt", 2))
[ret appendCString: ">"
withEncoding: OF_STRING_ENCODING_ASCII
withLength: 1];
length: 1];
else if (entityLength == 4 &&
!memcmp(entity, "quot", 4))
[ret appendCString: "\""
withEncoding: OF_STRING_ENCODING_ASCII
withLength: 1];
length: 1];
else if (entityLength == 4 &&
!memcmp(entity, "apos", 4))
[ret appendCString: "'"
withEncoding: OF_STRING_ENCODING_ASCII
withLength: 1];
length: 1];
else if (entityLength == 3 && !memcmp(entity, "amp", 3))
[ret appendCString: "&"
withEncoding: OF_STRING_ENCODING_ASCII
withLength: 1];
length: 1];
else if (entity[0] == '#') {
OFAutoreleasePool *pool;
OFString *tmp;
pool = [[OFAutoreleasePool alloc] init];
tmp = parse_numeric_entity(entity,
entityLength);
if (tmp == nil)
@throw [OFInvalidEncodingException
newWithClass: isa];
[ret appendString: tmp];
[pool release];
} else if (delegate != nil) {
OFAutoreleasePool *pool;
OFString *n, *tmp;
pool = [[OFAutoreleasePool alloc] init];
n = [OFString stringWithCString: entity
n = [OFString
stringWithUTF8String: entity
length: entityLength];
tmp = [delegate string: self
containsUnknownEntityNamed: n];
if (tmp == nil)
@throw [OFInvalidEncodingException
newWithClass: isa];
|
︙ | | |
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
|
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
|
-
+
-
-
+
+
-
+
+
-
+
+
-
+
+
-
+
+
-
+
+
-
+
|
inEntity = NO;
}
}
if (inEntity)
@throw [OFInvalidEncodingException newWithClass: isa];
[ret appendCString: string + last
[ret appendUTF8String: string + last
withLength: i - last];
[ret makeImmutable];
return ret;
}
#ifdef OF_HAVE_BLOCKS
- (OFString*)stringByXMLUnescapingWithBlock:
(of_string_xml_unescaping_block_t)block
{
const char *string;
size_t i, last, length;
BOOL inEntity;
OFMutableString *ret;
string = [self cString];
length = [self cStringLength];
string = [self UTF8String];
length = [self UTF8StringLength];
ret = [OFMutableString string];
last = 0;
inEntity = NO;
for (i = 0; i < length; i++) {
if (!inEntity && string[i] == '&') {
[ret appendCString: string + last
[ret appendUTF8String: string + last
withLength: i - last];
last = i + 1;
inEntity = YES;
} else if (inEntity && string[i] == ';') {
const char *entity = string + last;
size_t entityLength = i - last;
if (entityLength == 2 && !memcmp(entity, "lt", 2))
[ret appendCString: "<"
withEncoding: OF_STRING_ENCODING_ASCII
withLength: 1];
length: 1];
else if (entityLength == 2 && !memcmp(entity, "gt", 2))
[ret appendCString: ">"
withEncoding: OF_STRING_ENCODING_ASCII
withLength: 1];
length: 1];
else if (entityLength == 4 &&
!memcmp(entity, "quot", 4))
[ret appendCString: "\""
withEncoding: OF_STRING_ENCODING_ASCII
withLength: 1];
length: 1];
else if (entityLength == 4 &&
!memcmp(entity, "apos", 4))
[ret appendCString: "'"
withEncoding: OF_STRING_ENCODING_ASCII
withLength: 1];
length: 1];
else if (entityLength == 3 && !memcmp(entity, "amp", 3))
[ret appendCString: "&"
withEncoding: OF_STRING_ENCODING_ASCII
withLength: 1];
length: 1];
else if (entity[0] == '#') {
OFAutoreleasePool *pool;
OFString *tmp;
pool = [[OFAutoreleasePool alloc] init];
tmp = parse_numeric_entity(entity,
entityLength);
|
︙ | | |
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
|
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
|
-
+
-
+
|
} else {
OFAutoreleasePool *pool;
OFString *entityString, *tmp;
pool = [[OFAutoreleasePool alloc] init];
entityString = [OFString
stringWithCString: entity
stringWithUTF8String: entity
length: entityLength];
tmp = block(self, entityString);
if (tmp == nil)
@throw [OFInvalidEncodingException
newWithClass: isa];
[ret appendString: tmp];
[pool release];
}
last = i + 1;
inEntity = NO;
}
}
if (inEntity)
@throw [OFInvalidEncodingException newWithClass: isa];
[ret appendCString: string + last
[ret appendUTF8String: string + last
withLength: i - last];
[ret makeImmutable];
return ret;
}
#endif
@end
|
︙ | | |
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
|
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
|
-
-
-
+
+
+
+
+
+
+
+
+
+
+
-
+
+
|
* Unused in OFString, however, OFConstantString sets this to SIZE_MAX
* once it allocated and initialized the struct.
*/
size_t initialized;
}
#ifdef OF_HAVE_PROPERTIES
@property (readonly) const char *cString;
@property (readonly) size_t cStringLength;
@property (readonly) size_t length;
#endif
/**
* \brief Creates a new OFString.
*
* \return A new, autoreleased OFString
*/
+ string;
/**
* \brief Creates a new OFString from a UTF-8 encoded C string.
*
* \param cString A UTF-8 encoded C string to initialize the OFString with
* \param UTF8String A UTF-8 encoded C string to initialize the OFString with
* \return A new autoreleased OFString
*/
+ stringWithUTF8String: (const char*)UTF8String;
/**
* \brief Creates a new OFString from a UTF-8 encoded C string with the
* specified length.
*
* \param UTF8String A UTF-8 encoded C string to initialize the OFString with
* \param UTF8StringLength The length of the UTF-8 encoded C string
* \return A new autoreleased OFString
*/
+ stringWithCString: (const char*)cString;
+ stringWithUTF8String: (const char*)UTF8String
length: (size_t)UTF8StringLength;
/**
* \brief Creates a new OFString from a C string with the specified encoding.
*
* \param string A C string to initialize the OFString with
* \param encoding The encoding of the C string
* \return A new autoreleased OFString
|
︙ | | |
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
|
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
-
-
-
-
-
-
-
-
-
-
-
|
* \param cStringLength The length of the C string
* \return A new autoreleased OFString
*/
+ stringWithCString: (const char*)cString
encoding: (of_string_encoding_t)encoding
length: (size_t)cStringLength;
/**
* \brief Creates a new OFString from a UTF-8 encoded C string with the
* specified length.
*
* \param cString A UTF-8 encoded C string to initialize the OFString with
* \param cStringLength The length of the UTF-8 encoded C string
* \return A new autoreleased OFString
*/
+ stringWithCString: (const char*)cString
length: (size_t)cStringLength;
/**
* \brief Creates a new OFString from another string.
*
* \param string A string to initialize the OFString with
* \return A new autoreleased OFString
*/
+ stringWithString: (OFString*)string;
|
︙ | | |
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
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
|
-
+
+
+
+
+
+
+
+
+
+
+
-
+
+
|
+ stringWithContentsOfURL: (OFURL*)URL
encoding: (of_string_encoding_t)encoding;
/**
* \brief Initializes an already allocated OFString from a UTF-8 encoded C
* string.
*
* \param cString A UTF-8 encoded C string to initialize the OFString with
* \param UTF8String A UTF-8 encoded C string to initialize the OFString with
* \return An initialized OFString
*/
- initWithUTF8String: (const char*)UTF8String;
/**
* \brief Initializes an already allocated OFString from a UTF-8 encoded C
* string with the specified length.
*
* \param UTF8String A UTF-8 encoded C string to initialize the OFString with
* \param UTF8StringLength The length of the UTF-8 encoded C string
* \return An initialized OFString
*/
- initWithCString: (const char*)cString;
- initWithUTF8String: (const char*)UTF8String
length: (size_t)UTF8StringLength;
/**
* \brief Initializes an already allocated OFString from a C string with the
* specified encoding.
*
* \param cString A C string to initialize the OFString with
* \param encoding The encoding of the C string
|
︙ | | |
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
-
-
-
-
-
-
-
-
-
-
-
|
* \param cStringLength The length of the C string
* \return An initialized OFString
*/
- initWithCString: (const char*)cString
encoding: (of_string_encoding_t)encoding
length: (size_t)cStringLength;
/**
* \brief Initializes an already allocated OFString from a UTF-8 encoded C
* string with the specified length.
*
* \param cString A UTF-8 encoded C string to initialize the OFString with
* \param cStringLength The length of the UTF-8 encoded C string
* \return An initialized OFString
*/
- initWithCString: (const char*)cString
length: (size_t)cStringLength;
/**
* \brief Initializes an already allocated OFString with another string.
*
* \param string A string to initialize the OFString with
* \return An initialized OFString
*/
- initWithString: (OFString*)string;
|
︙ | | |
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
|
-
+
-
+
-
+
-
+
|
encoding: (of_string_encoding_t)encoding;
/**
* \brief Returns the OFString as a UTF-8 encoded C string.
*
* \return The OFString as a UTF-8 encoded C string
*/
- (const char*)cString;
- (const char*)UTF8String;
/**
* \brief Returns the OFString as a C string in the specified encoding.
*
* \param encoding The encoding for the C string
* \return The OFString as a C string in the specified encoding
*/
- (const char*)cStringWithEncoding: (of_string_encoding_t)encoding;;
/**
* \brief Returns the length of the string in Unicode characters.
*
* \return The length of the string in Unicode characters
*/
- (size_t)length;
/**
* \brief Returns the length of the string which cString would return.
* \brief Returns the number of bytes the string needs in UTF-8 encoding.
*
* \return The length of the string which cString would return
* \return The number of bytes the string needs in UTF-8 encoding.
*/
- (size_t)cStringLength;
- (size_t)UTF8StringLength;
/**
* \brief Returns the number of bytes the string needs in the specified
* encoding.
*
* \param encoding The encoding for the string
* \return The number of bytes the string needs in the specified encoding.
|
︙ | | |
︙ | | |
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
|
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
|
-
+
+
+
+
+
+
+
-
+
+
+
-
-
-
-
-
-
-
|
@implementation OFString
+ string
{
return [[[self alloc] init] autorelease];
}
+ stringWithCString: (const char*)cString
+ stringWithUTF8String: (const char*)UTF8String
{
return [[[self alloc] initWithUTF8String: UTF8String] autorelease];
}
+ stringWithUTF8String: (const char*)UTF8String
length: (size_t)UTF8StringLength
{
return [[[self alloc] initWithCString: cString] autorelease];
return [[[self alloc]
initWithUTF8String: UTF8String
length: UTF8StringLength] autorelease];
}
+ stringWithCString: (const char*)cString
encoding: (of_string_encoding_t)encoding
{
return [[[self alloc] initWithCString: cString
encoding: encoding] autorelease];
}
+ stringWithCString: (const char*)cString
encoding: (of_string_encoding_t)encoding
length: (size_t)cStringLength
{
return [[[self alloc] initWithCString: cString
encoding: encoding
length: cStringLength] autorelease];
}
+ stringWithCString: (const char*)cString
length: (size_t)cStringLength
{
return [[[self alloc] initWithCString: cString
length: cStringLength] autorelease];
}
+ stringWithString: (OFString*)string
{
return [[[self alloc] initWithString: string] autorelease];
}
|
︙ | | |
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
|
-
+
-
+
-
+
+
+
+
+
+
+
+
+
|
[self release];
@throw e;
}
return self;
}
- initWithCString: (const char*)cString
- initWithUTF8String: (const char*)UTF8String
{
return [self initWithCString: cString
return [self initWithCString: UTF8String
encoding: OF_STRING_ENCODING_UTF_8
length: strlen(cString)];
length: strlen(UTF8String)];
}
- initWithUTF8String: (const char*)UTF8String
length: (size_t)UTF8StringLength
{
return [self initWithCString: UTF8String
encoding: OF_STRING_ENCODING_UTF_8
length: UTF8StringLength];
}
- initWithCString: (const char*)cString
encoding: (of_string_encoding_t)encoding
{
return [self initWithCString: cString
encoding: encoding
|
︙ | | |
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
|
-
-
-
-
-
-
-
-
-
+
|
[self release];
@throw e;
}
return self;
}
- initWithCString: (const char*)cString
length: (size_t)cStringLength
{
return [self initWithCString: cString
encoding: OF_STRING_ENCODING_UTF_8
length: cStringLength];
}
- initWithString: (OFString*)string
{
self = [super init];
@try {
s = [self allocMemoryWithSize: sizeof(*s)];
memset(s, 0, sizeof(*s));
/*
* We need one call to make sure it's initialized (in case it's
* a constant string).
*/
s->cStringLength = [string cStringLength];
s->cStringLength = [string UTF8StringLength];
s->isUTF8 = string->s->isUTF8;
s->length = string->s->length;
s->cString = [self allocMemoryWithSize: s->cStringLength + 1];
memcpy(s->cString, string->s->cString, s->cStringLength + 1);
} @catch (id e) {
[self release];
|
︙ | | |
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
|
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
|
-
-
+
+
|
if (format == nil)
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
s = [self allocMemoryWithSize: sizeof(*s)];
memset(s, 0, sizeof(*s));
if ((cStringLength = of_vasprintf(&s->cString, [format cString],
arguments)) == -1)
if ((cStringLength = of_vasprintf(&s->cString,
[format UTF8String], arguments)) == -1)
@throw [OFInvalidFormatException newWithClass: isa];
s->cStringLength = cStringLength;
@try {
switch (of_string_check_utf8(s->cString,
cStringLength, &s->length)) {
|
︙ | | |
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
|
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
|
-
+
-
+
-
-
+
+
|
s = [self allocMemoryWithSize: sizeof(*s)];
memset(s, 0, sizeof(*s));
/*
* First needs to be a call to be sure it is initialized, in
* case it's a constant string.
*/
s->cStringLength = [firstComponent cStringLength];
s->cStringLength = [firstComponent UTF8StringLength];
s->isUTF8 = firstComponent->s->isUTF8;
s->length = firstComponent->s->length;
/* Calculate length and see if we need UTF-8 */
va_copy(argumentsCopy, arguments);
while ((component = va_arg(argumentsCopy, OFString*)) != nil) {
/* First needs to be a call, see above */
s->cStringLength += 1 + [component cStringLength];
s->cStringLength += 1 + [component UTF8StringLength];
s->length += 1 + component->s->length;
if (component->s->isUTF8)
s->isUTF8 = YES;
}
s->cString = [self allocMemoryWithSize: s->cStringLength + 1];
cStringLength = [firstComponent cStringLength];
memcpy(s->cString, [firstComponent cString], cStringLength);
cStringLength = [firstComponent UTF8StringLength];
memcpy(s->cString, [firstComponent UTF8String], cStringLength);
i = cStringLength;
while ((component = va_arg(arguments, OFString*)) != nil) {
/*
* We already sent each component a message, so we can
* be sure they are initialized and access them
* directly.
|
︙ | | |
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
|
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
|
-
+
+
|
{
char *tmp;
struct stat st;
@try {
OFFile *file;
if (stat([path cString], &st) == -1)
if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
&st) == -1)
@throw [OFOpenFileFailedException newWithClass: isa
path: path
mode: @"rb"];
if (st.st_size > SIZE_MAX)
@throw [OFOutOfRangeException newWithClass: isa];
|
︙ | | |
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
|
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
|
-
+
|
[self release];
@throw e;
}
return self;
}
- (const char*)cString
- (const char*)UTF8String
{
return s->cString;
}
- (const char*)cStringWithEncoding: (of_string_encoding_t)encoding
{
switch (encoding) {
|
︙ | | |
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
|
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
|
-
+
|
}
- (size_t)length
{
return s->length;
}
- (size_t)cStringLength
- (size_t)UTF8StringLength
{
return s->cStringLength;
}
- (size_t)cStringLengthWithEncoding: (of_string_encoding_t)encoding
{
switch (encoding) {
|
︙ | | |
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
|
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
|
-
+
|
OFString *otherString;
if (![object isKindOfClass: [OFString class]])
return NO;
otherString = object;
if ([otherString cStringLength] != s->cStringLength ||
if ([otherString UTF8StringLength] != s->cStringLength ||
otherString->s->length != s->length)
return NO;
if (strcmp(s->cString, otherString->s->cString))
return NO;
return YES;
|
︙ | | |
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
|
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
|
-
+
-
+
|
int compare;
if (![object isKindOfClass: [OFString class]])
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
otherString = object;
otherCStringLength = [otherString cStringLength];
otherCStringLength = [otherString UTF8StringLength];
minimumCStringLength = (s->cStringLength > otherCStringLength
? otherCStringLength : s->cStringLength);
if ((compare = memcmp(s->cString, [otherString cString],
if ((compare = memcmp(s->cString, [otherString UTF8String],
minimumCStringLength)) == 0) {
if (s->cStringLength > otherCStringLength)
return OF_ORDERED_DESCENDING;
if (s->cStringLength < otherCStringLength)
return OF_ORDERED_ASCENDING;
return OF_ORDERED_SAME;
}
|
︙ | | |
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
|
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
|
-
-
+
+
|
size_t i, j, otherCStringLength, minimumCStringLength;
int compare;
if (![otherString isKindOfClass: [OFString class]])
@throw [OFInvalidArgumentException newWithClass: isa
selector: _cmd];
otherCString = [otherString cString];
otherCStringLength = [otherString cStringLength];
otherCString = [otherString UTF8String];
otherCStringLength = [otherString UTF8StringLength];
if (!s->isUTF8) {
minimumCStringLength = (s->cStringLength > otherCStringLength
? otherCStringLength : s->cStringLength);
if ((compare = memcasecmp(s->cString, otherCString,
minimumCStringLength)) == 0) {
|
︙ | | |
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
|
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
|
-
-
+
+
-
-
+
+
-
+
|
@throw [OFInvalidEncodingException newWithClass: isa];
return character;
}
- (size_t)indexOfFirstOccurrenceOfString: (OFString*)string
{
const char *cString = [string cString];
size_t i, cStringLength = [string cStringLength];
const char *cString = [string UTF8String];
size_t i, cStringLength = [string UTF8StringLength];
if (cStringLength == 0)
return 0;
if (cStringLength > s->cStringLength)
return OF_INVALID_INDEX;
for (i = 0; i <= s->cStringLength - cStringLength; i++)
if (!memcmp(s->cString + i, cString, cStringLength))
return of_string_position_to_index(s->cString, i);
return OF_INVALID_INDEX;
}
- (size_t)indexOfLastOccurrenceOfString: (OFString*)string
{
const char *cString = [string cString];
size_t i, cStringLength = [string cStringLength];
const char *cString = [string UTF8String];
size_t i, cStringLength = [string UTF8StringLength];
if (cStringLength == 0)
return of_string_position_to_index(s->cString,
s->cStringLength);
if (cStringLength > s->cStringLength)
return OF_INVALID_INDEX;
for (i = s->cStringLength - cStringLength;; i--) {
if (!memcmp(s->cString + i, cString, cStringLength))
return of_string_position_to_index(s->cString, i);
/* Did not match and we're at the last char */
if (i == 0)
return OF_INVALID_INDEX;
}
}
- (BOOL)containsString: (OFString*)string
{
const char *cString = [string cString];
const char *cString = [string UTF8String];
size_t i, cStringLength = string->s->cStringLength;
if (cStringLength == 0)
return YES;
if (cStringLength > s->cStringLength)
return NO;
|
︙ | | |
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
|
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
|
-
+
|
if (s->isUTF8) {
start = of_string_index_to_position(s->cString, start,
s->cStringLength);
end = of_string_index_to_position(s->cString, end,
s->cStringLength);
}
return [OFString stringWithCString: s->cString + start
return [OFString stringWithUTF8String: s->cString + start
length: end - start];
}
- (OFString*)stringByAppendingString: (OFString*)string
{
OFMutableString *new;
|
︙ | | |
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
|
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
|
-
+
-
+
-
+
-
+
-
-
+
+
-
+
+
-
+
|
[new makeImmutable];
return new;
}
- (BOOL)hasPrefix: (OFString*)prefix
{
size_t cStringLength = [prefix cStringLength];
size_t cStringLength = [prefix UTF8StringLength];
if (cStringLength > s->cStringLength)
return NO;
return !memcmp(s->cString, [prefix cString], cStringLength);
return !memcmp(s->cString, [prefix UTF8String], cStringLength);
}
- (BOOL)hasSuffix: (OFString*)suffix
{
size_t cStringLength = [suffix cStringLength];
size_t cStringLength = [suffix UTF8StringLength];
if (cStringLength > s->cStringLength)
return NO;
return !memcmp(s->cString + (s->cStringLength - cStringLength),
[suffix cString], cStringLength);
[suffix UTF8String], cStringLength);
}
- (OFArray*)componentsSeparatedByString: (OFString*)delimiter
{
OFAutoreleasePool *pool;
OFMutableArray *array;
const char *cString = [delimiter cString];
size_t cStringLength = [delimiter cStringLength];
const char *cString = [delimiter UTF8String];
size_t cStringLength = [delimiter UTF8StringLength];
size_t i, last;
array = [OFMutableArray array];
pool = [[OFAutoreleasePool alloc] init];
if (cStringLength > s->cStringLength) {
[array addObject: [[self copy] autorelease]];
[pool release];
return array;
}
for (i = 0, last = 0; i <= s->cStringLength - cStringLength; i++) {
if (memcmp(s->cString + i, cString, cStringLength))
continue;
[array addObject: [OFString stringWithCString: s->cString + last
[array addObject:
[OFString stringWithUTF8String: s->cString + last
length: i - last]];
i += cStringLength - 1;
last = i + 1;
}
[array addObject: [OFString stringWithCString: s->cString + last]];
[array addObject: [OFString stringWithUTF8String: s->cString + last]];
[array makeImmutable];
[pool release];
return array;
}
|
︙ | | |
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
|
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
|
-
+
-
+
|
for (i = 0; i < pathCStringLength; i++) {
#ifndef _WIN32
if (s->cString[i] == OF_PATH_DELIMITER) {
#else
if (s->cString[i] == '/' || s->cString[i] == '\\') {
#endif
[ret addObject:
[OFString stringWithCString: s->cString + last
[OFString stringWithUTF8String: s->cString + last
length: i - last]];
last = i + 1;
}
}
[ret addObject: [OFString stringWithCString: s->cString + last
[ret addObject: [OFString stringWithUTF8String: s->cString + last
length: i - last]];
[ret makeImmutable];
[pool release];
return ret;
|
︙ | | |
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
|
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
|
-
+
-
+
-
+
-
+
|
/*
* Only one component, but the trailing delimiter might have been
* removed, so return a new string anyway.
*/
if (i < 0)
i = 0;
return [OFString stringWithCString: s->cString + i
return [OFString stringWithUTF8String: s->cString + i
length: pathCStringLength - i];
}
- (OFString*)stringByDeletingLastPathComponent
{
size_t i, pathCStringLength = s->cStringLength;
if (pathCStringLength == 0)
return @"";
#ifndef _WIN32
if (s->cString[pathCStringLength - 1] == OF_PATH_DELIMITER)
#else
if (s->cString[pathCStringLength - 1] == '/' ||
s->cString[pathCStringLength - 1] == '\\')
#endif
pathCStringLength--;
if (pathCStringLength == 0)
return [OFString stringWithCString: s->cString
return [OFString stringWithUTF8String: s->cString
length: 1];
for (i = pathCStringLength - 1; i >= 1; i--)
#ifndef _WIN32
if (s->cString[i] == OF_PATH_DELIMITER)
#else
if (s->cString[i] == '/' || s->cString[i] == '\\')
#endif
return [OFString stringWithCString: s->cString
return [OFString stringWithUTF8String: s->cString
length: i];
#ifndef _WIN32
if (s->cString[0] == OF_PATH_DELIMITER)
#else
if (s->cString[0] == '/' || s->cString[0] == '\\')
#endif
return [OFString stringWithCString: s->cString
return [OFString stringWithUTF8String: s->cString
length: 1];
return @".";
}
- (intmax_t)decimalValue
{
|
︙ | | |
︙ | | |
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
+
-
+
|
char portCString[7];
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
snprintf(portCString, 7, "%" PRIu16, port);
if (getaddrinfo([host cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
if (getaddrinfo([host cString], portCString, &hints, &res0))
portCString, &hints, &res0))
@throw [OFAddressTranslationFailedException newWithClass: isa
socket: self
host: host];
for (res = res0; res != NULL; res = res->ai_next) {
if ((sock = socket(res->ai_family, res->ai_socktype,
res->ai_protocol)) == INVALID_SOCKET)
|
︙ | | |
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
-
+
+
|
# ifdef OF_THREADS
OFDataArray *addrlist;
addrlist = [[OFDataArray alloc] initWithItemSize: sizeof(char**)];
[mutex lock];
# endif
if ((he = gethostbyname([host cString])) == NULL) {
if ((he = gethostbyname([host cStringWithEncoding:
OF_STRING_ENCODING_NATIVE])) == NULL) {
# ifdef OF_THREADS
[addrlist release];
[mutex unlock];
# endif
@throw [OFAddressTranslationFailedException newWithClass: isa
socket: self
host: host];
|
︙ | | |
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
+
-
+
|
char portCString[7];
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
snprintf(portCString, 7, "%" PRIu16, port);
if (getaddrinfo([host cStringWithEncoding: OF_STRING_ENCODING_NATIVE],
if (getaddrinfo([host cString], portCString, &hints, &res))
portCString, &hints, &res))
@throw [OFAddressTranslationFailedException newWithClass: isa
socket: self
host: host];
if ((sock = socket(res->ai_family, SOCK_STREAM, 0)) == INVALID_SOCKET)
@throw [OFBindFailedException newWithClass: isa
socket: self
|
︙ | | |
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
-
+
+
|
#else
struct hostent *he;
# ifdef OF_THREADS
[mutex lock];
# endif
if ((he = gethostbyname([host cString])) == NULL) {
if ((he = gethostbyname([host cStringWithEncoding:
OF_STRING_ENCODING_NATIVE])) == NULL) {
# ifdef OF_THREADS
[mutex unlock];
# endif
@throw [OFAddressTranslationFailedException newWithClass: isa
socket: self
host: host];
}
|
︙ | | |
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
-
+
+
-
+
+
|
@try {
if (getnameinfo((struct sockaddr*)sockAddr, sockAddrLen, host,
NI_MAXHOST, NULL, 0, NI_NUMERICHOST))
@throw [OFAddressTranslationFailedException
newWithClass: isa];
return [OFString stringWithCString: host];
return [OFString stringWithCString: host
encoding: OF_STRING_ENCODING_NATIVE];
} @finally {
[self freeMemory: host];
}
#else
# ifdef OF_THREADS
[mutex lock];
@try {
# endif
host = inet_ntoa(((struct sockaddr_in*)sockAddr)->sin_addr);
if (host == NULL)
@throw [OFAddressTranslationFailedException
newWithClass: isa];
return [OFString stringWithCString: host];
return [OFString stringWithCString: host
encoding: OF_STRING_ENCODING_NATIVE];
# ifdef OF_THREADS
} @finally {
[mutex unlock];
}
# endif
#endif
|
︙ | | |
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
-
+
-
+
+
-
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
+
-
+
-
+
+
-
-
+
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
+
-
+
-
+
-
+
-
+
-
-
+
+
+
-
+
-
+
-
+
|
{
return [[[self alloc] initWithString: string
relativeToURL: URL] autorelease];
}
- initWithString: (OFString*)string
{
char *cString, *cString2 = NULL;
char *UTF8String, *UTF8String2 = NULL;
self = [super init];
@try {
char *tmp, *tmp2;
if ((cString2 = strdup([string cString])) == NULL)
if ((UTF8String2 = strdup([string UTF8String])) == NULL)
@throw [OFOutOfMemoryException
newWithClass: isa
requestedSize: [string cStringLength]];
requestedSize: [string UTF8StringLength]];
cString = cString2;
UTF8String = UTF8String2;
if (!strncmp(cString, "file://", 7)) {
if (!strncmp(UTF8String, "file://", 7)) {
scheme = @"file";
path = [[OFString alloc] initWithCString: cString + 7];
path = [[OFString alloc]
initWithUTF8String: UTF8String + 7];
return self;
} else if (!strncmp(cString, "http://", 7)) {
} else if (!strncmp(UTF8String, "http://", 7)) {
scheme = @"http";
cString += 7;
} else if (!strncmp(cString, "https://", 8)) {
UTF8String += 7;
} else if (!strncmp(UTF8String, "https://", 8)) {
scheme = @"https";
cString += 8;
UTF8String += 8;
} else
@throw [OFInvalidFormatException newWithClass: isa];
if ((tmp = strchr(cString, '/')) != NULL) {
if ((tmp = strchr(UTF8String, '/')) != NULL) {
*tmp = '\0';
tmp++;
}
if ((tmp2 = strchr(cString, '@')) != NULL) {
if ((tmp2 = strchr(UTF8String, '@')) != NULL) {
char *tmp3;
*tmp2 = '\0';
tmp2++;
if ((tmp3 = strchr(cString, ':')) != NULL) {
if ((tmp3 = strchr(UTF8String, ':')) != NULL) {
*tmp3 = '\0';
tmp3++;
user = [[OFString alloc]
initWithCString: cString];
initWithUTF8String: UTF8String];
password = [[OFString alloc]
initWithCString: tmp3];
initWithUTF8String: tmp3];
} else
user = [[OFString alloc]
initWithCString: cString];
initWithUTF8String: UTF8String];
cString = tmp2;
UTF8String = tmp2;
}
if ((tmp2 = strchr(cString, ':')) != NULL) {
if ((tmp2 = strchr(UTF8String, ':')) != NULL) {
OFAutoreleasePool *pool;
OFString *portString;
*tmp2 = '\0';
tmp2++;
host = [[OFString alloc] initWithCString: cString];
host = [[OFString alloc]
initWithUTF8String: UTF8String];
pool = [[OFAutoreleasePool alloc] init];
portString = [OFString stringWithCString: tmp2];
portString = [OFString stringWithUTF8String: tmp2];
if ([portString decimalValue] > 65535)
@throw [OFInvalidFormatException
newWithClass: isa];
port = [portString decimalValue];
[pool release];
} else {
host = [[OFString alloc] initWithCString: cString];
host = [[OFString alloc]
initWithUTF8String: UTF8String];
if ([scheme isEqual: @"http"])
port = 80;
else if ([scheme isEqual: @"https"])
port = 443;
else
assert(0);
}
if ((cString = tmp) != NULL) {
if ((tmp = strchr(cString, '#')) != NULL) {
if ((UTF8String = tmp) != NULL) {
if ((tmp = strchr(UTF8String, '#')) != NULL) {
*tmp = '\0';
fragment = [[OFString alloc]
initWithCString: tmp + 1];
initWithUTF8String: tmp + 1];
}
if ((tmp = strchr(cString, '?')) != NULL) {
if ((tmp = strchr(UTF8String, '?')) != NULL) {
*tmp = '\0';
query = [[OFString alloc]
initWithCString: tmp + 1];
initWithUTF8String: tmp + 1];
}
if ((tmp = strchr(cString, ';')) != NULL) {
if ((tmp = strchr(UTF8String, ';')) != NULL) {
*tmp = '\0';
parameters = [[OFString alloc]
initWithCString: tmp + 1];
initWithUTF8String: tmp + 1];
}
path = [[OFString alloc] initWithFormat: @"/%s",
cString];
UTF8String];
} else
path = @"";
} @catch (id e) {
[self release];
@throw e;
} @finally {
free(cString2);
free(UTF8String2);
}
return self;
}
- initWithString: (OFString*)string
relativeToURL: (OFURL*)URL
{
char *cString, *cString2 = NULL;
char *UTF8String, *UTF8String2 = NULL;
if ([string containsString: @"://"])
return [self initWithString: string];
self = [super init];
@try {
char *tmp;
scheme = [URL->scheme copy];
host = [URL->host copy];
port = URL->port;
user = [URL->user copy];
password = [URL->password copy];
if ((cString2 = strdup([string cString])) == NULL)
if ((UTF8String2 = strdup([string UTF8String])) == NULL)
@throw [OFOutOfMemoryException
newWithClass: isa
requestedSize: [string cStringLength]];
requestedSize: [string UTF8StringLength]];
cString = cString2;
UTF8String = UTF8String2;
if ((tmp = strchr(cString, '#')) != NULL) {
if ((tmp = strchr(UTF8String, '#')) != NULL) {
*tmp = '\0';
fragment = [[OFString alloc] initWithCString: tmp + 1];
fragment = [[OFString alloc]
initWithUTF8String: tmp + 1];
}
if ((tmp = strchr(cString, '?')) != NULL) {
if ((tmp = strchr(UTF8String, '?')) != NULL) {
*tmp = '\0';
query = [[OFString alloc] initWithCString: tmp + 1];
query = [[OFString alloc] initWithUTF8String: tmp + 1];
}
if ((tmp = strchr(cString, ';')) != NULL) {
if ((tmp = strchr(UTF8String, ';')) != NULL) {
*tmp = '\0';
parameters = [[OFString alloc]
initWithCString: tmp + 1];
initWithUTF8String: tmp + 1];
}
if (*cString == '/')
path = [[OFString alloc] initWithCString: cString];
if (*UTF8String == '/')
path = [[OFString alloc]
initWithUTF8String: UTF8String];
else {
OFAutoreleasePool *pool;
OFString *s;
pool = [[OFAutoreleasePool alloc] init];
if ([URL->path hasSuffix: @"/"])
s = [OFString stringWithFormat: @"%@%s",
URL->path,
cString];
UTF8String];
else
s = [OFString stringWithFormat: @"%@/../%s",
URL->path,
cString];
UTF8String];
path = [resolve_relative_path(s) copy];
[pool release];
}
} @catch (id e) {
[self release];
@throw e;
} @finally {
free(cString2);
free(UTF8String2);
}
return self;
}
- initWithSerialization: (OFXMLElement*)element
{
|
︙ | | |
︙ | | |
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
|
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
|
-
+
-
+
-
-
+
+
+
-
-
+
+
-
+
-
-
+
+
|
defaultNS = parent->ns;
else if (parent != nil && parent->defaultNamespace != nil)
defaultNS = parent->defaultNamespace;
else
defaultNS = defaultNamespace;
i = 0;
length = [name cStringLength] + 3 + (level * indentation);
length = [name UTF8StringLength] + 3 + (level * indentation);
cString = [self allocMemoryWithSize: length];
memset(cString + i, ' ', level * indentation);
i += level * indentation;
/* Start of tag */
cString[i++] = '<';
if (prefix != nil && ![ns isEqual: defaultNS]) {
length += [prefix cStringLength] + 1;
length += [prefix UTF8StringLength] + 1;
@try {
cString = [self resizeMemory: cString
toSize: length];
} @catch (id e) {
[self freeMemory: cString];
@throw e;
}
memcpy(cString + i, [prefix cString], [prefix cStringLength]);
i += [prefix cStringLength];
memcpy(cString + i, [prefix UTF8String],
[prefix UTF8StringLength]);
i += [prefix UTF8StringLength];
cString[i++] = ':';
}
memcpy(cString + i, [name cString], [name cStringLength]);
i += [name cStringLength];
memcpy(cString + i, [name UTF8String], [name UTF8StringLength]);
i += [name UTF8StringLength];
/* xmlns if necessary */
if (prefix == nil && ((ns != nil && ![ns isEqual: defaultNS]) ||
(ns == nil && defaultNS != nil))) {
length += [ns cStringLength] + 9;
length += [ns UTF8StringLength] + 9;
@try {
cString = [self resizeMemory: cString
toSize: length];
} @catch (id e) {
[self freeMemory: cString];
@throw e;
}
memcpy(cString + i, " xmlns='", 8);
i += 8;
memcpy(cString + i, [ns cString], [ns cStringLength]);
i += [ns cStringLength];
memcpy(cString + i, [ns UTF8String], [ns UTF8StringLength]);
i += [ns UTF8StringLength];
cString[i++] = '\'';
}
/* Attributes */
attributesCArray = [attributes cArray];
attributesCount = [attributes count];
|
︙ | | |
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
|
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
|
-
+
-
-
+
+
-
-
-
+
+
+
-
-
-
+
+
+
-
-
+
+
|
if ([attributesCArray[j] namespace] != nil &&
(attributePrefix = [allNamespaces objectForKey:
[attributesCArray[j] namespace]]) == nil)
@throw [OFUnboundNamespaceException
newWithClass: isa
namespace: [attributesCArray[j] namespace]];
length += [attributeName cStringLength] +
length += [attributeName UTF8StringLength] +
(attributePrefix != nil ?
[attributePrefix cStringLength] + 1 : 0) +
[tmp cStringLength] + 4;
[attributePrefix UTF8StringLength] + 1 : 0) +
[tmp UTF8StringLength] + 4;
@try {
cString = [self resizeMemory: cString
toSize: length];
} @catch (id e) {
[self freeMemory: cString];
@throw e;
}
cString[i++] = ' ';
if (attributePrefix != nil) {
memcpy(cString + i, [attributePrefix cString],
[attributePrefix cStringLength]);
i += [attributePrefix cStringLength];
memcpy(cString + i, [attributePrefix UTF8String],
[attributePrefix UTF8StringLength]);
i += [attributePrefix UTF8StringLength];
cString[i++] = ':';
}
memcpy(cString + i, [attributeName cString],
[attributeName cStringLength]);
i += [attributeName cStringLength];
memcpy(cString + i, [attributeName UTF8String],
[attributeName UTF8StringLength]);
i += [attributeName UTF8StringLength];
cString[i++] = '=';
cString[i++] = '\'';
memcpy(cString + i, [tmp cString], [tmp cStringLength]);
i += [tmp cStringLength];
memcpy(cString + i, [tmp UTF8String], [tmp UTF8StringLength]);
i += [tmp UTF8StringLength];
cString[i++] = '\'';
[pool2 releaseObjects];
}
/* Childen */
if (children != nil) {
|
︙ | | |
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
|
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
|
-
-
+
+
-
+
|
child = [childrenCArray[j]
_XMLStringWithParent: self
namespaces: allNamespaces
indentation: (indent ? indentation : 0)
level: level + 1];
[tmp addNItems: [child cStringLength]
fromCArray: [child cString]];
[tmp addNItems: [child UTF8StringLength]
fromCArray: [child UTF8String]];
}
if (indent)
[tmp addItem: "\n"];
length += [tmp count] + [name cStringLength] + 2 +
length += [tmp count] + [name UTF8StringLength] + 2 +
(indent ? level * indentation : 0);
@try {
cString = [self resizeMemory: cString
toSize: length];
} @catch (id e) {
[self freeMemory: cString];
@throw e;
|
︙ | | |
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
|
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
|
-
+
-
-
-
+
+
+
-
-
+
+
-
+
|
memset(cString + i, ' ', level * indentation);
i += level * indentation;
}
cString[i++] = '<';
cString[i++] = '/';
if (prefix != nil) {
length += [prefix cStringLength] + 1;
length += [prefix UTF8StringLength] + 1;
@try {
cString = [self resizeMemory: cString
toSize: length];
} @catch (id e) {
[self freeMemory: cString];
@throw e;
}
memcpy(cString + i, [prefix cString],
[prefix cStringLength]);
i += [prefix cStringLength];
memcpy(cString + i, [prefix UTF8String],
[prefix UTF8StringLength]);
i += [prefix UTF8StringLength];
cString[i++] = ':';
}
memcpy(cString + i, [name cString], [name cStringLength]);
i += [name cStringLength];
memcpy(cString + i, [name UTF8String], [name UTF8StringLength]);
i += [name UTF8StringLength];
} else
cString[i++] = '/';
cString[i++] = '>';
assert(i == length);
[pool release];
@try {
ret = [OFString stringWithCString: cString
ret = [OFString stringWithUTF8String: cString
length: length];
} @finally {
[self freeMemory: cString];
}
return ret;
}
|
︙ | | |
︙ | | |
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
|
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
|
-
-
+
+
-
+
|
[cache addNItems: length
fromCArray: string];
else {
OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
OFString *tmp = [OFString stringWithCString: string
encoding: encoding
length: length];
[cache addNItems: [tmp cStringLength]
fromCArray: [tmp cString]];
[cache addNItems: [tmp UTF8StringLength]
fromCArray: [tmp UTF8String]];
[pool release];
}
}
static OFString*
transform_string(OFDataArray *cache, size_t cut, BOOL unescape,
OFObject <OFStringXMLUnescapingDelegate> *delegate)
{
OFMutableString *ret = [OFMutableString
stringWithCString: [cache cArray]
stringWithUTF8String: [cache cArray]
length: [cache count]];
[ret replaceOccurrencesOfString: @"\r\n"
withString: @"\n"];
[ret replaceOccurrencesOfString: @"\r"
withString: @"\n"];
|
︙ | | |
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
-
-
+
+
|
/* In OF_XMLPARSER_IN_TAG, there can be only spaces */
if (length - last > 0 && state != OF_XMLPARSER_IN_TAG)
cache_append(cache, buffer + last, encoding, length - last);
}
- (void)parseString: (OFString*)string
{
[self parseBuffer: [string cString]
withLength: [string cStringLength]];
[self parseBuffer: [string UTF8String]
withLength: [string UTF8StringLength]];
}
- (void)parseStream: (OFStream*)stream
{
char *buffer = [self allocMemoryWithSize: of_pagesize];
@try {
|
︙ | | |
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
|
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
-
-
+
+
-
+
+
-
+
|
return NO;
acceptProlog = NO;
pi = [pi substringWithRange: of_range(3, [pi length] - 3)];
pi = [pi stringByDeletingEnclosingWhitespaces];
cString = [pi cString];
length = [pi cStringLength];
cString = [pi UTF8String];
length = [pi UTF8StringLength];
for (i = last = 0; i < length; i++) {
switch (piState) {
case 0:
if (cString[i] == ' ' || cString[i] == '\t' ||
cString[i] == '\r' || cString[i] == '\n' ||
cString[i] == '\f')
continue;
last = i;
piState = 1;
i--;
break;
case 1:
if (cString[i] != '=')
continue;
attribute = [OFString stringWithCString: cString + last
attribute = [OFString
stringWithUTF8String: cString + last
length: i - last];
last = i + 1;
piState = 2;
break;
case 2:
if (cString[i] != '\'' && cString[i] != '"')
return NO;
piDelimiter = cString[i];
last = i + 1;
piState = 3;
break;
case 3:
if (cString[i] != piDelimiter)
continue;
value = [OFMutableString
stringWithCString: cString + last
stringWithUTF8String: cString + last
length: i - last];
if ([attribute isEqual: @"version"])
if (![value hasPrefix: @"1."])
return NO;
if ([attribute isEqual: @"encoding"]) {
|
︙ | | |
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
-
+
-
-
+
+
+
-
-
-
+
+
|
if ((length = *i - *last) > 0)
cache_append(cache, buffer + *last, encoding, length);
pool = [[OFAutoreleasePool alloc] init];
cacheCString = [cache cArray];
cacheLength = [cache count];
cacheString = [OFString stringWithCString: cacheCString
cacheString = [OFString stringWithUTF8String: cacheCString
length: cacheLength];
if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
name = [[OFString alloc] initWithCString: tmp + 1
length: cacheLength -
name = [[OFString alloc]
initWithUTF8String: tmp + 1
length: cacheLength - (tmp - cacheCString) - 1];
(tmp - cacheCString) -
1];
prefix = [[OFString alloc] initWithCString: cacheCString
prefix = [[OFString alloc]
initWithUTF8String: cacheCString
length: tmp - cacheCString];
} else {
name = [cacheString copy];
prefix = nil;
}
if (buffer[*i] == '>' || buffer[*i] == '/') {
|
︙ | | |
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
|
-
+
-
-
+
+
+
-
-
-
+
+
|
if ((length = *i - *last) > 0)
cache_append(cache, buffer + *last, encoding, length);
pool = [[OFAutoreleasePool alloc] init];
cacheCString = [cache cArray];
cacheLength = [cache count];
cacheString = [OFString stringWithCString: cacheCString
cacheString = [OFString stringWithUTF8String: cacheCString
length: cacheLength];
if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
name = [[OFString alloc] initWithCString: tmp + 1
length: cacheLength -
name = [[OFString alloc]
initWithUTF8String: tmp + 1
length: cacheLength - (tmp - cacheCString) - 1];
(tmp - cacheCString) -
1];
prefix = [[OFString alloc] initWithCString: cacheCString
prefix = [[OFString alloc]
initWithUTF8String: cacheCString
length: tmp - cacheCString];
} else {
name = [cacheString copy];
prefix = nil;
}
if (![[previous lastObject] isEqual: cacheString])
|
︙ | | |
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
|
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
|
-
+
-
-
+
+
-
+
-
+
|
return;
if ((length = *i - *last) > 0)
cache_append(cache, buffer + *last, encoding, length);
pool = [[OFAutoreleasePool alloc] init];
cacheString = [OFMutableString stringWithCString: [cache cArray]
cacheString = [OFMutableString stringWithUTF8String: [cache cArray]
length: [cache count]];
[cacheString deleteEnclosingWhitespaces];
/* Prevent a useless copy later */
[cacheString makeImmutable];
cacheCString = [cacheString cString];
cacheLength = [cacheString cStringLength];
cacheCString = [cacheString UTF8String];
cacheLength = [cacheString UTF8StringLength];
if ((tmp = memchr(cacheCString, ':', cacheLength)) != NULL) {
attributeName = [[OFString alloc]
initWithCString: tmp + 1
initWithUTF8String: tmp + 1
length: cacheLength - (tmp - cacheCString) - 1];
attributePrefix = [[OFString alloc]
initWithCString: cacheCString
initWithUTF8String: cacheCString
length: tmp - cacheCString];
} else {
attributeName = [cacheString copy];
attributePrefix = nil;
}
[pool release];
|
︙ | | |
︙ | | |
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
-
+
|
}
@try {
id object;
if ((object = va_arg(ctx->arguments, id)) != nil)
tmpLen = asprintf(&tmp, ctx->subformat,
[[object description] cString]);
[[object description] UTF8String]);
else
tmpLen = asprintf(&tmp, ctx->subformat,
"(nil)");
} @catch (id e) {
free(ctx->buffer);
@throw e;
} @finally {
|
︙ | | |
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
-
+
|
formatFlagsState,
formatFieldWidthState,
formatLengthModifierState,
formatConversionSpecifierState
};
int
of_vasprintf(char **cString, const char *format, va_list arguments)
of_vasprintf(char **UTF8String, const char *format, va_list arguments)
{
struct context ctx;
ctx.format = format;
ctx.formatLen = strlen(format);
memset(ctx.subformat, 0, MAX_SUBFORMAT_LEN + 1);
ctx.subformatLen = 0;
|
︙ | | |
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
|
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
|
-
+
-
+
-
+
|
ctx.formatLen - ctx.last)) {
free(ctx.buffer);
return -1;
}
ctx.buffer[ctx.bufferLen] = 0;
*cString = ctx.buffer;
*UTF8String = ctx.buffer;
return (ctx.bufferLen <= INT_MAX ? (int)ctx.bufferLen : -1);
}
int
of_asprintf(char **cString, const char *format, ...)
of_asprintf(char **UTF8String, const char *format, ...)
{
va_list arguments;
int ret;
va_start(arguments, format);
ret = of_vasprintf(cString, format, arguments);
ret = of_vasprintf(UTF8String, format, arguments);
va_end(arguments);
return ret;
}
|
︙ | | |
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
|
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
|
-
-
+
+
-
-
+
+
-
+
|
TEST(@"-[caseInsensitiveCompare:]",
[@"a" caseInsensitiveCompare: @"A"] == OF_ORDERED_SAME &&
[@"Ä" caseInsensitiveCompare: @"ä"] == OF_ORDERED_SAME &&
[@"я" caseInsensitiveCompare: @"Я"] == OF_ORDERED_SAME &&
[@"€" caseInsensitiveCompare: @"ß"] == OF_ORDERED_DESCENDING &&
[@"ß" caseInsensitiveCompare: @"→"] == OF_ORDERED_ASCENDING &&
[@"AA" caseInsensitiveCompare: @"z"] == OF_ORDERED_ASCENDING &&
[[OFString stringWithCString: "ABC"] caseInsensitiveCompare:
[OFString stringWithCString: "AbD"]] == [@"abc" compare: @"abd"])
[[OFString stringWithUTF8String: "ABC"] caseInsensitiveCompare:
[OFString stringWithUTF8String: "AbD"]] == [@"abc" compare: @"abd"])
TEST(@"-[hash] is the same if -[isEqual:] is YES",
[s[0] hash] == [s[2] hash])
TEST(@"-[description]", [[s[0] description] isEqual: s[0]])
TEST(@"-[appendString:] and -[appendCString:]",
R([s[1] appendCString: "1𝄞"]) && R([s[1] appendString: @"3"]) &&
TEST(@"-[appendString:] and -[appendUTF8String:]",
R([s[1] appendUTF8String: "1𝄞"]) && R([s[1] appendString: @"3"]) &&
R([s[0] appendString: s[1]]) && [s[0] isEqual: @"täs€1𝄞3"])
TEST(@"-[length]", [s[0] length] == 7)
TEST(@"-[cStringLength]", [s[0] cStringLength] == 13)
TEST(@"-[UTF8StringLength]", [s[0] UTF8StringLength] == 13)
TEST(@"-[hash]", [s[0] hash] == 0xD576830E)
TEST(@"-[characterAtIndex:]", [s[0] characterAtIndex: 0] == 't' &&
[s[0] characterAtIndex: 1] == 0xE4 &&
[s[0] characterAtIndex: 3] == 0x20AC &&
[s[0] characterAtIndex: 5] == 0x1D11E)
|
︙ | | |
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
-
-
+
+
+
|
TEST(@"-[uppercaseString]",
[[s[0] uppercaseString] isEqual: @"3𝄞1€SÄT"])
TEST(@"-[lowercaseString]", R([s[0] upper]) &&
[[s[0] lowercaseString] isEqual: @"3𝄞1€sät"])
TEST(@"+[stringWithCString:length:]",
(s[0] = [OFMutableString stringWithCString: "\xEF\xBB\xBF" "foobar"
TEST(@"+[stringWithUTF8String:length:]",
(s[0] = [OFMutableString stringWithUTF8String: "\xEF\xBB\xBF"
"foobar"
length: 6]) &&
[s[0] isEqual: @"foo"])
TEST(@"+[stringWithUnicodeString:]",
(s[1] = [OFString stringWithUnicodeString: ucstr]) &&
[s[1] isEqual: @"fööbär🀺"] &&
(s[1] = [OFString stringWithUnicodeString: sucstr]) &&
|
︙ | | |
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
|
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
|
-
-
+
+
-
+
-
+
-
+
|
TEST(@"+[stringWithContentsOfURL:encoding]", (s[1] = [OFString
stringWithContentsOfURL: [OFURL URLWithString:
@"file://testfile.txt"]
encoding: OF_STRING_ENCODING_ISO_8859_1]) &&
[s[1] isEqual: @"testäöü"])
TEST(@"-[appendCStringWithLength:]",
R([s[0] appendCString: "foo\xEF\xBB\xBF" "barqux" + 3
TEST(@"-[appendUTFString:withLength:]",
R([s[0] appendUTF8String: "foo\xEF\xBB\xBF" "barqux" + 3
withLength: 6]) && [s[0] isEqual: @"foobar"])
EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #1",
OFInvalidEncodingException,
[OFString stringWithCString: "\xE0\x80"])
[OFString stringWithUTF8String: "\xE0\x80"])
EXPECT_EXCEPTION(@"Detection of invalid UTF-8 encoding #2",
OFInvalidEncodingException,
[OFString stringWithCString: "\xF0\x80\x80\xC0"])
[OFString stringWithUTF8String: "\xF0\x80\x80\xC0"])
TEST(@"-[reverse] on UTF-8 strings",
(s[0] = [OFMutableString stringWithCString: "äöü€𝄞"]) &&
(s[0] = [OFMutableString stringWithUTF8String: "äöü€𝄞"]) &&
R([s[0] reverse]) && [s[0] isEqual: @"𝄞€üöä"])
TEST(@"Conversion of ISO 8859-1 to UTF-8",
[[OFString stringWithCString: "\xE4\xF6\xFC"
encoding: OF_STRING_ENCODING_ISO_8859_1]
isEqual: @"äöü"])
|
︙ | | |