Subversion Repositories spk

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 cycrow 1
#include "../../Base Engine/Common/String.h"
2
#include "../../Base Engine/Common/StringList.h"
3
 
4
 
5
String ParseBlock ( FILE *id, String *str, CStringList *strings )
6
{
7
	strings->Clear();
8
 
9
	String rest;
10
	String line = *str;
11
	int depth = -1;
12
	while ( !feof(id) )
13
	{
14
		if ( (line.Left(2) == "//") || (line.Empty()) )
15
		{
16
			line.GetEndOfLine ( id, NULL, false );
17
			continue;
18
		}
19
 
20
		line = line.GetToken ( "//", 1, 1 );
21
		line.RemoveChar ( 9 );
22
 
23
		if ( line.IsIn ( "{" ) )
24
			++depth;
25
		if ( line.IsIn ("}") )
26
		{
27
			if ( !depth )
28
			{
29
				rest += line.GetToken ( "}", 1, 1 );
30
				break;
31
			}
32
			else
33
			{
34
				rest += line.GetToken ( "}", 1, 1 );
35
				rest += "};";
36
				line = line.GetToken ( "}", 2 );
37
				--depth;
38
 
39
				if ( !line.Empty() )
40
				{
41
					line.GetEndOfLine ( id, NULL, false );
42
					continue;
43
				}
44
			}
45
		}
46
 
47
		rest += line;
48
		line.GetEndOfLine ( id, NULL, false );
49
	}
50
 
51
	String cmd = rest.GetToken ( "{", 1, 1 ).RemoveSpaces();
52
	rest = rest.GetToken ( "{", 2 );
53
 
54
	// parse strings
55
	String done;
56
	while ( rest.IsIn("\"") )
57
	{
58
		done += rest.GetToken ( "\"", 1, 1 );
59
		String s = rest.GetToken ( "\"", 2, 2 );
60
 
61
		if ( s.Empty() )
62
			break;
63
 
64
		strings->PushBack ( s );
65
		done += "\"";
66
		done += "$STRING";
67
		done += (long)strings->Count();
68
		done += "\"";
69
 
70
		rest = rest.GetToken ( "\"", 3 );
71
	}
72
 
73
	if ( !rest.Empty() )
74
		done += rest;
75
	done.RemoveChar ( ' ' );
76
 
77
	*str = done;
78
	return cmd;
79
}
80
 
81
String ReplaceString ( String s, CStringList *strings )
82
{
83
	int num = 1;
84
	for ( SStringList *it = strings->Head(); it; it = it->next, num++ )
85
	{
86
		String str = String("\"$STRING") + (long)num + "\"";
87
		s = s.FindReplace ( str, it->str );
88
	}
89
 
90
	return s;
91
}
92
 
93
 
94
int main ()
95
{
96
	FILE *id = fopen ( "custommenu.txt", "r+" );
97
	if ( id )
98
	{
99
		String section;
100
		while ( !feof ( id ) )
101
		{
102
			String line;
103
			line.GetEndOfLine ( id, NULL, false );
104
 
105
			if ( line.Empty() )
106
				continue;
107
			if ( line.Left(2) == "//" )
108
				continue;
109
 
110
			// do the brack
111
			if ( line.IsIn ( "{" ) )
112
			{
113
				// parse the block
114
				CStringList strings;
115
				String cmd = ParseBlock ( id, &line, &strings );
116
 
117
				printf ( "Cmd (%s) Rest=%s\n", cmd.c_str(), line.c_str() );
118
 
119
				// break the commands
120
				int num = 0;
121
				String *str = line.SplitToken ( ';', &num );
122
				for ( int i = 0; i < num; i++ )
123
				{
124
					String c = str[i].GetToken ( ":", 1, 1 ), r;
125
					if ( c.Empty() )
126
						continue;
127
 
128
					if ( c.IsIn ("{") )
129
					{
130
						if ( c.IsIn ("}") )
131
						{
132
							r = String("{") + c.GetToken ( "{", 2 ).GetToken ( "}", 1, 1 ) + "}";
133
							c = c.GetToken ( "{", 1, 1 );
134
						}
135
						else
136
						{
137
							section = c.GetToken ( "{", 1, 1 );
138
							c = c.GetToken ( "{", 2 );
139
							r = str[i].GetToken ( ":", 2 );
140
						}
141
					}
142
					else if ( c.IsIn ( "}" ) )
143
					{
144
						section = "";
145
						continue;
146
					}
147
					else
148
					{
149
						r = str[i].GetToken ( ":", 2 );
150
						if ( r.IsIn ( "}" ) )
151
						{
152
							r = r.GetToken ( "}", 1, 1 );
153
							section = "";
154
						}
155
					}
156
 
157
					if ( r.IsIn("\"") )
158
						r = ReplaceString ( r, &strings );
159
 
160
					if ( section.Empty() )
161
						printf ( "Cmd: %s, Rest: %s\n", c.c_str(), r.c_str() );
162
					else
163
						printf ( "(%s) Cmd: %s, Rest: %s\n", section.c_str(), c.c_str(), r.c_str() );
164
				}
165
			}
166
		}
167
	}
168
	char pause;
169
	scanf ( "%c", &pause );
170
 
171
	return 0;
172
}