Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -20,10 +20,11 @@ OFData.m \ OFData+CryptographicHashing.m \ OFData+MessagePackParsing.m \ OFDate.m \ OFDictionary.m \ + OFEmbeddedURIHandler.m \ OFEnumerator.m \ OFFileManager.m \ OFGZIPStream.m \ OFHMAC.m \ OFINICategory.m \ @@ -184,11 +185,10 @@ OFBitSetCharacterSet.m \ OFBytesValue.m \ OFCRC16.m \ OFCRC32.m \ OFCountedMapTableSet.m \ - OFEmbeddedURIHandler.m \ OFHuffmanTree.m \ OFINIFileSettings.m \ OFInvertedCharacterSet.m \ OFLHADecompressingStream.m \ OFMapTableDictionary.m \ Index: src/OFEmbeddedURIHandler.h ================================================================== --- src/OFEmbeddedURIHandler.h +++ src/OFEmbeddedURIHandler.h @@ -17,7 +17,26 @@ OF_ASSUME_NONNULL_BEGIN @interface OFEmbeddedURIHandler: OFURIHandler @end + +#ifdef __cplusplus +extern "C" { +#endif +/** + * @brief Register a file for the `embedded:` URI scheme. + * + * Usually, you should not use the directly, but rather generate a source file + * for a file to be embedded using the `objfw-embed` tool. + * + * @param path The path to the file under the `embedded:` scheme + * @param bytes The raw bytes for the file + * @param size The size of the file + */ +extern void OFRegisterEmbeddedFile(OFString *path, const uint8_t *bytes, + size_t size); +#ifdef __cplusplus +} +#endif OF_ASSUME_NONNULL_END Index: src/OFEmbeddedURIHandler.m ================================================================== --- src/OFEmbeddedURIHandler.m +++ src/OFEmbeddedURIHandler.m @@ -30,11 +30,11 @@ # import "OFOnce.h" # import "OFPlainMutex.h" #endif struct EmbeddedFile { - const char *name; + OFString *path; const uint8_t *bytes; size_t size; } *embeddedFiles = NULL; size_t numEmbeddedFiles = 0; #ifdef OF_HAVE_THREADS @@ -46,11 +46,11 @@ OFEnsure(OFPlainMutexNew(&mutex) == 0); } #endif void -OFRegisterEmbeddedFile(const char *name, const uint8_t *bytes, size_t size) +OFRegisterEmbeddedFile(OFString *path, const uint8_t *bytes, size_t size) { #ifdef OF_HAVE_THREADS static OFOnceControl onceControl = OFOnceControlInitValue; OFOnce(&onceControl, init); @@ -59,11 +59,11 @@ embeddedFiles = realloc(embeddedFiles, sizeof(*embeddedFiles) * (numEmbeddedFiles + 1)); OFEnsure(embeddedFiles != NULL); - embeddedFiles[numEmbeddedFiles].name = name; + embeddedFiles[numEmbeddedFiles].path = [path retain]; embeddedFiles[numEmbeddedFiles].bytes = bytes; embeddedFiles[numEmbeddedFiles].size = size; numEmbeddedFiles++; #ifdef OF_HAVE_THREADS @@ -72,11 +72,11 @@ } @implementation OFEmbeddedURIHandler - (OFStream *)openItemAtURI: (OFURI *)URI mode: (OFString *)mode { - const char *path; + OFString *path; if (![URI.scheme isEqual: @"embedded"] || URI.host.length > 0 || URI.port != nil || URI.user != nil || URI.password != nil || URI.query != nil || URI.fragment != nil) @throw [OFInvalidArgumentException exception]; @@ -84,20 +84,20 @@ if (![mode isEqual: @"r"]) @throw [OFOpenItemFailedException exceptionWithURI: URI mode: mode errNo: EROFS]; - if ((path = URI.path.UTF8String) == NULL) { + if ((path = URI.path) == nil) { @throw [OFInvalidArgumentException exception]; } #ifdef OF_HAVE_THREADS OFEnsure(OFPlainMutexLock(&mutex) == 0); @try { #endif for (size_t i = 0; i < numEmbeddedFiles; i++) { - if (strcmp(embeddedFiles[i].name, path) != 0) + if (![embeddedFiles[i].path isEqual: path]) continue; return [OFMemoryStream streamWithMemoryAddress: (void *) embeddedFiles[i].bytes Index: utils/objfw-embed ================================================================== --- utils/objfw-embed +++ utils/objfw-embed @@ -8,11 +8,15 @@ cat < #include -extern void OFRegisterEmbeddedFile(const char *, const uint8_t *, size_t); +#ifdef OF_COMPILING_OBJFW +# import "OFEmbeddedURIHandler.h" +#else +# import +#endif static const uint8_t bytes[] = { EOF od -vtx1 $1 | sed -e '/^[^ ][^ ]*$/d;s/ */ /g' -e 's/ $//g;s/^[^ ][^ ]* //' -e 's/ /, 0x/g' -e 's/^/ 0x/' -e 's/$/,/' cat <