-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcstring.h
More file actions
59 lines (47 loc) · 2.25 KB
/
cstring.h
File metadata and controls
59 lines (47 loc) · 2.25 KB
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
/* Copyright 2015 Golden Helix, Inc. */
#ifndef CSTRING_H
#define CSTRING_H
#include <QByteArray>
#include <QPair>
#include <QString>
// TODO: This class is going to break inheritance from QByteArray and
// grow ability to easily intern char* data allocated outside it's
// scope. It will also grow the ability to refcount a shared char*
// allocation while having it's individualized offset pointer into a
// null-separated list of strings.
//Store UTF8 string data in data arrays
class CString : public QByteArray
{
public:
CString() : QByteArray() {}
CString( const char * str ) : QByteArray( str ) {}
CString( const char * data, int size ) : QByteArray(data, size) {}
CString( const QByteArray & other ) : QByteArray(other) {}
CString( const CString & other ) : QByteArray(other) {}
CString( const QString & other ) : QByteArray(other.toUtf8()) {}
CString(int size, char ch ) : QByteArray(size, ch) {}
QString asQString() const { return QString::fromUtf8( constData(), size() ); }
operator QString () const { return asQString(); }
//replaces contents with null terminated input string
void read(const char * str);
//conversion functions that return missing values (missing.h) when
//failed to convert
static const QVector< QPair<CString, CString> >& trueFalsePairVector();
//true if value could be converted to a boolean
bool isBool(const QVector< QPair<CString, CString> >* trueFalsePairs = 0) const;
//one of known "true" strings, otherwise false
bool toPureBool(const QVector< QPair<CString, CString> >* trueFalsePairs = 0) const;
//one of known "true" or "false" strings, otherwise missing
char toBool(const QVector< QPair<CString, CString> >* trueFalsePairs = 0) const;
int toInt(bool* ok=0) const;
qint64 toInt64(bool* ok=0) const;
float toFloat(bool* ok=0) const;
double toDouble(bool* ok=0) const;
//Note that you just call constData() to get a const char *, but in
//most cases it will auto-cast for you.
};
/// Helper macro for sending a QString to c code that requires const char * (equivelant to qPrintable)
#define toC(string) (string).toLocal8Bit().constData()
/// Helper macro for interfacing with any C library. We always use UTF8 on persistant data (and SQLite)
#define to8(string) (string).toUtf8().constData()
#endif