tests/auto/qsharedmemory/lackey/main.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 
       
    44 #include <qscriptengine.h>
       
    45 
       
    46 #include <QtCore/QFile>
       
    47 #include <QtCore/QTextStream>
       
    48 #include <QTest>
       
    49 
       
    50 #include <qstringlist.h>
       
    51 #include <stdlib.h>
       
    52 #include <qsharedmemory.h>
       
    53 #include <qsystemsemaphore.h>
       
    54 #include <qsystemlock.h>
       
    55 
       
    56 class ScriptSystemSemaphore : public QObject
       
    57 {
       
    58     Q_OBJECT
       
    59 
       
    60 public:
       
    61     ScriptSystemSemaphore(QObject *parent = 0) : QObject(parent), ss(QString())
       
    62     {
       
    63     }
       
    64 
       
    65 public slots:
       
    66     bool acquire()
       
    67     {
       
    68         return ss.acquire();
       
    69     };
       
    70 
       
    71     bool release(int n = 1)
       
    72     {
       
    73         return ss.release(n);
       
    74     };
       
    75 
       
    76     void setKey(const QString &key, int n = 0)
       
    77     {
       
    78         ss.setKey(key, n);
       
    79     };
       
    80 
       
    81     QString key() const
       
    82     {
       
    83         return ss.key();
       
    84     }
       
    85 
       
    86 private:
       
    87     QSystemSemaphore ss;
       
    88 };
       
    89 
       
    90 class ScriptSystemLock : public QObject
       
    91 {
       
    92     Q_OBJECT
       
    93     Q_PROPERTY(QString key WRITE setKey READ key)
       
    94 
       
    95 public:
       
    96     ScriptSystemLock(QObject *parent = 0) : QObject(parent), sl(QString())
       
    97     {
       
    98     }
       
    99 
       
   100 public slots:
       
   101 
       
   102     bool lockReadOnly()
       
   103     {
       
   104         return sl.lock(QSystemLock::ReadOnly);
       
   105     }
       
   106 
       
   107     bool lock()
       
   108     {
       
   109         return sl.lock();
       
   110     };
       
   111 
       
   112     bool unlock()
       
   113     {
       
   114         return sl.unlock();
       
   115     };
       
   116 
       
   117     void setKey(const QString &key)
       
   118     {
       
   119         sl.setKey(key);
       
   120     };
       
   121 
       
   122     QString key() const
       
   123     {
       
   124         return sl.key();
       
   125     }
       
   126 
       
   127 private:
       
   128     QSystemLock sl;
       
   129 };
       
   130 
       
   131 class ScriptSharedMemory : public QObject
       
   132 {
       
   133     Q_OBJECT
       
   134     Q_PROPERTY(bool attached READ isAttached)
       
   135     Q_PROPERTY(QString key WRITE setKey READ key)
       
   136 
       
   137 public:
       
   138     enum SharedMemoryError
       
   139     {
       
   140         NoError = 0,
       
   141         PermissionDenied = 1,
       
   142         InvalidSize = 2,
       
   143         KeyError = 3,
       
   144         AlreadyExists = 4,
       
   145         NotFound = 5,
       
   146         LockError = 6,
       
   147         OutOfResources = 7,
       
   148         UnknownError = 8
       
   149     };
       
   150 
       
   151     ScriptSharedMemory(QObject *parent = 0) : QObject(parent)
       
   152     {
       
   153     }
       
   154 
       
   155 public slots:
       
   156     void sleep(int x) const
       
   157     {
       
   158         QTest::qSleep(x);
       
   159     }
       
   160 
       
   161     bool create(int size)
       
   162     {
       
   163         return sm.create(size);
       
   164     };
       
   165 
       
   166     bool createReadOnly(int size)
       
   167     {
       
   168         return sm.create(size, QSharedMemory::ReadOnly);
       
   169     };
       
   170 
       
   171     int size() const
       
   172     {
       
   173         return sm.size();
       
   174     };
       
   175 
       
   176     bool attach()
       
   177     {
       
   178         return sm.attach();
       
   179     };
       
   180 
       
   181     bool attachReadOnly()
       
   182     {
       
   183         return sm.attach(QSharedMemory::ReadOnly);
       
   184     };
       
   185 
       
   186     bool isAttached() const
       
   187     {
       
   188         return sm.isAttached();
       
   189     };
       
   190 
       
   191     bool detach()
       
   192     {
       
   193         return sm.detach();
       
   194     };
       
   195 
       
   196     int error() const
       
   197     {
       
   198         return (int)sm.error();
       
   199     };
       
   200 
       
   201     QString errorString() const
       
   202     {
       
   203         return sm.errorString();
       
   204     };
       
   205 
       
   206     void set(int i, QChar value)
       
   207     {
       
   208         ((char*)sm.data())[i] = value.toLatin1();
       
   209     }
       
   210 
       
   211     QString get(int i)
       
   212     {
       
   213         return QChar::fromLatin1(((char*)sm.data())[i]);
       
   214     }
       
   215 
       
   216     char *data() const
       
   217     {
       
   218         return (char*)sm.data();
       
   219     };
       
   220 
       
   221     void setKey(const QString &key)
       
   222     {
       
   223         sm.setKey(key);
       
   224     };
       
   225 
       
   226     QString key() const
       
   227     {
       
   228         return sm.key();
       
   229     }
       
   230 
       
   231     bool lock()
       
   232     {
       
   233         return sm.lock();
       
   234     }
       
   235 
       
   236     bool unlock()
       
   237     {
       
   238         return sm.unlock();
       
   239     }
       
   240 
       
   241 private:
       
   242     QSharedMemory sm;
       
   243 };
       
   244 
       
   245 Q_SCRIPT_DECLARE_QMETAOBJECT(ScriptSharedMemory, QObject*);
       
   246 Q_SCRIPT_DECLARE_QMETAOBJECT(ScriptSystemLock, QObject*);
       
   247 Q_SCRIPT_DECLARE_QMETAOBJECT(ScriptSystemSemaphore, QObject*);
       
   248 
       
   249 static void interactive(QScriptEngine &eng)
       
   250 {
       
   251 #ifdef Q_OS_WINCE
       
   252     fprintf(stderr, "Interactive mode not supported on Windows CE\n");
       
   253     return;
       
   254 #endif
       
   255     QTextStream qin(stdin, QFile::ReadOnly);
       
   256 
       
   257     const char *qscript_prompt = "qs> ";
       
   258     const char *dot_prompt = ".... ";
       
   259     const char *prompt = qscript_prompt;
       
   260 
       
   261     QString code;
       
   262 
       
   263     forever {
       
   264         QString line;
       
   265 
       
   266         printf("%s", prompt);
       
   267         fflush(stdout);
       
   268 
       
   269         line = qin.readLine();
       
   270         if (line.isNull())
       
   271         break;
       
   272 
       
   273         code += line;
       
   274         code += QLatin1Char('\n');
       
   275 
       
   276         if (line.trimmed().isEmpty()) {
       
   277             continue;
       
   278 
       
   279         } else if (! eng.canEvaluate(code)) {
       
   280             prompt = dot_prompt;
       
   281 
       
   282         } else {
       
   283             QScriptValue result = eng.evaluate(code);
       
   284             code.clear();
       
   285             prompt = qscript_prompt;
       
   286             if (!result.isUndefined())
       
   287                 fprintf(stderr, "%s\n", qPrintable(result.toString()));
       
   288         }
       
   289     }
       
   290 }
       
   291 
       
   292 int main(int argc, char *argv[])
       
   293 {
       
   294     QCoreApplication app(argc, argv);
       
   295 
       
   296     QScriptEngine eng;
       
   297     QScriptValue globalObject = eng.globalObject();
       
   298 
       
   299     QScriptValue sm = qScriptValueFromQMetaObject<ScriptSharedMemory>(&eng);
       
   300     eng.globalObject().setProperty("ScriptSharedMemory", sm);
       
   301 
       
   302     QScriptValue sl = qScriptValueFromQMetaObject<ScriptSystemLock>(&eng);
       
   303     eng.globalObject().setProperty("ScriptSystemLock", sl);
       
   304 
       
   305     QScriptValue ss = qScriptValueFromQMetaObject<ScriptSystemSemaphore>(&eng);
       
   306     eng.globalObject().setProperty("ScriptSystemSemaphore", ss);
       
   307 
       
   308 
       
   309     if (! *++argv) {
       
   310         interactive(eng);
       
   311         return EXIT_SUCCESS;
       
   312     }
       
   313 
       
   314     QStringList arguments = app.arguments();
       
   315     arguments.takeFirst();
       
   316 
       
   317     while (!arguments.isEmpty()) {
       
   318         QString fn = arguments.takeFirst();
       
   319 
       
   320         if (fn == QLatin1String("-i")) {
       
   321             interactive(eng);
       
   322             break;
       
   323         }
       
   324 
       
   325         QString contents;
       
   326 
       
   327         if (fn == QLatin1String("-")) {
       
   328             QTextStream stream(stdin, QFile::ReadOnly);
       
   329             contents = stream.readAll();
       
   330         } else {
       
   331             QFile file(fn);
       
   332 	    if (!file.exists()) {
       
   333                 fprintf(stderr, "%s doesn't exists\n", qPrintable(fn));
       
   334 	        return EXIT_FAILURE;
       
   335 	    }
       
   336             if (file.open(QFile::ReadOnly)) {
       
   337                 QTextStream stream(&file);
       
   338                 contents = stream.readAll();
       
   339                 file.close();
       
   340             }
       
   341         }
       
   342 
       
   343         if (contents.isEmpty())
       
   344             continue;
       
   345 
       
   346         if (contents[0] == '#') {
       
   347             contents.prepend("//");
       
   348             QScriptValue args = eng.newArray();
       
   349             args.setProperty("0", QScriptValue(&eng, fn));
       
   350             int i = 1;
       
   351             while (!arguments.isEmpty())
       
   352                 args.setProperty(i++, QScriptValue(&eng, arguments.takeFirst()));
       
   353             eng.currentContext()->activationObject().setProperty("args", args);
       
   354         }
       
   355         QScriptValue r = eng.evaluate(contents);
       
   356         if (eng.hasUncaughtException()) {
       
   357             int line = eng.uncaughtExceptionLineNumber();
       
   358             fprintf(stderr, "%d: %s\n\t%s\n\n", line, qPrintable(fn), qPrintable(r.toString()));
       
   359             return EXIT_FAILURE;
       
   360         }
       
   361         if (r.isNumber())
       
   362             return r.toInt32();
       
   363     }
       
   364 
       
   365     return EXIT_SUCCESS;
       
   366 }
       
   367 
       
   368 #include "main.moc"