180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2010 Google Inc.
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include <Foundation/Foundation.h>
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkOSFile.h"
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkString.h"
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querustruct SkFILE {
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    NSData* fData;
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    size_t  fOffset;
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    size_t  fLength;
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSkFILE* sk_fopen(const char cpath[], SkFILE_Flags flags) {
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (flags & kWrite_SkFILE_Flag) {
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return NULL;
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkString cname, csuffix;
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const char* start = strrchr(cpath, '/');
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (NULL == start) {
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        start = cpath;
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    } else {
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        start += 1;
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    const char* stop = strrchr(cpath, '.');
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (NULL == stop) {
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return NULL;
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    } else {
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        stop += 1;
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    cname.set(start, stop - start - 1);
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    csuffix.set(stop);
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    NSBundle* bundle = [NSBundle mainBundle];
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    NSString* name = [NSString stringWithUTF8String:cname.c_str()];
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    NSString* suffix = [NSString stringWithUTF8String:csuffix.c_str()];
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    NSString* path = [bundle pathForResource:name ofType:suffix];
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    NSData* data = [NSData dataWithContentsOfMappedFile:path];
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (data) {
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        [data retain];
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkFILE* rec = new SkFILE;
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        rec->fData = data;
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        rec->fOffset = 0;
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        rec->fLength = [data length];
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return reinterpret_cast<SkFILE*>(rec);
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return NULL;
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querusize_t sk_fgetsize(SkFILE* rec) {
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkASSERT(rec);
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return rec->fLength;
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool sk_frewind(SkFILE* rec) {
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkASSERT(rec);
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    rec->fOffset = 0;
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return true;
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querusize_t sk_fread(void* buffer, size_t byteCount, SkFILE* rec) {
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (NULL == buffer) {
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return rec->fLength;
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    } else {
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        size_t remaining = rec->fLength - rec->fOffset;
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (byteCount > remaining) {
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            byteCount = remaining;
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        memcpy(buffer, (char*)[rec->fData bytes] + rec->fOffset, byteCount);
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        rec->fOffset += byteCount;
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkASSERT(rec->fOffset <= rec->fLength);
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return byteCount;
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querusize_t sk_fwrite(const void* buffer, size_t byteCount, SkFILE* f) {
850a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    SkDEBUGFAIL("Not supported yet");
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return 0;
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid sk_fflush(SkFILE* f) {
900a657bbc2c6fc9daf699942e023050536d5ec95fDerek Sollenberger    SkDEBUGFAIL("Not supported yet");
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid sk_fclose(SkFILE* rec) {
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkASSERT(rec);
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    [rec->fData release];
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    delete rec;
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
99