Functions | Variables
gpreplace/main.cpp File Reference
#include <QGpCoreTools.h>
#include "gpreplaceVersion.h"
#include "gpreplaceInstallPath.h"

Functions

bool contains (QRegExp reg, QString &buf)
ApplicationHelphelp ()
int main (int argc, char **argv)
 PACKAGE_INFO (gpreplace, GPREPLACE)
void replace (QRegExp reg, QString &buf)

Variables

QString fileName
bool hasStringOption = false
bool interactive = false
QString outputFile
QString pattern
QString str

Function Documentation

bool contains ( QRegExp  reg,
QString &  buf 
)
int main ( int  argc,
char **  argv 
)

References contains(), QGpCoreTools::endl(), fileName, hasStringOption, help(), interactive, outputFile, pattern, replace(), sOut(), str, and QGpCoreTools::tr().

{
  CoreApplication a(argc, argv, help);
  QTextStream sOut(stdout);

  // Check arguments
  int i, j=1;
  for(i=1; i<argc; i++) {
    QString arg=argv[i];
    if(arg[0]=='-') {
      if(arg=="-f") {
        CoreApplication::checkOptionArg(i, argc, argv);
        fileName=argv[i];
      } else if(arg=="-p") {
        CoreApplication::checkOptionArg(i, argc, argv);
        pattern=QString::fromUtf8(argv[i]);
      } else if(arg=="-s") {
        CoreApplication::checkOptionArg(i, argc, argv);
        hasStringOption=true;
        str=argv[i];
      } else if(arg=="-i") {
        interactive=true;
      } else if(arg=="-o") {
        CoreApplication::checkOptionArg(i, argc, argv);
        outputFile=argv[i];
      } else {
        App::stream() << tr("gpreplace: bad option %1, see -help").arg(argv[i]) << endl;
        return 2;
      }
    } else {
      argv[j++]=argv[i];
    }
  }
  if(j < argc) {
    argv[j]=0;
    argc=j;
  }
  if(fileName.isEmpty()) {
    sOut << "Empty filename, see -h for help." << endl;
    return 2;
  }
  QFile f(fileName);
  if(!f.open(QIODevice::ReadOnly)) {
    sOut << "Cannot open input file " << fileName << endl;
    return 2;
  }
  App::stream() << pattern << endl;
  QTextStream s(&f);
  s.setCodec("UTF-8");
  QString buf=s.readAll();
  pattern.replace("\\!","!");
  QRegExp reg(pattern);

  if(hasStringOption)
    replace(reg, buf);
  else
    return contains(reg, buf) ? 0 : 1;

  return 0;
}
PACKAGE_INFO ( gpreplace  ,
GPREPLACE   
)
void replace ( QRegExp  reg,
QString &  buf 
)

References QGpCoreTools::endl(), QGpCoreTools::flush(), interactive, outputFile, sOut(), and str.

Referenced by main().

{
  QTextStream sOut(stdout);
  QTextStream sIn(stdin);
  str.replace("\\n","\n");
  if(interactive) {
    int from=0, cur;
    QString rep;
    while((cur=buf.indexOf(reg, from))>=0) {
      QString c=reg.cap(0);
      QString fStr=str;
      for(int i=0;i<10;i++)
        fStr.replace(QString("\\%1").arg(i), reg.cap(i));

      if(interactive) {
        QString context;

        int bContextCur=cur;
        bContextCur=buf.lastIndexOf("\n",bContextCur);
        if(bContextCur<0)
          bContextCur=0;
        int eContextCur=cur+c.size();
        eContextCur=buf.indexOf("\n",eContextCur);
        if(eContextCur<0)
          eContextCur=buf.size()-1;

        int bContext=cur;
        for(int i=0;i<5 && bContext>=0;i++)
          bContext=buf.lastIndexOf("\n",bContext-1);
        if(bContext<0)
          bContext=0;
        if(bContextCur>bContext) {
          context=buf.mid(bContext, bContextCur-bContext);
          context.replace("\n", "\n|| ");
          sOut << context;
        }

        context=buf.mid(bContextCur, eContextCur-bContextCur);
        context.replace("\n", "\n-- ");
        sOut << context;
        context=buf.mid(bContextCur, cur-bContextCur)+fStr;
        if(eContextCur>(cur+c.size()))
          context+=buf.mid(cur+c.size(), eContextCur-(cur+c.size()));
        context.replace("\n", "\n++ ");
        sOut << context;

        int eContext=eContextCur;
        for(int i=0;i<5 && eContext>=0;i++)
          eContext=buf.indexOf("\n",eContext+1);
        if(eContext<0)
          eContext=buf.size()-1;
        if(eContext>eContextCur) {
          context=buf.mid(eContextCur, eContext-eContextCur);
          context.replace("\n", "\n|| ");
          sOut << context;
        }

        sOut << endl << endl;
        sOut << "Do you want to replace? (y/n/a) [y] " << flush;
        CoreApplication::instance()->debugUserInterrupts(false);
        rep=sIn.readLine();
        CoreApplication::instance()->debugUserInterrupts(true);
        rep.toLower();
      }
      if(rep=="n") {
        from=cur+c.size();
      } else {
        buf.replace(cur, c.size(), fStr);
        from=cur+fStr.size();
      }
      if(rep=="a")
        interactive=false;
    }
  } else {
    // Basic Qt implementation already contains support for \1 \2, ... sub expresions
    buf.replace(reg, str);
  }
  if(outputFile.isEmpty()) {
    sOut.setCodec("UTF-8");
    sOut << buf;
  } else {
    QFile f(outputFile);
    if(!f.open(QIODevice::WriteOnly)) {
      sOut << "Cannot open output file " << outputFile << endl;
    }
    QTextStream s(&f);
    s.setCodec("UTF-8");
    s << buf;
  }
}

