ld65 debug file parser in work.

This commit is contained in:
harry 2023-02-12 12:54:20 -05:00
parent 07617997f4
commit 0beac5d725
2 changed files with 256 additions and 0 deletions

View File

@ -1,5 +1,6 @@
// ld65dbg.cpp
#include <stdio.h>
#include <ctype.h>
#include "ld65dbg.h"
@ -26,9 +27,239 @@ namespace ld65
{
}
//---------------------------------------------------------------------------------------------------
database::dbgLine::dbgLine(size_t bufferSize)
{
buf = NULL;
bufSize = 0;
readPtr = 0;
allocBuffer( bufferSize );
}
//---------------------------------------------------------------------------------------------------
database::dbgLine::~dbgLine(void)
{
if (buf)
{
::free(buf); buf = NULL;
}
bufSize = 0;
readPtr = 0;
}
//---------------------------------------------------------------------------------------------------
void database::dbgLine::allocBuffer(size_t bufferSize)
{
if (buf)
{
::free(buf); buf = NULL;
}
bufSize = 0;
readPtr = 0;
buf = static_cast<char*>( ::malloc( bufferSize ) );
if (buf == NULL)
{
bufSize = 0;
}
else
{
buf[0] = 0;
bufSize = bufferSize;
}
readPtr = 0;
}
//---------------------------------------------------------------------------------------------------
const char *database::dbgLine::readFromFile( FILE *fp )
{
readPtr = 0;
return fgets(buf, bufSize, fp);
}
//---------------------------------------------------------------------------------------------------
int database::dbgLine::readToken( char *tk, size_t tkSize )
{
int charsRead = 0;
size_t i,j;
i=readPtr; j=0;
if ( buf[i] != 0 )
{
while (isspace(buf[i])) i++;
if ( isalpha(buf[i]) || (buf[i] == '_') )
{
while ( isalnum(buf[i]) || (buf[i] == '_') )
{
if (j < tkSize)
{
tk[j] = buf[i]; j++;
}
i++;
}
}
else if (buf[i] != 0)
{
if (j < tkSize)
{
tk[j] = buf[i]; j++;
}
i++;
}
}
charsRead = j;
readPtr = i;
if (j < tkSize)
{
tk[j] = 0;
}
else
{
tk[tkSize-1] = 0;
}
return charsRead;
}
//---------------------------------------------------------------------------------------------------
int database::dbgLine::readKeyValuePair( char *keyValueBuffer, size_t keyValueBufferSize )
{
int charsRead = 0;
size_t i,j;
bool isStringLiteral = false;
i=readPtr; j=0;
if ( buf[i] != 0 )
{
while (isspace(buf[i])) i++;
if ( isalpha(buf[i]) || (buf[i] == '_') )
{
while ( isalnum(buf[i]) || (buf[i] == '_') )
{
if (j < keyValueBufferSize)
{
keyValueBuffer[j] = buf[i]; j++;
}
i++;
}
}
else if (buf[i] != 0)
{
if (j < keyValueBufferSize)
{
keyValueBuffer[j] = buf[i]; j++;
}
i++;
}
while (isspace(buf[i])) i++;
}
if ( buf[i] == '=' )
{
if (j < keyValueBufferSize)
{
keyValueBuffer[j] = buf[i]; j++;
}
i++;
while (isspace(buf[i])) i++;
while ( (buf[i] != 0) )
{
if ( !isStringLiteral && buf[i] != ',' )
{
break;
}
else if ( buf[i] == '\"' )
{
isStringLiteral = !isStringLiteral;
}
else
{
if (j < keyValueBufferSize)
{
if (!isspace(buf[i]))
{
keyValueBuffer[j] = buf[i]; j++;
}
}
}
i++;
}
if (buf[i] == ',')
{
i++;
}
}
charsRead = j;
readPtr = i;
if (j < keyValueBufferSize)
{
keyValueBuffer[j] = 0;
}
else
{
keyValueBuffer[keyValueBufferSize-1] = 0;
}
return charsRead;
}
//---------------------------------------------------------------------------------------------------
int database::dbgLine::splitKeyValuePair( char *keyValueBuffer, char **keyPtr, char **valuePtr )
{
size_t i=0;
if (keyPtr != nullptr)
{
*keyPtr = keyValueBuffer;
}
while (keyValueBuffer[i] != 0)
{
if (keyValueBuffer[i] == '=')
{
keyValueBuffer[i] = 0; i++; break;
}
i++;
}
if (valuePtr != nullptr)
{
*valuePtr = &keyValueBuffer[i];
}
return 0;
}
//---------------------------------------------------------------------------------------------------
int database::dbgFileLoad( const char *dbgFilePath )
{
FILE *fp;
dbgLine line;
char lineType[64];
char keyValueBuffer[1024];
fp = ::fopen( dbgFilePath, "r");
if (fp == NULL)
{
return -1;
}
while ( line.readFromFile(fp) != NULL )
{
printf("%s", line.getLine());
if ( line.readToken( lineType, sizeof(lineType) ) )
{
printf("%s\n", lineType);
while ( line.readKeyValuePair( keyValueBuffer, sizeof(keyValueBuffer) ) )
{
char *key, *val;
line.splitKeyValuePair( keyValueBuffer, &key, &val );
printf(" Key '%s' -> Value '%s' \n", key, val );
}
}
}
::fclose(fp);
return 0;
}

View File

@ -1,6 +1,7 @@
// ld65dbg.h
//
#pragma once
#include <stdio.h>
#include <string>
#include <map>
@ -69,5 +70,29 @@ namespace ld65
private:
std::map<int, scope*> scopeMap;
std::map<int, segment*> segmentMap;
class dbgLine
{
public:
dbgLine(size_t bufferSize = 1024);
~dbgLine(void);
const char *readFromFile( FILE *fp );
const char *getLine(void){ return buf; };
int readToken( char *tk, size_t tkSize );
int readKeyValuePair( char *keyValueBuffer, size_t keyValueBufferSize );
static int splitKeyValuePair( char *keyValueBuffer, char **keyPtr, char **valuePtr );
private:
void allocBuffer(size_t bufferSize);
size_t readPtr;
size_t bufSize;
char *buf;
};
};
};