"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/win/WinConsole.cpp" (12 Feb 2021, 1594 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "WinConsole.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0.0_vs_2.0.1.

    1 /***************************************************************************
    2  *   Copyright (C) 2004-2021 by Pere Constans
    3  *   constans@molspaces.com
    4  *   cb2Bib version 2.0.1. Licensed under the GNU GPL version 3.
    5  *   See the LICENSE file that comes with this distribution.
    6  ***************************************************************************/
    7 #include "WinConsole.h"
    8 
    9 #include <QEventLoop>
   10 #include <QSysInfo>
   11 
   12 #include <fcntl.h>
   13 #include <io.h>
   14 #include <iostream>
   15 
   16 
   17 WinConsole::WinConsole(QObject* parento) : QObject(parento)
   18 {
   19     // Exclude _setmode on older Windows consoles
   20     if (int(QSysInfo::productVersion().toDouble()) > 7)
   21     {
   22         try
   23         {
   24             fflush(stdout);
   25             _setmode(_fileno(stdout), _O_U16TEXT);
   26         }
   27         catch (...)
   28         {
   29             fflush(stdout);
   30             _setmode(_fileno(stdout), _O_TEXT);
   31         }
   32     }
   33 }
   34 
   35 WinConsole::~WinConsole() {}
   36 
   37 
   38 int WinConsole::execute(const QString& program, const QStringList& arguments)
   39 {
   40     QEventLoop eventloop(this);
   41     _process.setProcessChannelMode(QProcess::MergedChannels);
   42 
   43     connect(&_process, SIGNAL(readyReadStandardOutput()), this, SLOT(output()));
   44     connect(&_process, SIGNAL(finished(int,QProcess::ExitStatus)), &eventloop, SLOT(quit()));
   45 
   46     _process.start(program, arguments);
   47     if (_process.waitForStarted())
   48         eventloop.exec();
   49     output();
   50     std::wcout << std::endl;
   51 
   52     return _process.exitCode();
   53 }
   54 
   55 void WinConsole::output()
   56 {
   57     while (_process.canReadLine())
   58         std::wcout << QString::fromUtf8(_process.readLine()).toStdWString();
   59 }