1/* 2 * Copyright 2010 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8#include <Foundation/Foundation.h> 9#include "SkOSFile.h" 10#include "SkString.h" 11 12struct SkFILE { 13 NSData* fData; 14 size_t fOffset; 15 size_t fLength; 16}; 17 18SkFILE* sk_fopen(const char cpath[], SkFILE_Flags flags) { 19 if (flags & kWrite_SkFILE_Flag) { 20 return NULL; 21 } 22 23 SkString cname, csuffix; 24 25 const char* start = strrchr(cpath, '/'); 26 if (NULL == start) { 27 start = cpath; 28 } else { 29 start += 1; 30 } 31 const char* stop = strrchr(cpath, '.'); 32 if (NULL == stop) { 33 return NULL; 34 } else { 35 stop += 1; 36 } 37 38 cname.set(start, stop - start - 1); 39 csuffix.set(stop); 40 41 NSBundle* bundle = [NSBundle mainBundle]; 42 NSString* name = [NSString stringWithUTF8String:cname.c_str()]; 43 NSString* suffix = [NSString stringWithUTF8String:csuffix.c_str()]; 44 NSString* path = [bundle pathForResource:name ofType:suffix]; 45 NSData* data = [NSData dataWithContentsOfMappedFile:path]; 46 47 if (data) { 48 [data retain]; 49 SkFILE* rec = new SkFILE; 50 rec->fData = data; 51 rec->fOffset = 0; 52 rec->fLength = [data length]; 53 return reinterpret_cast<SkFILE*>(rec); 54 } 55 return NULL; 56} 57 58size_t sk_fgetsize(SkFILE* rec) { 59 SkASSERT(rec); 60 return rec->fLength; 61} 62 63bool sk_frewind(SkFILE* rec) { 64 SkASSERT(rec); 65 rec->fOffset = 0; 66 return true; 67} 68 69size_t sk_fread(void* buffer, size_t byteCount, SkFILE* rec) { 70 if (NULL == buffer) { 71 return rec->fLength; 72 } else { 73 size_t remaining = rec->fLength - rec->fOffset; 74 if (byteCount > remaining) { 75 byteCount = remaining; 76 } 77 memcpy(buffer, (char*)[rec->fData bytes] + rec->fOffset, byteCount); 78 rec->fOffset += byteCount; 79 SkASSERT(rec->fOffset <= rec->fLength); 80 return byteCount; 81 } 82} 83 84size_t sk_fwrite(const void* buffer, size_t byteCount, SkFILE* f) { 85 SkASSERT(!"Not supported yet"); 86 return 0; 87} 88 89void sk_fflush(SkFILE* f) { 90 SkASSERT(!"Not supported yet"); 91} 92 93void sk_fclose(SkFILE* rec) { 94 SkASSERT(rec); 95 [rec->fData release]; 96 delete rec; 97} 98 99