]> err.no Git - sope/blob - sope-mime/NGMime/NGMimeJoinedData.m
minor code cleanups
[sope] / sope-mime / NGMime / NGMimeJoinedData.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #include "common.h"
24 #include "NGMimeJoinedData.h"
25 #include "timeMacros.h"
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include "NGMimeFileData.h"
31
32 @implementation NGMimeJoinedData
33
34 - (id)init {
35   if ((self = [super init])) {
36     self->joinedDataObjects = [[NSMutableArray alloc] initWithCapacity:16];
37   }
38   return self;
39 }
40
41 - (void)dealloc {
42   [self->joinedDataObjects release];
43   [super dealloc];
44 }
45
46 - (id)copyWithZone:(NSZone*)zone {
47     return RETAIN(self);
48 }
49
50 - (NSArray *)_joinedDataObjects {
51   return self->joinedDataObjects;
52 }
53
54 - (void)appendData:(NSData *)_data {
55   if ([_data isKindOfClass:[NGMimeJoinedData class]]) {
56     [self->joinedDataObjects addObjectsFromArray:
57          [(NGMimeJoinedData *)_data _joinedDataObjects]];
58   }
59   else
60     [self->joinedDataObjects addObject:_data];
61 }
62
63 - (void)appendBytes:(const void *)_bytes
64   length:(unsigned int)_length
65 {
66   NSMutableData *data;
67
68   data = (NSMutableData *)[self->joinedDataObjects lastObject];
69
70   if ([data isKindOfClass:[NSMutableData class]]) {
71     [data appendBytes:_bytes length:_length];
72   }
73   else {
74     data = [NSMutableData dataWithBytes:_bytes length:_length];
75     [self->joinedDataObjects addObject:data];
76   }
77 }
78
79 - (BOOL)writeToFile:(NSString*)_path
80   atomically:(BOOL)_useAuxiliaryFile
81 {
82   NSString      *filename = nil;
83   NSEnumerator  *enumerator;
84   NSData        *data;
85   volatile BOOL result;
86     
87   int fd;
88     
89   filename = _path;
90
91   fd = open([filename fileSystemRepresentation],
92             O_WRONLY | O_CREAT | O_TRUNC, 0600);
93   if (fd == -1) {
94     fprintf(stderr, "Could not open file for writing %s: %s\n",
95             [filename fileSystemRepresentation], strerror(errno));
96     return NO;
97   }
98
99   result     = YES;
100   enumerator = [self->joinedDataObjects objectEnumerator];
101
102   while ((data = [enumerator nextObject])) {
103
104     TIME_START("write bytes ");
105
106     if ([data isKindOfClass:[NGMimeFileData class]]) {
107       if (![(NGMimeFileData *)data appendDataToFileDesc:fd]) {
108         fprintf(stderr, "Failed to write %i bytes to %s: %s\n",
109                 [data length],
110                 [filename fileSystemRepresentation], strerror(errno));
111         close(fd);
112       }
113     }
114     else {
115       const void *bytes;
116       unsigned   len;
117
118       TIME_START("bytes ...")
119         bytes  = [data bytes];
120         len    = [data length];
121       TIME_END;
122       
123       if (write(fd, bytes, len) != (int)len) {
124         fprintf(stderr, "Failed to write %i bytes to %s: %s\n",
125                 len, [filename fileSystemRepresentation], strerror(errno));
126         close(fd);
127         return NO;
128       }
129     }
130     TIME_END;
131   }
132   close(fd);
133         
134   return result;
135 }
136
137 - (NSString *)description {
138   return [NSString stringWithFormat:@"<0x%08X[%@]: joinedDataObjects=%d>",
139                    self, NSStringFromClass([self class]),
140                    [self->joinedDataObjects count]];
141 }
142
143 @end /* NGMimeJoinedData */