| 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; |
| 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) { |
| 99 |
|
return(fnum); |
| 100 |
|
} |
| 101 |
|
|
| 102 |
< |
/* Retrieve the indexed field from parameter (first field index is 1) */ |
| 102 |
> |
/* Retrieve the indexed field from object (first field index is 1) */ |
| 103 |
|
IDF_FIELD * |
| 104 |
< |
idf_getfield(IDF_PARAMETER *param, int fn) |
| 104 |
> |
idf_getfield(IDF_OBJECT *param, int fn) |
| 105 |
|
{ |
| 106 |
|
IDF_FIELD *fld; |
| 107 |
|
|
| 113 |
|
return(fld); |
| 114 |
|
} |
| 115 |
|
|
| 116 |
< |
/* Delete the specified parameter from loaded IDF */ |
| 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; |
| 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 |
< |
/* Move the specified parameter to the given position in the IDF */ |
| 158 |
> |
/* Move the specified object to the given position in the IDF */ |
| 159 |
|
int |
| 160 |
< |
idf_movparam(IDF_LOADED *idf, IDF_PARAMETER *param, IDF_PARAMETER *prev) |
| 160 |
> |
idf_movobject(IDF_LOADED *idf, IDF_OBJECT *param, IDF_OBJECT *prev) |
| 161 |
|
{ |
| 162 |
< |
IDF_PARAMETER *pptr, *plast; |
| 162 |
> |
IDF_OBJECT *pptr, *plast; |
| 163 |
|
|
| 164 |
|
if ((idf == NULL) | (param == NULL)) |
| 165 |
|
return(0); |
| 187 |
|
return(1); |
| 188 |
|
} |
| 189 |
|
|
| 190 |
< |
/* Get a named parameter list */ |
| 191 |
< |
IDF_PARAMETER * |
| 192 |
< |
idf_getparam(IDF_LOADED *idf, const char *pname) |
| 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 */ |
| 225 |
|
{ |
| 226 |
|
int incomm = 0; |
| 227 |
|
char *cp = buf; |
| 228 |
< |
char dummyc; |
| 228 |
> |
char dummys[2]; |
| 229 |
|
int c; |
| 230 |
|
|
| 231 |
|
if ((buf == NULL) | (len <= 0)) { |
| 232 |
< |
buf = &dummyc; |
| 233 |
< |
len = 1; |
| 232 |
> |
buf = dummys; |
| 233 |
> |
len = sizeof(dummys); |
| 234 |
|
} |
| 235 |
|
while ((c = getc(fp)) != EOF && |
| 236 |
|
(isspace(c) || (incomm += (c == '!')))) { |
| 237 |
|
if (c == '\n') |
| 238 |
|
incomm = 0; |
| 239 |
< |
if (cp-buf < len-1) |
| 239 |
> |
if (cp-buf < len-2) |
| 240 |
|
*cp++ = c; |
| 241 |
+ |
else if (cp-buf == len-2) |
| 242 |
+ |
*cp++ = '\n'; |
| 243 |
|
} |
| 244 |
|
*cp = '\0'; |
| 245 |
|
if (c != EOF) |
| 246 |
|
ungetc(c, fp); |
| 247 |
|
} |
| 248 |
|
|
| 249 |
< |
/* Read a parameter and fields from an open file and add to end of list */ |
| 250 |
< |
IDF_PARAMETER * |
| 251 |
< |
idf_readparam(IDF_LOADED *idf, FILE *fp) |
| 249 |
> |
/* Read a object and fields from an open file and add to end of list */ |
| 250 |
> |
IDF_OBJECT * |
| 251 |
> |
idf_readobject(IDF_LOADED *idf, FILE *fp) |
| 252 |
|
{ |
| 253 |
< |
char abuf[IDF_MAXARGL], cbuf[IDF_MAXLINE]; |
| 253 |
> |
char abuf[IDF_MAXARGL], cbuf[100*IDF_MAXLINE]; |
| 254 |
|
int delim; |
| 255 |
< |
IDF_PARAMETER *pnew; |
| 255 |
> |
IDF_OBJECT *pnew; |
| 256 |
|
|
| 257 |
|
if ((delim = idf_read_argument(abuf, fp, 1)) == EOF) |
| 258 |
|
return(NULL); |
| 259 |
< |
idf_read_comment(cbuf, IDF_MAXLINE, fp); |
| 260 |
< |
pnew = idf_newparam(idf, abuf, cbuf, NULL); |
| 259 |
> |
idf_read_comment(cbuf, sizeof(cbuf), fp); |
| 260 |
> |
pnew = idf_newobject(idf, abuf, cbuf, NULL); |
| 261 |
|
while (delim == ',') |
| 262 |
|
if ((delim = idf_read_argument(abuf, fp, 1)) != EOF) { |
| 263 |
< |
idf_read_comment(cbuf, IDF_MAXLINE, fp); |
| 263 |
> |
idf_read_comment(cbuf, sizeof(cbuf), fp); |
| 264 |
|
idf_addfield(pnew, abuf, cbuf); |
| 265 |
|
} |
| 266 |
|
if (delim != ';') |
| 267 |
< |
fputs("Expected ';' at end of parameter list!\n", stderr); |
| 267 |
> |
fputs("Expected ';' at end of object list!\n", stderr); |
| 268 |
|
return(pnew); |
| 269 |
|
} |
| 270 |
|
|
| 345 |
|
idf = idf_create(hdrcomm); /* create IDF struct */ |
| 346 |
|
if (idf == NULL) |
| 347 |
|
return(NULL); |
| 348 |
< |
/* read each parameter */ |
| 349 |
< |
while (idf_readparam(idf, fp) != NULL) |
| 348 |
> |
/* read each object */ |
| 349 |
> |
while (idf_readobject(idf, fp) != NULL) |
| 350 |
|
; |
| 351 |
|
if (fp != stdin) /* close file if not stdin */ |
| 352 |
|
fclose(fp); |
| 353 |
|
return(idf); /* success! */ |
| 354 |
|
} |
| 355 |
|
|
| 356 |
< |
/* Write a parameter and fields to an open file */ |
| 356 |
> |
/* Check string for end-of-line */ |
| 357 |
> |
static int |
| 358 |
> |
idf_hasEOL(const char *s) |
| 359 |
> |
{ |
| 360 |
> |
while (*s) |
| 361 |
> |
if (*s++ == '\n') |
| 362 |
> |
return(1); |
| 363 |
> |
return(0); |
| 364 |
> |
} |
| 365 |
> |
|
| 366 |
> |
/* Write a object and fields to an open file */ |
| 367 |
|
int |
| 368 |
< |
idf_writeparam(IDF_PARAMETER *param, FILE *fp, int incl_comm) |
| 368 |
> |
idf_writeparam(IDF_OBJECT *param, FILE *fp, int incl_comm) |
| 369 |
|
{ |
| 370 |
|
IDF_FIELD *fptr; |
| 371 |
|
|
| 375 |
|
fputc(',', fp); |
| 376 |
|
if (incl_comm) |
| 377 |
|
fputs(param->rem, fp); |
| 378 |
+ |
else |
| 379 |
+ |
fputc('\n', fp); |
| 380 |
|
for (fptr = param->flist; fptr != NULL; fptr = fptr->next) { |
| 381 |
|
if (!incl_comm) |
| 382 |
< |
fputs("\n ", fp); |
| 382 |
> |
fputs(" ", fp); |
| 383 |
|
fputs(fptr->val, fp); |
| 384 |
< |
fputc((fptr->next==NULL ? ';' : ','), fp); |
| 384 |
> |
if (fptr->next == NULL) { |
| 385 |
> |
fputc(';', fp); |
| 386 |
> |
if (incl_comm && !idf_hasEOL(fptr->rem)) |
| 387 |
> |
fputc('\n', fp); |
| 388 |
> |
} else |
| 389 |
> |
fputc(',', fp); |
| 390 |
|
if (incl_comm) |
| 391 |
|
fputs(fptr->rem, fp); |
| 392 |
+ |
else |
| 393 |
+ |
fputc('\n', fp); |
| 394 |
|
} |
| 395 |
|
if (!incl_comm) |
| 396 |
< |
fputs("\n\n", fp); |
| 396 |
> |
fputc('\n', fp); |
| 397 |
|
return(!ferror(fp)); |
| 398 |
|
} |
| 399 |
|
|
| 402 |
|
idf_write(IDF_LOADED *idf, const char *fname, int incl_comm) |
| 403 |
|
{ |
| 404 |
|
FILE *fp; |
| 405 |
< |
IDF_PARAMETER *pptr; |
| 405 |
> |
IDF_OBJECT *pptr; |
| 406 |
|
|
| 407 |
|
if (idf == NULL) |
| 408 |
|
return(0); |
| 431 |
|
if (idf->hrem != NULL) |
| 432 |
|
free(idf->hrem); |
| 433 |
|
while (idf->pfirst != NULL) |
| 434 |
< |
idf_delparam(idf, idf->pfirst); |
| 434 |
> |
idf_delobject(idf, idf->pfirst); |
| 435 |
|
lu_done(&idf->ptab); |
| 436 |
|
} |