Add manual page
[hex-a-hop.git] / packfile.h
blobbffcdbb3add61da2b3f8722badbf34d73ae93c60
1 /*
2 Copyright (C) 2005-2007 Tom Beaumont
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 struct PackFile1
22 class Entry {
23 int len;
24 public:
25 char name[1];
27 Entry* GetNext()
29 return (Entry*)((char*)name + len);
32 void* Data()
34 char* pos = name;
35 while (*pos!=0)
36 pos++;
37 return pos+1;
40 int DataLen()
42 return len - strlen(name) - 1;
46 int numfiles;
47 Entry** e;
48 void* data;
50 PackFile1() : e(0), data(0), numfiles(0)
53 Entry* Find(const char* name)
55 if (numfiles==0) return 0;
56 int a=0, b=numfiles;
57 while (b>a)
59 const int mid = (a+b)>>1;
60 int diff = strcmp(name, e[mid]->name);
61 if (diff==0)
62 return e[mid];
63 else if (diff < 0)
64 b=mid;
65 else
66 a=mid+1;
68 return 0;
71 void Read(FILE* f)
73 if (numfiles || e || data)
74 FATAL("Calling Packfile1::Read when already initialised.");
76 int size;
77 fseek(f, -(int)sizeof(size), SEEK_END);
78 int end_offset = ftell(f);
79 fread(&size, sizeof(size), 1, f);
80 fseek(f, end_offset - size, SEEK_SET);
82 data = malloc(size);
83 char* data_end = (char*)data + size;
84 fread(data, 1, size, f);
86 numfiles=0;
87 Entry* i = (Entry*)data;
88 while ((void*)i < data_end)
90 numfiles++;
91 i = i->GetNext();
94 e = new Entry* [numfiles];
96 i = (Entry*)data;
97 for (int j=0; j<numfiles; j++, i = i->GetNext())
98 e[j] = i;
101 ~PackFile1()
103 free(data);