Skip to content

Commit

Permalink
encode: section_order adjust num_sections internally
Browse files Browse the repository at this point in the history
  • Loading branch information
rurban committed Jan 5, 2024
1 parent 8635cc1 commit 5471f05
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 45 deletions.
85 changes: 50 additions & 35 deletions src/encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -3076,14 +3076,18 @@ section_order_trace (const BITCODE_BL numsections,

static int
section_move_top (Dwg_Section_Type_r13 *psection_order,
BITCODE_RL *pnum,
Dwg_Section_Type_r13 sec_id)
{
Dwg_Section_Type_r13 old_first = psection_order[0];
LOG_TRACE ("section_move_top %u\n", sec_id);
assert(*pnum <= SECTION_R13_SIZE);
if (psection_order[0] == sec_id)
return 0;
{
LOG_TRACE ("section_move_top %u (already)\n", sec_id);
return 0;
}

for (unsigned i = 1; i < SECTION_R13_SIZE; i++)
for (unsigned i = 1; i < *pnum; i++)
{
// f x x i y y y
if (psection_order[i] == (unsigned)sec_id) { // found at i
Expand All @@ -3094,6 +3098,7 @@ section_move_top (Dwg_Section_Type_r13 *psection_order,
(i - 1) * sizeof (Dwg_Section_Type_r13));
psection_order[1] = old_first;
// i f x x y y y
LOG_TRACE ("section_move_top %u (re-order)\n", sec_id);
return 0;
}
}
Expand All @@ -3102,16 +3107,20 @@ section_move_top (Dwg_Section_Type_r13 *psection_order,
// move x'n right by 1
// f x x x y y y
memmove (&psection_order[2], &psection_order[1],
(SECTION_R13_SIZE - 2) * sizeof (Dwg_Section_Type_r13));
(*pnum - 1) * sizeof (Dwg_Section_Type_r13));
psection_order[1] = old_first;
LOG_TRACE ("section_move_top %u (inserted)\n", sec_id);
(*pnum)++;
assert(*pnum <= SECTION_R13_SIZE);
return 1;
}

static unsigned section_find (Dwg_Section_Type_r13 *psection_order,
BITCODE_RL num,
Dwg_Section_Type_r13 id)
{
LOG_TRACE ("section_find %u\n", (unsigned)id);
for (unsigned i = 0; i < SECTION_R13_SIZE; i++)
for (unsigned i = 0; i < num; i++)
{
if (psection_order[i] == id) { // found at i
return i;
Expand All @@ -3122,37 +3131,43 @@ static unsigned section_find (Dwg_Section_Type_r13 *psection_order,

static int
section_remove (Dwg_Section_Type_r13 *psection_order,
BITCODE_RL *pnum,
Dwg_Section_Type_r13 id)
{
unsigned i = section_find (psection_order, id);
LOG_TRACE ("section_remove %u\n", (unsigned)id);
if (i >= SECTION_R13_SIZE) // not found
unsigned i = section_find (psection_order, *pnum, id);
LOG_TRACE ("section_remove %u [%u]\n", (unsigned)id, i);
if (i >= *pnum) // not found
return 0;
// move left
assert(*pnum > 0);
(*pnum)--;
memmove (&psection_order[i], &psection_order[i + 1],
(SECTION_R13_SIZE - 1 - i) * sizeof (Dwg_Section_Type_r13));
psection_order[SECTION_R13_SIZE - 1] = SECTION_R13_SIZE;
(*pnum - i) * sizeof (Dwg_Section_Type_r13));
psection_order[*pnum] = SECTION_R13_SIZE; // sentinel (invalid)
return 1;
}

static int
section_move_before (Dwg_Section_Type_r13 *psection_order,
BITCODE_RL *pnum,
Dwg_Section_Type_r13 id, Dwg_Section_Type_r13 before)
{
int ret = 0;
unsigned b;
unsigned id_pos;
Dwg_Section_Type_r13 old_before;
LOG_TRACE ("section_move_before %u %u\n", (unsigned)id, (unsigned)before);
b = section_find (psection_order, before);
b = section_find (psection_order, *pnum, before);
// find before
if (b >= SECTION_R13_SIZE) // not found
return 0;
// x x b y y
old_before = psection_order[b];
assert(*pnum + 1 <= SECTION_R13_SIZE);
memmove (&psection_order[b + 1], &psection_order[b],
(SECTION_R13_SIZE - b - 1) * sizeof (Dwg_Section_Type_r13));
if (!section_remove (psection_order, id)) // if not exist: insert
(*pnum - b) * sizeof (Dwg_Section_Type_r13));
(*pnum)++;
if (!section_remove (psection_order, pnum, id)) // if not exist: insert
ret = 1;
psection_order[b] = id;
// x x i b y
Expand Down Expand Up @@ -3771,46 +3786,46 @@ dwg_encode (Dwg_Data *restrict dwg, Bit_Chain *restrict dat)
&& dwg->header.thumbnail_address
< dwg->header.section[SECTION_HEADER_R13].address)
{
if (section_move_top ((Dwg_Section_Type_r13 *)&section_order,
SECTION_THUMBNAIL_R13))
dwg->header.num_sections++;
section_move_top ((Dwg_Section_Type_r13 *)&section_order,
&dwg->header.num_sections,
SECTION_THUMBNAIL_R13);
// thumbnail_position = before_header; // r13 if empty
}
else if (dwg->secondheader.sections[SECTION_HEADER_R13].address)
{
if (dwg->header.thumbnail_address
< dwg->secondheader.sections[SECTION_HEADER_R13].address)
{
if (section_move_top ((Dwg_Section_Type_r13 *)&section_order,
SECTION_THUMBNAIL_R13))
dwg->header.num_sections++;
section_move_top ((Dwg_Section_Type_r13 *)&section_order,
&dwg->header.num_sections,
SECTION_THUMBNAIL_R13);
// thumbnail_position = before_header; // r13 if empty
}
else if (dwg->secondheader.sections[SECTION_HANDLES_R13].address
&& dwg->header.thumbnail_address
< dwg->secondheader.sections[SECTION_HANDLES_R13]
.address)
{
if (section_move_before ((Dwg_Section_Type_r13 *)&section_order,
SECTION_THUMBNAIL_R13,
SECTION_HANDLES_R13))
dwg->header.num_sections++;
section_move_before ((Dwg_Section_Type_r13 *)&section_order,
&dwg->header.num_sections,
SECTION_THUMBNAIL_R13,
SECTION_HANDLES_R13);
}
}
}
else
{
PRE (R_13c3)
{
if (section_move_before ((Dwg_Section_Type_r13 *)&section_order,
SECTION_THUMBNAIL_R13, SECTION_HANDLES_R13))
dwg->header.num_sections++;
section_move_before ((Dwg_Section_Type_r13 *)&section_order,
&dwg->header.num_sections,
SECTION_THUMBNAIL_R13, SECTION_HANDLES_R13);
}
SINCE (R_2004)
{
if (section_move_top ((Dwg_Section_Type_r13 *)&section_order,
SECTION_THUMBNAIL_R13))
dwg->header.num_sections++;
section_move_top ((Dwg_Section_Type_r13 *)&section_order,
&dwg->header.num_sections,
SECTION_THUMBNAIL_R13);
}
}
VERSIONS (R_13b1, R_2000)
Expand All @@ -3821,16 +3836,16 @@ dwg_encode (Dwg_Data *restrict dwg, Bit_Chain *restrict dat)
&& dwg->header.thumbnail_address
> dwg->secondheader.sections[SECTION_AUXHEADER_R2000].address)
{
if (section_move_top ((Dwg_Section_Type_r13 *)&section_order,
SECTION_AUXHEADER_R2000))
dwg->header.num_sections++;
section_move_top ((Dwg_Section_Type_r13 *)&section_order,
&dwg->header.num_sections,
SECTION_AUXHEADER_R2000);
}
}
VERSION (R_13)
{
if (section_move_before ((Dwg_Section_Type_r13 *)&section_order,
SECTION_TEMPLATE_R13, SECTION_HANDLES_R13))
dwg->header.num_sections++;
section_move_before ((Dwg_Section_Type_r13 *)&section_order,
&dwg->header.num_sections,
SECTION_TEMPLATE_R13, SECTION_HANDLES_R13);
}
if (DWG_LOGLEVEL >= DWG_LOGLEVEL_TRACE)
section_order_trace (dwg->header.num_sections,
Expand Down
20 changes: 10 additions & 10 deletions test/unit-testing/encode_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// CLANG_DIAG_RESTORE
#include "tests_common.h"

unsigned size = SECTION_R13_SIZE;
BITCODE_RL size = SECTION_R13_SIZE;
Dwg_Section_Type_r13 section_order[SECTION_R13_SIZE] = { 0 };

static int
Expand Down Expand Up @@ -60,14 +60,14 @@ test_section_find (void)
section_reset ();
for (Dwg_Section_Type_r13 i = 0; (unsigned)i <= size; i++)
{
id = section_find ((Dwg_Section_Type_r13 *)&section_order, i);
id = section_find ((Dwg_Section_Type_r13 *)&section_order, size, i);
if (id != (unsigned)i) // 7 not found, returns SECTION_R13_SIZE
{
fail ("section_find %u => %u", (unsigned)i, (unsigned)id);
section_order_trace (size, (Dwg_Section_Type_r13 *)&section_order);
}
}
id = section_find ((Dwg_Section_Type_r13 *)&section_order, size + 1);
id = section_find ((Dwg_Section_Type_r13 *)&section_order, size, size + 1);
if (id != SECTION_R13_SIZE) // not found
{
fail ("section_find %u => %u", size + 1, id);
Expand All @@ -84,7 +84,7 @@ test_section_move_top (void)
for (Dwg_Section_Type_r13 i = 0; (unsigned)i < size - 1; i++)
{
// without insert
if (section_move_top ((Dwg_Section_Type_r13 *)&section_order, i))
if (section_move_top ((Dwg_Section_Type_r13 *)&section_order, &size, i))
{
err++;
fail ("section_move_top existing %u", (unsigned)i);
Expand All @@ -102,7 +102,7 @@ test_section_move_top (void)
}
}
// with insert
if (!section_move_top ((Dwg_Section_Type_r13 *)&section_order, size))
if (!section_move_top ((Dwg_Section_Type_r13 *)&section_order, &size, size))
{
err++;
fail ("section_move_top insert");
Expand Down Expand Up @@ -131,9 +131,9 @@ test_section_remove (void)
for (unsigned i = 0; i < size; i++)
{
Dwg_Section_Type_r13 id = maxrand (size);
if (section_remove ((Dwg_Section_Type_r13 *)&section_order, id))
if (section_remove ((Dwg_Section_Type_r13 *)&section_order, &size, id))
sz--;
section_order_trace (sz, (Dwg_Section_Type_r13 *)&section_order);
section_order_trace (size, (Dwg_Section_Type_r13 *)&section_order);
if (find_duplicates ())
{
err++;
Expand All @@ -148,7 +148,7 @@ test_section_remove (void)

static void test_section_move_before(void) {
int err = 0;
size = SECTION_R13_SIZE - 1;
size = SECTION_R13_SIZE - 2;
section_reset ();
for (unsigned i = 0; i < size; i++)
{
Expand All @@ -157,7 +157,7 @@ static void test_section_move_before(void) {
while (before == id)
before = maxrand(size);
if (section_move_before ((Dwg_Section_Type_r13 *)&section_order,
id, before))
&size, id, before))
{
fail ("move_before %u", (unsigned)id);
err++;
Expand All @@ -166,7 +166,7 @@ static void test_section_move_before(void) {
err += find_duplicates ();
}
if (!section_move_before ((Dwg_Section_Type_r13 *)&section_order,
6, 4))
&size, 6, 4))
{
fail ("move_before %u inserts", 6);
err++;
Expand Down

0 comments on commit 5471f05

Please sign in to comment.