Variable Documentation

QString fileName
bool hasStringOption = false

Referenced by main().

bool interactive = false

Referenced by main(), and replace().

QString outputFile
QString pattern
QString str

Referenced by RingBrowser::add(), GeopsySLink::SeedLink::addStream(), GeopsyCore::GeopsyCoreEngine::askTimeReference(), QGpCoreTools::CoreApplicationPrivate::backTrace(), QGpCoreTools::CoreApplicationPrivate::backTraceBug(), MonoStation::StationResults::comments(), SciFigs::IrregularGrid2DPlot::coordinateTipInfo(), createBands(), createCurve(), createCurveName(), createDots(), createManyCurves(), MonoStation::StatisticResults::createObjects(), QGpCoreTools::ApplicationHelp::encodeToHtml(), QGpCoreTools::encodeToHtml(), QGpGuiTools::encodeToHtml(), QGpCoreTools::ApplicationHelp::encodeToLatex(), QGpCoreWave::Seismic1DModel::exportHerrmann(), GeopsyCore::SubSignalPool::exportTable(), GeopsyCore::SignalFileFormat::fromContent(), GeopsyAbout::GeopsyAbout(), GeopsyGui::ArrayDefinition::getEndTime(), QGpCompatibility::CompatAutocorrCurves::getRadiusCount(), GeopsyGui::ArrayDefinition::getStartTime(), GpCoreTools::CoreApplication::getStdin(), QGpCoreTools::CoreApplicationPrivate::getStdin(), GpCoreTools::htmlFilter(), GeopsyCore::SubSignalPool::importTable(), ToolSpectrum::initStations(), ToolStructure::initStations(), ToolHV::initStations(), ToolNR::initStations(), QGpCoreWave::Seismic1DModel::interpole(), QGpCoreTools::LineParser::LineParser(), EllipticityCurveTargetWidget::load(), GeopsyCore::GeopsyPreferences::load(), GeopsyGui::RingEditor::loadBut_clicked(), ArrayCore::RingCouples::log(), main(), HeaderMap::missingFields(), QGpCompatibility::CompatFunctionList::modesCount(), GeopsyGui::PickLayer::mouseMoveEvent(), GeopsyCore::SignalFile::name(), ThreadLauncher::newThread(), ToolRefrad::on_outputFileBrowse_clicked(), ToolSPACd::on_outputFileBrowse_clicked(), GeopsyGui::CityLoader::on_pathBrowse_clicked(), QGpGuiTools::operator>>(), QGpCoreTools::operator>>(), DimensionItem::paint(), QGpGuiTools::ColorHistogram::paintEvent(), QGpCompatibility::CompatInversionReport::parameterName(), GeopsySLinkGui::SeedLinkStreamItem::parent(), TimeReader::parse(), QGpCoreTools::DateTimeParser::parse(), SciFigs::Axis::printThickness(), ThreadLauncher::processBaseName(), replace(), QGpCoreTools::CoreApplicationPrivate::reportBug(), QGpCoreTools::CoreApplicationPrivate::reportBugNow(), GeopsyGui::RingEditor::saveBut_clicked(), QGpCoreTools::Number::secondsToTime(), SpacSelector::selectSamples(), GeopsySLinkGui::SeedLinkLoader::selectStreams(), MainWindow::setBackground(), QGpCoreTools::ConsoleProgress::setCaption(), SciFigs::PaletteInterpole::setColors(), ParamLayerWidget::setFrom(), GpCoreTools::CoreApplication::setHelp(), QGpCoreTools::CoreApplicationPrivate::setHelp(), SciFigs::OrderTip::setIndex(), ParamLayerWidget::setIndex(), NAModelsPlotProperties::setParameterList(), NAModelsPlot::setParamX(), NAModelsPlot::setParamY(), ToolRefrad::setPickCount(), QGpCompatibility::CompatAutocorrCurves::setRadius(), QGpCompatibility::CompatHodoChroneData::setSource(), QGpCoreTools::LineParser::setString(), SciFigs::PaletteInterpole::setValues(), sortCurve(), ToolTFA::start(), QGpCoreTools::StringStream::StringStream(), CoordReader::terminate(), GpCoreTools::TraceStamp::toString(), QGpCoreTools::TraceStamp::toString(), Route::toString(), QGpCoreTools::PermutationVector::toString(), GeopsyCore::GuralpRecords::toString(), GpCoreTools::Variant::toString(), GeopsyCore::TimeRangeList::toString(), QGpCoreWave::Resistivity1DModel::toString(), QGpCoreWave::Mode::toString(), QGpCoreTools::MatrixData< T >::toString(), QGpCoreWave::RefractionDippingModel::toString(), QGpCoreWave::Seismic1DModel::toString(), QGpCoreTools::SparseMatrix< T >::toUserString(), QGpCoreTools::MatrixData< T >::toUserString(), TargetListWidget::updateInfo(), GpCoreTools::CoreApplication::version(), QGpCoreTools::CoreApplicationPrivate::version(), GeopsyCore::Signal::writeSeg2(), QGpCoreTools::Message::wrongTextFormat(), QGpCoreTools::GoogleEarthKML::Point::xml_setProperty(), and GeopsyCore::SignalGroup::xml_writeProperties().

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines