123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- //
- // XCForwarder.m
- // SAGESSE
- //
- // Created by SAGESSE on 2019/4/30.
- //
- #import <Foundation/Foundation.h>
- /// Declare the initialization method forward.
- @interface NSObject (XCForwarder)
- +(void*)XCForwarderInit;
- @end
- /// Init the Forwarder.
- static void* forwarder = nil;
- __attribute__((constructor)) void XCForwarderInit() {
-
- // printf("%s\n", __func__);
- if ([NSObject respondsToSelector:@selector(XCForwarderInit)]) {
- forwarder = [NSObject XCForwarderInit];
- }
- }
- /// Load the file from the Forwarder.
- unsigned char* XCForwarderLoadData(const char* name, const char* mode, size_t* osize) {
-
- // printf("%s %s - %s\n", __func__, name, mode);
- if (forwarder != nil) {
- return ((__typeof(&XCForwarderLoadData))forwarder)(name, mode, osize);
- }
-
- FILE* fp = fopen(name, mode);
-
- if (fp == nil) {
- *osize = 0;
- return nil;
- }
-
- fseek(fp,0,SEEK_END);
- ssize_t size = ftell(fp);
- fseek(fp,0,SEEK_SET);
-
- unsigned char* buffer = malloc(size + 1);
- size_t readed = fread(buffer, 1, size, fp);
-
- if (readed >= 0) {
- buffer[readed] = '\0';
- }
-
- *osize = readed;
- fclose(fp);
- return buffer;
- }
|