src/extprogs.cpp

changeset 286
7a562bf3d829
parent 274
d232fe4d88a6
child 288
2980d7fd948e
equal deleted inserted replaced
285:836e77323ab0 286:7a562bf3d829
50 "Rectifier", 50 "Rectifier",
51 }; 51 };
52 52
53 // ============================================================================= 53 // =============================================================================
54 static bool checkProgPath (str path, const extprog prog) { 54 static bool checkProgPath (str path, const extprog prog) {
55 if (~path) 55 if (path.length () > 0)
56 return true; 56 return true;
57 57
58 const char* name = g_extProgNames[prog]; 58 const char* name = g_extProgNames[prog];
59 59
60 critical (fmt ("Couldn't run %s as no path has " 60 critical (fmt ("Couldn't run %1 as no path has "
61 "been defined for it. Use the configuration dialog's External Programs " 61 "been defined for it. Use the configuration dialog's External Programs "
62 "tab to define a path for %s.", name, name)); 62 "tab to define a path for %1.", name));
63 return false; 63 return false;
64 } 64 }
65 65
66 // ============================================================================= 66 // =============================================================================
67 static void processError (const extprog prog, QProcess& proc) { 67 static void processError (const extprog prog, QProcess& proc) {
68 const char* name = g_extProgNames[prog]; 68 const char* name = g_extProgNames[prog];
69 str errmsg; 69 str errmsg;
70 70
71 switch (proc.error ()) { 71 switch (proc.error ()) {
72 case QProcess::FailedToStart: 72 case QProcess::FailedToStart:
73 errmsg = fmt ("Failed to launch %s. Check that you have set the proper path " 73 errmsg = fmt ("Failed to launch %1. Check that you have set the proper path "
74 "to %s and that you have the proper permissions to launch it.", name, name); 74 "to %1 and that you have the proper permissions to launch it.", name);
75 break; 75 break;
76 76
77 case QProcess::Crashed: 77 case QProcess::Crashed:
78 errmsg = fmt ("%s crashed.", name); 78 errmsg = fmt ("%1 crashed.", name);
79 break; 79 break;
80 80
81 case QProcess::WriteError: 81 case QProcess::WriteError:
82 case QProcess::ReadError: 82 case QProcess::ReadError:
83 errmsg = fmt ("I/O error while interacting with %s.", name); 83 errmsg = fmt ("I/O error while interacting with %1.", name);
84 break; 84 break;
85 85
86 case QProcess::UnknownError: 86 case QProcess::UnknownError:
87 errmsg = fmt ("Unknown error occurred while executing %s.", name); 87 errmsg = fmt ("Unknown error occurred while executing %1.", name);
88 break; 88 break;
89 89
90 case QProcess::Timedout: 90 case QProcess::Timedout:
91 errmsg = fmt ("%s timed out.", name); 91 errmsg = fmt ("%1 timed out.", name);
92 break; 92 break;
93 } 93 }
94 94
95 critical (errmsg); 95 critical (errmsg);
96 } 96 }
119 119
120 writeObjects (objs, fp); 120 writeObjects (objs, fp);
121 for (LDObject* obj : objs) 121 for (LDObject* obj : objs)
122 delete obj; 122 delete obj;
123 } else { 123 } else {
124 str line = fmt ("%s\r\n", obj->raw ().chars ()); 124 str line = obj->raw () + "\r\n";
125 fwrite (line.chars(), 1, ~line, fp); 125 fwrite (qchars (line), 1, line.length (), fp);
126 } 126 }
127 } 127 }
128 } 128 }
129 129
130 void writeObjects (vector<LDObject*>& objects, str fname) { 130 void writeObjects (vector<LDObject*>& objects, str fname) {
131 // Write the input file 131 // Write the input file
132 FILE* fp = fopen (fname, "w"); 132 FILE* fp = fopen (qchars (fname), "w");
133 if (!fp) { 133 if (!fp) {
134 critical (fmt ("Couldn't open temporary file %s for writing.\n", fname.chars ())); 134 critical (fmt ("Couldn't open temporary file %1 for writing.\n", fname));
135 return; 135 return;
136 } 136 }
137 137
138 writeObjects (objects, fp); 138 writeObjects (objects, fp);
139
140 #if 0
141 ushort idx = rand ();
142 printf ("%s -> debug_%u\n", fname.chars (), idx);
143 QFile::copy (fname.chars (), fmt ("debug_%u", idx));
144 #endif // 0
145
146 fclose (fp); 139 fclose (fp);
147 } 140 }
148 141
149 // ============================================================================= 142 // =============================================================================
150 void writeSelection (str fname) { 143 void writeSelection (str fname) {
168 void runUtilityProcess (extprog prog, str path, QString argvstr) { 161 void runUtilityProcess (extprog prog, str path, QString argvstr) {
169 QTemporaryFile input, output; 162 QTemporaryFile input, output;
170 str inputname, outputname; 163 str inputname, outputname;
171 QStringList argv = argvstr.split (" ", QString::SkipEmptyParts); 164 QStringList argv = argvstr.split (" ", QString::SkipEmptyParts);
172 165
173 printf ("cmdline: %s %s\n", path.chars (), qchars (argvstr)); 166 printf ("cmdline: %s %s\n", qchars (path), qchars (argvstr));
174 167
175 if (!mkTempFile (input, inputname) || !mkTempFile (output, outputname)) 168 if (!mkTempFile (input, inputname) || !mkTempFile (output, outputname))
176 return; 169 return;
177 170
178 QProcess proc; 171 QProcess proc;
179 172
180 // Init stdin 173 // Init stdin
181 FILE* stdinfp = fopen (inputname, "w"); 174 FILE* stdinfp = fopen (qchars (inputname), "w");
182 175
183 // Begin! 176 // Begin!
184 proc.setStandardInputFile (inputname); 177 proc.setStandardInputFile (inputname);
185 proc.start (path, argv); 178 proc.start (path, argv);
186 179
208 #ifndef RELEASE 201 #ifndef RELEASE
209 QFile::copy (fname, "./debug_lastOutput"); 202 QFile::copy (fname, "./debug_lastOutput");
210 #endif // RELEASE 203 #endif // RELEASE
211 204
212 // Read the output file 205 // Read the output file
213 FILE* fp = fopen (fname, "r"); 206 FILE* fp = fopen (qchars (fname), "r");
214 if (!fp) { 207 if (!fp) {
215 critical (fmt ("Couldn't open temporary file %s for reading.\n", fname.chars ())); 208 critical (fmt ("Couldn't open temporary file %1 for reading.\n", fname));
216 return; 209 return;
217 } 210 }
218 211
219 vector<LDObject*> objs = loadFileContents (fp, null); 212 vector<LDObject*> objs = loadFileContents (fp, null);
220 213
286 // Make temp files for the input and output files 279 // Make temp files for the input and output files
287 if (!mkTempFile (indat, inDATName) || !mkTempFile (outdat, outDATName)) 280 if (!mkTempFile (indat, inDATName) || !mkTempFile (outdat, outDATName))
288 return; 281 return;
289 282
290 // Compose the command-line arguments 283 // Compose the command-line arguments
291 str argv = fmt ("%s %s %f -a %f %s %s", 284 str argv = join ({
292 (axis == X) ? "-x" : (axis == Y) ? "-y" : "-z", 285 (axis == X) ? "-x" : (axis == Y) ? "-y" : "-z",
293 (mode == Distance) ? "-d" : (mode == Symmetry) ? "-s" : (mode == Projection) ? "-p" : "-r", 286 (mode == Distance) ? "-d" : (mode == Symmetry) ? "-s" : (mode == Projection) ? "-p" : "-r",
294 depth, condAngle, inDATName.chars (), outDATName.chars ()); 287 depth,
288 "-a",
289 condAngle,
290 inDATName,
291 outDATName
292 });
295 293
296 writeSelection (inDATName); 294 writeSelection (inDATName);
297 runUtilityProcess (Ytruder, prog_ytruder, argv); 295 runUtilityProcess (Ytruder, prog_ytruder, argv);
298 insertOutput (outDATName, false, {}); 296 insertOutput (outDATName, false, {});
299 } 297 }
343 // Make temp files for the input and output files 341 // Make temp files for the input and output files
344 if (!mkTempFile (indat, inDATName) || !mkTempFile (outdat, outDATName)) 342 if (!mkTempFile (indat, inDATName) || !mkTempFile (outdat, outDATName))
345 return; 343 return;
346 344
347 // Compose arguments 345 // Compose arguments
348 str argv = fmt ("%s %s %s %s -t %f %s %s", 346 str argv = join ({
349 (condense == false) ? "-q" : "", 347 (condense == false) ? "-q" : "",
350 (subst == false) ? "-r" : "", 348 (subst == false) ? "-r" : "",
351 (condlineCheck) ? "-a" : "", 349 (condlineCheck) ? "-a" : "",
352 (colorize) ? "-c" : "", 350 (colorize) ? "-c" : "",
353 coplthres, inDATName.chars (), outDATName.chars ()); 351 "-t",
352 coplthres,
353 inDATName,
354 outDATName
355 });
354 356
355 writeSelection (inDATName); 357 writeSelection (inDATName);
356 runUtilityProcess (Rectifier, prog_rectifier, argv); 358 runUtilityProcess (Rectifier, prog_rectifier, argv);
357 insertOutput (outDATName, true, {}); 359 insertOutput (outDATName, true, {});
358 } 360 }
436 !mkTempFile (edgesdat, edgesDATName)) 438 !mkTempFile (edgesdat, edgesDATName))
437 { 439 {
438 return; 440 return;
439 } 441 }
440 442
441 str parms = fmt ("%s %s -s %f", 443 str parms = join ({
442 (cb_colorize->isChecked ()) ? "-c" : "", 444 (cb_colorize->isChecked ()) ? "-c" : "",
443 (cb_nocondense->isChecked ()) ? "-t" : "", 445 (cb_nocondense->isChecked ()) ? "-t" : "",
444 dsb_prescale->w ()->value ()); 446 "-s",
445 447 dsb_prescale->w ()->value ()
446 str argv_normal = fmt ("%s %s %s %s", parms.chars (), inDATName.chars (), cutDATName.chars (), outDATName.chars ()); 448 });
447 str argv_inverse = fmt ("%s %s %s %s", parms.chars (), cutDATName.chars (), inDATName.chars (), outDAT2Name.chars ()); 449
450 str argv_normal = join ({
451 parms,
452 inDATName,
453 cutDATName,
454 outDATName
455 });
456
457 str argv_inverse = join ({
458 parms,
459 cutDATName,
460 inDATName,
461 outDAT2Name
462 });
448 463
449 writeColorGroup (inCol, inDATName); 464 writeColorGroup (inCol, inDATName);
450 writeColorGroup (cutCol, cutDATName); 465 writeColorGroup (cutCol, cutDATName);
451 runUtilityProcess (Intersector, prog_intersector, argv_normal); 466 runUtilityProcess (Intersector, prog_intersector, argv_normal);
452 insertOutput (outDATName, false, {inCol}); 467 insertOutput (outDATName, false, {inCol});
455 runUtilityProcess (Intersector, prog_intersector, argv_inverse); 470 runUtilityProcess (Intersector, prog_intersector, argv_inverse);
456 insertOutput (outDAT2Name, false, {cutCol}); 471 insertOutput (outDAT2Name, false, {cutCol});
457 } 472 }
458 473
459 if (cb_edges->isChecked ()) { 474 if (cb_edges->isChecked ()) {
460 runUtilityProcess (Isecalc, prog_isecalc, fmt ("%s %s %s", inDATName.chars (), cutDATName.chars (), edgesDATName.chars ())); 475 runUtilityProcess (Isecalc, prog_isecalc, join ({inDATName, cutDATName, edgesDATName}));
461 insertOutput (edgesDATName, false, {}); 476 insertOutput (edgesDATName, false, {});
462 } 477 }
463 } 478 }
464 479
465 // ======================================================================================================================================= 480 // =======================================================================================================================================
518 str in1DATName, in2DATName, outDATName; 533 str in1DATName, in2DATName, outDATName;
519 534
520 if (!mkTempFile (in1dat, in1DATName) || !mkTempFile (in2dat, in2DATName) || !mkTempFile (outdat, outDATName)) 535 if (!mkTempFile (in1dat, in1DATName) || !mkTempFile (in2dat, in2DATName) || !mkTempFile (outdat, outDATName))
521 return; 536 return;
522 537
523 str argv = fmt ("%s %s %s %s %s %s %s", 538 str argv = join ({
524 (cb_oldsweep->isChecked () ? "-s" : ""), 539 (cb_oldsweep->isChecked () ? "-s" : ""),
525 (cb_reverse->isChecked () ? "-r" : ""), 540 (cb_reverse->isChecked () ? "-r" : ""),
526 (dsb_segsplit->value () != 0 ? fmt ("-l %f", dsb_segsplit->value ()).c () : ""), 541 (dsb_segsplit->value () != 0 ? fmt ("-l %1", dsb_segsplit->value ()) : ""),
527 (sb_bias->value () != 0 ? fmt ("-s %d", sb_bias->value ()).c () : ""), 542 (sb_bias->value () != 0 ? fmt ("-s %1", sb_bias->value ()) : ""),
528 in1DATName.c (), in2DATName.c (), outDATName.c ()); 543 in1DATName,
544 in2DATName,
545 outDATName
546 });
529 547
530 writeColorGroup (in1Col, in1DATName); 548 writeColorGroup (in1Col, in1DATName);
531 writeColorGroup (in2Col, in2DATName); 549 writeColorGroup (in2Col, in2DATName);
532 runUtilityProcess (Coverer, prog_coverer, argv); 550 runUtilityProcess (Coverer, prog_coverer, argv);
533 insertOutput (outDATName, false, {}); 551 insertOutput (outDATName, false, {});
565 str in1DATName, in2DATName, outDATName; 583 str in1DATName, in2DATName, outDATName;
566 584
567 if (!mkTempFile (in1dat, in1DATName) || !mkTempFile (in2dat, in2DATName) || !mkTempFile (outdat, outDATName)) 585 if (!mkTempFile (in1dat, in1DATName) || !mkTempFile (in2dat, in2DATName) || !mkTempFile (outdat, outDATName))
568 return; 586 return;
569 587
570 str argv = fmt ("%s %s %s", in1DATName.c (), in2DATName.c (), outDATName.c ()); 588 str argv = join ({
589 in1DATName,
590 in2DATName,
591 outDATName
592 });
571 593
572 writeColorGroup (in1Col, in1DATName); 594 writeColorGroup (in1Col, in1DATName);
573 writeColorGroup (in2Col, in2DATName); 595 writeColorGroup (in2Col, in2DATName);
574 runUtilityProcess (Isecalc, prog_isecalc, argv); 596 runUtilityProcess (Isecalc, prog_isecalc, argv);
575 insertOutput (outDATName, false, {}); 597 insertOutput (outDATName, false, {});

mercurial