Merge pull request #12 from Ragora/EccFixBase

Fix for Ecc failing on empty files
This commit is contained in:
SLAwww 2016-03-13 00:06:45 +02:00
commit b8527ace7b
2 changed files with 126 additions and 11 deletions

View File

@ -227,6 +227,109 @@ void ReplaceFile(const char *strOld, const char *strNew)
remove(strOld); remove(strOld);
rename(strNew, strOld); 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(char *filename)
{
ESStatus result = ESStatus::Empty;
// Read a temporary buffer of the entire file contents
fseek(_fInput, 0, SEEK_END);
size_t length = ftell(_fInput);
char* temporaryBuffer = (char*)malloc(length);
fseek(_fInput, 0, SEEK_SET);
fread(temporaryBuffer, length, 1, _fInput);
fclose(_fInput);
// 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);
if (result == ESStatus::Good)
_fInput = FOpen(filename, "r");
return result;
}
}
while(currentSequence = strtok(NULL, "\n"));
free(temporaryBuffer);
if (result == ESStatus::Good)
_fInput = FOpen(filename, "r");
return result;
}
/* Replace a file with a new file if they are different. /* Replace a file with a new file if they are different.
* Used to keep .h files from constantly changing when you change the implementation. * Used to keep .h files from constantly changing when you change the implementation.
*/ */
@ -283,6 +386,18 @@ int main(int argc, char *argv[])
} }
// open the input file // open the input file
_fInput = FOpen(argv[1], "r"); _fInput = FOpen(argv[1], "r");
// Make sure we're loading a valid ES file
ESStatus status = GetESStatus(argv[1]);
switch (status)
{
case ESStatus::Empty:
return EXIT_SUCCESS;
case ESStatus::Error:
return EXIT_FAILURE;
}
//printf("%s\n", argv[1]); //printf("%s\n", argv[1]);
// open all the output files // open all the output files
char *strImplementation = ChangeFileNameExtension(argv[1], ".cpp_tmp"); char *strImplementation = ChangeFileNameExtension(argv[1], ".cpp_tmp");

View File

@ -13,6 +13,7 @@ You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc., with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
#include <ctype.h>
#include <assert.h> #include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -27,4 +28,3 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <unistd.h> #include <unistd.h>
#define _fullpath(x, y, z) realpath(y, x) #define _fullpath(x, y, z) realpath(y, x)
#endif #endif