aboutsummaryrefslogtreecommitdiff
path: root/hgehelp/hgestrings.cpp
blob: d4c5d2dfdc80f97136cae04c405c044bb61ce33c (plain) (blame)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
** Haaf's Game Engine 1.7
** Copyright (C) 2003-2007, Relish Games
** hge.relishgames.com
**
** hgeStringTable helper class implementation
*/


#include "../../include/hgestrings.h"
#include <ctype.h>

const char STRHEADERTAG[]="[HGESTRINGTABLE]";
const char STRFORMATERROR[]="String table %s has incorrect format.";


HGE *hgeStringTable::hge=0;


hgeStringTable::hgeStringTable(const char *filename)
{
	int i;
	void *data;
	DWORD size;
	char *desc, *pdesc;
	NamedString *str;
	char str_name[MAXSTRNAMELENGTH];
	char *str_value, *pvalue;
	
	hge=hgeCreate(HGE_VERSION);
	strings=0;

	// load string table file
	data=hge->Resource_Load(filename, &size);
	if(!data) return;

	desc = new char[size+1];
	memcpy(desc,data,size);
	desc[size]=0;
	hge->Resource_Free(data);

	// check header
	if(memcmp(desc, STRHEADERTAG, sizeof(STRHEADERTAG)-1))
	{
		hge->System_Log(STRFORMATERROR, filename);
		delete[] desc;	
		return;
	}

	pdesc=desc+sizeof(STRHEADERTAG);
	str_value=new char[8192];

	for(;;)
	{
		// skip whitespaces
		while(isspace(*pdesc)) pdesc++;
		if(!*pdesc) break;

		// skip comments
		if(*pdesc==';')
		{
			while(*pdesc && *pdesc != '\n') pdesc++;
			pdesc++;
			continue;
		}

		// get string name -> str_name
		i=0;
		while(pdesc[i] && pdesc[i]!='=' && !isspace(pdesc[i]) && i<MAXSTRNAMELENGTH)
		{
			str_name[i]=pdesc[i];
			i++;
		}
		str_name[i]=0;
		pdesc+=i;

		// skip string name overflow characters
		while(*pdesc && *pdesc!='=' && !isspace(*pdesc)) pdesc++;
		if(!*pdesc) break;

		// skip whitespaces to '='
		while(isspace(*pdesc)) pdesc++;
		if(*pdesc!='=')	{ hge->System_Log(STRFORMATERROR, filename); break; }
		pdesc++;

		// skip whitespaces to '"'
		while(isspace(*pdesc)) pdesc++;
		if(*pdesc!='"')	{ hge->System_Log(STRFORMATERROR, filename); break;	}
		pdesc++;

		// parse string value till the closing '"' -> str_value
		// consider: \", \n, \\, LF, CR, whitespaces at line begin/end
		pvalue=str_value;

		while(*pdesc && *pdesc!='"')
		{
			if(*pdesc=='\n' || *pdesc=='\r')
			{
				while(isspace(*pdesc)) pdesc++;

				pvalue--;
				while(pvalue>=str_value && isspace(*pvalue)) pvalue--;
				pvalue++; *pvalue=' '; pvalue++;

				continue;
			}

			if(*pdesc=='\\')
			{
				pdesc++;
				if(!*pdesc) continue;
				if(*pdesc=='n') *pvalue='\n';
				else *pvalue=*pdesc;
				pvalue++;
				pdesc++;
				continue;
			}

			*pvalue=*pdesc; pvalue++;
			pdesc++;
		}

		*pvalue=0;

		// add the parsed string to the list
		str=new NamedString;
		strcpy(str->name, str_name);
		str->string=new char[strlen(str_value)+1];
		strcpy(str->string, str_value);
		str->next=strings;
		strings=str;

		if(!*pdesc) break;
		pdesc++;
	}

	delete[] str_value;
	delete[] desc;
}

hgeStringTable::~hgeStringTable()
{
	NamedString *str, *strnext;

	str=strings;
	while(str)
	{
		strnext=str->next;
		delete[] str->string;
		delete str;
		str=strnext;
	}

	hge->Release();
}

char *hgeStringTable::GetString(const char *name)
{
	NamedString *str=strings;

	while(str)
	{
		if(!strcmp(name, str->name)) return str->string;
		str=str->next;
	}

	return 0;
}