-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextCompiler.h
138 lines (119 loc) · 2.68 KB
/
TextCompiler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#ifndef __TEXTCOMPILER_H__
#define __TEXTCOMPILER_H__
namespace TextCompiler
{
// Write an indirect value
template <typename type> inline void WriteValue(type const * const ptr, idFile *out, bool byteSwap=false)
{
if(out != NULL)
{
if(!byteSwap)
{
out->Write(ptr, sizeof(type));
}
else
{
byte const * const p = (byte *)ptr;
for(int i=sizeof(type)-1;i>=0;i--)
{
out->Write(&(p[i]), 1);
}
}
}
}
// Write a direct value
template <typename type> inline void WriteValue(type const val, idFile *out, bool byteSwap=false)
{
if(out != NULL)
{
if(!byteSwap)
{
out->Write(&val, sizeof(type));
}
else
{
byte const * const p = (byte *)&val;
for(int i=sizeof(type)-1;i>=0;i--)
{
out->Write(&(p[i]), 1);
}
}
}
}
template <typename type> inline type ReadValue(idFile *in)
{
type ret;
in->Read(&ret, sizeof(type));
return ret;
}
// specialization write for idStr's
template <> inline void WriteValue(idStr const * const ptr, idFile *out, bool byteSwap)
{
if(out != NULL)
{
// if less than 32, then we don't need a trailing null
// this is because 5 bits can be used to represent string length in the token header
// zero length strings end up needing to be null terminated
if((ptr->Length() < 32) && (ptr->Length() != 0))
{
out->Write(ptr->c_str(), ptr->Length());
}
else
{
out->Write(ptr->c_str(), ptr->Length()+1);
}
}
}
// specialization read for idStr's
template <> inline idStr ReadValue(idFile *in)
{
char c;
idStr str;
in->Read(&c, 1);
while(c != '\0')
{
str.Append(c);
in->Read(&c, 1);
}
str.Append(c);
return str;
}
template <typename type> inline void WriteArray(type const * const ptr, unsigned int count, idFile *out)
{
WriteValue<unsigned int>(&count, out);
for(unsigned int i=0;i<count; i++)
{
WriteValue<type>(&(ptr[i]), out);
}
}
template <typename type> inline type *ReadArray(idFile *in, unsigned int *count=NULL)
{
unsigned int len;
len = ReadValue<unsigned int>(in);
type *buffer = (type *)malloc(len*sizeof(type));
type *ptr = buffer;
for(unsigned int i=0;i<len;i++)
{
*ptr = ReadValue<type>(in);
ptr++;
}
if(count != NULL)
*count = len;
return buffer;
}
template <typename type> inline void WriteIdList(idList<type> const * const ptr, idFile *out)
{
WriteArray<type>(ptr->Ptr(), ptr->Num(), out);
}
template <typename type> inline idList<type> ReadIdList(idFile *in)
{
idList<type> ret;
ret.Clear();
unsigned int count;
idAutoPtr<type> array(ReadArray<type>(in, &count));
for(unsigned int i=0;i<count;i++)
ret.Append(array[i]);
return ret;
}
};
#endif