Start breaking up the parsing

main
Alan Francis 1 year ago
parent 69fa45e1ba
commit 3c6b757d79
  1. 270
      configmodel.c

@ -14,6 +14,14 @@
cregex_program_t* InitialisePattern(CONST_STRPTR pattern);
Array RunPattern(CONST_STRPTR text, cregex_program_t* patternProgram);
SECTIONPTR SectionCreateWithName(CONST_STRPTR primary);
SECTIONPTR SectionCreateWithNameAndSubname(CONST_STRPTR primary, CONST_STRPTR secondary);
VOID SectionFree(SECTIONPTR abstractSection);
VARIABLEPTR VariableCreate(CONST_STRPTR key, CONST_STRPTR rawValue);
VOID VariableFree(VARIABLEPTR abstractVariable);
#define WHITESPACE "[ \\t\\n\\r\\f\\v]"
#define RX_BLANK_LINE "^[ \t\n\r\f\v]*($|#|;)"
@ -34,7 +42,8 @@ struct ConfigFile
struct Section
{
StringArray nameComponents;
CONST_STRPTR primary_key;
CONST_STRPTR secondary_key;
LineArray lines;
};
@ -60,12 +69,8 @@ struct Variable
struct Line
{
STRPTR rawText;
union
{
struct Variable* variable;
struct Section* section;
}
object;
struct Variable* variable;
struct Section* section;
};
@ -103,8 +108,161 @@ CONFIGFILEPTR ConfigFileRead(CONST_STRPTR filename)
return result;
}
VOID ConfigFileSave(CONFIGFILEPTR config);
VOID ConfigFileSave(CONFIGFILEPTR config)
{
}
LINEPTR LineCreate(CONST_STRPTR buffer, ULONG size)
{
struct Line* result = NULL;
StringArray matches = NULL;
if( size > 0 )
{
result = AllocVec(sizeof(struct Line), MEMF_CLEAR);
result->rawText = AllocVec(size+1, MEMF_CLEAR);
CopyMem(buffer, result->rawText, size);
if( matches = RunPattern(result->rawText, sectionPatternProgram) )
{
if( SizeOfArray(matches) == 3 )
{
Printf("\nsection {%s}\n", StringArrayValues(matches)[1]);
result->section = SectionCreateWithName(StringArrayValues(matches)[1]);
}
else if( SizeOfArray(matches) == 5 )
{
Printf("\nsection {%s.%s}\n", StringArrayValues(matches)[1], StringArrayValues(matches)[3]);
result->section = SectionCreateWithNameAndSubname(StringArrayValues(matches)[1], StringArrayValues(matches)[3]);
}
// Printf("\nsection size=%ld\n", SizeOfArray(stringArray));
// StringArrayForEach(stringArray, Printf("{{%s}}",aString););
StringArrayFree(matches);
}
else
{
if( matches = RunPattern(result->rawText, variablePatternProgram) )
{
Printf("\nvariable {%s} = {%s}\n", StringArrayValues(matches)[1], StringArrayValues(matches)[2]);
// StringArrayForEach(stringArray, Printf("{{%s}}",aString););
result->variable = VariableCreate(StringArrayValues(matches)[1], StringArrayValues(matches)[2]);
StringArrayFree(matches);
}
else
{
if( matches = RunPattern(result->rawText, blankPatternProgram) )
{
Printf("\nYY %s\n", result->rawText);
StringArrayFree(matches);
}
else
{
Printf("\nXX %s\n", result->rawText);
}
}
}
}
return result;
}
LINEPTR LineReadIncludingContinuation(BPTR file)
{
UBYTE* buffer = AllocVec(512, MEMF_CLEAR);
ULONG bufLength = 512;
ULONG bytesReadTotal = 0;
UBYTE* read = NULL;
struct Line* result = NULL;
// read the whole line including continuation
do
{
read = FGets(file, &(buffer[bytesReadTotal]), bufLength-bytesReadTotal);
bytesReadTotal = strlen(buffer);
}
while( read != NULL && bytesReadTotal >= 2 && bytesReadTotal < bufLength && buffer[bytesReadTotal-1] == '\n' && buffer[bytesReadTotal-2] == '\\' );
// make a line
if( bytesReadTotal > 0 )
{
result = LineCreate(buffer, bytesReadTotal);
}
FreeVec(buffer);
return result;
}
VOID LineFree(LINEPTR abstractLine)
{
struct Line* line = (struct Line*)abstractLine;
if( line != NULL )
{
if( line->rawText != NULL )
{
FreeVec(line->rawText);
}
if( line->section != NULL )
{
SectionFree(line->section);
}
if( line->variable != NULL )
{
VariableFree(line->variable);
}
FreeVec(line);
}
}
CONST_STRPTR LineGetRawText(LINEPTR abstractLine)
{
struct Line* line = (struct Line*)abstractLine;
if( line != NULL )
{
return (CONST_STRPTR)line->rawText;
}
else
{
return NULL;
}
}
SECTIONPTR SectionCreateWithName(CONST_STRPTR primary)
{
return NULL;
}
SECTIONPTR SectionCreateWithNameAndSubname(CONST_STRPTR primary, CONST_STRPTR secondary)
{
return NULL;
}
VOID SectionFree(SECTIONPTR abstractSection)
{
}
VARIABLEPTR VariableCreate(CONST_STRPTR key, CONST_STRPTR rawValue)
{
return NULL;
}
VOID VariableFree(VARIABLEPTR abstractVariable)
{
}
// ----------------------------------------------------
// ----------------------------------------------------
// ----------------------------------------------------
// ----------------------------------------------------
// ----------------------------------------------------
VOID InitialisePatterns(VOID)
{
sectionPatternProgram = InitialisePattern(RX_SECTION_LINE);
@ -183,99 +341,3 @@ Array RunPattern(CONST_STRPTR text, cregex_program_t* patternProgram)
}
return result;
}
LINEPTR LineReadIncludingContinuation(BPTR file)
{
UBYTE* buffer = AllocVec(512, MEMF_CLEAR);
ULONG bufLength = 512;
ULONG bytesReadTotal = 0;
UBYTE* read = NULL;
struct Line* result = NULL;
do
{
read = FGets(file, &(buffer[bytesReadTotal]), bufLength-bytesReadTotal);
bytesReadTotal = strlen(buffer);
}
while( read != NULL && bytesReadTotal >= 2 && bytesReadTotal < bufLength && buffer[bytesReadTotal-1] == '\n' && buffer[bytesReadTotal-2] == '\\' );
if( bytesReadTotal > 0 )
{
StringArray stringArray = NULL;
result = AllocVec(sizeof(struct Line), MEMF_CLEAR);
result->rawText = AllocVec(bytesReadTotal+1, MEMF_CLEAR);
CopyMem(buffer, result->rawText, bytesReadTotal-1);
// Printf("\n\nraw text = {{{%s}}}\n", result->rawText); //
stringArray = RunPattern(result->rawText, sectionPatternProgram);
if( stringArray )
{
if( SizeOfArray(stringArray) == 3 )
{
Printf("\nsection {%s}\n", StringArrayValues(stringArray)[1]);
}
else if( SizeOfArray(stringArray) == 5 )
{
Printf("\nsection {%s.%s}\n", StringArrayValues(stringArray)[1], StringArrayValues(stringArray)[3]);
}
// Printf("\nsection size=%ld\n", SizeOfArray(stringArray));
// StringArrayForEach(stringArray, Printf("{{%s}}",aString););
StringArrayFree(stringArray);
}
else
{
stringArray = RunPattern(result->rawText, variablePatternProgram);
if( stringArray )
{
Printf("\nvariable {%s} = {%s}\n", StringArrayValues(stringArray)[1], StringArrayValues(stringArray)[2]);
// StringArrayForEach(stringArray, Printf("{{%s}}",aString););
StringArrayFree(stringArray);
}
else
{
stringArray = RunPattern(result->rawText, blankPatternProgram);
if( stringArray )
{
Printf("\nYY %s\n", result->rawText);
StringArrayFree(stringArray);
}
else
{
Printf("\nXX %s\n", result->rawText);
}
}
}
}
FreeVec(buffer);
return result;
}
VOID LineFree(LINEPTR abstractLine)
{
struct Line* line = (struct Line*)abstractLine;
if( line != NULL )
{
if( line->rawText != NULL )
{
FreeVec(line->rawText);
}
FreeVec(line);
}
}
CONST_STRPTR LineGetRawText(LINEPTR abstractLine)
{
struct Line* line = (struct Line*)abstractLine;
if( line != NULL )
{
return (CONST_STRPTR)line->rawText;
}
else
{
return NULL;
}
}

Loading…
Cancel
Save