All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreStringNode.h
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
QoreStringNode.h
QoreStringNode Class Definition
Qore Programming Language
Copyright 2003 - 2013 David Nichols
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _QORE_QORESTRINGNODE_H
#define _QORE_QORESTRINGNODE_H
#include <qore/AbstractQoreNode.h>
#include <qore/QoreString.h>
class QoreStringNode : public SimpleValueQoreNode, public QoreString {
private:
DLLLOCAL QoreStringNode(QoreString *str);
DLLLOCAL QoreStringNode& operator=(const QoreStringNode&);
DLLLOCAL virtual bool getAsBoolImpl() const;
DLLLOCAL virtual int getAsIntImpl() const;
DLLLOCAL virtual int64 getAsBigIntImpl() const;
DLLLOCAL virtual double getAsFloatImpl() const;
protected:
DLLEXPORT virtual ~QoreStringNode();
public:
DLLEXPORT QoreStringNode();
DLLEXPORT QoreStringNode(const char *str, const QoreEncoding *enc = QCS_DEFAULT);
DLLEXPORT QoreStringNode(const QoreString &str);
DLLEXPORT QoreStringNode(const QoreStringNode &str);
DLLEXPORT QoreStringNode(const std::string &str, const QoreEncoding *enc = QCS_DEFAULT);
// copies binary object and makes a base64-encoded string out of it
DLLEXPORT QoreStringNode(const BinaryNode *b);
DLLEXPORT QoreStringNode(const BinaryNode *bin, qore_size_t maxlinelen);
DLLEXPORT QoreStringNode(char *nbuf, qore_size_t nlen, qore_size_t nallocated, const QoreEncoding *enc);
DLLEXPORT QoreStringNode(const char *str, qore_size_t len, const QoreEncoding *new_qorecharset = QCS_DEFAULT);
// creates a string from a single character
DLLEXPORT QoreStringNode(char c);
DLLEXPORT int getAsString(QoreString &str, int format_offset, ExceptionSink *xsink) const;
DLLEXPORT QoreString *getAsString(bool &del, int format_offset, ExceptionSink *xsink) const;
DLLEXPORT virtual QoreString *getStringRepresentation(bool &del) const;
DLLEXPORT virtual void getStringRepresentation(QoreString &str) const;
DLLEXPORT virtual DateTime *getDateTimeRepresentation(bool &del) const;
DLLEXPORT virtual void getDateTimeRepresentation(DateTime &dt) const;
DLLEXPORT virtual AbstractQoreNode *realCopy() const;
DLLEXPORT virtual bool is_equal_soft(const AbstractQoreNode *v, ExceptionSink *xsink) const;
DLLEXPORT virtual bool is_equal_hard(const AbstractQoreNode *v, ExceptionSink *xsink) const;
DLLEXPORT virtual const char *getTypeName() const;
DLLEXPORT QoreStringNode *convertEncoding(const QoreEncoding *nccs, ExceptionSink *xsink) const;
DLLEXPORT QoreStringNode *substr(qore_offset_t offset, ExceptionSink *xsink) const;
DLLEXPORT QoreStringNode *substr(qore_offset_t offset, qore_offset_t length, ExceptionSink *xsink) const;
DLLEXPORT QoreStringNode *reverse() const;
// copy function
DLLEXPORT QoreStringNode *copy() const;
DLLEXPORT static QoreStringNode *createAndConvertEncoding(const char *str, const QoreEncoding *from, const QoreEncoding *to, ExceptionSink *xsink);
DLLEXPORT QoreStringNode *parseBase64ToString(ExceptionSink *xsink) const;
DLLEXPORT QoreStringNode* parseBase64ToString(const QoreEncoding* enc, ExceptionSink* xsink) const;
DLLEXPORT QoreStringNode *stringRefSelf() const;
DLLLOCAL QoreStringNode(const char *str, const QoreEncoding *from, const QoreEncoding *to, ExceptionSink *xsink);
DLLEXPORT QoreStringNode *extract(qore_offset_t offset, ExceptionSink *xsink);
DLLEXPORT QoreStringNode *extract(qore_offset_t offset, qore_offset_t length, ExceptionSink *xsink);
DLLEXPORT QoreStringNode *extract(qore_offset_t offset, qore_offset_t length, const AbstractQoreNode *strn, ExceptionSink *xsink);
DLLLOCAL QoreStringNode(struct qore_string_private *p);
DLLLOCAL static const char *getStaticTypeName() {
return "string";
}
DLLLOCAL static qore_type_t getStaticTypeCode() {
return NT_STRING;
}
DLLLOCAL virtual AbstractQoreNode *parseInit(LocalVar *oflag, int pflag, int &lvids, const QoreTypeInfo *&typeInfo);
};
class QoreStringNodeMaker : public QoreStringNode {
private:
DLLLOCAL QoreStringNodeMaker(const QoreStringNodeMaker& str);
DLLLOCAL QoreStringNodeMaker& operator=(const QoreStringNodeMaker&);
public:
DLLEXPORT QoreStringNodeMaker(const char* fmt, ...);
};
extern QoreStringNode *NullString;
class QoreStringValueHelper {
private:
QoreString *str;
bool del;
DLLLOCAL QoreStringValueHelper(const QoreStringValueHelper&); // not implemented
DLLLOCAL QoreStringValueHelper& operator=(const QoreStringValueHelper&); // not implemented
DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
public:
DLLLOCAL QoreStringValueHelper(const AbstractQoreNode *n) {
if (n) {
//optimization to remove the need for a virtual function call in the most common case
if (n->getType() == NT_STRING) {
del = false;
str = const_cast< QoreStringNode *>(reinterpret_cast<const QoreStringNode *>(n));
}
else
str = n->getStringRepresentation(del);
}
else {
str = NullString;
del = false;
}
}
DLLLOCAL QoreStringValueHelper(const AbstractQoreNode *n, const QoreEncoding *enc, ExceptionSink *xsink) {
if (n) {
//optimization to remove the need for a virtual function call in the most common case
if (n->getType() == NT_STRING) {
del = false;
str = const_cast< QoreStringNode *>(reinterpret_cast<const QoreStringNode *>(n));
}
else
str = n->getStringRepresentation(del);
if (str->getEncoding() != enc) {
QoreString *t = str->convertEncoding(enc, xsink);
if (!t)
return;
if (del)
delete str;
str = t;
del = true;
}
}
else {
str = NullString;
del = false;
}
}
DLLLOCAL ~QoreStringValueHelper() {
if (del)
delete str;
}
DLLLOCAL const QoreString *operator->() { return str; }
DLLLOCAL const QoreString *operator*() { return str; }
DLLLOCAL QoreString *giveString() {
if (!str)
return 0;
if (!del)
return str->copy();
QoreString *rv = str;
del = false;
str = 0;
return rv;
}
DLLLOCAL bool is_temp() const {
return del;
}
};
class QoreStringNodeValueHelper {
private:
QoreStringNode *str;
bool temp;
DLLLOCAL QoreStringNodeValueHelper(const QoreStringNodeValueHelper&);
DLLLOCAL QoreStringNodeValueHelper& operator=(const QoreStringNodeValueHelper&);
DLLLOCAL void* operator new(size_t);
public:
DLLLOCAL QoreStringNodeValueHelper(const AbstractQoreNode *n) {
if (!n) {
str = NullString;
temp = false;
return;
}
qore_type_t ntype = n->getType();
if (ntype == NT_STRING) {
str = const_cast< QoreStringNode *>(reinterpret_cast<const QoreStringNode *>(n));
temp = false;
}
else {
str = new QoreStringNode();
n->getStringRepresentation(*(static_cast<QoreString *>(str)));
temp = true;
}
}
DLLLOCAL ~QoreStringNodeValueHelper() {
if (temp)
str->deref();
}
DLLLOCAL const QoreStringNode *operator->() { return str; }
DLLLOCAL const QoreStringNode *operator*() { return str; }
DLLLOCAL QoreStringNode *getReferencedValue() {
if (temp)
temp = false;
else if (str)
str->ref();
return str;
}
};
#include <qore/ReferenceHolder.h>
typedef SimpleRefHolder<QoreStringNode> QoreStringNodeHolder;
extern QoreString NothingTypeString;
class QoreNodeAsStringHelper {
private:
QoreString *str;
bool del;
DLLLOCAL QoreNodeAsStringHelper(const QoreNodeAsStringHelper&); // not implemented
DLLLOCAL QoreNodeAsStringHelper& operator=(const QoreNodeAsStringHelper&); // not implemented
DLLLOCAL void* operator new(size_t); // not implemented, make sure it is not new'ed
public:
DLLEXPORT QoreNodeAsStringHelper(const AbstractQoreNode *n, int format_offset, ExceptionSink *xsink);
DLLLOCAL ~QoreNodeAsStringHelper() {
if (del)
delete str;
}
DLLLOCAL const QoreString *operator->() { return str; }
DLLLOCAL const QoreString *operator*() { return str; }
DLLLOCAL QoreString *giveString() {
if (!str)
return 0;
if (!del)
return str->copy();
QoreString *rv = str;
del = false;
str = 0;
return rv;
}
};
#endif