ObjFW  Diff

Differences From Artifact [4ec189c3b9]:

To Artifact [4bb745a125]:


13
14
15
16
17
18
19

20
21
22
23
24
25
26
27

28
29
30
31
32
33
34

#define _GNU_SOURCE
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#ifdef HAVE_MADVISE
#include <sys/mman.h>
#else
#define madvise(addr, len, advise)
#endif

#import "OFString.h"
#import "OFArray.h"

#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "macros.h"

#import "asprintf.h"
#import "unicode.h"








>

|

|




>







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#define _GNU_SOURCE
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/stat.h>
#ifdef HAVE_MADVISE
# include <sys/mman.h>
#else
# define madvise(addr, len, advise)
#endif

#import "OFString.h"
#import "OFArray.h"
#import "OFFile.h"
#import "OFAutoreleasePool.h"
#import "OFExceptions.h"
#import "macros.h"

#import "asprintf.h"
#import "unicode.h"

273
274
275
276
277
278
279












280
281
282
283
284
285
286
	return ret;
}

+ stringWithString: (OFString*)str
{
	return [[[self alloc] initWithString: str] autorelease];
}













- init
{
	[super init];

	string = NULL;








>
>
>
>
>
>
>
>
>
>
>
>







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
	return ret;
}

+ stringWithString: (OFString*)str
{
	return [[[self alloc] initWithString: str] autorelease];
}

+ stringWithContentsOfFile: (OFString*)path
{
	return [[[self alloc] initWithContentsOfFile: path] autorelease];
}

+ stringWithContentsOfFile: (OFString*)path
		  encoding: (enum of_string_encoding)encoding
{
	return [[[self alloc] initWithContentsOfFile: path
					    encoding: encoding] autorelease];
}

- init
{
	[super init];

	string = NULL;

620
621
622
623
624
625
626


















































627
628
629
630
631
632
633
		 * Compiler bug? Anyway, [self dealloc] will do here as we
		 * don't reimplement dealloc.
		 */
		free(string);
		[self dealloc];
		@throw e;
	}



















































	return self;
}

- (const char*)cString
{
	return string;







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
664
665
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
		 * Compiler bug? Anyway, [self dealloc] will do here as we
		 * don't reimplement dealloc.
		 */
		free(string);
		[self dealloc];
		@throw e;
	}

	return self;
}

- initWithContentsOfFile: (OFString*)path
{
	return [self initWithContentsOfFile: path
				   encoding: OF_STRING_ENCODING_UTF_8];
}

- initWithContentsOfFile: (OFString*)path
		encoding: (enum of_string_encoding)encoding
{
	OFFile *file = nil;
	char *tmp;
	struct stat s;

	if (stat([path cString], &s) == -1) {
		Class c = isa;
		[super dealloc];
		@throw [OFInitializationFailedException newWithClass: c];
	}

	if ((tmp = malloc(s.st_size)) == NULL) {
		Class c = isa;
		[super dealloc];
		@throw [OFOutOfMemoryException newWithClass: c
						       size: s.st_size];
	}

	@try {
		file = [[OFFile alloc] initWithPath: path
					       mode: @"rb"];
		[file readExactlyNBytes: s.st_size
			     intoBuffer: tmp];
	} @catch (OFException *e) {
		free(tmp);
		[super dealloc];
		@throw e;
	} @finally {
		[file release];
	}

	@try {
		self = [self initWithCString: tmp
				    encoding: encoding
				      length: s.st_size];
	} @finally {
		free(tmp);
	}

	return self;
}

- (const char*)cString
{
	return string;