Skip to content

Commit

Permalink
dwgadd: fix read* logic, add tests
Browse files Browse the repository at this point in the history
Fixes GH #1008. This broke with the refactoring for 0.13
Thanks to @khlukekim for the report
  • Loading branch information
rurban committed Sep 17, 2024
1 parent 27118c4 commit 8e0dd24
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 21 deletions.
44 changes: 26 additions & 18 deletions examples/dwgadd.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ static unsigned int loglevel;
# define FMT_NAME "%[a-zA-Z0-9_]"
# define FMT_TAG "%[^ !]"
# define FMT_TBL "\"%[a-zA-Z0-9._ -]\""
# define FMT_PATH "\"%[a-zA-Z0-9_. \\-]\""
# define FMT_PATH "\"%[a-zA-Z0-9_. \\-/]\""
# define FMT_ANY "\"%[^\"]\""
#else
# define SSCANF_S sscanf
# define SZ
# define FMT_NAME "%119[a-zA-Z0-9_]"
# define FMT_TAG "%119[^ !]"
# define FMT_TBL "\"%119[a-zA-Z0-9._ -]\""
# define FMT_PATH "\"%119[a-zA-Z0-9_. \\-]\""
# define FMT_PATH "\"%119[a-zA-Z0-9_. \\-/]\""
# define FMT_ANY "\"%119[^\"]\""
#endif

Expand Down Expand Up @@ -332,6 +332,7 @@ main (int argc, char *argv[])
if (!out_dat.fh)
{
LOG_ERROR ("Could not write %s", outfile)
free (dat.chain);
exit (1);
}
out_dat.from_version = dwg.header.from_version;
Expand Down Expand Up @@ -607,6 +608,12 @@ fn_error (const char *msg)
if (!hdr) \
fn_error ("Missing block header\n");

#define ADD_INITIAL \
mspace = dwg_model_space_object (dwg); \
hdr = mspace ? mspace->tio.object->tio.BLOCK_HEADER : NULL; \
orig_num = dwg->num_objects; \
initial = 0

