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); // get octree header |
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 *inspec) |
50 |
+ |
{ |
51 |
+ |
if (hlen) { |
52 |
+ |
free(header); header = NULL; hlen = 0; |
53 |
+ |
} |
54 |
+ |
if (!inspec || !*inspec) |
55 |
+ |
return false; |
56 |
+ |
if (inspec[0] == '!') // save command as header? |
57 |
+ |
return AddHeader(inspec+1); |
58 |
+ |
// attempt to read from file |
59 |
+ |
FILE *fp = fopen(inspec, "rb"); |
60 |
+ |
if (!fp) return false; |
61 |
+ |
bool ok = (getheader(fp, add2header, this) >= 0); |
62 |
+ |
fclose(fp); |
63 |
+ |
return ok; |
64 |
+ |
} |
65 |
+ |
|
66 |
+ |
// Add a string to header (adds newline if missing) |
67 |
+ |
bool |
68 |
+ |
RadSimulManager::AddHeader(const char *str) |
69 |
+ |
{ |
70 |
+ |
if (!str) return false; |
71 |
+ |
int len = strlen(str); |
72 |
+ |
while (len && str[len-1] == '\n') |
73 |
+ |
--len; // don't copy EOL(s) |
74 |
+ |
if (!len) |
75 |
+ |
return false; |
76 |
+ |
if (hlen) |
77 |
+ |
header = (char *)realloc(header, hlen+len+2); |
78 |
+ |
else |
79 |
+ |
header = (char *)malloc(len+2); |
80 |
+ |
if (!header) { |
81 |
+ |
hlen = 0; // XXX should report? |
82 |
+ |
return false; |
83 |
+ |
} |
84 |
+ |
memcpy(header+hlen, str, len); |
85 |
+ |
hlen += len; |
86 |
+ |
header[hlen++] = '\n'; // add single EOL |
87 |
+ |
header[hlen] = '\0'; |
88 |
+ |
return true; |
89 |
+ |
} |
90 |
+ |
|
91 |
+ |
// helper function to check for white-space and quotations |
92 |
+ |
static int |
93 |
+ |
check_special(const char *s) |
94 |
+ |
{ |
95 |
+ |
int space_found = 0; |
96 |
+ |
|
97 |
+ |
while (*s) { |
98 |
+ |
if ((*s == '"') | (*s == '\'')) |
99 |
+ |
return *s; // quotes have priority |
100 |
+ |
if (isspace(*s)) |
101 |
+ |
space_found = *s; |
102 |
+ |
s++; |
103 |
+ |
} |
104 |
+ |
return space_found; |
105 |
+ |
} |
106 |
+ |
|
107 |
+ |
// Append program line to header |
108 |
+ |
bool |
109 |
+ |
RadSimulManager::AddHeader(int ac, const char *av[]) |
110 |
+ |
{ |
111 |
+ |
if ((ac <= 0) | !av) return false; |
112 |
+ |
int len = 0; |
113 |
+ |
int n; |
114 |
+ |
for (n = 0; n < ac; n++) { |
115 |
+ |
if (!av[n]) return false; |
116 |
+ |
len += strlen(av[n]) + 3; |
117 |
+ |
} |
118 |
+ |
if (hlen) // add to header |
119 |
+ |
header = (char *)realloc(header, hlen+len+1); |
120 |
+ |
else |
121 |
+ |
header = (char *)malloc(len+1); |
122 |
+ |
|
123 |
+ |
for (n = 0; n < ac; n++) { |
124 |
+ |
int c = check_special(av[n]); |
125 |
+ |
len = strlen(av[n]); |
126 |
+ |
if (c | !len) { // need to quote argument? |
127 |
+ |
if ((c == '"') | (c == '\n')) c = '\''; |
128 |
+ |
else c = '"'; |
129 |
+ |
header[hlen++] = c; |
130 |
+ |
strcpy(header+hlen, av[n]); |
131 |
+ |
hlen += len; |
132 |
+ |
header[hlen++] = c; |
133 |
+ |
} else { |
134 |
+ |
strcpy(header+hlen, av[n]); |
135 |
+ |
hlen += len; |
136 |
+ |
} |
137 |
+ |
header[hlen++] = ' '; |
138 |
+ |
} |
139 |
+ |
header[hlen-1] = '\n'; // terminate line |
140 |
+ |
header[hlen] = '\0'; |
141 |
+ |
return true; |
142 |
+ |
} |
143 |
+ |
|
144 |
|
// How many processors are available? |
145 |
|
int |
146 |
|
RadSimulManager::GetNCores() |
210 |
|
int |
211 |
|
RadSimulManager::Cleanup(bool everything) |
212 |
|
{ |
213 |
+ |
NewHeader(); |
214 |
|
if (!ray_pnprocs) |
102 |
– |
ray_pdone(everything); |
103 |
– |
else |
215 |
|
ray_done(everything); |
216 |
+ |
else |
217 |
+ |
ray_pdone(everything); |
218 |
|
return 0; |
219 |
|
} |
220 |
|
|