Ejemplo n.º 1
0
        /// <summary>
        /// Preprocesses the specified input file.
        /// </summary>
        /// <param name="inputFile">The path of the input file.</param>
        /// <returns>The path of the file created by the preprocessor.</returns>
        private Stream Preprocess()
        {
            PreProcessor preProcessor = new PreProcessor(m_options);
            string       ppCommand    = PreProcessor.Command;
            string       ppArguments  = preProcessor.Arguments;
            string       traceMessage = String.Format("Executing preprocessor with following command line:\r\n{0} {1}", ppCommand, ppArguments);
            NamedPipe    pipe         = null;

            Trace.WriteLineIf(GlobalSettings.Verbosity.TraceVerbose, traceMessage);
            ProcessStartInfo ppStartInfo = new ProcessStartInfo(ppCommand, ppArguments);

            ppStartInfo.CreateNoWindow         = true;
            ppStartInfo.UseShellExecute        = false;
            ppStartInfo.RedirectStandardOutput = true;
            ppStartInfo.RedirectStandardError  = true;

            try {
                if (m_options.UseNamedPipes)
                {
                    pipe = new NamedPipe(preProcessor.NamedPipeName);
                }

                m_ppProcess                     = new Process();
                m_ppProcess.StartInfo           = ppStartInfo;
                m_ppProcess.OutputDataReceived += PreProcessor_OutputDataReceived;
                m_ppProcess.ErrorDataReceived  += PreProcessor_ErrorDataReceived;
                m_ppProcess.Start();
                m_ppProcess.BeginErrorReadLine();
                m_ppProcess.BeginOutputReadLine();

                Stream stream;
                if (pipe == null)
                {
                    m_ppProcess.WaitForExit();

                    if (m_ppProcess.ExitCode != 0)
                    {
                        throw new TerminateException(ExitCode.PreprocessorError);
                    }

                    stream = new FileStream(m_options.TempFile, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan);
                }
                else
                {
                    if (!m_ppProcess.HasExited)
                    {
                        stream = pipe.Open();
                    }
                    else
                    {
                        throw new TerminateException(ExitCode.PreprocessorError);
                    }
                }


                return(stream);
            }
            catch (TerminateException) {
                this.m_isCanceled = true;
                if (m_ppProcess != null)
                {
                    m_ppProcess.Dispose();
                    m_ppProcess = null;
                }
                throw;
            }
            catch (Exception ex) {
                if (m_ppProcess != null)
                {
                    // Detach the event handler before disposing. If they are not connected
                    // and the program execution stoped because of an exception the pre
                    // processor may send an error message when the named piped is
                    // destroyed.
                    m_ppProcess.OutputDataReceived -= PreProcessor_OutputDataReceived;
                    m_ppProcess.ErrorDataReceived  -= PreProcessor_ErrorDataReceived;
                    m_ppProcess.Dispose();
                    m_ppProcess = null;
                }
                if (!IsCanceledOrTerminated)
                {
                    string message = ppCommand + " " + ppArguments + ":\r\n" + ex.Message;
                    Trace.WriteLineIf(GlobalSettings.Verbosity.TraceError, message);
                }
                throw new TerminateException(ExitCode.ArgumentsError);
            }
        }