1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
/*
* COPYRIGHT
*
* pcb-rnd, interactive printed circuit board design
* Copyright (C) 2020 Tibor 'Igor2' Palinkas
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Contact:
* Project page: http://repo.hu/projects/pcb-rnd
* lead developer: http://repo.hu/projects/pcb-rnd/contact.html
* mailing list: pcb-rnd (at) list.repo.hu (send "subscribe")
*/
/* Query language - listing functions */
#define PCB dontuse
static int fnc_mklist(pcb_qry_exec_t *ectx, int argc, pcb_qry_val_t *argv, pcb_qry_val_t *res)
{
int n;
res->type = PCBQ_VT_LST;
vtp0_init(&res->data.lst);
for(n = 0; n < argc; n++) {
if (argv[n].type == PCBQ_VT_OBJ)
vtp0_append(&res->data.lst, argv[n].data.obj);
}
return 0;
}
static int fnc_violation(pcb_qry_exec_t *ectx, int argc, pcb_qry_val_t *argv, pcb_qry_val_t *res)
{
int n;
if ((argc < 2) || (argc % 2 != 0))
return -1;
/* argument sanity checks */
for(n = 0; n < argc; n+=2) {
pcb_qry_val_t *val = &argv[n+1];
pcb_qry_drc_ctrl_t ctrl = pcb_qry_drc_ctrl_decode(argv[n].data.obj);
switch(ctrl) {
case PCB_QRY_DRC_GRP1:
case PCB_QRY_DRC_GRP2:
if (val->type != PCBQ_VT_OBJ)
return -1;
break;
case PCB_QRY_DRC_EXPECT:
case PCB_QRY_DRC_MEASURE:
break; /* accept as is */
case PCB_QRY_DRC_TEXT:
break; /* accept anything */
default:
return -1;
}
}
res->type = PCBQ_VT_LST;
vtp0_init(&res->data.lst);
for(n = 0; n < argc; n+=2) {
pcb_qry_drc_ctrl_t ctrl = pcb_qry_drc_ctrl_decode(argv[n].data.obj);
pcb_qry_val_t *val = &argv[n+1];
pcb_obj_qry_const_t *tmp;
if (ctrl == PCB_QRY_DRC_TEXT) {
char *str = "<invalid node>", buff[128];
switch(val->type) {
case PCBQ_VT_VOID: str = "<void>"; break;
case PCBQ_VT_OBJ: str = "<obj>"; break;
case PCBQ_VT_LST: str = "<list>"; break;
case PCBQ_VT_COORD: rnd_snprintf(buff, sizeof(buff), "%mH$", argv[n+1].data.crd); str = buff; break;
case PCBQ_VT_LONG: rnd_snprintf(buff, sizeof(buff), "%ld", argv[n+1].data.lng); str = buff; break;
case PCBQ_VT_DOUBLE: rnd_snprintf(buff, sizeof(buff), "%f", argv[n+1].data.dbl); str = buff; break;
case PCBQ_VT_STRING: str = (char *)argv[n+1].data.str; break;
}
str = rnd_strdup(str == NULL ? "" : str);
vtp0_append(&res->data.lst, argv[n].data.obj);
vtp0_append(&res->data.lst, str);
vtp0_append(&ectx->autofree, str);
}
else if ((ctrl == PCB_QRY_DRC_EXPECT) || (ctrl == PCB_QRY_DRC_MEASURE)) {
tmp = malloc(sizeof(pcb_obj_qry_const_t));
tmp->val = *val;
tmp->type = PCB_OBJ_QRY_CONST;
vtp0_append(&res->data.lst, argv[n].data.obj);
vtp0_append(&res->data.lst, tmp);
vtp0_append(&ectx->autofree, tmp);
}
else switch(val->type) {
case PCBQ_VT_OBJ:
vtp0_append(&res->data.lst, argv[n].data.obj);
vtp0_append(&res->data.lst, argv[n+1].data.obj);
break;
case PCBQ_VT_COORD:
case PCBQ_VT_LONG:
case PCBQ_VT_DOUBLE:
tmp = malloc(sizeof(pcb_obj_qry_const_t));
memcpy(&tmp->val, val, sizeof(pcb_qry_val_t));
vtp0_append(&res->data.lst, argv[n].data.obj);
vtp0_append(&res->data.lst, tmp);
vtp0_append(&ectx->autofree, tmp);
break;
case PCBQ_VT_VOID:
case PCBQ_VT_LST:
case PCBQ_VT_STRING:
/* can't be on a violation list at the moment */
break;
}
}
return 0;
}
static int fnc_netlist(pcb_qry_exec_t *ectx, int argc, pcb_qry_val_t *argv, pcb_qry_val_t *res)
{
long n;
htsp_entry_t *e;
pcb_netlist_t *nl = &ectx->pcb->netlist[PCB_NETLIST_EDITED];
res->type = PCBQ_VT_LST;
vtp0_init(&res->data.lst);
if (nl->used != 0)
for(e = htsp_first(nl), n = 0; e != NULL; e = htsp_next(nl, e), n++)
vtp0_append(&res->data.lst, e->value);
return 0;
}
static int fnc_layerlist(pcb_qry_exec_t *ectx, int argc, pcb_qry_val_t *argv, pcb_qry_val_t *res)
{
long n;
res->type = PCBQ_VT_LST;
vtp0_init(&res->data.lst);
for(n = 0; n < ectx->pcb->Data->LayerN; n++)
vtp0_append(&res->data.lst, &ectx->pcb->Data->Layer[n]);
return 0;
}
static int fnc_net_cb(pcb_find_t *fctx, pcb_any_obj_t *new_obj, pcb_any_obj_t *arrived_from, pcb_found_conn_type_t ctype)
{
vtp0_t *v = fctx->user_data;
vtp0_append(v, new_obj);
return 0;
}
static void fnc_find_init(pcb_qry_exec_t *ectx, pcb_find_t *fctx, vtp0_t *resvt, int rats)
{
memset(fctx, 0, sizeof(pcb_find_t));
fctx->consider_rats = rats;
if (resvt != NULL) {
fctx->found_cb = fnc_net_cb;
fctx->user_data = resvt;
}
pcb_find_from_obj(fctx, ectx->pcb->Data, NULL);
}
static void fnc_find_from(pcb_qry_exec_t *ectx, pcb_find_t *fctx, pcb_any_obj_t *from)
{
if (!PCB_FIND_IS_MARKED(fctx, from))
pcb_find_from_obj_next(fctx, ectx->pcb->Data, from);
}
static int fnc_net_any(pcb_qry_exec_t *ectx, int argc, pcb_qry_val_t *argv, pcb_qry_val_t *res, int find_all, int netseg)
{
pcb_net_t *net;
pcb_net_term_t *t;
pcb_find_t fctx;
if ((argv[0].type != PCBQ_VT_OBJ) || (argv[0].data.obj->type != PCB_OBJ_NET))
return -1;
if (find_all)
fnc_find_init(ectx, &fctx, netseg ? NULL : &res->data.lst, 0);
res->type = PCBQ_VT_LST;
vtp0_init(&res->data.lst);
net = (pcb_net_t *)argv[0].data.obj;
for(t = pcb_termlist_first(&net->conns); t != NULL; t = pcb_termlist_next(t)) {
pcb_any_obj_t *o = pcb_term_find_name(ectx->pcb, ectx->pcb->Data, PCB_LYT_COPPER, t->refdes, t->term, NULL, NULL);
if (o != NULL) {
if (find_all) {
if (netseg && !PCB_FIND_IS_MARKED(&fctx, o)) /* report first (unmarked) object of the seg */
vtp0_append(&res->data.lst, o);
fnc_find_from(ectx, &fctx, o); /* mark all objects on this seg, potentiall also reporting them */
}
else
vtp0_append(&res->data.lst, o); /* no search required, just report all terminals */
}
}
if (find_all)
pcb_find_free(&fctx);
return 0;
}
static int fnc_netterms(pcb_qry_exec_t *ectx, int argc, pcb_qry_val_t *argv, pcb_qry_val_t *res)
{
return fnc_net_any(ectx, argc, argv, res, 0, 0);
}
static int fnc_netobjs(pcb_qry_exec_t *ectx, int argc, pcb_qry_val_t *argv, pcb_qry_val_t *res)
{
return fnc_net_any(ectx, argc, argv, res, 1, 0);
}
static int fnc_netsegs(pcb_qry_exec_t *ectx, int argc, pcb_qry_val_t *argv, pcb_qry_val_t *res)
{
return fnc_net_any(ectx, argc, argv, res, 1, 1);
}
static int fnc_subcobjs(pcb_qry_exec_t *ectx, int argc, pcb_qry_val_t *argv, pcb_qry_val_t *res)
{
if ((argv[0].type != PCBQ_VT_OBJ) || (argv[0].data.obj->type != PCB_OBJ_SUBC))
return -1;
res->type = PCBQ_VT_LST;
vtp0_init(&res->data.lst);
pcb_qry_list_all_subc(res, (pcb_subc_t *)argv[0].data.obj, PCB_OBJ_ANY & (~PCB_OBJ_LAYER));
return 0;
}
|