📄 markfile.cpp
字号:
/*____________________________________________________________________________*\
*
Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*____________________________________________________________________________*|
*
* $Source: /cvsroot/pi3web/Pi3Web_200/Source/Parse/MarkFile.cpp,v $
* $Date: 2003/05/13 18:42:08 $
*
Description:
\*____________________________________________________________________________*/
//$SourceTop:$
#include <assert.h>
#include <ctype.h>
#include "PICompat.h"
#include "MarkFile.h"
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
bool MarkupFile::PutCh(char c)
{
if ( iLen<BUF_SIZE )
{
szBuf[iLen++]=c;
return true;
}
else
return Overflow();
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int MarkupFile::GetCh2()
{
if ( ppPtr && ppPtr<pePtr )
{ char ch=*ppPtr; ppPtr++; return ch; }
else
{
unsigned long ulSize;
ppPtr=(char *)pReader->Read( &ulSize );
if ( ppPtr )
{
assert( ulSize );
pePtr=&( ppPtr[ulSize] );
return GetCh2();
}
else
{ return PI_EOF; };
};
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Track line and position in line.
\*____________________________________________________________________________*/
int MarkupFile::GetCh1()
{
lX++;
return GetCh2();
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Could cause a stack overflow if its not inline.
\*____________________________________________________________________________*/
int MarkupFile::GetCh0()
{
char ch=GetCh1();
if ( ch=='\r' ) return GetCh0();
if ( ch=='\n' ) { NL(); };
return ch;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Could cause a stack overflow if its not inline.
\*____________________________________________________________________________*/
int MarkupFile::GetCh()
{
switch( cLastCharacter )
{
case PI_EOF:
{
char cTmp = GetCh0();
if ( cTmp=='\\' )
{
cLastCharacter = '\\';
return GetCh();
};
return cTmp;
};
assert( 0 ); /* --- must not get here --- */
case '\\':
{
char cTmp = GetCh0();
if ( cTmp=='\n' )
{ cLastCharacter = PI_EOF;
return GetCh(); }
else
{ cLastCharacter = cTmp;
return '\\'; };
};
assert( 0 ); /* --- must not get here --- */
default:
{
char cTmp = cLastCharacter;
cLastCharacter = PI_EOF;
return cTmp;
};
assert( 0 ); /* --- must not get here --- */
};
assert( 0 ); /* --- must not get here --- */
return -1;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
MarkupFile::MarkupFile( const char *pFileName, ostream &osErr )
: pReader( PI_NEW( FileReader ) ),
sFileName( pFileName ),
os( osErr ),
lX( -1 ),
lY( -1 ),
iErrors( 0 ),
iWarnings( 0 ),
ppPtr(0),
pePtr(0),
iLen(0),
cLastCharacter( PI_EOF )
{
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
MarkupFile::~MarkupFile()
{
PI_DELETE( pReader );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Emit the current sequence.
\*____________________________________________________________________________*/
void MarkupFile::emit( bool bSkip )
{
/* ---
The current character is a new type of pattern, emit the
old pattern (if any), and reset pointers accordingly
--- */
assert( iLen );
if ( iLen>1 )
{
switch( eInputState )
{
/* --- whitespace at the start of a line --- */
case start_line:
break;
/* --- normal text --- */
case normal:
Text();
break;
/* --- new line --- */
case nl:
break;
/* --- text comment --- */
case comment:
Comment();
break;
/* --- markup --- */
case start_markup:
case markup_c1:
case markup_c2:
case markup:
Markup();
break;
/* --- markdown --- */
case markdown:
Markdown();
break;
/* --- marked up comment --- */
case markup_e1:
case markup_e2:
case markup_comment:
CommentMarkup();
break;
default:
assert(0/* Programming error! */);
};
};
if ( bSkip )
{ iLen=0; }
else
{
*szBuf=szBuf[iLen-1];
iLen=1;
}
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
bool MarkupFile::Init()
{
assert( pReader );
if ( !pReader->Open( sFileName ) )
{
Error() << "Unable to open file '" << sFileName << "'" << endl;
return false;
};
/* --- reset variables --- */
NF();
iLen=0;
eInputState=start_line;
return true;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
A file is either an HTML file or a Configuration file, the flag
bHTML tells which. Differences are:-
- HTML doesn't recognise lines starting with '#' as comment lines
- HTML doesn't attach any significance to new line characters,
configuration files are line oriented.
\*____________________________________________________________________________*/
bool MarkupFile::InternalLex( bool bHTML )
{
if ( !Init() ) return false;
int iQuoteLine = -1; /* line where quotation started */
PIString sQuoteFile; /* file where quotation started */
int iInQuotes = 0; /* indicate that text is quoted */
for(;;)
{
char ch;
if ( bHTML )
{
ch = GetCh0();
ch = PutCh( ch ) ? ch : PI_EOF;
if ( ch==PI_EOF )
{
emit(true);
if ( iInQuotes )
{
Error() << "End of file reading quoted text. Quotation \
started at line " << iQuoteLine << " in file '" << sQuoteFile << "'." << endl;
};
return true;
};
}
else
{
ch = GetCh();
ch = PutCh( ch ) ? ch : PI_EOF;
if ( ch==PI_EOF )
{
emit(true);
if ( iInQuotes )
{
Error() << "End of file reading quoted text. Quotation \
started at line " << iQuoteLine << " in file '" << sQuoteFile << "'." << endl;
};
return true;
}
};
if ( ch=='\n' && !bHTML )
{
emit(true);
eInputState=start_line;
}
else
{
if ( ch=='\n' )
{
assert(iLen);
iLen--;
};
switch( eInputState )
{
case start_line:
if ( ch=='"' && !bHTML )
{
if ( iInQuotes )
{
iInQuotes = 0;
}
else
{
iInQuotes = 1;
iQuoteLine = GetY();
sQuoteFile = GetFileName();
};
}
else if ( !iInQuotes )
{
if ( ch=='<' )
{ emit(true); eInputState=start_markup; }
else if ( !bHTML && ch=='#' )
{ emit(true); eInputState=comment; }
else if ( !isspace(ch) )
{ emit(false); eInputState=normal; };
};
break;
case normal:
if ( ch=='"' && !bHTML )
{
if ( iInQuotes )
{
iInQuotes = 0;
}
else
{
iInQuotes = 1;
iQuoteLine = GetY();
sQuoteFile = GetFileName();
};
}
else if ( !iInQuotes )
{
if ( ch=='<' )
{ emit(true); eInputState=start_markup; }
};
break;
case comment:
break;
case start_markup:
if ( ch=='!' )
{ eInputState=markup_c1; }
else
{
if ( ch=='/' )
{ eInputState=markdown; }
else
{ eInputState=markup; };
};
break;
case markup_c1:
if ( ch=='-' )
{ eInputState=markup_c2; }
else
{ eInputState=markup; };
break;
case markup_c2:
if ( ch=='-' )
{ eInputState=markup_comment; }
else
{ eInputState=markup; };
break;
case markup_comment:
if ( ch=='-' )
{ eInputState=markup_e1; };
break;
case markup_e1:
if ( ch=='-' )
{ eInputState=markup_e2; }
else
{ eInputState=markup_comment; };
break;
case markup_e2:
if ( ch=='>' )
{ emit(true); eInputState=normal; }
else
{ eInputState=markup_comment; };
break;
case markup:
if ( ch=='>' )
{ emit(true); eInputState=normal; };
break;
case markdown:
if ( ch=='>' )
{ emit(true); eInputState=normal; };
break;
default:
assert(0/* programming error */);
};
};
};
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
bool MarkupFile::LexBlocksFile()
{
if ( !Init() ) return false;
for(;;)
{
char ch;
ch = GetCh0();
ch = PutCh( ch ) ? ch : PI_EOF;
if ( ch==PI_EOF )
{ emit(true); return true; };
switch( eInputState )
{
case start_line:
if ( ch=='<' )
{ emit(false); eInputState=start_markup; }
else if ( !isspace(ch) )
{ emit(false); eInputState=normal; };
break;
case normal:
if ( ch=='<' )
{ emit(false); eInputState=start_markup; }
break;
case start_markup:
if ( ch=='!' )
{ eInputState=markup_c1; }
else
{ eInputState=normal; }
break;
case markup_c1:
if ( ch=='-' )
{ eInputState=markup_c2; }
else
{ eInputState=normal; };
break;
case markup_c2:
if ( ch=='-' )
{ eInputState=markup_comment; }
else
{ eInputState=normal; };
break;
case markup_comment:
if ( ch=='-' )
{ eInputState=markup_e1; };
break;
case markup_e1:
if ( ch=='-' )
{ eInputState=markup_e2; }
else
{ eInputState=markup_comment; };
break;
case markup_e2:
if ( ch=='>' )
{ emit(true); eInputState=normal; }
else
{ eInputState=markup_comment; };
break;
default:
assert(0/* programming error */);
};
};
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
ostream &MarkupFile::Error()
{
if ( lY>0 )
{
os << "[" << GetFileName() << ", line " << GetY() << "]";
}
else
{
os << "[no file/line information]";
};
iErrors++;
return os;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
ostream &MarkupFile::Warning()
{
os << "Warning ";
if ( lY>0 )
{
os << GetY() << "," << GetX();
}
else
{
os << "<no file/line information>";
};
os << ": ";
iWarnings++;
return os;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -