CiftiLib
A C++ library for CIFTI-2 and CIFTI-1 files
AString.h
1#ifndef __ASTRING_H__
2#define __ASTRING_H__
3
4/*LICENSE_START*/
5/*
6 * Copyright (c) 2014, Washington University School of Medicine
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without modification,
10 * are permitted provided that the following conditions are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <string>
32#include <vector>
33
34#include "stdint.h"
35
36#ifdef __ASTRING_H_HAVE_IMPL__
37#undef __ASTRING_H_HAVE_IMPL__
38#endif
39
40#ifdef CIFTILIB_USE_QT
41#define __ASTRING_H_HAVE_IMPL__
42#include <QString>
43namespace cifti
44{
45 struct AString : public QString
46 {//QT doesn't convert from std::string, and conversions have to be member functions
47 AString() : QString() {}
48
49 //some QString constructors are explicit, so instead only make conversion constructors for whatever works with assignment to QString
50 //the cast is required to avoid recursing through AString
51 template <typename T>
52 AString(const T& rhs) : QString()
53 {
54 *(static_cast<QString*>(this)) = rhs;
55 }
56
57 AString(const std::string& rhs) : QString()
58 {
59 (*this) = fromStdString(rhs);
60 }
61 };
62#define ASTRING_TO_CSTR(mystr) ((mystr).toLocal8Bit().constData())
63#define ASTRING_UTF8_RAW(mystr) ((mystr).toUtf8().constData())
64 inline std::string AString_to_std_string(const AString& mystr)
65 {
66 QByteArray temparray = mystr.toLocal8Bit();
67 return std::string(temparray.constData(), temparray.size());
68 }
69 inline AString AString_from_latin1(const char* data, const int& size)
70 {
71 return QString::fromLatin1(data, size);
72 }
73 inline AString AString_substr(const AString& mystr, const int& first, const int& count = -1)
74 {
75 return mystr.mid(first, count);
76 }
77 inline bool AString_endsWith(const AString& test, const AString& pattern)
78 {
79 return test.endsWith(pattern);
80 }
81 template <typename T>
82 AString AString_number(const T& num)
83 {
84 return QString::number(num);
85 }
86 template <typename T>
87 AString AString_number_fixed(const T& num, const int& numDecimals)
88 {
89 return QString::number(num, 'f', numDecimals);
90 }
91}
92#endif //CIFTILIB_USE_QT
93
94#ifdef CIFTILIB_USE_XMLPP
95#define __ASTRING_H_HAVE_IMPL__
96#include "glibmm/convert.h"
97#include "glibmm/ustring.h"
98#include <iomanip>
99namespace cifti
100{
101 typedef Glib::ustring AString;
102#define ASTRING_TO_CSTR(mystr) (Glib::locale_from_utf8((mystr)).c_str())
103#define ASTRING_UTF8_RAW(mystr) ((mystr).data())
104 inline std::string AString_to_std_string(const AString& mystr)
105 {
106 return Glib::locale_from_utf8(mystr);
107 }
108 inline AString AString_from_latin1(const char* data, const int& size)
109 {
110 return Glib::convert(std::string(data, size), "UTF-8", "ISO-8859-1");
111 }
112 inline AString AString_substr(const AString& mystr, const Glib::ustring::size_type& first, const Glib::ustring::size_type& count = std::string::npos)
113 {//HACK: Glib::ustring::npos is undefined at link time with glibmm 2.4 for unknown reasons, but the header says it is equal to std::string's, so use it instead
114 return mystr.substr(first, count);
115 }
116 inline bool AString_endsWith(const AString& test, const AString& pattern)
117 {
118 return test.substr(test.size() - pattern.size()) == pattern;
119 }
120 template <typename T>
121 AString AString_number(const T& num)
122 {
123 return Glib::ustring::format(num);
124 }
125 template <typename T>
126 AString AString_number_fixed(const T& num, const int& numDecimals)
127 {
128 return Glib::ustring::format(std::fixed, std::setprecision(numDecimals), num);
129 }
130}
131#endif //CIFTILIB_USE_XMLPP
132
133#ifndef __ASTRING_H_HAVE_IMPL__
134#error "you must define either CIFTILIB_USE_QT or CIFTILIB_USE_XMLPP to select what unicode string implementation to use"
135#endif
136
137namespace cifti
138{
139 //more helper functions
140 std::vector<AString> AString_split(const AString& input, const char& delim);
141 std::vector<AString> AString_split_whitespace(const AString& input);
142 int64_t AString_toInt(const AString& input, bool& ok);
143 float AString_toFloat(const AString& input, bool& ok);
144}
145
146#endif //__ASTRING_H__
namespace for all CiftiLib functionality
Definition: CiftiBrainModelsMap.h:42