Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
expander.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19// ---------------------------------------------------------------------------
20// File: expander.cpp
21// Author: Matthew D. Campbell
22// Creation Date: 9/13/2002
23// Description: Key/value pair template expansion class
24// ---------------------------------------------------------------------------
25
26#include "debug.h"
27#include "expander.h"
28
29Expander::Expander( const std::string& leftMarker, const std::string& rightMarker ) :
30 m_left(leftMarker), m_right(rightMarker)
31{
32}
33
34void Expander::addExpansion( const std::string& key, const std::string val )
35{
36 m_expansions[key] = val;
37}
38
39void Expander::clear( void )
40{
41 m_expansions.clear();
42}
43
44void Expander::expand( const std::string& input,
45 std::string& output,
46 bool stripUnknown )
47{
48 output = "";
49 unsigned int pos = input.find(m_left);
50 unsigned int lastpos = input.npos;
51 while (pos != input.npos)
52 {
53 // first, tack on the non-expansion part we just skipped over
54 if (pos > 0)
55 {
56 if (lastpos == input.npos)
57 {
58 // first time
59 output.append(input.substr(0, pos));
60 //DEBUG_LOG(("First time, output='%s'\n", output.c_str()));
61 }
62 else
63 {
64 // done this before
65 std::string sub = input.substr(lastpos, pos-lastpos);
66 //DEBUG_LOG(("*** lastpos = %d, pos=%d, sub='%s'\n", lastpos, pos, sub.c_str()));
67 output.append(sub);
68 //DEBUG_LOG(("output='%s'\n", output.c_str()));
69 }
70 }
71 else
72 {
73 //DEBUG_LOG(("pos == 0\n"));
74 }
75
76 // pos is the first position of a possible expansion
77 //DEBUG_LOG(("Looking for endpos via '%s' in '%s'\n", m_right.c_str(), input.substr(pos+m_left.length()).c_str()));
78 unsigned int endpos = input.find(m_right, pos+m_left.length());
79 //DEBUG_LOG(("substr is %d-%d of '%s'\n", pos, endpos, input.c_str()));
80 if (endpos != input.npos)
81 {
82 // found a complete token - expand it
83 std::string sub = input.substr(pos+m_left.length(), endpos-pos-m_left.length());
84 //DEBUG_LOG(("found token: '%s'\n", sub.c_str()));
85
86 ExpansionMap::iterator it = m_expansions.find(sub);
87 if (it == m_expansions.end())
88 {
89 // unknown key
90 if (stripUnknown)
91 {
92 //output.append("<<CENSORED>>");
93 }
94 else
95 {
96 output.append(input.substr(pos, endpos-pos+m_right.length()));
97 }
98 }
99 else
100 {
101 std::string toExpand = it->second;
102 std::string expanded = "";
103 //DEBUG_LOG(("###### expanding '%s'\n", toExpand.c_str()));
104 expand(toExpand, expanded, stripUnknown);
105 //DEBUG_LOG(("###### expanded '%s'\n", expanded.c_str()));
106 output.append(expanded);
107 }
108 }
109
110 lastpos = endpos+m_right.length();
111
112 pos = input.find(m_left, pos+m_left.length());
113 }
114
115 if (lastpos != input.npos)
116 {
117 // tack on last bit
118 output.append(input.substr(lastpos));
119 }
120 else if (!output.length() && pos == input.npos && lastpos == input.npos)
121 {
122 output = input;
123 }
124}
125
126
void expand(const std::string &input, std::string &output, bool stripUnknown=false)
Definition expander.cpp:44
std::string m_left
Definition expander.h:49
std::string m_right
Definition expander.h:50
ExpansionMap m_expansions
Definition expander.h:48
void clear(void)
Definition expander.cpp:39
Expander(const std::string &leftMarker, const std::string &rightMarker)
Definition expander.cpp:29
void addExpansion(const std::string &key, const std::string val)
Definition expander.cpp:34