Skip to content

Commit 84695e7

Browse files
authored
Merge pull request #140 from moofemp/feature/logic-substring
Add logic_substring
2 parents d6822c6 + f1a8638 commit 84695e7

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//====================== By Holly Liberatore / MoofEMP ======================//
2+
//
3+
// Purpose: Takes a string parameter and returns a substring defined by keyvalues
4+
//
5+
//===========================================================================//
6+
7+
#include "cbase.h"
8+
9+
#define SF_SUBSTRING_START_DISABLED (1 << 0)
10+
11+
class CLogicSubstring : public CLogicalEntity
12+
{
13+
public:
14+
DECLARE_CLASS( CLogicSubstring, CLogicalEntity );
15+
DECLARE_DATADESC();
16+
17+
CLogicSubstring( void ) { }
18+
19+
void InputDisable( inputdata_t &inputData );
20+
void InputEnable( inputdata_t &inputData );
21+
void InputInValue( inputdata_t &inputData );
22+
void InputSetLength( inputdata_t &inputData );
23+
void InputSetStartPos( inputdata_t &inputData );
24+
25+
void Spawn(void);
26+
27+
private:
28+
int m_nLength;
29+
int m_nStartPos;
30+
31+
bool m_bEnabled;
32+
33+
COutputString m_OutValue;
34+
};
35+
36+
LINK_ENTITY_TO_CLASS( logic_substring, CLogicSubstring );
37+
38+
BEGIN_DATADESC( CLogicSubstring )
39+
40+
DEFINE_FIELD( m_bEnabled, FIELD_BOOLEAN ),
41+
42+
DEFINE_KEYFIELD(m_nLength, FIELD_INTEGER, "length" ),
43+
DEFINE_KEYFIELD(m_nStartPos, FIELD_INTEGER, "startPos" ),
44+
45+
DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ),
46+
DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ),
47+
DEFINE_INPUTFUNC( FIELD_STRING, "InValue", InputInValue ),
48+
DEFINE_INPUTFUNC( FIELD_INTEGER, "SetLength", InputSetLength ),
49+
DEFINE_INPUTFUNC( FIELD_INTEGER, "SetStartPos", InputSetStartPos ),
50+
51+
DEFINE_OUTPUT( m_OutValue, "OutValue" ),
52+
53+
END_DATADESC()
54+
55+
//-----------------------------------------------------------------------------
56+
// Purpose: Disable or enable the entity (disabling prevents any input functions from running)
57+
//-----------------------------------------------------------------------------
58+
void CLogicSubstring::InputDisable( inputdata_t &inputData ) { m_bEnabled = false; }
59+
void CLogicSubstring::InputEnable ( inputdata_t &inputData ) { m_bEnabled = true ; }
60+
61+
//-----------------------------------------------------------------------------
62+
// Purpose: Trim substring from input
63+
// Output: Substring
64+
//-----------------------------------------------------------------------------
65+
void CLogicSubstring::InputInValue( inputdata_t &inputData )
66+
{
67+
if( !m_bEnabled ) return;
68+
69+
int inputLength = Q_strlen(inputData.value.String());
70+
int startPosCheck = m_nStartPos < 0 ? inputLength + m_nStartPos : m_nStartPos;
71+
if( startPosCheck < 0 )
72+
{
73+
startPosCheck = 0;
74+
}
75+
int lengthCheck = (m_nLength < 0 || m_nLength > inputLength - startPosCheck ? inputLength - startPosCheck : m_nLength) + 1;
76+
if( lengthCheck < 1 || startPosCheck > inputLength )
77+
{
78+
m_OutValue.Set( MAKE_STRING(""), inputData.pActivator, this );
79+
return;
80+
}
81+
char* strOutValue = (char*)malloc( lengthCheck );
82+
Q_strncpy( strOutValue, inputData.value.String() + startPosCheck, lengthCheck );
83+
m_OutValue.Set( AllocPooledString(strOutValue), inputData.pActivator, this );
84+
free(strOutValue);
85+
}
86+
87+
//-----------------------------------------------------------------------------
88+
// Purpose: Setter methods for keyvalues
89+
//-----------------------------------------------------------------------------
90+
void CLogicSubstring::InputSetLength( inputdata_t &inputData )
91+
{
92+
if( !m_bEnabled ) return;
93+
94+
m_nLength = inputData.value.Int();
95+
}
96+
97+
void CLogicSubstring::InputSetStartPos( inputdata_t &inputData )
98+
{
99+
if( !m_bEnabled ) return;
100+
101+
m_nStartPos = inputData.value.Int();
102+
}
103+
104+
//-----------------------------------------------------------------------------
105+
// Purpose: Respond to spawnflags when entity spawns
106+
//-----------------------------------------------------------------------------
107+
void CLogicSubstring::Spawn( void )
108+
{
109+
m_bEnabled = !HasSpawnFlags( SF_SUBSTRING_START_DISABLED );
110+
}

0 commit comments

Comments
 (0)