10 |
|
*/ |
11 |
|
|
12 |
|
#include <unistd.h> |
13 |
+ |
#include <ctype.h> |
14 |
|
#include "RtraceSimulManager.h" |
15 |
|
#include "source.h" |
16 |
|
|
23 |
|
return true; |
24 |
|
Cleanup(); |
25 |
|
} |
26 |
< |
if (!octn) |
26 |
> |
if (!octn) // don't support stdin octree |
27 |
|
return false; |
28 |
|
|
29 |
+ |
NewHeader(octn); // load header if we can |
30 |
|
ray_init((char *)octn); |
31 |
+ |
return true; |
32 |
+ |
} |
33 |
+ |
|
34 |
+ |
// callback function for loading header |
35 |
+ |
static int |
36 |
+ |
add2header(char *str, void *p) |
37 |
+ |
{ |
38 |
+ |
if (isheadid(str)) |
39 |
+ |
return 0; |
40 |
+ |
if (isformat(str)) |
41 |
+ |
return 0; |
42 |
+ |
|
43 |
+ |
return (*(RadSimulManager *)p).AddHeader(str); |
44 |
+ |
} |
45 |
+ |
|
46 |
+ |
// Prepare header from previous input (or clear) |
47 |
+ |
// Normally called during octree load |
48 |
+ |
bool |
49 |
+ |
RadSimulManager::NewHeader(const char *fname) |
50 |
+ |
{ |
51 |
+ |
if (header) { |
52 |
+ |
free(header); header = NULL; |
53 |
+ |
} |
54 |
+ |
if (!fname || fname[0] == '!') |
55 |
+ |
return false; |
56 |
+ |
FILE *fp = fopen(fname, "rb"); |
57 |
+ |
bool ok = (getheader(fp, add2header, this) >= 0); |
58 |
+ |
fclose(fp); |
59 |
+ |
return ok; |
60 |
+ |
} |
61 |
+ |
|
62 |
+ |
// Add a string to header (adds newline if none) |
63 |
+ |
bool |
64 |
+ |
RadSimulManager::AddHeader(const char *str) |
65 |
+ |
{ |
66 |
+ |
if (!str) return false; |
67 |
+ |
int len = strlen(str); |
68 |
+ |
if (!len || str[0] == '\n') return false; |
69 |
+ |
int olen = 0; |
70 |
+ |
if (header) { |
71 |
+ |
olen = strlen(header); |
72 |
+ |
header = (char *)realloc(header, olen+len+2); |
73 |
+ |
} else |
74 |
+ |
header = (char *)malloc(len+2); |
75 |
+ |
if (!header) return false; |
76 |
+ |
strcpy(header+olen, str); |
77 |
+ |
if (str[len-1] != '\n') { |
78 |
+ |
header[olen+len++] = '\n'; |
79 |
+ |
header[olen+len] = '\0'; |
80 |
+ |
} |
81 |
+ |
return true; |
82 |
+ |
} |
83 |
+ |
|
84 |
+ |
// helper function to check for white-space and quotations |
85 |
+ |
static int |
86 |
+ |
check_special(const char *s) |
87 |
+ |
{ |
88 |
+ |
int space_found = 0; |
89 |
+ |
|
90 |
+ |
while (*s) { |
91 |
+ |
if ((*s == '"') | (*s == '\'')) |
92 |
+ |
return *s; // quotes have priority |
93 |
+ |
if (isspace(*s)) |
94 |
+ |
space_found = *s; |
95 |
+ |
s++; |
96 |
+ |
} |
97 |
+ |
return space_found; |
98 |
+ |
} |
99 |
+ |
|
100 |
+ |
// Append program line to header |
101 |
+ |
bool |
102 |
+ |
RadSimulManager::AddHeader(int ac, const char *av[]) |
103 |
+ |
{ |
104 |
+ |
if ((ac <= 0) | !av) return false; |
105 |
+ |
int len = 0; |
106 |
+ |
int n; |
107 |
+ |
for (n = 0; n < ac; n++) { |
108 |
+ |
if (!av[n]) return false; |
109 |
+ |
len += strlen(av[n]) + 3; |
110 |
+ |
} |
111 |
+ |
int hlen = 0; |
112 |
+ |
if (header) { // add to header |
113 |
+ |
hlen = strlen(header); |
114 |
+ |
header = (char *)realloc(header, hlen+len+1); |
115 |
+ |
} else |
116 |
+ |
header = (char *)malloc(len+1); |
117 |
+ |
for (n = 0; n < ac; n++) { |
118 |
+ |
int c = check_special(av[n]); |
119 |
+ |
if (c) { // need to quote argument? |
120 |
+ |
if (c == '"') c = '\''; |
121 |
+ |
else c = '"'; |
122 |
+ |
header[hlen++] = c; |
123 |
+ |
strcpy(header+hlen, av[n]); |
124 |
+ |
hlen += strlen(av[n]); |
125 |
+ |
header[hlen++] = c; |
126 |
+ |
} else { |
127 |
+ |
strcpy(header+hlen, av[n]); |
128 |
+ |
hlen += strlen(av[n]); |
129 |
+ |
} |
130 |
+ |
header[hlen++] = ' '; |
131 |
+ |
} |
132 |
+ |
header[hlen-1] = '\n'; // terminate line |
133 |
+ |
header[hlen] = '\0'; |
134 |
|
return true; |
135 |
|
} |
136 |
|
|