📄 _pi3expr.cpp
字号:
if ( iFlagsLen>0 )
{ pszFlagsBuf[iFlagsLen] = '\0'; };
/* --- write in string --- */
int iStringLen = pString->Write( tContext, pszBuffer, iLength );
if ( iStringLen==-1 )
{ return -1; };
if (
iStringLen>=iFieldLength ||
iStringLen>=iLength )
{ return iStringLen; };
assert( iPaddingLen );
#if 0
/*
** Centering disabled, because this code is not right - REDO
**
** NOTE: 'center' has also been removed from the documentation
*/
/* --- align the field correctly --- */
if ( iFlagsLen>0 && !PIUtil_strncmpi( pszFlagsBuf, "center", 6 ) )
{
int iInitialPadding = ( iFieldLength - iStringLen ) / 2;
int iEndPadding = ( iFieldLength - iStringLen ) - iInitialPadding;
if ( iInitialPadding )
{
if ( iInitialPadding<iLength )
{
int iTheLen = iStringLen;
if ( iTheLen>(iLength-iInitialPadding) )
{ iTheLen = (iLength - iInitialPadding ); };
memmove( &(pszBuffer[iInitialPadding]), pszBuffer,
iTheLen );
};
};
const char *pPadding = sPadding;
int iPos;
for( iPos=0; iPos<iInitialPadding; )
{
int iTheLen = iInitialPadding - iPos;
if ( iTheLen>iPaddingLen )
{ iTheLen = iPaddingLen; };
if ( iPos<iLength )
{
if ( iTheLen<(iLength-iPos) )
{ iTheLen = (iLength - iPos ); };
strncpy( &(pszBuffer[iPos]), pPadding, iTheLen );
};
iPos+=iTheLen;
}
pszBuffer = &( pszBuffer[iInitialPadding+iStringLen] );
for( iPos=0; iPos<iEndPadding; )
{
int iTheLen = iEndPadding - iPos;
if ( iTheLen>iPaddingLen )
{ iTheLen = iPaddingLen; };
strncpy( &(pszBuffer[iPos]), pPadding, iTheLen );
iPos+=iTheLen;
}
return iFieldLength;
}
#endif
if ( iFlagsLen>0 && !PIUtil_strncmpi( pszFlagsBuf, "right", 5 ) )
{
int iTmp = iFieldLength - iStringLen;
/* --
Move text to the right
--- */
if ( iTmp<iLength )
{
int iTheLen = iStringLen;
if ( (iLength-iTmp)<iTheLen )
{ iTheLen = (iLength-iTmp); };
memmove( &(pszBuffer[iTmp]), pszBuffer, iTheLen );
};
/* ---
Write in the padding
--- */
const char *pPadding = sPadding;
if ( iTmp>iLength )
{ iTmp = iLength; };
for( int iPos=0; iPos<iTmp; )
{
int iTheLen = iTmp - iPos;
if ( iTheLen>iPaddingLen )
{ iTheLen = iPaddingLen; };
strncpy( &(pszBuffer[iPos]), pPadding, iTheLen );
iPos += iTheLen;
}
return iFieldLength;
}
else /* default is left */
{
/* --- real field size to fill --- */
int iToFill = iFieldLength < iLength ? iFieldLength : iLength;
/* --- real number of spaces to pad --- */
int iToPad = iToFill - iStringLen;
assert( iPaddingLen );
if ( iToPad>0 && (iPaddingLen>0)/* sanity */ )
{
pszBuffer = &( pszBuffer[iStringLen] );
const char *pPadding = sPadding;
for( int iPos=0; iPos<iToPad; )
{
int iTheLen = iToPad - iPos;
if ( iTheLen>iPaddingLen )
{ iTheLen = iPaddingLen; };
strncpy( &(pszBuffer[iPos]), pPadding, iTheLen );
iPos += iTheLen;
};
};
return iFieldLength;
};
};
virtual int Min() { return 2; };
virtual int Max() { return 4; };
/* ---
Optimize expression. Return 1 on success, 0 on error
--- */
virtual int Compile( DblList &lExpressions, PIString & /* sError */ )
{
DblListIterator i( lExpressions );
if ( i.BadIndex() ) { return 0; };
_Pi3Expression *pEString = (_Pi3Expression *)i.Current();
i++;
if ( i.BadIndex() ) { return 0; };
_Pi3Expression *pELength = (_Pi3Expression *)i.Current();
_Pi3Expression *pEFlags;
_Pi3Expression *pEPadding;
i++;
if ( i.BadIndex() )
{ pEFlags = PI_NEW( _Pi3Expression() ); }
else
{
pEFlags = (_Pi3Expression *)i.Current();
i++;
};
if ( i.BadIndex() )
{ pEPadding = PI_NEW( _Pi3Expression() ); }
else
{ pEPadding = (_Pi3Expression *)i.Current(); };
/* --- clear the list --- */
assert( lExpressions.Size()>=Min() );
assert( lExpressions.Size()<=Max() );
lExpressions.Clear();
pString = ExpressionToFunctionArgument( pEString );
pLength = ExpressionToFunctionArgument( pELength );
pFlags = ExpressionToFunctionArgument( pEFlags );
pPadding = ExpressionToFunctionArgument( pEPadding );
if ( !pString || !pLength || !pFlags || !pPadding )
{ return 0; };
if ( pLength->eType==FunctionArgument::STRING )
{
FunctionArgument *pSaveLength = pLength;
pLength = PI_NEW( FunctionArgument( atoi(*(pLength->pString)) ) );
PI_DELETE( pSaveLength );
};
/* --- done --- */
iOK = 1;
return 1;
};
virtual int IsStatic() const
{
assert( pString );
assert( pLength );
assert( pFlags );
assert( pPadding );
return pString->eType==FunctionArgument::STRING &&
pLength->eType==FunctionArgument::NUMBER &&
pFlags->eType==FunctionArgument::STRING &&
pPadding->eType==FunctionArgument::STRING;
};
};
/*____________________________________________________________________________*\
*
Class:
Description:
\*____________________________________________________________________________*/
class IfFunctionComponent : public FunctionComponent
{
private:
FunctionArgument *pBoolean;
FunctionArgument *pOnTrue;
FunctionArgument *pOnFalse;
public:
IfFunctionComponent()
: FunctionComponent(),
pBoolean( 0 ),
pOnTrue( 0 ),
pOnFalse( 0 )
{};
virtual ~IfFunctionComponent()
{
PI_DELETE( pBoolean );
PI_DELETE( pOnTrue );
PI_DELETE( pOnFalse );
};
virtual const char *GetName() const { return "if"; };
/* --- */
virtual void WriteDebugTree( int iIndent ) const
{
for(int i=0; i<iIndent; i++ )
{ cerr << INDENT_PATTERN; };
cerr << "Function:'" << GetName() << "'";
if ( IsStatic() )
{ cerr << "<Static>"; }
else
{ cerr << "<Dynamic>"; }
assert( pBoolean );
pBoolean->WriteDebugTree( iIndent );
cerr << ", ";
assert( pOnTrue );
pOnTrue->WriteDebugTree( iIndent );
cerr << ", ";
assert( pOnFalse );
pOnFalse->WriteDebugTree( iIndent );
};
/* --- */
virtual int Write( Context &tContext, char *pszBuffer, int iLength ) const
{
assert( pBoolean );
assert( pOnTrue );
assert( pOnFalse );
/* --- switch on the length of pBoolean, to write out --- */
switch( pBoolean->Write( tContext, 0, 0 ) )
{
case -1: return -1;
case 0: return pOnFalse->Write( tContext, pszBuffer, iLength );
default: return pOnTrue->Write( tContext, pszBuffer, iLength );
};
};
virtual int Min() { return 2; };
virtual int Max() { return 3; };
/* ---
Optimize expression. Return 1 on success, 0 on error
--- */
virtual int Compile( DblList &lExpressions, PIString & /* sError */ )
{
DblListIterator i( lExpressions );
if ( i.BadIndex() ) { return 0; };
_Pi3Expression *pEBoolean = (_Pi3Expression *)i.Current();
i++;
if ( i.BadIndex() ) { return 0; };
_Pi3Expression *pEOnTrue = (_Pi3Expression *)i.Current();
_Pi3Expression *pEOnFalse = 0;
i++;
if ( i.BadIndex() )
{ pEOnFalse = PI_NEW( _Pi3Expression() ); }
else
{
pEOnFalse = (_Pi3Expression *)i.Current();
i++;
};
/* --- clear the list --- */
assert( lExpressions.Size()>=Min() );
assert( lExpressions.Size()<=Max() );
lExpressions.Clear();
pBoolean = ExpressionToFunctionArgument( pEBoolean );
pOnTrue = ExpressionToFunctionArgument( pEOnTrue );
pOnFalse = ExpressionToFunctionArgument( pEOnFalse );
if ( !pBoolean || !pOnTrue || !pOnFalse )
{ return 0; };
/* --- done --- */
iOK = 1;
return 1;
};
virtual int IsStatic() const
{
assert( pBoolean );
assert( pOnTrue );
assert( pOnFalse );
return pBoolean->eType==FunctionArgument::STRING &&
pOnTrue->eType==FunctionArgument::STRING &&
pOnFalse->eType==FunctionArgument::STRING;
};
};
/*____________________________________________________________________________*\
*
Class:
Description:
\*____________________________________________________________________________*/
class AndFunctionComponent : public FunctionComponent
{
private:
FunctionArgument *pArg1;
FunctionArgument *pArg2;
public:
AndFunctionComponent()
: FunctionComponent(),
pArg1( 0 ),
pArg2( 0 )
{};
virtual ~AndFunctionComponent()
{
PI_DELETE( pArg1 );
PI_DELETE( pArg2 );
};
virtual const char *GetName() const { return "and"; };
/* --- */
virtual void WriteDebugTree( int /* iIndent */ ) const
{
#if 0
for(int i=0; i<iIndent; i++ )
{ cerr << INDENT_PATTERN; };
cerr << "Function:'" << GetName() << "'";
if ( IsStatic() )
{ cerr << "<Static>"; }
else
{ cerr << "<Dynamic>"; }
assert( pBoolean );
pBoolean->WriteDebugTree( iIndent );
cerr << ", ";
assert( pOnTrue );
pOnTrue->WriteDebugTree( iIndent );
cerr << ", ";
assert( pOnFalse );
pOnFalse->WriteDebugTree( iIndent );
#endif
};
/* --- */
virtual int Write( Context &tContext, char *pszBuffer, int iLength ) const
{
assert( pArg1 );
assert( pArg2 );
/* --- switch on pArg1 --- */
switch( pArg1->Write( tContext, 0, 0 ) )
{
case -1: return -1;
case 0: return 0;
default:;
};
/* --- switch on pArg2 --- */
switch( pArg2->Write( tContext, 0, 0 ) )
{
case -1: return -1;
case 0: return 0;
default:;
};
if ( pszBuffer && iLength )
{
int iLen = ( iLength < 4 ) ? iLength : 4;
strncpy( pszBuffer, "true", iLen );
};
return 4;
};
virtual int Min() { return 2; };
virtual int Max() { return 2; };
/* ---
Optimize expression. Return 1 on success, 0 on error
--- */
virtual int Compile( DblList &lExpressions, PIString & /* sError */ )
{
DblListIterator i( lExpressions );
if ( i.BadIndex() ) { return 0; };
_Pi3Expression *pEArg1 = (_Pi3Expression *)i.Current();
i++;
if ( i.BadIndex() ) { return 0; };
_Pi3Expression *pEArg2 = (_Pi3Expression *)i.Current();
i++;
/* --- clear the list --- */
assert( lExpressions.Size()>=Min() );
assert( lExpressions.Size()<=Max() );
lExpressions.Clear();
pArg1 = ExpressionToFunctionArgument( pEArg1 );
pArg2 = ExpressionToFunctionArgument( pEArg2 );
if ( !pArg1 || !pArg2 )
{ return 0; };
/* --- done --- */
iOK = 1;
return 1;
};
virtual int IsStatic() const
{
assert( pArg1 );
assert( pArg2 );
return pArg1->eType==FunctionArgument::STRING &&
pArg2->eType==FunctionArgument::STRING;
};
};
/*____________________________________________________________________________*\
*
Class:
Description:
\*____________________________________________________________________________*/
class OrFunctionComponent : public FunctionComponent
{
private:
FunctionArgument *pArg1;
FunctionArgument *pArg2;
public:
OrFunctionComponent()
: FunctionComponent(),
pArg1( 0 ),
pArg2( 0 )
{};
virtual ~OrFunctionComponent()
{
PI_DELETE( pArg1 );
PI_DELETE( pArg2 );
};
virtual const char *GetName() const { return "and"; };
/* --- */
virtual void WriteDebugTree( int /* iIndent */ ) const
{
#if 0
for(int i=0; i<iIndent; i++ )
{ cerr << INDENT_PATTERN; };
cerr << "Function:'" << GetName() << "'";
if ( IsStatic() )
{ cerr << "<Static>"; }
else
{ cerr << "<Dynamic>"; }
assert( pBoolean );
pBoolean->WriteDebugTree( iIndent );
cerr << ", ";
assert( pOnTrue );
pOnTrue->WriteDebugTree( iIndent );
cerr << ", ";
assert( pOnFalse );
pOnFalse->WriteDebugTree( iIndent );
#endif
};
/* --- */
virtual int Write( Context &tContext, char *pszBuffer, int iLength ) const
{
assert( pArg1 );
assert( pArg2 );
/* --- switch on pArg1 --- */
switch( pArg1->Write( tContext, 0, 0 ) )
{
case -1: return -1;
case 0: break;
default:;
goto or_succeeded;
};
/* --- switch on pArg2 --- */
switch( pArg2->Write( tContext, 0, 0 ) )
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -