WARNING: USE THIS SOFTWARE AT YOUR OWN RISK! THIS IS EXPERIMENTAL SOFTWARE NOT INTENDED FOR PRODUCTION USE! Zuble is currently an early stage prototype. As such Zuble is minimally tested and inherently unstable. It is provided for experimental, development, and demonstration purposes only. Zuble QML Types   |  Zuble C++ Classes   |  Zuble Overview
Zuble  0.1
Zuble Framework C++/QML extension API
ZTextStream.cpp
Go to the documentation of this file.
1 /*
2  * Zuble - A run-time system for QML/Javascript applications
3  * Copyright (C) 2013, 2014 Bob Dinitto
4  *
5  * Filename: ZTextStream.cpp
6  * Created on: 11/9/2014
7  * Author: Bob Dinitto
8  *
9  * Zuble is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #include "ZTextStream.h"
26 #include "ZByteArray.h"
27 #include <QtQml>
28 #include <cstdio>
29 #include "zglobal.h"
30 
31 namespace Zbl
32 {
33 
34 QVariant ZTextStream::m_tags;
35 
36 ZTextStream::ZTextStream(QObject *parent) :
37  QObject(parent), m_d(nullptr)
38 {
39  qDebug("ZTextStream creating input stream");
40 
41  m_t = new QTextStream(stdin, QFile::ReadOnly);
42 
43  if(!m_t)
44  throw ZblException(
45  "ZTextStream::ZTextStream - error creating QTextStream object");
46 
47  createTags();
48 
49  initZioDevice();
50 }
51 
53  QObject(parent), m_d(device)
54 {
55  m_t = new QTextStream();
56 
57  if(!m_t)
58  throw ZblException(
59  "ZTextStream::ZTextStream - error creating QTextStream object");
60 
61  m_t->setDevice(m_d.data());
62 
63  createTags();
64 
65  initZioDevice();
66 }
67 
68 ZTextStream::ZTextStream(ZqStringPtr string, QObject *parent) :
69  QObject(parent), m_d(nullptr), m_s(string)
70 {
71  m_t = new QTextStream();
72 
73  if(!m_t)
74  throw ZblException(
75  "ZTextStream::ZTextStream - error creating QTextStream object");
76 
77  m_t->setString(m_s.data());
78 
79  createTags();
80 
81  initZioDevice();
82 }
83 
85 {
86  if(m_t)
87  delete m_t;
88 }
89 
90 
92 {
93  if(!m_tags.isNull())
94  return;
95 
96  QVariantMap map;
97 
98  map.insert("SmartNotation", SmartNotation);
99  map.insert("FixedNotation", FixedNotation);
100  map.insert("ScientificNotation", ScientificNotation);
101 
102  map.insert("AlignLeft", AlignLeft);
103  map.insert("AlignRight", AlignRight);
104  map.insert("AlignCenter", AlignCenter);
105  map.insert("AlignAccountingStyle", AlignAccountingStyle);
106 
107  map.insert("Ok", Ok);
108  map.insert("ReadPastEnd", ReadPastEnd);
109  map.insert("ReadCorruptData", ReadCorruptData);
110  map.insert("WriteFailed", WriteFailed);
111 
112  map.insert("ShowBase", ShowBase);
113  map.insert("ForcePoint", ForcePoint);
114  map.insert("ForceSign", ForceSign);
115  map.insert("UppercaseBase", UppercaseBase);
116  map.insert("UppercaseDigits", UppercaseDigits);
117 
118  m_tags = QVariant::fromValue(map);
119 }
120 
122 {
123  m_zd = new ZioDevice(this);
124 
125  connect(m_zd, SIGNAL(getCurrentDevice(QIODevice**)),
126  SLOT(getQDevice(QIODevice**)), Qt::DirectConnection);
127 }
128 
130 {
131  if(!m_d && !m_s)
132  throw ZblException("No device or string object available to stream.");
133 
134  if(!m_t)
135  throw ZblException("Text stream object not initialized.");
136 }
137 
139 {
140  if(m_d && !m_d->isReadable())
141  throw ZblException(
142  "Error, device is not readable.");
143 }
144 
146 {
147  validateData();
148 
149  if(m_d && !m_d->isWritable())
150  throw ZblException(
151  "Error, device is not writable.");
152 }
153 
155 {
156  qmlRegisterType<ZTextStream>("org.zuble.qml", 1, 0, "ZTextStream");
157 }
158 
159 void ZTextStream::setLocale(const QString &locale)
160 {
162  validateData();
163  m_t->setLocale(QLocale(locale));
166 }
167 
169 {
170  return m_tags;
171 }
172 
174 {
178 }
179 
181 {
182  *device = m_t->device();
183 }
184 
185 
187 {
189  validateData();
190  ZBL_SLOT_END_RETURN(m_t->autoDetectUnicode(), false,
192 }
193 
194 void ZTextStream::setAutoDetectUnicode(bool autoDetectOn)
195 {
197  validateData();
198  m_t->setAutoDetectUnicode(autoDetectOn);
200  setAutoDetectUnicode failed)
201 }
202 
204 {
206  validateData();
207  ZBL_SLOT_END_RETURN(static_cast<uint>(m_t->status()), false,
209 }
210 
211 bool ZTextStream::atEnd() const
212 {
214  validateData();
215  ZBL_SLOT_END_RETURN(m_t->atEnd(), true,
217 }
218 
219 QVariant ZTextStream::pos() const
220 {
222  validateData();
223  ZBL_SLOT_END_RETURN(QVariant(m_t->pos()), true,
224  Z_FAC_JS, ZTextStream::pos, pos failed)
225 }
226 
228 {
230  validateData();
231  ZBL_SLOT_END_RETURN(static_cast<uint>(m_t->numberFlags()), 0,
233 }
234 
236 {
238  validateData();
239  m_t->setNumberFlags(QTextStream::NumberFlags(flags));
241  setNumberFlags failed)
242 }
243 
245 {
247  validateData();
248  ZBL_SLOT_END_RETURN(static_cast<uint>(m_t->realNumberNotation()), 0,
250 }
251 
253 {
255  validateData();
256  m_t->setRealNumberNotation(QTextStream::RealNumberNotation(notation));
258  setRealNumberNotation failed)
259 }
260 
262 {
264  validateData();
265  ZBL_SLOT_END_RETURN(static_cast<uint>(m_t->fieldAlignment()), 0,
267 }
268 
269 void ZTextStream::setFieldAlignment(uint alignment)
270 {
272  validateData();
273  m_t->setFieldAlignment(QTextStream::FieldAlignment(alignment));
275  setFieldAlignment failed)
276 }
277 
279 {
281  validateData();
282  ZBL_SLOT_END_RETURN(m_t->fieldWidth(), 0,
284 }
285 
287 {
289  validateData();
290  m_t->setFieldWidth(width);
292  setFieldWidth failed)
293 }
294 
296 {
298  validateData();
299  ZBL_SLOT_END_RETURN(m_t->realNumberPrecision(), 0,
301 }
302 
304 {
306  validateData();
307  m_t->setRealNumberPrecision(precision);
309  setRealNumberPrecision failed)
310 }
311 
313 {
315  validateData();
316  ZBL_SLOT_END_RETURN(static_cast<uint>(m_t->padChar().unicode()), 0,
318 }
319 
321 {
323  validateData();
324  m_t->setPadChar(static_cast<char>(ch));
326  setPadChar failed)
327 }
328 
330 {
332  validateData();
333  ZBL_SLOT_END_RETURN(m_t->integerBase(), 0,
335 }
336 
338 {
340  validateData();
341  m_t->setIntegerBase(base);
343  setIntegerBase failed)
344 }
345 
346 QString ZTextStream::getString() const
347 {
348  QString retString;
350  validateData();
351  retString = *m_t->string();
352  ZBL_SLOT_END_RETURN(retString, retString,
354 }
355 
357 {
359  validateData();
360  m_t->resetStatus();
362  resetStatus failed)
363 }
364 
366 {
368  //m_t.clear();
369  if(m_t)
370  {
371  delete m_t;
372  m_t = nullptr;
373  }
374  m_d.clear();
376 }
377 
379 {
381  validateData();
382  m_t->reset();
384  reset failed)
385 }
386 
388 {
390  validateData();
391  m_t->flush();
393  flush failed)
394 }
395 
396 bool ZTextStream::seek(QVariant pos)
397 {
399  validateData();
400  ZBL_SLOT_END_RETURN(m_t->seek(pos.toLongLong()), false,
402 }
403 
405 {
407  validateData();
408  m_t->skipWhiteSpace();
410  skipWhiteSpace failed)
411 }
412 
413 QString ZTextStream::readLine(qint64 maxSize)
414 {
415  qDebug("ZTextStream::readLine maxsize = %d", maxSize);
418  ZBL_SLOT_END_RETURN(m_t->readLine(maxSize), QString(),
420 }
421 
422 QString ZTextStream::read(qint64 maxSize)
423 {
424  qDebug("ZTextStream::read");
427  ZBL_SLOT_END_RETURN(m_t->read(maxSize), QString(),
429 }
430 
432 {
433  qDebug("ZTextStream::readAll");
436  ZBL_SLOT_END_RETURN(m_t->readAll(), QString(),
438 }
439 
440 #define readNumericDataType(dataType, className, methodName) \
441  dataType className::methodName() \
442  { \
443  qDebug("%s::%s", #className, #methodName); \
444  dataType data = 0; \
445  ZBL_SLOT_BEGIN_TRY \
446  validateReadable(); \
447  *m_t >> data; \
448  ZBL_SLOT_END_RETURN(data, 0, \
449  Z_FAC_JS, className::methodName, methodName failed) \
450  }
451 
454 readNumericDataType(signed int, ZTextStream, readInt)
455 readNumericDataType(unsigned int, ZTextStream, readUInt)
456 readNumericDataType(signed long, ZTextStream, readLong)
457 readNumericDataType(unsigned long, ZTextStream, readULong)
458 readNumericDataType(qlonglong, ZTextStream, readLongLong)
459 readNumericDataType(qulonglong, ZTextStream, readULongLong)
460 readNumericDataType(float, ZTextStream, readFloat)
461 readNumericDataType(double, ZTextStream, readDouble)
462 
463 uint ZTextStream::readChar()
464 {
465  qDebug("ZTextStream::readChar");
466  char c = 0;
469  *m_t >> c;
470  ZBL_SLOT_END_RETURN(c, 0,
471  Z_FAC_JS, ZTextStream::readChar, readChar failed)
472 }
473 
475 {
476  qDebug("ZTextStream::readString");
477  QString data;
480  *m_t >> data;
481  ZBL_SLOT_END_RETURN(data, data,
483 }
484 
486 {
487  qDebug("ZTextStream::readBytes");
488  QByteArray data;
491  *m_t >> data;
492  ZBL_SLOT_END_RETURN(new ZByteArray(data), NULL,
494 }
495 
496 
497 
498 #define writeNumericDataType(dataType, className, methodName) \
499  bool className::methodName(dataType data) \
500  { \
501  qDebug("%s::%s", #className, #methodName); \
502  ZBL_SLOT_BEGIN_TRY \
503  validateWritable(); \
504  *m_t << data; \
505  ZBL_SLOT_END_RETURN(true, false, \
506  Z_FAC_JS, className::methodName, methodName failed) \
507  }
508 
509 
520 
521 bool ZTextStream::writeChar(uint data)
522 {
523  qDebug("ZTextStream::writeChar");
526  *m_t << static_cast<char>(data);
527  ZBL_SLOT_END_RETURN(true, false,
529 }
530 
531 bool ZTextStream::writeBytes(QObject* data)
532 {
533  qDebug("ZTextStream::writeBytes");
536  ZByteArray* zb = qobject_cast<ZByteArray*>(data);
537  if(!zb)
538  throw ZblException("Error: ZByteArray object expected. "
539  "Object passed was not a ZByteArray.");
540 
541  *m_t << zb->m_ba.data();
542  ZBL_SLOT_END_RETURN(true, false,
544 }
545 
546 bool ZTextStream::writeString(const QString& text)
547 {
548  qDebug("ZTextStream::writeString");
551  *m_t << text;
552  ZBL_SLOT_END_RETURN(true, false,
554 }
555 
556 bool ZTextStream::writeLine(const QString& text)
557 {
558  qDebug("ZTextStream::writeLine");
561  *m_t << text << endl;
562  ZBL_SLOT_END_RETURN(true, false,
564 }
565 
566 
567 } // Zbl
568 
static void registerType()
Registers ZTextStream as a QML type.
void setRealNumberNotation(uint notation)
ZqIODevicePtr m_d
Definition: ZTextStream.h:217
This class supports streaming of text and binary data.
Definition: ZByteArray.h:41
QString getString() const
static QVariant m_tags
QVariantMap of QTextStream enumerations for use by Javascript programs.
Definition: ZTextStream.h:236
ZioDevice * m_zd
Pointer to contained ZioDevice object.
Definition: ZTextStream.h:227
Q_INVOKABLE bool writeShort(signed short data)
Q_INVOKABLE QString readLine(qint64 maxSize)
Q_INVOKABLE bool writeLine(const QString &text)
Q_INVOKABLE bool writeChar(uint data)
void validateReadable() const
ZTextStream(QObject *parent=nullptr)
Definition: ZTextStream.cpp:36
Q_INVOKABLE void skipWhiteSpace()
Q_INVOKABLE bool writeFloat(float data)
void setFieldWidth(int width)
virtual ~ZTextStream()
Definition: ZTextStream.cpp:84
#define Z_FAC_JS
Definition: zglobal.h:123
bool getAutoDetectUnicode() const
int getFieldWidth() const
Q_INVOKABLE bool writeLong(signed long data)
uint getRealNumberNotation() const
void setAutoDetectUnicode(bool autoDetectOn)
Q_INVOKABLE signed long readLong()
Q_INVOKABLE bool writeUShort(unsigned short data)
Q_INVOKABLE double readDouble()
Q_INVOKABLE bool writeBytes(QObject *data)
void setNumberFlags(uint flags)
bool atEnd() const
Q_INVOKABLE bool writeUInt(unsigned int data)
Q_INVOKABLE bool writeULongLong(qulonglong data)
Q_INVOKABLE qulonglong readULongLong()
QSharedPointer< QString > ZqStringPtr
Definition: zglobal.h:165
void setPadChar(uint ch)
void validateWritable() const
Q_INVOKABLE signed short readShort()
QTextStream * m_t
Definition: ZTextStream.h:220
A javascript wrapper for QIODevice.
Definition: ZioDevice.h:40
void getQDevice(QIODevice **device)
Q_INVOKABLE void release()
Definition: ZAndGate.cpp:6
#define ZBL_SLOT_BEGIN_TRY
Definition: zglobal.h:128
Q_INVOKABLE signed int readInt()
Q_INVOKABLE uint readChar()
Q_INVOKABLE QString readString()
void setRealNumberPrecision(int precision)
uint getStatus() const
QVariant pos() const
uint getPadChar() const
uint getFieldAlignment() const
Q_INVOKABLE bool writeDouble(double data)
Q_INVOKABLE void setLocale(const QString &locale)
#define ZBL_SLOT_END_VOID(facility, code, error_message)
Definition: zglobal.h:134
QObject * getJsDevice()
Q_INVOKABLE unsigned long readULong()
Q_INVOKABLE void flush()
Q_INVOKABLE QString read(qint64 maxSize)
Q_INVOKABLE bool seek(QVariant pos)
Q_INVOKABLE float readFloat()
readNumericDataType(qint16, ZDataStream, readInt16) readNumericDataType(quint16
Q_INVOKABLE bool writeInt(signed int data)
uint getNumberFlags() const
Zuble&#39;s Qt Exception Object.
Definition: ZblException.h:45
QObject device
A ZioDevice object presents this object&#39;s QIODevice interface to Javascript programs.
Definition: ZTextStream.h:120
void setIntegerBase(int base)
Q_INVOKABLE bool writeULong(unsigned long data)
int getRealNumberPrecision() const
int getIntegerBase() const
Q_INVOKABLE void reset()
Q_INVOKABLE void resetStatus()
Q_INVOKABLE QObject * readBytes()
Q_INVOKABLE unsigned short readUShort()
Q_INVOKABLE bool writeLongLong(qlonglong data)
Q_INVOKABLE qlonglong readLongLong()
void setFieldAlignment(uint alignment)
void validateData() const
QVariant getTags()
QSharedPointer< QIODevice > ZqIODevicePtr
Definition: zglobal.h:164
writeNumericDataType(qint16, ZDataStream, writeInt16) writeNumericDataType(quint16
Q_INVOKABLE bool writeString(const QString &text)
Q_INVOKABLE unsigned int readUInt()
#define ZBL_SLOT_END_RETURN(return_success, return_failed, facility, code, error_message)
Definition: zglobal.h:141
Reads and writes UTF-8 text to an i/o stream.
Definition: ZTextStream.h:49
ZqByteArrayPtr m_ba
Definition: ZByteArray.h:103
Q_INVOKABLE QString readAll()
ZqStringPtr m_s
Definition: ZTextStream.h:222