1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/***************************************************************************
commandengine.cpp - kfr commands feature class
-------------------
begin : fri aug 13 15:29:46 CEST 2004
copyright : (C) 2004 Emiliano Gulmini
email : emi_barbarossa@yahoo.it
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
// QT
#include <qdatetime.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qdom.h>
// KDE
#include <kuser.h>
#include <krandomsequence.h>
#include <kprocess.h>
// local
#include "commandengine.h"
QString CommandEngine::datetime(const QString& opt, const QString& arg)
{
Q_UNUSED(arg);
if(opt == "iso")
return QDateTime::currentDateTime(Qt::LocalTime).toString(Qt::ISODate);
if(opt == "local")
return QDateTime::currentDateTime(Qt::LocalTime).toString(Qt::LocalDate);
return QString::null;
}
QString CommandEngine::user(const QString& opt, const QString& arg)
{
Q_UNUSED(arg);
KUser u;
if(opt == "uid")
return QString::number(u.uid(),10);
if(opt == "gid")
return QString::number(u.gid(),10);
if(opt == "loginname")
return u.loginName();
if(opt == "fullname")
return u.fullName();
if(opt == "homedir")
return u.homeDir();
if(opt == "shell")
return u.shell();
return QString::null;
}
QString CommandEngine::loadfile(const QString& opt, const QString& arg)
{
Q_UNUSED(arg);
QFile f(opt);
if(!f.open(IO_ReadOnly)) return QString::null;
QTextStream t(&f);
QString s = t.read();
f.close();
return s;
}
QString CommandEngine::empty(const QString& opt, const QString& arg)
{
Q_UNUSED(opt);
Q_UNUSED(arg);
return "";
}
QString CommandEngine::mathexp(const QString& opt, const QString& arg)
{
/* We will use bc 1.06 by Philip A. Nelson <philnelson@acm.org> */
//Q_UNUSED(opt);
Q_UNUSED(arg);
QString tempOpt = opt;
tempOpt.replace("ln","l");
tempOpt.replace("sin","s");
tempOpt.replace("cos","c");
tempOpt.replace("arctan","a");
tempOpt.replace("exp","e");
QString program = "var=("+tempOpt+");print var";
QString script = "echo '"+program+"' | bc -l;";
KProcess* proc = new KProcess();
proc->setUseShell(true);
*(proc) << script;
connect(proc, SIGNAL(receivedStdout(KProcess*,char*,int)), this, SLOT(slotGetScriptOutput(KProcess*,char*,int)));
connect(proc, SIGNAL(receivedStderr(KProcess*,char*,int)), this, SLOT(slotGetScriptError(KProcess*,char*,int)));
connect(proc, SIGNAL(processExited(KProcess*)), this, SLOT(slotProcessExited(KProcess*)));
//Through slotGetScriptOutput, m_processOutput contains the result of the KProcess call
if(!proc->start(KProcess::Block, KProcess::All))
{
return QString::null;
}
else
{
proc->wait();
}
if(proc)
delete proc;
QString tempbuf = m_processOutput;
m_processOutput = QString::null;
return tempbuf;
}
QString CommandEngine::random(const QString& opt, const QString& arg)
{
Q_UNUSED(arg);
long seed;
if(opt.isEmpty())
{
QDateTime dt;
seed = dt.toTime_t();
}
else
seed = opt.toLong();
KRandomSequence seq(seed);
return QString::number(seq.getLong(1000000),10);
}
QString CommandEngine::stringmanip(const QString& opt, const QString& arg)
{
Q_UNUSED(opt);
Q_UNUSED(arg);
return "";
}
QString CommandEngine::variableValue(const QString &variable)
{
QString s = variable;
s.remove("[$").remove("$]").remove(" ");
if(s.contains(":") == 0)
return variable;
else
{
QString leftValue = s.section(":",0,0),
midValue = s.section(":",1,1),
rightValue = s.section(":",2,2);
QString opt = midValue;
QString arg = rightValue;
if(leftValue == "stringmanip")
return stringmanip(opt, arg);
if(leftValue == "datetime")
return datetime(opt, arg);
if(leftValue == "user")
return user(opt, arg);
if(leftValue == "loadfile")
return loadfile(opt, arg);
if(leftValue == "empty")
return empty(opt, arg);
if(leftValue == "mathexp")
return mathexp(opt, arg);
if(leftValue == "random")
return random(opt, arg);
return variable;
}
}
//SLOTS
void CommandEngine::slotGetScriptError(KProcess* proc, char* s, int i)
{
Q_UNUSED(proc);
Q_UNUSED(proc);
QCString temp(s,i+1);
if(temp.isEmpty() || temp == "\n") return;
}
void CommandEngine::slotGetScriptOutput(KProcess* proc, char* s, int i)
{
Q_UNUSED(proc);
QCString temp(s,i+1);
if(temp.isEmpty() || temp == "\n") return;
m_processOutput += QString::fromLocal8Bit(temp);
}
void CommandEngine::slotProcessExited(KProcess* proc)
{
Q_UNUSED(proc);
}
#include "commandengine.moc"
|