Skip to content

Instantly share code, notes, and snippets.

@indragiek
Created November 8, 2011 02:39
Show Gist options
  • Save indragiek/1346858 to your computer and use it in GitHub Desktop.
Save indragiek/1346858 to your computer and use it in GitHub Desktop.
libxml2 SAX + file input stream
- (BOOL)parse
{
NSArray *libraryDatabases = [[[NSUserDefaults standardUserDefaults] persistentDomainForName:@"com.apple.iApps"] objectForKey:@"iTunesRecentDatabases"];
NSURL *libraryURL = (([libraryDatabases count])) ? [NSURL URLWithString:[libraryDatabases objectAtIndex:0]] : nil;
assert(libraryURL);
const char *libPath = [libraryURL.path UTF8String];
const int min = 4;
FILE *fp = fopen(libPath, "r");
assert(fp);
char buffer[min];
int i, c;
for (i = 0; (c = getc(fp)) != EOF && i < min; buffer[i++] = c);
if (i != min) {
fclose(fp);
return NO;
}
xmlParserCtxtPtr ctx = xmlCreatePushParserCtxt(&_saxHandlerStruct, (__bridge void*)self, buffer, 4, NULL);
int options = XML_PARSE_RECOVER | XML_PARSE_NOENT | XML_PARSE_DTDLOAD;
xmlCtxtUseOptions(ctx, options);
c = 0;
while (c != EOF) {
char chunk[PAGE_SIZE];
for (i = 0; (c = getc(fp)) != EOF && i < PAGE_SIZE; chunk[i++] = c);
xmlParseChunk(ctx, chunk, i, 0);
}
xmlParseChunk(ctx, NULL, 0, 1);
xmlFreeParserCtxt(ctx);
xmlCleanupParser();
fclose(fp);
return YES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment