📄 getmetadataforfile.c
字号:
#include <CoreFoundation/CoreFoundation.h>#include <CoreServices/CoreServices.h> #include "chm_lib.h"#include <stdio.h>#include <stdlib.h>#include <strings.h>#include <unistd.h>#include <sys/stat.h>#include <sys/types.h>/* -----------------------------------------------------------------------------Step 1Set the UTI types the importer supportsModify the CFBundleDocumentTypes entry in Info.plist to containan array of Uniform Type Identifiers (UTI) for the LSItemContentTypes that your importer can handle----------------------------------------------------------------------------- *//* -----------------------------------------------------------------------------Step 2 Implement the GetMetadataForFile functionImplement the GetMetadataForFile function below to scrape the relevantmetadata from your document and return it as a CFDictionary using standard keys(defined in MDItem.h) whenever possible.----------------------------------------------------------------------------- *//* -----------------------------------------------------------------------------Step 3 (optional) If you have defined new attributes, update the schema.xml fileEdit the schema.xml file to include the metadata keys that your importer returns.Add them to the <allattrs> and <displayattrs> elements.Add any custom types that your importer requires to the <attributes> element<attribute name="com_mycompany_metadatakey" type="CFString" multivalued="true"/>----------------------------------------------------------------------------- */CFMutableStringRef text;int _extract_callback(struct chmFile *h, struct chmUnitInfo *ui, void *context){ if (ui->length != 0) { LONGINT64 len, remain=ui->length; LONGUINT64 offset = 0; if(strlen(ui->path)!=0){ if(!strstr(ui->path,".htm")) return CHM_ENUMERATOR_CONTINUE; } else return CHM_ENUMERATOR_CONTINUE; Boolean skip=FALSE, space=FALSE; int i; int j; unsigned char newbuffer[32769]; while (remain != 0) { unsigned char buffer[32768]; len = chm_retrieve_object(h, ui, buffer, offset, 32768); j=0; if (len > 0) { char *c; for(c=buffer;*c;c++) { switch(*c){ case '<': skip=TRUE; break; case '>': skip=FALSE; break; case 0xd: case 0xa: if(!space){newbuffer[j++]= ' ';space=TRUE;};break; case '&': if(!skip){ i=1; while(*(c+i)){ if(*(c+i)==' ') break; else if(*(c+i)==';'){ c+=i; break; } i++; } }; break; case ' ': if(!space && !skip){ newbuffer[j++]= *c; space=TRUE;} break; default: if(!skip && ((*c>='A' && *c<='Z') || (*c>='a' && *c<='z')) ){ newbuffer[j++]= *c; if(space) space=FALSE; }; break; } } newbuffer[j++]='\0'; CFStringAppendCString(text,newbuffer,kCFStringEncodingMacRoman); offset += len; remain -= len; } else break; } } return CHM_ENUMERATOR_CONTINUE;}/* -----------------------------------------------------------------------------Get metadata attributes from fileThis function's job is to extract useful information your file format supportsand return it as a dictionary----------------------------------------------------------------------------- */Boolean GetMetadataForFile(void* thisInterface, CFMutableDictionaryRef attributes, CFStringRef contentTypeUTI, CFStringRef pathToFile){ /* Pull any available metadata from the file at the specified path */ /* Return the attribute keys and attribute values in the dict */ /* Return TRUE if successful, FALSE if there was no data provided */ char buffer[CFStringGetLength(pathToFile)+1]; CFStringGetCString(pathToFile,buffer,CFStringGetLength(pathToFile)+1,kCFStringEncodingMacRoman); struct chmFile *h; h = chm_open(buffer); if (h == NULL) return FALSE; text=CFStringCreateMutable(0,kCFStringEncodingMacRoman); if (! chm_enumerate(h,CHM_ENUMERATE_ALL,_extract_callback,NULL)) return FALSE; chm_close(h); CFDictionaryAddValue(attributes, kMDItemTextContent, text); CFRelease(text); return TRUE;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -