@implementation NGLdapSearchResultEnumerator
- (id)initWithConnection:(NGLdapConnection *)_con messageID:(int)_mid {
- self->connection = [_con retain];
- self->handle = [_con ldapHandle];
- self->msgid = _mid;
- self->startTime = [[NSDate date] timeIntervalSince1970];
+ if ((self = [super init]) != nil) {
+ self->connection = [_con retain];
+ self->handle = [_con ldapHandle];
+ self->msgid = _mid;
+
+ // TODO: -timeIntervalSince1970 deprecated on Cocoa
+ self->startTime = [[NSDate date] timeIntervalSince1970];
+ }
return self;
}
+- (id)init {
+ return [self initWithConnection:NULL messageID:-1];
+}
+
- (void)dealloc {
[self->connection release];
[super dealloc];
}
- (NSTimeInterval)duration {
+ // TODO: -timeIntervalSince1970 deprecated on Cocoa
return [[NSDate date] timeIntervalSince1970] - self->startTime;
}
return self->index;
}
+- (void)setTimeout:(NSTimeInterval)_value {
+ self->timeout = _value;
+}
+- (NSTimeInterval)timeout {
+ return self->timeout;
+}
+
/* enumerator */
- (void)cancel {
top = NULL;
if (self->timeout > 0) {
to.tv_sec = self->timeout;
- to.tv_sec = (long)(self->timeout * 1000.0) - (to.tv_sec * 1000);
+ to.tv_usec = (long)(self->timeout * 1000.0) - (to.tv_sec * 1000);
top = &to;
}
-
+
res = ldap_result(self->handle, self->msgid, 0, top, &msg);
+ if (msg == NULL)
+ return nil;
- if (msg) {
- switch(res) {
+ switch(res) {
#if defined(LDAP_RES_SEARCH_REFERENCE)
- case LDAP_RES_SEARCH_REFERENCE: {
- int rres;
- char **rptr;
- LDAPControl **ctrl;
-
- rres = ldap_parse_reference(self->handle, msg, &rptr, &ctrl,
- 0 /* don't free msg */);
- if (rres == LDAP_SUCCESS) {
- }
- else {
- /* error */
- NSLog(@"%s: couldn't parse result reference ..", __PRETTY_FUNCTION__);
- }
-
- NSLog(@"WARNING: doesn't support result references yet ..");
+ case LDAP_RES_SEARCH_REFERENCE: {
+ int rres;
+ char **rptr;
+ LDAPControl **ctrl;
+
+ rres = ldap_parse_reference(self->handle, msg, &rptr, &ctrl,
+ 0 /* don't free msg */);
+ if (rres == LDAP_SUCCESS) {
+ }
+ else {
+ /* error */
+ NSLog(@"%s: couldn't parse result reference ..", __PRETTY_FUNCTION__);
+ }
+
+ NSLog(@"ERROR(%s): does not support result references yet ..",
+ __PRETTY_FUNCTION__);
- if (rptr) ldap_value_free(rptr);
- if (ctrl) ldap_controls_free(ctrl);
+ if (rptr != NULL) ldap_value_free(rptr);
+ if (ctrl != NULL) ldap_controls_free(ctrl);
- break;
- }
+ break;
+ }
#endif
- case LDAP_RES_SEARCH_ENTRY: {
- int resultCount;
+ case LDAP_RES_SEARCH_ENTRY: {
+ int resultCount;
- if ((resultCount = ldap_count_entries(self->handle, msg)) == -1) {
- /* failed */
- int err;
+ if ((resultCount = ldap_count_entries(self->handle, msg)) == -1) {
+ /* failed */
+ int err;
- err = ldap_result2error(self->handle, msg, 1 /* free msg */);
+ err = ldap_result2error(self->handle, msg, 1 /* free msg */);
- [[self->connection _exceptionForErrorCode:err
- operation:@"count-fetch"
- userInfo:nil]
- raise];
- return nil;
- }
- else if (resultCount == 1) {
- LDAPMessage *result;
- NSString *dn = nil;
- char *tmp;
- NSArray *attributes;
+ [[self->connection _exceptionForErrorCode:err
+ operation:@"count-fetch"
+ userInfo:nil]
+ raise];
+ return nil;
+ }
+
+ if (resultCount == 1) {
+ LDAPMessage *result;
+ NSString *dn = nil;
+ char *tmp;
+ NSArray *attributes;
- if ((result = ldap_first_entry(self->handle, msg)) == NULL) {
- /* could not get entry */
- int err;
+ if ((result = ldap_first_entry(self->handle, msg)) == NULL) {
+ /* could not get entry */
+ int err;
- err = ldap_result2error(self->handle, msg, 1 /* free msg */);
+ err = ldap_result2error(self->handle, msg, 1 /* free msg */);
- [[self->connection _exceptionForErrorCode:resultCount
- operation:@"fetch"
- userInfo:nil]
- raise];
+ [[self->connection _exceptionForErrorCode:resultCount
+ operation:@"fetch"
+ userInfo:nil]
+ raise];
- return nil;
- }
+ return nil;
+ }
- /* get distinguished name */
+ /* get distinguished name */
- if ((tmp = ldap_get_dn(self->handle, result))) {
- NS_DURING {
- dn = [[[NSString alloc] initWithUTF8String:tmp] autorelease];
- }
- NS_HANDLER {
- fprintf(stderr, "Got exception %s while NSUTF8StringEncoding, "
- "use defaultCStringEncoding",
- [[localException description] cString]);
- dn = nil;
- }
- NS_ENDHANDLER;
-
- if (dn == nil)
- dn = [[[NSString alloc] initWithCString:tmp] autorelease];
-
- free(tmp);
- }
- /* get all attributes */
+ if ((tmp = ldap_get_dn(self->handle, result)) != NULL) {
+ // TODO: slow ..., somehow fix that.
+
+ /* try UTF-8 (as per spec?) */
+
+ NS_DURING {
+ dn = [[[NSString alloc] initWithUTF8String:tmp] autorelease];
+ }
+ NS_HANDLER {
+ fprintf(stderr, "Got exception %s while NSUTF8StringEncoding, "
+ "use defaultCStringEncoding",
+ [[localException description] cString]);
+ dn = nil;
+ }
+ NS_ENDHANDLER;
+
+ /* try system encoding (Latin-1 on libFoundation) */
+
+ if (dn == nil) // TODO: print a warning?
+ dn = [[[NSString alloc] initWithCString:tmp] autorelease];
+
+ if (tmp != NULL) free(tmp);
+ }
+
+ /* get all attributes */
- attributes = [self _attributesFromResult:result];
+ attributes = [self _attributesFromResult:result];
- if (result) {
- // TODO: ldap_msgfree(result); // do not release result-msg ???
- result = NULL;
- }
-
- record = [[NGLdapEntry alloc] initWithDN:dn attributes:attributes];
-
- [attributes release]; attributes = nil;
- }
- else if (resultCount == 0) {
- /* no more results */
- record = nil;
- }
- break;
+ if (result != NULL) {
+ // TODO: ldap_msgfree(result); // do not release result-msg ???
+ result = NULL;
}
+
+ record = [[NGLdapEntry alloc] initWithDN:dn attributes:attributes];
+
+ [attributes release]; attributes = nil;
+ }
+ else if (resultCount == 0) {
+ /* no more results */
+ record = nil;
+ }
+ break;
+ }
- case LDAP_RES_SEARCH_RESULT:
- self->handle = NULL;
- [self->connection release]; self->connection = nil;
- break;
+ case LDAP_RES_SEARCH_RESULT:
+ self->handle = NULL;
+ [self->connection release]; self->connection = nil;
+ break;
- default:
- NSLog(@"unexpected msg-code: %X", res);
- break;
- }
- if (msg)
- ldap_msgfree(msg);
+ default:
+ NSLog(@"NGLdap(%s): unexpected msg-code: %X", __PRETTY_FUNCTION__,res);
+ break;
}
+ if (msg != NULL)
+ ldap_msgfree(msg);
- if (record)
+ if (record != nil)
self->index++;
return [record autorelease];
- (NSString *)description {
NSMutableString *s;
-
+
s = [NSMutableString stringWithCapacity:100];
[s appendFormat:@"<0x%08X[%@]:", self, NSStringFromClass([self class])];