static int
dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
{
Expand Down Expand Up @@ -729,21 +736,22 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
LOG_INSANE ("< \"%.*s\"...\n\n", 80, p);
if (memBEGINc (p, "readdwg"))
{
if (dwg)
if (!initial || dwg->num_objects)
{
LOG_ERROR ("readdwg seen, but DWG already exists");
LOG_ERROR ("readdwg seen, but DWG already initialized");
exit (1);
}
if (1 == SSCANF_S (p, "readdwg " FMT_PATH, text SZ))
{
LOG_INFO ("readdwg %s\n", text)
if ((error = dwg_read_file (text, *dwgp)) > DWG_ERR_CRITICAL)
if ((error = dwg_read_file (text, *dwgp)) >= DWG_ERR_CRITICAL)
{
LOG_ERROR ("Invalid readdwg \"%s\" => error 0x%x", text,
error);
exit (1);
}
dwg = *dwgp;
ADD_INITIAL;
p = next_line (p, end);
continue;
}
Expand All @@ -755,21 +763,22 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
}
else if (memBEGINc (p, "readdxf"))
{
if (dwg)
if (!initial || dwg->num_objects)
{
LOG_ERROR ("readdxf seen, but DWG already exists");
LOG_ERROR ("readdxf seen, but DWG already initialized");
exit (1);
}
if (1 == SSCANF_S (p, "readdxf " FMT_PATH, text SZ))
{
LOG_INFO ("readdxf %s\n", text)
if ((error = dxf_read_file (text, *dwgp)) > DWG_ERR_CRITICAL)
if ((error = dxf_read_file (text, *dwgp)) >= DWG_ERR_CRITICAL)
{
LOG_ERROR ("Invalid readdxf \"%s\" => error 0x%x", text,
error);
exit (1);
}
dwg = *dwgp;
ADD_INITIAL;
p = next_line (p, end);
continue;
}
Expand All @@ -781,9 +790,9 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
}
else if (memBEGINc (p, "readjson"))
{
if (dwg)
if (!initial || dwg->num_objects)
{
LOG_ERROR ("readjson seen, but DWG already exists");
LOG_ERROR ("readjson seen, but DWG already initialized");
exit (1);
}
if (1 == SSCANF_S (p, "readjson " FMT_PATH, text SZ))
Expand All @@ -795,14 +804,17 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
dat_read_file (&in_dat, in_dat.fh, text);
if (!in_dat.fh
|| (error = dwg_read_json (&in_dat, *dwgp))
> DWG_ERR_CRITICAL)
>= DWG_ERR_CRITICAL)
{
LOG_ERROR ("Invalid readjson \"%s\" => error 0x%x", text,
error);
free (in_dat.chain);
exit (1);
}
fclose (in_dat.fh);
free (in_dat.chain);
dwg = *dwgp;
ADD_INITIAL;
p = next_line (p, end);
continue;
}
Expand All @@ -814,7 +826,7 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
}
else if (memBEGINc (p, "imperial"))
{
if (!initial)
if (!initial || dwg->num_objects)
{
LOG_ERROR ("`imperial' directive out of header section");
exit (1);
Expand All @@ -829,7 +841,7 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
double f_ver;
char s_ver[16];

if (!initial)
if (!initial || dwg->num_objects)
{
LOG_ERROR ("`version' directive out of header section");
exit (1);
Expand Down Expand Up @@ -865,11 +877,7 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
else
dwg_add_Document (dwg, imperial);
*dwgp = dwg;

mspace = dwg_model_space_object (dwg);
hdr = mspace ? mspace->tio.object->tio.BLOCK_HEADER : NULL;
orig_num = dwg->num_objects;
initial = 0;
ADD_INITIAL;
LOG_TRACE ("==========================================\n");
}

Expand Down
45 changes: 42 additions & 3 deletions examples/dwgadd_test.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ abs_builddir="@abs_builddir@"
top_builddir="@top_builddir@"
EXEEXT="@EXEEXT@"
TESTS_ENVIRONMENT="@AM_TESTS_ENVIRONMENT@"
HAVE_ASAN="@HAVE_ASAN@"
srcdir="@abs_srcdir@"
echo srcdir: "$srcdir"

Expand Down Expand Up @@ -34,7 +35,7 @@ dwgaddtest() {
ret=$?
else
#PRE=
#if test "$V" = "r1.4" -a -n "@HAVE_ASAN@"
#if test "$V" = "r1.4" -a -n $HAVE_ASAN
#then
# PRE="env ASAN_OPTIONS=detect_leaks=0"
#fi
Expand Down Expand Up @@ -102,8 +103,46 @@ dwgaddtest AC1015 r2000 $tmp

echo "point (0 0 0)" >$tmp
cat $tmp
dwgaddtest AC1015 r2000 $tmp
dwgaddtest AC1009 r11 $tmp
dwgaddtest AC1015 r2000 $tmp

echo "readdwg \"dwgadd_test.dwg\"" >$tmp
echo "point (10 0 0)" >>$tmp
cat $tmp
dwgaddtest AC1015 r2000 $tmp

if [ -f "$abs_builddir"/../dxf ]; then
cd "$abs_builddir"/.. || exit 1
./dxf -o examples/dwgadd_test.dxf examples/dwgadd_test.dwg
rm -f -- ./dwgadd_test_examples.log
cd "$abs_builddir" || exit 1
if [ -f dwgadd_test.dxf ]; then
echo "readdxf \"dwgadd_test.dxf\"" >$tmp
echo "point (10 0 0)" >>$tmp
cat $tmp
if [ "$HAVE_ASAN" = "yes" ]
then
env ASAN_OPTIONS=detect_leaks=0 dwgaddtest AC1015 r2000 $tmp
else
dwgaddtest AC1015 r2000 $tmp
fi
fi
fi

if [ -f "$abs_builddir"/../json ]; then
cd "$abs_builddir"/.. || exit 1
cp examples/dwgadd_test.dwg .
./json dwgadd_test.dwg
mv dwgadd_test.json examples/
rm -f dwgadd_test.dwg dwgadd_test.log
cd "$abs_builddir" || exit 1
if [ -f dwgadd_test.json ]; then
echo "readjson \"dwgadd_test.json\"" >$tmp
echo "point (20 0 0)" >>$tmp
cat $tmp
dwgaddtest AC1015 r2000 $tmp
fi
fi

# cleanup
rm -f dwgadd_test.dwg $tmp
rm -f -- dwgadd_test.dwg dwgadd_test.dxf dwgadd_test.json $tmp

0 comments on commit 8e0dd24

Please sign in to comment.