2 |
|
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
|
/* |
5 |
< |
* Wrap BSDF data in valid WINDOW XML wrapper |
5 |
> |
* Wrap BSDF data in valid WINDOW XML file |
6 |
|
* |
7 |
< |
* G. Ward January 2015 |
7 |
> |
* G. Ward February 2015 |
8 |
|
*/ |
9 |
|
|
10 |
|
#include <ctype.h> |
22 |
|
/* input files (can be stdin_name) */ |
23 |
|
const char *xml_input = NULL; |
24 |
|
/* unit for materials & geometry */ |
25 |
< |
const char *attr_unit = "millimeter"; |
25 |
> |
const char *attr_unit = "meter"; |
26 |
|
const char legal_units[] = "meter|foot|inch|centimeter|millimeter"; |
27 |
|
/* system materials & geometry */ |
28 |
|
const char *mgf_geometry = NULL; |
49 |
|
{"t", 1, "Thickness"}, |
50 |
|
{"h", 1, "Height"}, |
51 |
|
{"w", 1, "Width"}, |
52 |
< |
{"\0", -1, NULL} /* terminator */ |
52 |
> |
{"\0", 0, NULL} /* terminator */ |
53 |
|
}; |
54 |
|
/* field assignments */ |
55 |
|
#define MAXASSIGN 12 |
130 |
|
return ""; |
131 |
|
if (inpspec == stdin_name) { /* read from stdin */ |
132 |
|
fp = stdin; |
133 |
< |
} else if (*inpspec == '!') { /* read from command */ |
133 |
> |
} else if (inpspec[0] == '!') { /* read from command */ |
134 |
|
fp = popen(inpspec+1, "r"); |
135 |
|
if (fp == NULL) { |
136 |
|
fprintf(stderr, "Cannot start process '%s'\n", |
137 |
< |
inpspec+1); |
137 |
> |
inpspec); |
138 |
|
return ""; |
139 |
|
} |
140 |
|
} else { /* else load file */ |
186 |
|
if (str == NULL) |
187 |
|
goto memerr; |
188 |
|
} |
189 |
< |
if (*inpspec != '!') |
189 |
> |
if (inpspec[0] != '!') |
190 |
|
fclose(fp); |
191 |
|
else if (pclose(fp)) |
192 |
< |
fprintf(stderr, "Error running command '%s'\n", inpspec+1); |
192 |
> |
fprintf(stderr, "Error running command '%s'\n", inpspec); |
193 |
|
return str; |
194 |
|
memerr: |
195 |
|
fprintf(stderr, "%s: error allocating memory\n", inpspec); |
196 |
|
if (fp != NULL) |
197 |
< |
(*inpspec == '!') ? pclose(fp) : fclose(fp); |
197 |
> |
(inpspec[0] == '!') ? pclose(fp) : fclose(fp); |
198 |
|
return ""; |
199 |
|
} |
200 |
|
|
232 |
|
} |
233 |
|
sbuf[j++] = *fnext++; |
234 |
|
} |
235 |
< |
sbuf[j] = '\0'; /* check nick-names */ |
235 |
> |
sbuf[j] = '\0'; /* check known field */ |
236 |
|
for (j = 0; XMLfieldID[j].nickName[0]; j++) |
237 |
< |
if (!strcasecmp(sbuf, XMLfieldID[j].nickName)) { |
237 |
> |
if (!strcasecmp(sbuf, XMLfieldID[j].nickName) || |
238 |
> |
!strcasecmp(sbuf, XMLfieldID[j].fullName)) { |
239 |
|
strcpy(sbuf, XMLfieldID[j].fullName); |
240 |
|
break; |
241 |
|
} |
434 |
|
fflush(stdout); |
435 |
|
if (df->fname == stdin_name) { |
436 |
|
copy_and_close(fileno(stdin)); |
437 |
< |
} else if (*df->fname != '!') { |
437 |
> |
} else if (df->fname[0] != '!') { |
438 |
|
if (!copy_and_close(open(df->fname, O_RDONLY))) { |
439 |
|
fprintf(stderr, "%s: error reading from '%s'\n", |
440 |
|
caller, df->fname); |
441 |
|
return 0; |
442 |
|
} |
443 |
|
} else if (system(df->fname+1)) { |
444 |
< |
fprintf(stderr, "%s: error running '%s'\n", caller, df->fname+1); |
444 |
> |
fprintf(stderr, "%s: error running '%s'\n", caller, df->fname); |
445 |
|
return 0; |
446 |
|
} |
447 |
|
puts("\t\t\t</ScatteringData>"); |
454 |
|
static int |
455 |
|
writeBSDF(const char *caller, ezxml_t fl) |
456 |
|
{ |
457 |
< |
char *xml = ezxml_toxml(fl); |
457 |
> |
char *xml = ezxml_toxml(fl); /* store XML in string */ |
458 |
|
int ei, i; |
459 |
|
/* locate trailer */ |
460 |
|
for (ei = strlen(xml)-strlen("</Layer></Optical></WindowElement>"); |
478 |
|
return 0; |
479 |
|
} |
480 |
|
fputs(xml+ei, stdout); /* write trailer */ |
481 |
< |
free(xml); |
481 |
> |
free(xml); /* free string */ |
482 |
|
return (fflush(stdout) == 0); |
483 |
|
} |
484 |
|
|
486 |
|
static int |
487 |
|
wrapBSDF(const char *caller) |
488 |
|
{ |
489 |
< |
const char *xml_path = getpath((char *)xml_input, getrlibpath(), R_OK); |
489 |
> |
const char *xml_path = xml_input; |
490 |
|
ezxml_t fl, wtl; |
491 |
|
/* load previous XML/template */ |
492 |
< |
if (xml_path == NULL) { |
493 |
< |
fprintf(stderr, "%s: cannot find XML file named '%s'\n", |
494 |
< |
caller, xml_input==NULL ? "NULL" : xml_input); |
495 |
< |
return 0; |
492 |
> |
if (xml_input == stdin_name) { |
493 |
> |
fl = ezxml_parse_fp(stdin); |
494 |
> |
} else if (xml_input[0] == '!') { |
495 |
> |
FILE *pfp = popen(xml_input+1, "r"); |
496 |
> |
if (pfp == NULL) { |
497 |
> |
fprintf(stderr, "%s: cannot start process '%s'\n", |
498 |
> |
caller, xml_input); |
499 |
> |
return 0; |
500 |
> |
} |
501 |
> |
fl = ezxml_parse_fp(pfp); |
502 |
> |
if (pclose(pfp)) { |
503 |
> |
fprintf(stderr, "%s: error running '%s'\n", |
504 |
> |
caller, xml_input); |
505 |
> |
return 0; |
506 |
> |
} |
507 |
> |
} else { |
508 |
> |
xml_path = getpath((char *)xml_input, getrlibpath(), R_OK); |
509 |
> |
if (xml_path == NULL) { |
510 |
> |
fprintf(stderr, "%s: cannot find XML file named '%s'\n", |
511 |
> |
caller, xml_input==NULL ? "NULL" : xml_input); |
512 |
> |
return 0; |
513 |
> |
} |
514 |
> |
fl = ezxml_parse_file(xml_path); |
515 |
|
} |
496 |
– |
fl = ezxml_parse_file(xml_path); |
516 |
|
if (fl == NULL) { |
517 |
< |
fprintf(stderr, "%s: cannot open XML path '%s'\n", |
499 |
< |
caller, xml_path); |
517 |
> |
fprintf(stderr, "%s: cannot load XML '%s'\n", caller, xml_path); |
518 |
|
return 0; |
519 |
|
} |
520 |
|
if (ezxml_error(fl)[0]) { |
521 |
< |
fprintf(stderr, "%s: error in XML %s: %s\n", caller, xml_path, |
521 |
> |
fprintf(stderr, "%s: error in XML '%s': %s\n", caller, xml_path, |
522 |
|
ezxml_error(fl)); |
523 |
|
goto failure; |
524 |
|
} |
598 |
|
{ |
599 |
|
fputs("Usage: ", stderr); |
600 |
|
fputs(pname, stderr); |
601 |
< |
fputs(" [options] [input.xml]\n", stderr); |
601 |
> |
fputs(" [-W][-a {kf|kh|kq|t3|t4}][-u unit][-g geom][-f 'x=string;y=string']", stderr); |
602 |
> |
fputs(" [-s spectr][-tb inp][-tf inp][-rb inp][-rf inp]", stderr); |
603 |
> |
fputs(" [input.xml]\n", stderr); |
604 |
|
exit(1); |
605 |
|
} |
606 |
|
|
637 |
|
argv[0]); |
638 |
|
return 1; |
639 |
|
} |
640 |
< |
if (strstr(argv[i], legal_units) == NULL) { |
641 |
< |
fprintf(stderr, "%s: unit must be one of (%s)\n", |
640 |
> |
if (strstr(legal_units, argv[i]) == NULL) { |
641 |
> |
fprintf(stderr, "%s: -u unit must be one of (%s)\n", |
642 |
|
argv[0], legal_units); |
643 |
|
return 1; |
644 |
|
} |