src/extprogs.cc

changeset 609
a8dc74a809c6
parent 606
3dd6f343ec06
child 617
f5059d144438
equal deleted inserted replaced
608:487db37f0bb3 609:a8dc74a809c6
97 "Edger2" 97 "Edger2"
98 }; 98 };
99 99
100 // ============================================================================= 100 // =============================================================================
101 // ----------------------------------------------------------------------------- 101 // -----------------------------------------------------------------------------
102 static bool mkTempFile (QTemporaryFile& tmp, QString& fname)
103 {
104 if (!tmp.open())
105 return false;
106
107 fname = tmp.fileName();
108 tmp.close();
109 return true;
110 }
111
112 // =============================================================================
113 // -----------------------------------------------------------------------------
102 static bool checkProgPath (const extprog prog) 114 static bool checkProgPath (const extprog prog)
103 { 115 {
104 QString& path = *g_extProgPaths[prog]; 116 QString& path = *g_extProgPaths[prog];
105 117
106 if (path.length() > 0) 118 if (path.length() > 0)
152 return ""; 164 return "";
153 } 165 }
154 166
155 // ============================================================================= 167 // =============================================================================
156 // ----------------------------------------------------------------------------- 168 // -----------------------------------------------------------------------------
157 static bool mkTempFile (QTemporaryFile& tmp, QString& fname) 169 static void writeObjects (const QList<LDObject*>& objects, QFile& f)
158 {
159 if (!tmp.open())
160 return false;
161
162 fname = tmp.fileName();
163 tmp.close();
164 return true;
165 }
166
167 // =============================================================================
168 // -----------------------------------------------------------------------------
169 static void writeObjects (const QList<LDObject*>& objects, File& f)
170 { 170 {
171 for (LDObject* obj : objects) 171 for (LDObject* obj : objects)
172 { 172 {
173 if (obj->getType() == LDObject::ESubfile) 173 if (obj->getType() == LDObject::ESubfile)
174 { 174 {
179 179
180 for (LDObject* obj : objs) 180 for (LDObject* obj : objs)
181 obj->deleteSelf(); 181 obj->deleteSelf();
182 } 182 }
183 else 183 else
184 f.write (obj->raw() + "\r\n"); 184 f.write ((obj->raw() + "\r\n").toUtf8());
185 } 185 }
186 } 186 }
187 187
188 // ============================================================================= 188 // =============================================================================
189 // ----------------------------------------------------------------------------- 189 // -----------------------------------------------------------------------------
190 static void writeObjects (const QList<LDObject*>& objects, QString fname) 190 static void writeObjects (const QList<LDObject*>& objects, QString fname)
191 { 191 {
192 // Write the input file 192 // Write the input file
193 File f (fname, File::Write); 193 QFile f (fname);
194 194
195 if (!f) 195 if (!f.open (QIODevice::WriteOnly | QIODevice::Text))
196 { 196 {
197 critical (fmt ("Couldn't open temporary file %1 for writing.\n", fname)); 197 critical (fmt ("Couldn't open temporary file %1 for writing: %2\n", fname, f.errorString()));
198 return; 198 return;
199 } 199 }
200 200
201 writeObjects (objects, f); 201 writeObjects (objects, f);
202 f.close(); 202 f.close();
232 232
233 // ============================================================================= 233 // =============================================================================
234 // ----------------------------------------------------------------------------- 234 // -----------------------------------------------------------------------------
235 bool runUtilityProcess (extprog prog, QString path, QString argvstr) 235 bool runUtilityProcess (extprog prog, QString path, QString argvstr)
236 { 236 {
237 QTemporaryFile input, output; 237 QTemporaryFile input;
238 QString inputname, outputname;
239 QStringList argv = argvstr.split (" ", QString::SkipEmptyParts); 238 QStringList argv = argvstr.split (" ", QString::SkipEmptyParts);
240 239
241 #ifndef _WIN32 240 #ifndef _WIN32
242 if (*g_extProgWine[prog]) 241 if (*g_extProgWine[prog])
243 { 242 {
246 } 245 }
247 #endif // _WIN32 246 #endif // _WIN32
248 247
249 log ("cmdline: %1 %2\n", path, argv.join (" ")); 248 log ("cmdline: %1 %2\n", path, argv.join (" "));
250 249
251 // Temporary files for stdin and stdout 250 if (!input.open())
252 if (!mkTempFile (input, inputname) || !mkTempFile (output, outputname))
253 return false; 251 return false;
254 252
255 QProcess proc; 253 QProcess proc;
256 254
257 // Init stdin
258 File stdinfp (inputname, File::Write);
259
260 // Begin! 255 // Begin!
261 proc.setStandardInputFile (inputname); 256 proc.setStandardInputFile (input.fileName());
262 proc.start (path, argv); 257 proc.start (path, argv);
263 258
264 if (!proc.waitForStarted()) 259 if (!proc.waitForStarted())
265 { 260 {
266 critical (fmt ("Couldn't start %1: %2\n", g_extProgNames[prog], processErrorString (prog, proc))); 261 critical (fmt ("Couldn't start %1: %2\n", g_extProgNames[prog], processErrorString (prog, proc)));
267 return false; 262 return false;
268 } 263 }
269 264
270 // Write an enter, the utility tools all expect one 265 // Write an enter, the utility tools all expect one
271 stdinfp.write ("\n"); 266 input.write ("\n");
272 267
273 // Wait while it runs 268 // Wait while it runs
274 proc.waitForFinished(); 269 proc.waitForFinished();
275 270
276 QString err = ""; 271 QString err = "";
298 #ifdef DEBUG 293 #ifdef DEBUG
299 QFile::copy (fname, "./debug_lastOutput"); 294 QFile::copy (fname, "./debug_lastOutput");
300 #endif // RELEASE 295 #endif // RELEASE
301 296
302 // Read the output file 297 // Read the output file
303 File f (fname, File::Read); 298 QFile f (fname);
304 299
305 if (!f) 300 if (!f.open (QIODevice::ReadOnly))
306 { 301 {
307 critical (fmt ("Couldn't open temporary file %1 for reading.\n", fname)); 302 critical (fmt ("Couldn't open temporary file %1 for reading.\n", fname));
308 return; 303 return;
309 } 304 }
310 305
312 307
313 // If we replace the objects, delete the selection now. 308 // If we replace the objects, delete the selection now.
314 if (replace) 309 if (replace)
315 g_win->deleteSelection(); 310 g_win->deleteSelection();
316 311
317 for (const int colnum : colorsToReplace) 312 for (int colnum : colorsToReplace)
318 g_win->deleteByColor (colnum); 313 g_win->deleteByColor (colnum);
319 314
320 // Insert the new objects 315 // Insert the new objects
321 getCurrentDocument()->clearSelection(); 316 getCurrentDocument()->clearSelection();
322 317
323 for (LDObject * obj : objs) 318 for (LDObject* obj : objs)
324 { 319 {
325 if (!obj->isScemantic()) 320 if (!obj->isScemantic())
326 { 321 {
327 obj->deleteSelf(); 322 obj->deleteSelf();
328 continue; 323 continue;

mercurial