From 706814653dfb4a781c7cf70cbe6b2251d189c20a Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Sun, 3 Apr 2016 02:07:52 -0400 Subject: [PATCH] Simplify dealing with empty files. Why write a parser when you already have a perfectly good one already? :) --- Sources/Ecc/Main.cpp | 122 ------------------------------------------- Sources/Ecc/Parser.y | 3 +- 2 files changed, 2 insertions(+), 123 deletions(-) diff --git a/Sources/Ecc/Main.cpp b/Sources/Ecc/Main.cpp index 3f88e62..528e4d3 100644 --- a/Sources/Ecc/Main.cpp +++ b/Sources/Ecc/Main.cpp @@ -228,110 +228,6 @@ void ReplaceFile(const char *strOld, const char *strNew) rename(strNew, strOld); } -enum ESStatus -{ - /* Appears to be non-empty, ready to parse. */ - Good, - /* Appears to be empty, ignore it. */ - Empty, - /* Error occured during status check. */ - Error, -}; - -/* Determine whether or not our target ES file is indeed valid input. */ -ESStatus GetESStatus() -{ - ESStatus result = ESStatus::Empty; - - // Read a temporary buffer of the entire file contents - fseek(_fInput, 0, SEEK_END); - size_t length = ftell(_fInput); - - // Hard-stop on Empty out of paranoia - if (length == 0) - return result; - - char* temporaryBuffer = (char*)malloc(length); - - if (!temporaryBuffer) - return ESStatus::Error; - - fseek(_fInput, 0, SEEK_SET); - fread(temporaryBuffer, length, 1, _fInput); - fseek(_fInput, 0, SEEK_SET); - - // Loop through each line - char* currentSequence = strtok(temporaryBuffer, "\n"); - - // No newlines, but it might still be valid. - if (!currentSequence) - currentSequence = temporaryBuffer; - - bool inBlockComment = false; - do - { - size_t sequenceLength = strlen(currentSequence); - - for (size_t iteration = 0; iteration < sequenceLength; iteration++) - { - // If we're still in a block comment, find the closing */ - if (inBlockComment) - { - char* blockClosing = strstr(currentSequence, "*/"); - if (!blockClosing) - break; - else - { - inBlockComment = false; - iteration = ((size_t)blockClosing - (size_t)currentSequence) + 2; - } - } - - // If we find a // sequence, simply skip this line - if (currentSequence[iteration] == '/' && currentSequence[iteration + 1] == '/') - break; - - // If we find a /* on this line but not a closing */, skip this line - if (currentSequence[iteration] == '/' && currentSequence[iteration + 1] == '*') - { - // Is there a closing */ on this line? - char* blockClosing = strstr(currentSequence, "*/"); - - if (!blockClosing) - { - inBlockComment = true; - break; - } - else - { - iteration = ((size_t)blockClosing - (size_t)currentSequence) + 2; - inBlockComment = false; - continue; - } - } - - if (iteration >= sequenceLength) - break; - - // If we got to this point, we should be able to read only a number on this line - for (size_t checkIteration = 0; checkIteration < sequenceLength; checkIteration++) - if (currentSequence[checkIteration] != '\n' && currentSequence[checkIteration] != 0x20 && !isdigit(currentSequence[checkIteration])) - { - result = ESStatus::Error; - break; - } - else if (currentSequence[checkIteration] != '\n' && currentSequence[checkIteration] != 0x20) - result = ESStatus::Good; - - free(temporaryBuffer); - return result; - } - } - while(currentSequence = strtok(NULL, "\n")); - - return result; -} - /* Replace a file with a new file if they are different. * Used to keep .h files from constantly changing when you change the implementation. */ @@ -389,24 +285,6 @@ int main(int argc, char *argv[]) // open the input file _fInput = FOpen(argv[1], "r"); - // Make sure we're loading a valid ES file - ESStatus status = GetESStatus(); - - switch (status) - { - case ESStatus::Empty: - { - fclose(_fInput); - return EXIT_SUCCESS; - } - case ESStatus::Error: - { - fclose(_fInput); - printf("Ecc encountered an error during the es verification.\n"); - return EXIT_FAILURE; - } - } - //printf("%s\n", argv[1]); // open all the output files char *strImplementation = ChangeFileNameExtension(argv[1], ".cpp_tmp"); diff --git a/Sources/Ecc/Parser.y b/Sources/Ecc/Parser.y index ff0ff44..977882b 100644 --- a/Sources/Ecc/Parser.y +++ b/Sources/Ecc/Parser.y @@ -243,7 +243,8 @@ void DeclareFeatureProperties(void) * Global structure of the source file. */ program - : c_int { + : /* empty file */ {} + | c_int { int iID = atoi($1.strString); if(iID>32767) { yyerror("Maximum allowed id for entity source file is 32767");