simplefileparsing.c
来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 1,457 行 · 第 1/3 页
C
1,457 行
UINT32 Index;
SkipWhiteSpace (&mGlobals.SourceFile);
if (EndOfFile (&mGlobals.SourceFile)) {
return FALSE;
}
Index = 0;
while (!EndOfFile (&mGlobals.SourceFile) && (Index < Len)) {
if (IsWhiteSpace (&mGlobals.SourceFile)) {
if (Index > 0) {
Str[Index] = 0;
return TRUE;
}
return FALSE;
} else {
Str[Index] = mGlobals.SourceFile.FileBufferPtr[0];
mGlobals.SourceFile.FileBufferPtr++;
Index++;
}
}
return FALSE;
}
BOOLEAN
SFPSkipToToken (
T_CHAR *Str
)
{
unsigned int Len;
T_CHAR *SavePos;
Len = t_strlen (Str);
SavePos = mGlobals.SourceFile.FileBufferPtr;
SkipWhiteSpace (&mGlobals.SourceFile);
while (!EndOfFile (&mGlobals.SourceFile)) {
if (t_strncmp (Str, mGlobals.SourceFile.FileBufferPtr, Len) == 0) {
mGlobals.SourceFile.FileBufferPtr += Len;
return TRUE;
}
mGlobals.SourceFile.FileBufferPtr++;
SkipWhiteSpace (&mGlobals.SourceFile);
}
mGlobals.SourceFile.FileBufferPtr = SavePos;
return FALSE;
}
BOOLEAN
SFPGetNumber (
unsigned int *Value
)
/*++
Routine Description:
Check the token at the current file position for a numeric value.
May be either decimal or hex.
Arguments:
Value - pointer where to store the value
Returns:
FALSE - current token is not a number
TRUE - current token is a number
--*/
{
SkipWhiteSpace (&mGlobals.SourceFile);
if (EndOfFile (&mGlobals.SourceFile)) {
return FALSE;
}
if (isdigit (mGlobals.SourceFile.FileBufferPtr[0])) {
//
// Check for hex value
//
if ((mGlobals.SourceFile.FileBufferPtr[0] == T_CHAR_0) && (mGlobals.SourceFile.FileBufferPtr[1] == T_CHAR_LC_X)) {
if (!isxdigit (mGlobals.SourceFile.FileBufferPtr[2])) {
return FALSE;
}
mGlobals.SourceFile.FileBufferPtr += 2;
sscanf (mGlobals.SourceFile.FileBufferPtr, "%x", Value);
while (isxdigit (mGlobals.SourceFile.FileBufferPtr[0])) {
mGlobals.SourceFile.FileBufferPtr++;
}
return TRUE;
} else {
*Value = atoi (mGlobals.SourceFile.FileBufferPtr);
while (isdigit (mGlobals.SourceFile.FileBufferPtr[0])) {
mGlobals.SourceFile.FileBufferPtr++;
}
return TRUE;
}
} else {
return FALSE;
}
}
STATUS
SFPCloseFile (
VOID
)
/*++
Routine Description:
Close the file being parsed.
Arguments:
None.
Returns:
STATUS_SUCCESS - the file was closed
STATUS_ERROR - no file is currently open
--*/
{
if (mGlobals.SourceFile.FileBuffer != NULL) {
free (mGlobals.SourceFile.FileBuffer);
memset (&mGlobals.SourceFile, 0, sizeof (mGlobals.SourceFile));
return STATUS_SUCCESS;
}
return STATUS_ERROR;
}
static
STATUS
ProcessIncludeFile (
SOURCE_FILE *SourceFile,
SOURCE_FILE *ParentSourceFile
)
/*++
Routine Description:
Given a source file, open the file and parse it
Arguments:
SourceFile - name of file to parse
ParentSourceFile - for error reporting purposes, the file that #included SourceFile.
Returns:
Standard status.
--*/
{
static unsigned int NestDepth = 0;
char FoundFileName[MAX_PATH];
STATUS Status;
Status = STATUS_SUCCESS;
NestDepth++;
//
// Print the file being processed. Indent so you can tell the include nesting
// depth.
//
if (mGlobals.VerboseFile) {
fprintf (stdout, "%*cProcessing file '%s'\n", NestDepth * 2, ' ', SourceFile->FileName);
fprintf (stdout, "Parent source file = '%s'\n", ParentSourceFile);
}
//
// Make sure we didn't exceed our maximum nesting depth
//
if (NestDepth > MAX_NEST_DEPTH) {
Error (NULL, 0, 0, SourceFile->FileName, "max nesting depth (%d) exceeded", NestDepth);
Status = STATUS_ERROR;
goto Finish;
}
//
// Try to open the file locally, and if that fails try along our include paths.
//
strcpy (FoundFileName, SourceFile->FileName);
if ((SourceFile->Fptr = fopen (FoundFileName, "rb")) == NULL) {
return STATUS_ERROR;
}
//
// Process the file found
//
ProcessFile (SourceFile);
Finish:
//
// Close open files and return status
//
if (SourceFile->Fptr != NULL) {
fclose (SourceFile->Fptr);
SourceFile->Fptr = NULL;
}
return Status;
}
static
STATUS
ProcessFile (
SOURCE_FILE *SourceFile
)
/*++
Routine Description:
Given a source file that's been opened, read the contents into an internal
buffer and pre-process it to remove comments.
Arguments:
SourceFile - structure containing info on the file to process
Returns:
Standard status.
--*/
{
//
// Get the file size, and then read the entire thing into memory.
// Allocate extra space for a terminator character.
//
fseek (SourceFile->Fptr, 0, SEEK_END);
SourceFile->FileSize = ftell (SourceFile->Fptr);
if (mGlobals.VerboseFile) {
printf ("FileSize = %d (0x%X)\n", SourceFile->FileSize, SourceFile->FileSize);
}
fseek (SourceFile->Fptr, 0, SEEK_SET);
SourceFile->FileBuffer = (T_CHAR *) malloc (SourceFile->FileSize + sizeof (T_CHAR));
if (SourceFile->FileBuffer == NULL) {
Error (NULL, 0, 0, "memory allocation failure", NULL);
return STATUS_ERROR;
}
fread ((void *) SourceFile->FileBuffer, SourceFile->FileSize, 1, SourceFile->Fptr);
SourceFile->FileBuffer[(SourceFile->FileSize / sizeof (T_CHAR))] = T_CHAR_NULL;
//
// Pre-process the file to replace comments with spaces
//
PreprocessFile (SourceFile);
SourceFile->LineNum = 1;
return STATUS_SUCCESS;
}
static
void
PreprocessFile (
SOURCE_FILE *SourceFile
)
/*++
Routine Description:
Preprocess a file to replace all carriage returns with NULLs so
we can print lines (as part of error messages) from the file to the screen.
Arguments:
SourceFile - structure that we use to keep track of an input file.
Returns:
Nothing.
--*/
{
BOOLEAN InComment;
BOOLEAN SlashSlashComment;
int LineNum;
RewindFile (SourceFile);
InComment = FALSE;
SlashSlashComment = FALSE;
while (!EndOfFile (SourceFile)) {
//
// If a line-feed, then no longer in a comment if we're in a // comment
//
if (SourceFile->FileBufferPtr[0] == T_CHAR_LF) {
SourceFile->FileBufferPtr++;
SourceFile->LineNum++;
if (InComment && SlashSlashComment) {
InComment = FALSE;
SlashSlashComment = FALSE;
}
} else if (SourceFile->FileBufferPtr[0] == T_CHAR_CR) {
//
// Replace all carriage returns with a NULL so we can print stuff
//
SourceFile->FileBufferPtr[0] = 0;
SourceFile->FileBufferPtr++;
//
// Check for */ comment end
//
} else if (InComment &&
!SlashSlashComment &&
(SourceFile->FileBufferPtr[0] == T_CHAR_STAR) &&
(SourceFile->FileBufferPtr[1] == T_CHAR_SLASH)
) {
SourceFile->FileBufferPtr[0] = T_CHAR_SPACE;
SourceFile->FileBufferPtr++;
SourceFile->FileBufferPtr[0] = T_CHAR_SPACE;
SourceFile->FileBufferPtr++;
InComment = FALSE;
} else if (InComment) {
SourceFile->FileBufferPtr[0] = T_CHAR_SPACE;
SourceFile->FileBufferPtr++;
//
// Check for // comments
//
} else if ((SourceFile->FileBufferPtr[0] == T_CHAR_SLASH) && (SourceFile->FileBufferPtr[1] == T_CHAR_SLASH)) {
InComment = TRUE;
SlashSlashComment = TRUE;
//
// Check for /* comment start
//
} else if ((SourceFile->FileBufferPtr[0] == T_CHAR_SLASH) && (SourceFile->FileBufferPtr[1] == T_CHAR_STAR)) {
SourceFile->FileBufferPtr[0] = T_CHAR_SPACE;
SourceFile->FileBufferPtr++;
SourceFile->FileBufferPtr[0] = T_CHAR_SPACE;
SourceFile->FileBufferPtr++;
SlashSlashComment = FALSE;
InComment = TRUE;
} else {
SourceFile->FileBufferPtr++;
}
}
//
// Could check for end-of-file and still in a comment, but
// should not be necessary. So just restore the file pointers.
//
RewindFile (SourceFile);
//
// Dump the reformatted file if verbose mode
//
if (mGlobals.VerboseFile) {
LineNum = 1;
printf ("%04d: ", LineNum);
while (!EndOfFile (SourceFile)) {
if (SourceFile->FileBufferPtr[0] == T_CHAR_LF) {
printf ("'\n%04d: '", ++LineNum);
} else {
printf ("%c", SourceFile->FileBufferPtr[0]);
}
SourceFile->FileBufferPtr++;
}
printf ("'\n");
printf ("FileSize = %d (0x%X)\n", SourceFile->FileSize, SourceFile->FileSize);
RewindFile (SourceFile);
}
}
BOOLEAN
SFPGetQuotedString (
T_CHAR *Str,
int Length
)
/*++
Routine Description:
Retrieve a quoted-string from the input file.
Arguments:
Str - pointer to a copy of the quoted string parsed
Length - size of buffer pointed to by Str
Returns:
TRUE - next token in input stream was a quoted string, and
the string value was returned in Str
FALSE - otherwise
--*/
{
SkipWhiteSpace (&mGlobals.SourceFile);
if (EndOfFile (&mGlobals.SourceFile)) {
return FALSE;
}
if (mGlobals.SourceFile.FileBufferPtr[0] == T_CHAR_DOUBLE_QUOTE) {
mGlobals.SourceFile.FileBufferPtr++;
while (Length > 0) {
if (EndOfFile (&mGlobals.SourceFile)) {
return FALSE;
}
//
// Check for closing quote
//
if (mGlobals.SourceFile.FileBufferPtr[0] == T_CHAR_DOUBLE_QUOTE) {
mGlobals.SourceFile.FileBufferPtr++;
*Str = 0;
return TRUE;
}
*Str = mGlobals.SourceFile.FileBufferPtr[0];
Str++;
Length--;
mGlobals.SourceFile.FileBufferPtr++;
}
}
//
// First character was not a quote, or the input string length was
// insufficient to contain the quoted string, so return failure code.
//
return FALSE;
}
BOOLEAN
SFPIsEOF (
VOID
)
/*++
Routine Description:
Return TRUE of FALSE to indicate whether or not we've reached the end of the
file we're parsing.
Arguments:
NA
Returns:
TRUE - EOF reached
FALSE - otherwise
--*/
{
SkipWhiteSpace (&mGlobals.SourceFile);
return EndOfFile (&mGlobals.SourceFile);
}
#if 0
static
T_CHAR *
GetQuotedString (
SOURCE_FILE *SourceFile,
BOOLEAN Optional
)
{
T_CHAR *String;
T_CHAR *Start;
T_CHAR *Ptr;
unsigned int Len;
BOOLEAN PreviousBackslash;
if (SourceFile->FileBufferPtr[0] != T_CHAR_DOUBLE_QUOTE) {
if (Optional == FALSE) {
Error (SourceFile->FileName, SourceFile->LineNum, 0, "expected quoted string", "%S", SourceFile->FileBufferPtr);
}
return NULL;
}
Len = 0;
SourceFile->FileBufferPtr++;
Start = Ptr = SourceFile->FileBufferPtr;
PreviousBackslash = FALSE;
while (!EndOfFile (SourceFile)) {
if ((SourceFile->FileBufferPtr[0] == T_CHAR_DOUBLE_QUOTE) && (PreviousBackslash == FALSE)) {
break;
} else if (SourceFile->FileBufferPtr[0] == T_CHAR_CR) {
Warning (SourceFile->FileName, SourceFile->LineNum, 0, "carriage return found in quoted string", "%S", Start);
PreviousBackslash = FALSE;
} else if (SourceFile->FileBufferPtr[0] == T_CHAR_BACKSLASH) {
PreviousBackslash = TRUE;
} else {
PreviousBackslash = FALSE;
}
SourceFile->FileBufferPtr++;
Len++;
}
if (SourceFile->FileBufferPtr[0] != T_CHAR_DOUBLE_QUOTE) {
Warning (SourceFile->FileName, SourceFile->LineNum, 0, "missing closing quote on string", "%S", Start);
} else {
SourceFile->FileBufferPtr++;
}
//
// Now allocate memory for the string and save it off
//
String = (T_CHAR *) malloc ((Len + 1) * sizeof (T_CHAR));
if (String == NULL) {
Error (NULL, 0, 0, "memory allocation failed", NULL);
return NULL;
}
//
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?