| 20 |
|
#define getc getc_unlocked |
| 21 |
|
#endif |
| 22 |
|
|
| 23 |
< |
/* Create a new parameter with empty field list (comment optional) */ |
| 24 |
< |
IDF_PARAMETER * |
| 25 |
< |
idf_newparam(IDF_LOADED *idf, const char *pname, const char *comm, |
| 26 |
< |
IDF_PARAMETER *prev) |
| 23 |
> |
/* Create a new object with empty field list (comment optional) */ |
| 24 |
> |
IDF_OBJECT * |
| 25 |
> |
idf_newobject(IDF_LOADED *idf, const char *pname, const char *comm, |
| 26 |
> |
IDF_OBJECT *prev) |
| 27 |
|
{ |
| 28 |
|
LUENT *pent; |
| 29 |
< |
IDF_PARAMETER *pnew; |
| 29 |
> |
IDF_OBJECT *pnew; |
| 30 |
|
|
| 31 |
|
if ((idf == NULL) | (pname == NULL)) |
| 32 |
|
return(NULL); |
| 34 |
|
pent = lu_find(&idf->ptab, pname); |
| 35 |
|
if (pent == NULL) |
| 36 |
|
return(NULL); |
| 37 |
< |
if (pent->key == NULL) { /* new parameter name/type? */ |
| 37 |
> |
if (pent->key == NULL) { /* new object name/type? */ |
| 38 |
|
pent->key = (char *)malloc(strlen(pname)+1); |
| 39 |
|
if (pent->key == NULL) |
| 40 |
|
return(NULL); |
| 41 |
|
strcpy(pent->key, pname); |
| 42 |
|
} |
| 43 |
< |
pnew = (IDF_PARAMETER *)malloc(sizeof(IDF_PARAMETER)+strlen(comm)); |
| 43 |
> |
pnew = (IDF_OBJECT *)malloc(sizeof(IDF_OBJECT)+strlen(comm)); |
| 44 |
|
if (pnew == NULL) |
| 45 |
|
return(NULL); |
| 46 |
|
strcpy(pnew->rem, comm); |
| 47 |
+ |
pnew->nfield = 0; |
| 48 |
|
pnew->flist = NULL; |
| 49 |
|
pnew->pname = pent->key; /* add to table */ |
| 50 |
< |
pnew->pnext = (IDF_PARAMETER *)pent->data; |
| 50 |
> |
pnew->pnext = (IDF_OBJECT *)pent->data; |
| 51 |
|
pent->data = (char *)pnew; |
| 52 |
|
pnew->dnext = NULL; /* add to file list */ |
| 53 |
|
if (prev != NULL || (prev = idf->plast) != NULL) { |
| 62 |
|
return(pnew); |
| 63 |
|
} |
| 64 |
|
|
| 65 |
< |
/* Add a field to the given parameter and follow with the given text */ |
| 65 |
> |
/* Add a field to the given object and follow with the given text */ |
| 66 |
|
int |
| 67 |
< |
idf_addfield(IDF_PARAMETER *param, const char *fval, const char *comm) |
| 67 |
> |
idf_addfield(IDF_OBJECT *param, const char *fval, const char *comm) |
| 68 |
|
{ |
| 69 |
|
int fnum = 1; /* returned argument number */ |
| 70 |
|
IDF_FIELD *fnew, *flast; |
| 77 |
|
if (fnew == NULL) |
| 78 |
|
return(0); |
| 79 |
|
fnew->next = NULL; |
| 80 |
< |
cp = fnew->arg; /* copy argument and comments */ |
| 80 |
> |
cp = fnew->val; /* copy value and comments */ |
| 81 |
|
while ((*cp++ = *fval++)) |
| 82 |
|
; |
| 83 |
|
fnew->rem = cp; |
| 84 |
|
while ((*cp++ = *comm++)) |
| 85 |
|
; |
| 86 |
< |
/* add to parameter's field list */ |
| 86 |
> |
/* add to object's field list */ |
| 87 |
|
if ((flast = param->flist) != NULL) { |
| 88 |
|
++fnum; |
| 89 |
|
while (flast->next != NULL) { |
| 95 |
|
param->flist = fnew; |
| 96 |
|
else |
| 97 |
|
flast->next = fnew; |
| 98 |
+ |
param->nfield++; |
| 99 |
|
return(fnum); |
| 100 |
|
} |
| 101 |
|
|
| 102 |
< |
/* Delete the specified parameter from loaded IDF */ |
| 102 |
> |
/* Retrieve the indexed field from object (first field index is 1) */ |
| 103 |
> |
IDF_FIELD * |
| 104 |
> |
idf_getfield(IDF_OBJECT *param, int fn) |
| 105 |
> |
{ |
| 106 |
> |
IDF_FIELD *fld; |
| 107 |
> |
|
| 108 |
> |
if ((param == NULL) | (fn <= 0)) |
| 109 |
> |
return(NULL); |
| 110 |
> |
fld = param->flist; |
| 111 |
> |
while ((--fn > 0) & (fld != NULL)) |
| 112 |
> |
fld = fld->next; |
| 113 |
> |
return(fld); |
| 114 |
> |
} |
| 115 |
> |
|
| 116 |
> |
/* Delete the specified object from loaded IDF */ |
| 117 |
|
int |
| 118 |
< |
idf_delparam(IDF_LOADED *idf, IDF_PARAMETER *param) |
| 118 |
> |
idf_delobject(IDF_LOADED *idf, IDF_OBJECT *param) |
| 119 |
|
{ |
| 120 |
|
LUENT *pent; |
| 121 |
< |
IDF_PARAMETER *pptr, *plast; |
| 121 |
> |
IDF_OBJECT *pptr, *plast; |
| 122 |
|
|
| 123 |
|
if ((idf == NULL) | (param == NULL)) |
| 124 |
|
return(0); |
| 125 |
< |
/* remove from parameter table */ |
| 125 |
> |
/* remove from object table */ |
| 126 |
|
pent = lu_find(&idf->ptab, param->pname); |
| 127 |
< |
for (plast = NULL, pptr = (IDF_PARAMETER *)pent->data; |
| 127 |
> |
for (plast = NULL, pptr = (IDF_OBJECT *)pent->data; |
| 128 |
|
pptr != NULL; plast = pptr, pptr = pptr->pnext) |
| 129 |
|
if (pptr == param) |
| 130 |
|
break; |
| 136 |
|
plast->pnext = param->pnext; |
| 137 |
|
/* remove from global list */ |
| 138 |
|
for (plast = NULL, pptr = idf->pfirst; |
| 139 |
< |
pptr != NULL; plast = pptr, pptr = pptr->dnext) |
| 140 |
< |
if (pptr == param) |
| 141 |
< |
break; |
| 126 |
< |
if (pptr == NULL) |
| 127 |
< |
return(0); |
| 139 |
> |
pptr != param; plast = pptr, pptr = pptr->dnext) |
| 140 |
> |
if (pptr == NULL) |
| 141 |
> |
return(0); |
| 142 |
|
if (plast == NULL) |
| 143 |
|
idf->pfirst = param->dnext; |
| 144 |
|
else |
| 151 |
|
param->flist = fdel->next; |
| 152 |
|
free(fdel); |
| 153 |
|
} |
| 154 |
< |
free(param); /* free parameter struct */ |
| 154 |
> |
free(param); /* free object struct */ |
| 155 |
|
return(1); |
| 156 |
|
} |
| 157 |
|
|
| 158 |
< |
/* Get a named parameter list */ |
| 159 |
< |
IDF_PARAMETER * |
| 160 |
< |
idf_getparam(IDF_LOADED *idf, const char *pname) |
| 158 |
> |
/* Move the specified object to the given position in the IDF */ |
| 159 |
> |
int |
| 160 |
> |
idf_movobject(IDF_LOADED *idf, IDF_OBJECT *param, IDF_OBJECT *prev) |
| 161 |
|
{ |
| 162 |
+ |
IDF_OBJECT *pptr, *plast; |
| 163 |
+ |
|
| 164 |
+ |
if ((idf == NULL) | (param == NULL)) |
| 165 |
+ |
return(0); |
| 166 |
+ |
/* quick check if already there */ |
| 167 |
+ |
if (param == (prev==NULL ? idf->pfirst : prev->dnext)) |
| 168 |
+ |
return(1); |
| 169 |
+ |
/* find in IDF list, first*/ |
| 170 |
+ |
for (plast = NULL, pptr = idf->pfirst; |
| 171 |
+ |
pptr != param; plast = pptr, pptr = pptr->dnext) |
| 172 |
+ |
if (pptr == NULL) |
| 173 |
+ |
return(0); |
| 174 |
+ |
if (plast == NULL) |
| 175 |
+ |
idf->pfirst = param->dnext; |
| 176 |
+ |
else |
| 177 |
+ |
plast->dnext = param->dnext; |
| 178 |
+ |
if (idf->plast == param) |
| 179 |
+ |
idf->plast = plast; |
| 180 |
+ |
if (prev == NULL) { /* means they want it at beginning */ |
| 181 |
+ |
param->dnext = idf->pfirst; |
| 182 |
+ |
idf->pfirst = param; |
| 183 |
+ |
} else { |
| 184 |
+ |
param->dnext = prev->dnext; |
| 185 |
+ |
prev->dnext = param; |
| 186 |
+ |
} |
| 187 |
+ |
return(1); |
| 188 |
+ |
} |
| 189 |
+ |
|
| 190 |
+ |
/* Get a named object list */ |
| 191 |
+ |
IDF_OBJECT * |
| 192 |
+ |
idf_getobject(IDF_LOADED *idf, const char *pname) |
| 193 |
+ |
{ |
| 194 |
|
if ((idf == NULL) | (pname == NULL)) |
| 195 |
|
return(NULL); |
| 196 |
|
|
| 197 |
< |
return((IDF_PARAMETER *)lu_find(&idf->ptab,pname)->data); |
| 197 |
> |
return((IDF_OBJECT *)lu_find(&idf->ptab,pname)->data); |
| 198 |
|
} |
| 199 |
|
|
| 200 |
|
/* Read an argument including terminating ',' or ';' -- return which */ |
| 232 |
|
buf = &dummyc; |
| 233 |
|
len = 1; |
| 234 |
|
} |
| 235 |
< |
while ((c = getc(fp)) != EOF && isspace(c) | incomm) { |
| 236 |
< |
if (c == '!') |
| 237 |
< |
++incomm; |
| 192 |
< |
else if (c == '\n') |
| 235 |
> |
while ((c = getc(fp)) != EOF && |
| 236 |
> |
(isspace(c) || (incomm += (c == '!')))) { |
| 237 |
> |
if (c == '\n') |
| 238 |
|
incomm = 0; |
| 239 |
|
if (cp-buf < len-1) |
| 240 |
|
*cp++ = c; |
| 244 |
|
ungetc(c, fp); |
| 245 |
|
} |
| 246 |
|
|
| 247 |
< |
/* Read a parameter and fields from an open file and add to end of list */ |
| 248 |
< |
IDF_PARAMETER * |
| 249 |
< |
idf_readparam(IDF_LOADED *idf, FILE *fp) |
| 247 |
> |
/* Read a object and fields from an open file and add to end of list */ |
| 248 |
> |
IDF_OBJECT * |
| 249 |
> |
idf_readobject(IDF_LOADED *idf, FILE *fp) |
| 250 |
|
{ |
| 251 |
|
char abuf[IDF_MAXARGL], cbuf[IDF_MAXLINE]; |
| 252 |
|
int delim; |
| 253 |
< |
IDF_PARAMETER *pnew; |
| 253 |
> |
IDF_OBJECT *pnew; |
| 254 |
|
|
| 255 |
|
if ((delim = idf_read_argument(abuf, fp, 1)) == EOF) |
| 256 |
|
return(NULL); |
| 257 |
|
idf_read_comment(cbuf, IDF_MAXLINE, fp); |
| 258 |
< |
pnew = idf_newparam(idf, abuf, cbuf, NULL); |
| 258 |
> |
pnew = idf_newobject(idf, abuf, cbuf, NULL); |
| 259 |
|
while (delim == ',') |
| 260 |
|
if ((delim = idf_read_argument(abuf, fp, 1)) != EOF) { |
| 261 |
|
idf_read_comment(cbuf, IDF_MAXLINE, fp); |
| 262 |
|
idf_addfield(pnew, abuf, cbuf); |
| 263 |
|
} |
| 264 |
|
if (delim != ';') |
| 265 |
< |
fprintf(stderr, "Expected ';' at end of parameter list\n"); |
| 265 |
> |
fputs("Expected ';' at end of object list!\n", stderr); |
| 266 |
|
return(pnew); |
| 267 |
|
} |
| 268 |
|
|
| 301 |
|
return(idf); |
| 302 |
|
} |
| 303 |
|
|
| 304 |
+ |
/* Add comment(s) to header */ |
| 305 |
+ |
int |
| 306 |
+ |
idf_add2hdr(IDF_LOADED *idf, const char *hdrcomm) |
| 307 |
+ |
{ |
| 308 |
+ |
int olen, len; |
| 309 |
+ |
|
| 310 |
+ |
if ((idf == NULL) | (hdrcomm == NULL)) |
| 311 |
+ |
return(0); |
| 312 |
+ |
len = strlen(hdrcomm); |
| 313 |
+ |
if (!len) |
| 314 |
+ |
return(0); |
| 315 |
+ |
if (idf->hrem == NULL) |
| 316 |
+ |
olen = 0; |
| 317 |
+ |
else |
| 318 |
+ |
olen = strlen(idf->hrem); |
| 319 |
+ |
if (olen) |
| 320 |
+ |
idf->hrem = (char *)realloc(idf->hrem, olen+len+1); |
| 321 |
+ |
else |
| 322 |
+ |
idf->hrem = (char *)malloc(len+1); |
| 323 |
+ |
if (idf->hrem == NULL) |
| 324 |
+ |
return(0); |
| 325 |
+ |
strcpy(idf->hrem+olen, hdrcomm); |
| 326 |
+ |
return(1); |
| 327 |
+ |
} |
| 328 |
+ |
|
| 329 |
|
/* Load an Input Data File */ |
| 330 |
|
IDF_LOADED * |
| 331 |
|
idf_load(const char *fname) |
| 332 |
|
{ |
| 333 |
< |
char *hdrcomm; |
| 333 |
> |
char hdrcomm[256*IDF_MAXLINE]; |
| 334 |
|
FILE *fp; |
| 335 |
|
IDF_LOADED *idf; |
| 336 |
|
|
| 339 |
|
else if ((fp = fopen(fname, "r")) == NULL) |
| 340 |
|
return(NULL); |
| 341 |
|
/* read header comments */ |
| 342 |
< |
hdrcomm = (char *)malloc(100*IDF_MAXLINE); |
| 273 |
< |
idf_read_comment(hdrcomm, 100*IDF_MAXLINE, fp); |
| 342 |
> |
idf_read_comment(hdrcomm, sizeof(hdrcomm), fp); |
| 343 |
|
idf = idf_create(hdrcomm); /* create IDF struct */ |
| 275 |
– |
free(hdrcomm); |
| 344 |
|
if (idf == NULL) |
| 345 |
|
return(NULL); |
| 346 |
< |
/* read each parameter */ |
| 347 |
< |
while (idf_readparam(idf, fp) != NULL) |
| 346 |
> |
/* read each object */ |
| 347 |
> |
while (idf_readobject(idf, fp) != NULL) |
| 348 |
|
; |
| 349 |
|
if (fp != stdin) /* close file if not stdin */ |
| 350 |
|
fclose(fp); |
| 351 |
|
return(idf); /* success! */ |
| 352 |
|
} |
| 353 |
|
|
| 354 |
< |
/* Write a parameter and fields to an open file */ |
| 354 |
> |
/* Write a object and fields to an open file */ |
| 355 |
|
int |
| 356 |
< |
idf_writeparam(IDF_PARAMETER *param, FILE *fp) |
| 356 |
> |
idf_writeparam(IDF_OBJECT *param, FILE *fp, int incl_comm) |
| 357 |
|
{ |
| 358 |
|
IDF_FIELD *fptr; |
| 359 |
|
|
| 361 |
|
return(0); |
| 362 |
|
fputs(param->pname, fp); |
| 363 |
|
fputc(',', fp); |
| 364 |
< |
fputs(param->rem, fp); |
| 364 |
> |
if (incl_comm) |
| 365 |
> |
fputs(param->rem, fp); |
| 366 |
|
for (fptr = param->flist; fptr != NULL; fptr = fptr->next) { |
| 367 |
< |
fputs(fptr->arg, fp); |
| 367 |
> |
if (!incl_comm) |
| 368 |
> |
fputs("\n ", fp); |
| 369 |
> |
fputs(fptr->val, fp); |
| 370 |
|
fputc((fptr->next==NULL ? ';' : ','), fp); |
| 371 |
< |
fputs(fptr->rem, fp); |
| 371 |
> |
if (incl_comm) |
| 372 |
> |
fputs(fptr->rem, fp); |
| 373 |
|
} |
| 374 |
+ |
if (!incl_comm) |
| 375 |
+ |
fputs("\n\n", fp); |
| 376 |
|
return(!ferror(fp)); |
| 377 |
|
} |
| 378 |
|
|
| 379 |
|
/* Write out an Input Data File */ |
| 380 |
|
int |
| 381 |
< |
idf_write(IDF_LOADED *idf, const char *fname) |
| 381 |
> |
idf_write(IDF_LOADED *idf, const char *fname, int incl_comm) |
| 382 |
|
{ |
| 383 |
|
FILE *fp; |
| 384 |
< |
IDF_PARAMETER *pptr; |
| 384 |
> |
IDF_OBJECT *pptr; |
| 385 |
|
|
| 386 |
|
if (idf == NULL) |
| 387 |
|
return(0); |
| 389 |
|
fp = stdout; /* open file if not stdout */ |
| 390 |
|
else if ((fp = fopen(fname, "w")) == NULL) |
| 391 |
|
return(0); |
| 392 |
< |
fputs(idf->hrem, fp); /* write header then parameters */ |
| 392 |
> |
if (incl_comm) |
| 393 |
> |
fputs(idf->hrem, fp); /* write header then parameters */ |
| 394 |
|
for (pptr = idf->pfirst; pptr != NULL; pptr = pptr->dnext) |
| 395 |
< |
if (!idf_writeparam(pptr, fp)) { |
| 395 |
> |
if (!idf_writeparam(pptr, fp, incl_comm>0)) { |
| 396 |
|
fclose(fp); |
| 397 |
|
return(0); |
| 398 |
|
} |
| 410 |
|
if (idf->hrem != NULL) |
| 411 |
|
free(idf->hrem); |
| 412 |
|
while (idf->pfirst != NULL) |
| 413 |
< |
idf_delparam(idf, idf->pfirst); |
| 413 |
> |
idf_delobject(idf, idf->pfirst); |
| 414 |
|
lu_done(&idf->ptab); |
| 415 |
|
} |