From 0beac5d725ed6a9a0780cff48069f9fbd8686667 Mon Sep 17 00:00:00 2001 From: harry Date: Sun, 12 Feb 2023 12:54:20 -0500 Subject: [PATCH] ld65 debug file parser in work. --- src/ld65dbg.cpp | 231 ++++++++++++++++++++++++++++++++++++++++++++++++ src/ld65dbg.h | 25 ++++++ 2 files changed, 256 insertions(+) diff --git a/src/ld65dbg.cpp b/src/ld65dbg.cpp index 5b9a5379..104ea187 100644 --- a/src/ld65dbg.cpp +++ b/src/ld65dbg.cpp @@ -1,5 +1,6 @@ // ld65dbg.cpp #include +#include #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( ::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; } diff --git a/src/ld65dbg.h b/src/ld65dbg.h index cdbec1c2..25ea09c3 100644 --- a/src/ld65dbg.h +++ b/src/ld65dbg.h @@ -1,6 +1,7 @@ // ld65dbg.h // #pragma once +#include #include #include @@ -69,5 +70,29 @@ namespace ld65 private: std::map scopeMap; std::map 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; + }; }; };