-
Notifications
You must be signed in to change notification settings - Fork 2
/
Source.mm
204 lines (158 loc) · 4.77 KB
/
Source.mm
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
#import "Source.h"
@implementation Source
- (void) _clear {
uri_.clear();
distribution_.clear();
type_.clear();
description_.clear();
label_.clear();
origin_.clear();
depiction_.clear();
support_.clear();
version_.clear();
defaultIcon_.clear();
if (record_ != nil) {
[record_ release];
record_ = nil;
}
if (host_ != nil) {
[host_ release];
host_ = nil;
}
if (authority_ != nil) {
[authority_ release];
authority_ = nil;
}
}
- (void) dealloc {
[self _clear];
[super dealloc];
}
+ (NSArray *) _attributeKeys {
return [NSArray arrayWithObjects:@"description", @"distribution", @"host", @"key", @"label", @"name", @"origin", @"trusted", @"type", @"uri", @"version", nil];
}
- (NSArray *) attributeKeys {
return [[self class] _attributeKeys];
}
+ (BOOL) isKeyExcludedFromWebScript:(const char *)name {
return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name];
}
- (void) setMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool {
[self _clear];
trusted_ = index->IsTrusted();
uri_.set(pool, index->GetURI());
distribution_.set(pool, index->GetDist());
type_.set(pool, index->GetType());
debReleaseIndex *dindex(dynamic_cast<debReleaseIndex *>(index));
if (dindex != NULL) {
FileFd fd;
if (!fd.Open(dindex->MetaIndexFile("Release"), FileFd::ReadOnly))
_error->Discard();
else {
pkgTagFile tags(&fd);
pkgTagSection section;
tags.Step(section);
struct {
const char *name_;
CYString *value_;
} names[] = {
{"default-icon", &defaultIcon_},
{"depiction", &depiction_},
{"description", &description_},
{"label", &label_},
{"origin", &origin_},
{"support", &support_},
{"version", &version_},
};
for (size_t i(0); i != sizeof(names) / sizeof(names[0]); ++i) {
const char *start, *end;
if (section.Find(names[i].name_, start, end)) {
CYString &value(*names[i].value_);
value.set(pool, start, end - start);
}
}
}
}
record_ = [Sources_ objectForKey:[self key]];
if (record_ != nil)
record_ = [record_ retain];
NSURL *url([NSURL URLWithString:uri_]);
host_ = [url host];
if (host_ != nil)
host_ = [[host_ lowercaseString] retain];
if (host_ != nil)
authority_ = host_;
else
authority_ = [url path];
if (authority_ != nil)
authority_ = [authority_ retain];
}
- (Source *) initWithMetaIndex:(metaIndex *)index inPool:(apr_pool_t *)pool {
if ((self = [super init]) != nil) {
[self setMetaIndex:index inPool:pool];
} return self;
}
- (NSComparisonResult) compareByNameAndType:(Source *)source {
NSDictionary *lhr = [self record];
NSDictionary *rhr = [source record];
if (lhr != rhr)
return lhr == nil ? NSOrderedDescending : NSOrderedAscending;
NSString *lhs = [self name];
NSString *rhs = [source name];
if ([lhs length] != 0 && [rhs length] != 0) {
unichar lhc = [lhs characterAtIndex:0];
unichar rhc = [rhs characterAtIndex:0];
if (isalpha(lhc) && !isalpha(rhc))
return NSOrderedAscending;
else if (!isalpha(lhc) && isalpha(rhc))
return NSOrderedDescending;
}
return [lhs compare:rhs options:LaxCompareOptions_];
}
- (NSString *) depictionForPackage:(NSString *)package {
return depiction_.empty() ? nil : [depiction_ stringByReplacingOccurrencesOfString:@"*" withString:package];
}
- (NSString *) supportForPackage:(NSString *)package {
return support_.empty() ? nil : [support_ stringByReplacingOccurrencesOfString:@"*" withString:package];
}
- (NSDictionary *) record {
return record_;
}
- (BOOL) trusted {
return trusted_;
}
- (NSString *) uri {
return uri_;
}
- (NSString *) distribution {
return distribution_;
}
- (NSString *) type {
return type_;
}
- (NSString *) key {
return [NSString stringWithFormat:@"%@:%@:%@", (NSString *) type_, (NSString *) uri_, (NSString *) distribution_];
}
- (NSString *) host {
return host_;
}
- (NSString *) name {
return origin_.empty() ? authority_ : origin_;
}
- (NSString *) description {
return description_;
}
- (NSString *) label {
return label_.empty() ? authority_ : label_;
}
- (NSString *) origin {
return origin_;
}
- (NSString *) version {
return version_;
}
- (NSString *) defaultIcon {
return defaultIcon_;
}
@end
/* }}} */