Skip to content

Commit

Permalink
dwgadd: block adds the BLOCK_HEADER also
Browse files Browse the repository at this point in the history
and check for valid names before
  • Loading branch information
rurban committed Mar 10, 2024
1 parent 4495da2 commit 8169cbf
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 25 deletions.
39 changes: 33 additions & 6 deletions examples/dwgadd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1162,8 +1162,14 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
if (strlen (text) && text[strlen (text) - 1] == '"')
text[strlen (text) - 1] = '\0'; // strip the \"
CHK_MISSING_BLOCK_HEADER
ent = (lastent_t){ .u.block = dwg_add_BLOCK (hdr, text),
.type = DWG_TYPE_BLOCK };
if (!dwg_is_valid_name (dwg, text))
fn_error ("Invalid BLOCK name\n");
else
{
dwg_add_BLOCK_HEADER (dwg, text);
ent = (lastent_t){ .u.block = dwg_add_BLOCK (hdr, text),
.type = DWG_TYPE_BLOCK };
}
}
else
// clang-format off
Expand All @@ -1188,10 +1194,13 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
pt1.x, pt1.y, pt1.z, text, scale.x, scale.y, scale.z,
deg2rad (rot));
CHK_MISSING_BLOCK_HEADER
insert = ent = (lastent_t){ .u.insert = dwg_add_INSERT (
hdr, &pt1, text, scale.x, scale.y,
scale.z, deg2rad (rot)),
.type = DWG_TYPE_INSERT };
if (!dwg_is_valid_name (dwg, text))
fn_error ("Invalid BLOCK name\n");
else
insert = ent = (lastent_t){ .u.insert = dwg_add_INSERT (
hdr, &pt1, text, scale.x, scale.y,
scale.z, deg2rad (rot)),
.type = DWG_TYPE_INSERT };
}
else
// clang-format off
Expand All @@ -1212,6 +1221,8 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
hdr_s, pt1.x, pt1.y, pt1.z, text, scale.x, scale.y, scale.z,
deg2rad (rot), i1, i2, f1, f2);
CHK_MISSING_BLOCK_HEADER
if (!dwg_is_valid_name (dwg, text))
fn_error ("Invalid block name\n");
insert = ent
= (lastent_t){ .u.minsert = dwg_add_MINSERT (
hdr, &pt1, text, scale.x, scale.y, scale.z,
Expand Down Expand Up @@ -1648,6 +1659,8 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
text SZ, s1 SZ, &u))
{
LOG_TRACE ("add_DICTIONARY \"%s\" \"%s\" %u\n", text, s1, u);
if (!dwg_is_valid_name (dwg, text))
fn_error ("Invalid dictionary name\n");
dict = ent = (lastent_t){ .u.dictionary = dwg_add_DICTIONARY (
dwg, text, s1, (unsigned long)u),
.type = DWG_TYPE_DICTIONARY };
Expand All @@ -1661,6 +1674,8 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
if (dict.type != DWG_TYPE_DICTIONARY)
fn_error ("xrecord: missing dictionary\n");
LOG_TRACE ("add_XRECORD dictionary \"%s\"\n", text);
if (!dwg_is_valid_name (dwg, text))
fn_error ("Invalid dictionary name\n");
ent = (lastent_t){ .u.xrecord
= dwg_add_XRECORD (dict.u.dictionary, text),
.type = DWG_TYPE_XRECORD };
Expand Down Expand Up @@ -1688,6 +1703,8 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
{
LOG_TRACE ("add_VIEWPORT %s \"%s\"\n", hdr_s, text);
CHK_MISSING_BLOCK_HEADER
if (!dwg_is_valid_name (dwg, text))
fn_error ("Invalid table record name\n");
ent = (lastent_t){ .u.viewport = dwg_add_VIEWPORT (hdr, text),
.type = DWG_TYPE_VIEWPORT };
}
Expand Down Expand Up @@ -1796,6 +1813,8 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
if (version <= R_11)
fn_error ("Invalid entity MLINESTYLE <r13\n");
LOG_TRACE ("add_MLINESTYLE \"%s\"\n", text);
if (!dwg_is_valid_name (dwg, text))
fn_error ("Invalid style name\n");
ent = (lastent_t){ .u.mlinestyle = dwg_add_MLINESTYLE (dwg, text),
.type = DWG_TYPE_MLINESTYLE };
}
Expand All @@ -1806,6 +1825,8 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
else if (1 == SSCANF_S (p, "layer " FMT_TBL, text SZ))
{
LOG_TRACE ("add_LAYER \"%s\"\n", text);
if (!dwg_is_valid_name (dwg, text))
fn_error ("Invalid table record name\n");
ent = (lastent_t){ .u.layer = dwg_add_LAYER (dwg, text),
.type = DWG_TYPE_LAYER };
}
Expand Down Expand Up @@ -1858,6 +1879,8 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
if (version < R_11)
fn_error ("Invalid table DIMSTYLE <r11\n");
LOG_TRACE ("add_DIMSTYLE \"%s\"\n", text);
if (!dwg_is_valid_name (dwg, text))
fn_error ("Invalid table record name\n");
ent = (lastent_t){ .u.dimstyle = dwg_add_DIMSTYLE (dwg, text),
.type = DWG_TYPE_DIMSTYLE };
}
Expand All @@ -1870,6 +1893,8 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
if (version <= R_11)
fn_error ("Invalid object GROUP < r13\n");
LOG_TRACE ("add_GROUP \"%s\"\n", text);
if (!dwg_is_valid_name (dwg, text))
fn_error ("Invalid group name\n");
ent = (lastent_t){ .u.group = dwg_add_GROUP (dwg, text),
.type = DWG_TYPE_GROUP };
}
Expand Down Expand Up @@ -1906,6 +1931,8 @@ dwg_add_dat (Dwg_Data **dwgp, Bit_Chain *dat)
fn_error ("layout viewport: last entity is not a viewport\n");
if (strlen (s1) && text[strlen (s1) - 1] == '"')
text[strlen (s1) - 1] = '\0'; // strip the \"
if (!dwg_is_valid_name (dwg, text))
fn_error ("Invalid table record name\n");
if (!error)
ent = (lastent_t){ .u.layout = dwg_add_LAYOUT (obj, text, s1),
.type = DWG_TYPE_LAYOUT };
Expand Down
6 changes: 3 additions & 3 deletions examples/dwgadd.example_r10
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ HEADER.INSBASE = (1 0 0)
HEADER.LIMMIN = (12 9)
point (2 0 0)
text "test" (0.0 1.0 0.0) 8
block "bloko"
block "BLOKO"
attdef 8 0 "prompt" (0.0 1.0 0.0) TAG "default_text"
line (0 1 0) (1 1 0)
endblk
insert (0 1 0) "bloko" 1 1 1 0
insert (0 1 0) "BLOKO" 1 1 1 0
attrib 8 0 (0.0 2.0 0.0) TAG "text1"
polyline_2d 2 ((0 1) (1 1))
polyline_3d 2 ((0 1 0) (1 1 0))
Expand All @@ -29,4 +29,4 @@ dimension_linear (2 0 0) (2 1 0) (0 0 0) 90
3dface (0 0 0) (2 0 0) (2 1 0) (0 2 0)
solid (0 0 0) (2 0) (2 1) (0 2)
trace (0 0 0) (2 0) (2 1) (0 2)
shape "roman.shx" (0 0 0) 2 45
shape "ROMAN.SHX" (0 0 0) 2 45
10 changes: 5 additions & 5 deletions examples/dwgadd.example_r11
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ HEADER.INSBASE = (1 0 0)
HEADER.LIMMIN = (12 9)
point (2 0 0)
text "test" (0.0 1.0 0.0) 8
block "bloko"
block "BLOKO"
attdef 8 0 "prompt" (0.0 1.0 0.0) TAG "default_text"
line (0 1 0) (1 1 0)
endblk
insert (0 1 0) "bloko" 1 1 1 0
insert (0 1 0) "BLOKO" 1 1 1 0
attrib 8 0 (0.0 2.0 0.0) TAG "text1"
polyline_2d 2 ((0 1) (1 1))
polyline_3d 2 ((0 1 0.5) (1 1 -0.5))
Expand All @@ -29,9 +29,9 @@ dimension_linear (2 0 0) (2 1 0) (0 0 0) 90
3dface (0 0 0) (2 0 0) (2 1 0) (0 2 0)
solid (0 0 0) (2 0) (2 1) (0 2)
trace (0 0 0) (2 0) (2 1) (0 2)
shape "roman.shx" (0 0 0) 2 45
dimstyle "Dim1"
shape "ROMAN.SHX" (0 0 0) 2 45
dimstyle "DIM1"
dimstyle.DIMSCALE = 2.0
dimstyle.DIMUPT = 1
ucs (0 0 0) (1 0 0) (2.5 0 0) "Ucs1"
ucs (0 0 0) (1 0 0) (2.5 0 0) "UCS1"
ucs.ucs_elevation = 1.0
6 changes: 3 additions & 3 deletions examples/dwgadd.example_r1_4
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ HEADER.INSBASE = (1 0)
HEADER.LIMMIN = (12 9)
point (2 0 0)
text "test" (0.0 1.0 0.0) 8
block "bloko"
block "BLOKO"
line (0 1 0) (1 1 0)
endblk
insert (0 1 0) "bloko" 1 1 1 0
insert (0 1 0) "BLOKO" 1 1 1 0
arc (0.0 1.0 0.0) 0.5 0.0 3.0
circle (0.0 1.0 0.0) 0.5
solid (0 0 0) (2 0) (2 1) (0 2)
trace (0 0 0) (2 0) (2 1) (0 2)
shape "roman.shx" (0 0 0) 2 45
shape "ROMAN.SHX" (0 0 0) 2 45
6 changes: 3 additions & 3 deletions examples/dwgadd.example_r2_10
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ HEADER.INSBASE = (1 0 0)
HEADER.LIMMIN = (12 9)
point (2 0 0)
text "test" (0.0 1.0 0.0) 8
block "bloko"
block "BLOKO"
attdef 8 0 "prompt" (0.0 1.0 0.0) TAG "default_text"
line (0 1 0) (1 1 0)
endblk
insert (0 1 0) "bloko" 1 1 1 0
insert (0 1 0) "BLOKO" 1 1 1 0
attrib 8 0 (0.0 2.0 0.0) TAG "text1"
polyline_2d 2 ((0 1) (2 3))
polyline_3d 2 ((0 1 2) (3 4 5))
Expand All @@ -20,4 +20,4 @@ polyline_pface 5 3 ((0 0 0) (2 0 0) (2 2 0) (1 2 0) (1 1 0)) ((0 1 2 3) (1 2 3 4
polyline_mesh 3 2 ((0 0 0) (2 0 0) (2 2 0) (1 2 0) (1 1 0) (0 1 0))
solid (0 0 0) (2 0) (2 1) (0 2)
trace (0 0 0) (2 0) (2 1) (0 2)
shape "roman.shx" (0 0 0) 2 45
shape "ROMAN.SHX" (0 0 0) 2 45
23 changes: 18 additions & 5 deletions src/dwg_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -22631,6 +22631,7 @@ dwg_add_Document (Dwg_Data *restrict dwg, const int imperial)
}
if (version > R_11)
{
const char *standard = dwg->header.version < R_13 ? "STANDARD" : "Standard";
// LAYER: (0.1.10)
layer = dwg_add_LAYER (dwg, (const BITCODE_T) "0");
layer->color = (BITCODE_CMC){ 7, CMC_DEFAULTS };
Expand All @@ -22644,7 +22645,7 @@ dwg_add_Document (Dwg_Data *restrict dwg, const int imperial)
// if (ctrl)
// dwg->layer_control = ctrl->tio.object->tio.LAYER_CONTROL;
// STYLE: (0.1.11)
style = dwg_add_STYLE (dwg, "Standard");
style = dwg_add_STYLE (dwg, standard);
style->font_file = dwg_add_u8_input (dwg, "txt");
style->last_height = 0.2;
// TEXTSTYLE: (5.1.11) [H 7]
Expand Down Expand Up @@ -23403,6 +23404,11 @@ dwg_add_INSERT (Dwg_Object_BLOCK_HEADER *restrict blkhdr,
// TODO scale_flag
_obj->rotation = rotation;
ADD_CHECK_ANGLE (_obj->rotation);
if (!dwg_is_valid_name (dwg, name)) {
API_UNADD_ENTITY;
LOG_ERROR("Invalid blockname %s", name);
return NULL;
}
hdrref = dwg_find_tablehandle (dwg, name, "BLOCK");
if (hdrref)
{
Expand All @@ -23422,6 +23428,12 @@ dwg_add_INSERT (Dwg_Object_BLOCK_HEADER *restrict blkhdr,
blkhdr->inserts[blkhdr->num_inserts - 1]
= dwg_add_handleref (dwg, 4, obj->handle.value, NULL);
}
else
{
API_UNADD_ENTITY;
LOG_ERROR("block %s not found", name);
return NULL;
}
if (dwg->header.version < R_2_0b)
_obj->block_name = strdup (name);
if (dwg->header.version <= R_12)
Expand Down Expand Up @@ -24005,9 +24017,10 @@ dwg_add_LINE (Dwg_Object_BLOCK_HEADER *restrict blkhdr,
static void
dwg_require_DIMSTYLE_Standard (Dwg_Data *restrict dwg)
{
if (!(dwg_find_tablehandle_silent (dwg, "Standard", "DIMSTYLE")))
const char *standard = dwg->header.version < R_13 ? "STANDARD" : "Standard";
if (!(dwg_find_tablehandle_silent (dwg, standard, "DIMSTYLE")))
{
Dwg_Object_DIMSTYLE *std = dwg_add_DIMSTYLE (dwg, "Standard");
Dwg_Object_DIMSTYLE *std = dwg_add_DIMSTYLE (dwg, standard);
if (std)
dwg->header_vars.DIMSTYLE = dwg_add_handleref (
dwg, 5, dwg_obj_generic_handlevalue (std), NULL);
Expand Down Expand Up @@ -25302,7 +25315,7 @@ dwg_add_APPID (Dwg_Data *restrict dwg, const char *restrict name)
EXPORT Dwg_Object_DIMSTYLE *
dwg_add_DIMSTYLE (Dwg_Data *restrict dwg, const char *restrict name)
{
if (name && strNE (name, "Standard"))
if (name && strNE (name, dwg->header.version < R_13 ? "STANDARD" : "Standard"))
dwg_require_DIMSTYLE_Standard (dwg);
{
API_ADD_TABLE (DIMSTYLE, DIMSTYLE_CONTROL, {
Expand Down Expand Up @@ -25466,7 +25479,7 @@ dwg_add_MLINESTYLE (Dwg_Data *restrict dwg, const char *restrict name)
}
}

_obj->name = strEQc (name, "Standard") ? dwg_add_u8_input (dwg, "STANDARD")
_obj->name = strEQc (name, "Standard") ? dwg_add_u8_input (dwg, "Standard")
: dwg_add_u8_input (dwg, name);
_obj->fill_color = (BITCODE_CMC){ 256, CMC_DEFAULTS };
if (strEQc (name, "Standard") || strEQc (name, "STANDARD"))
Expand Down

0 comments on commit 8169cbf

Please sign in to comment.