MMap.h revision e8321394237fa04e1a3f066d5b96025f48c36dfc
1//
2// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7#ifndef _MMAP_INCLUDED_
8#define _MMAP_INCLUDED_
9
10//
11// Encapsulate memory mapped files
12//
13
14class TMMap {
15public:
16    TMMap(const char* fileName) :
17        fSize(-1), // -1 is the error value returned by GetFileSize()
18        fp(NULL),
19        fBuff(0)   // 0 is the error value returned by MapViewOfFile()
20    {
21        if ((fp = fopen(fileName, "r")) == NULL)
22            return;
23        char c = getc(fp);
24        fSize = 0;
25        while (c != EOF) {
26            fSize++;
27            c = getc(fp);
28        }
29        if (c == EOF)
30            fSize++;
31        rewind(fp);
32        fBuff = (char*)malloc(sizeof(char) * fSize);
33        int count = 0;
34        c = getc(fp);
35        while (c != EOF) {
36            fBuff[count++] = c;
37            c = getc(fp);
38        }
39        fBuff[count++] = c;
40    }
41
42    char* getData() { return fBuff; }
43    int   getSize() { return fSize; }
44
45    ~TMMap() {
46        if (fp != NULL)
47            fclose(fp);
48    }
49
50private:
51    int             fSize;      // size of file to map in
52    FILE *fp;
53    char*           fBuff;      // the actual data;
54};
55
56#endif // _MMAP_INCLUDED_
57