From 6a116e5f8d814f22ecf835fc166636564c6c4927 Mon Sep 17 00:00:00 2001 From: Alan C Francis Date: Sun, 31 Dec 2023 21:35:49 +0000 Subject: [PATCH] add before blanks working as long as no lines have been deleted. --- configfile.c | 2 +- configmodel.c | 38 ++++++++++++++++++-------------------- configmodel.h | 2 +- containers/linearray.c | 10 ++++++++-- sectionstore.c | 4 ++-- 5 files changed, 30 insertions(+), 26 deletions(-) diff --git a/configfile.c b/configfile.c index f7a3174..b3dd04b 100644 --- a/configfile.c +++ b/configfile.c @@ -214,7 +214,7 @@ VOID ConfigFileAddVariable(CONFIGFILEPTR abstractConfigFile, CONST_STRPTR compou // now get the section section = SectionStoreFindOrCreateSection(configFile->sectionStore, compoundKey); // add the line to the section - SectionAddLine(section, line); + SectionAddLine(section, line, TRUE); } } diff --git a/configmodel.c b/configmodel.c index 549675b..f91dc85 100644 --- a/configmodel.c +++ b/configmodel.c @@ -15,7 +15,7 @@ struct Section CONST_STRPTR primary; CONST_STRPTR secondary; LineArray lines; - BOOL lastLineWasEmpty; + ULONG insertPos; // we want to add new entries at end, but before any blanks }; enum VariableType @@ -248,7 +248,7 @@ SECTIONPTR SectionCreateWithNameAndSubname(CONST_STRPTR primary, CONST_STRPTR se { ULONG length = strlen(primary); result = AllocVec(sizeof(struct Section), MEMF_CLEAR); - result->lastLineWasEmpty = FALSE; + result->insertPos = 0; result->primary = AllocVec(length+1, MEMF_CLEAR); CopyMem(primary, (STRPTR)result->primary, length); @@ -269,7 +269,7 @@ VOID SectionAddSectionLine(SECTIONPTR abstractSection, LINEPTR abstractLine) { // sets self onto line and then adds line to the collection LineSetSection(abstractLine, abstractSection); - SectionAddLine(abstractSection, abstractLine); + SectionAddLine(abstractSection, abstractLine, FALSE); } // we need to know if there are blank lines at the end and if this is a variable, @@ -290,32 +290,30 @@ VOID SectionAddSectionLine(SECTIONPTR abstractSection, LINEPTR abstractLine) // [baz] // etc -VOID SectionAddLine(SECTIONPTR abstractSection, LINEPTR abstractLine) +VOID SectionAddLine(SECTIONPTR abstractSection, LINEPTR abstractLine, BOOL addBeforeBlanks) { struct Section* section = (struct Section*)abstractSection; if( section != NULL ) { - //Printf("\n\SAD1 (%ld bytes avail)\n\n", AvailMem(0)); - - // if the last line is empty and new line is NOT, insert new line before the blank. - if( section->lastLineWasEmpty && !LineIsEmpty(abstractLine) ) + if( addBeforeBlanks==FALSE || section->insertPos == SizeOfArray(section->lines) ) { - LINEPTR lastLine = ArrayBackValue(LINEPTR, section->lines); - ULONG lastIndex = SizeOfArray(section->lines)-1; - //Printf("\n\SAD2 (%ld bytes avail)\n\n", AvailMem(0)); - LineArrayValues(section->lines)[lastIndex] = abstractLine; - LineArrayAppend(section->lines, lastLine); - //Printf("\n\SAD3 (%ld bytes avail)\n\n", AvailMem(0)); + LineArrayAppend(section->lines, abstractLine); + if(!LineIsEmpty(abstractLine) ) + { + section->insertPos = SizeOfArray(section->lines); + printf("xx (%s) insertpos = %lu\n", SectionCanonicalName(abstractSection), section->insertPos); + LineDump(abstractLine); + printf("xx\n"); + } } else { - //Printf("\n\SAD4 (%ld bytes avail)\n\n", AvailMem(0)); - LineArrayAppend(section->lines, abstractLine); - //Printf("\n\SAD5 (%ld bytes avail)\n\n", AvailMem(0)); + LineArrayInsert(section->lines, abstractLine, section->insertPos); + section->insertPos += 1; + printf("yy (%s) insertpos = %lu\n", SectionCanonicalName(abstractSection), section->insertPos); + LineDump(abstractLine); + printf("yy\n"); } - //Printf("\n\SAD6 (%ld bytes avail)\n\n", AvailMem(0)); - section->lastLineWasEmpty = LineIsEmpty(abstractLine); - //Printf("\n\SAD7 (%ld bytes avail)\n\n", AvailMem(0)); } } diff --git a/configmodel.h b/configmodel.h index 1aaa115..da7183c 100644 --- a/configmodel.h +++ b/configmodel.h @@ -21,7 +21,7 @@ SECTIONPTR SectionCreateWithName(CONST_STRPTR primary); SECTIONPTR SectionCreateWithNameAndSubname(CONST_STRPTR primary, CONST_STRPTR secondary); VOID SectionFree(SECTIONPTR section); VOID SectionAddSectionLine(SECTIONPTR section, LINEPTR line); -VOID SectionAddLine(SECTIONPTR section, LINEPTR line); +VOID SectionAddLine(SECTIONPTR abstractSection, LINEPTR abstractLine, BOOL addBeforeBlanks); CONST_STRPTR SectionCanonicalName(SECTIONPTR section); VOID SectionDump(SECTIONPTR section); CONST_STRPTR SectionSerialize(SECTIONPTR section); diff --git a/containers/linearray.c b/containers/linearray.c index 3577572..3f077e3 100644 --- a/containers/linearray.c +++ b/containers/linearray.c @@ -24,8 +24,14 @@ VOID LineArrayAppend(LineArray array, LINEPTR value) VOID LineArrayInsert(LineArray array, LINEPTR value, ULONG pos) { - AppendToArray(LINEPTR, array, value); -// InsertToArray(LINEPTR, array, pos, value); + if( InsertArrayElements(array, pos, 1) ) + { + LineArrayValues(array)[pos] = value; + } + else + { + printf("booooo\n"); + } } VOID LineArrayFree(LineArray array, BOOL freeLines) diff --git a/sectionstore.c b/sectionstore.c index d20e4b5..bee31a0 100644 --- a/sectionstore.c +++ b/sectionstore.c @@ -170,13 +170,13 @@ VOID SectionStoreAddLineToCurrentSection(SECTIONSTOREPTR abstractSectionStore, L if( variable = parseVariable(line, store->variablePatternProgram) ) { LineSetInitialVariable(line, variable); - SectionAddLine(section, line); + SectionAddLine(section, line, FALSE); } else { if( parseBlank(line, store->blankPatternProgram) ) { - SectionAddLine(section, line); + SectionAddLine(section, line, FALSE); } else {