2010-11-18 13:55:52 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
|
**
|
2010-12-17 17:14:20 +01:00
|
|
|
** No Commercial Usage
|
2010-11-18 13:55:52 +01:00
|
|
|
**
|
2010-12-17 17:14:20 +01:00
|
|
|
** This file contains pre-release code and may not be distributed.
|
|
|
|
|
** You may use this file in accordance with the terms and conditions
|
|
|
|
|
** contained in the Technology Preview License Agreement accompanying
|
|
|
|
|
** this package.
|
2010-11-18 13:55:52 +01:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
2010-12-17 17:14:20 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
** If you have questions regarding the use of this file, please contact
|
|
|
|
|
** Nokia at qt-info@nokia.com.
|
2010-11-18 13:55:52 +01:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef SPLIT_H
|
|
|
|
|
#define SPLIT_H
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
|
|
void trimFront(std::string &s);
|
|
|
|
|
void trimBack(std::string &s);
|
|
|
|
|
void simplify(std::string &s);
|
|
|
|
|
|
|
|
|
|
// Split by character separator.
|
|
|
|
|
template <class Iterator>
|
|
|
|
|
void split(const std::string &s, char sep, Iterator it)
|
|
|
|
|
{
|
|
|
|
|
const std::string::size_type size = s.size();
|
|
|
|
|
for (std::string::size_type pos = 0; pos < size; ) {
|
|
|
|
|
std::string::size_type nextpos = s.find(sep, pos);
|
|
|
|
|
if (nextpos == std::string::npos)
|
|
|
|
|
nextpos = size;
|
|
|
|
|
const std::string token = s.substr(pos, nextpos - pos);
|
|
|
|
|
*it = token;
|
|
|
|
|
++it;
|
|
|
|
|
pos = nextpos + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Format numbers, etc, as a string.
|
|
|
|
|
template <class Streamable>
|
|
|
|
|
std::string toString(const Streamable s)
|
|
|
|
|
{
|
|
|
|
|
std::ostringstream str;
|
|
|
|
|
str << s;
|
|
|
|
|
return str.str();
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-29 12:35:19 +01:00
|
|
|
// Format numbers, etc, as a wstring.
|
|
|
|
|
template <class Streamable>
|
|
|
|
|
std::wstring toWString(const Streamable s)
|
|
|
|
|
{
|
|
|
|
|
std::wostringstream str;
|
|
|
|
|
str << s;
|
|
|
|
|
return str.str();
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-25 17:17:06 +01:00
|
|
|
bool endsWith(const std::string &haystack, const char *needle);
|
2010-12-13 16:25:54 +01:00
|
|
|
inline bool endsWith(const std::string &haystack, char needle)
|
|
|
|
|
{ return !haystack.empty() && haystack.at(haystack.size() - 1) == needle; }
|
2010-11-25 17:17:06 +01:00
|
|
|
|
2010-11-18 13:55:52 +01:00
|
|
|
// Read an integer from a string as '10' or '0xA'
|
|
|
|
|
template <class Integer>
|
|
|
|
|
bool integerFromString(const std::string &s, Integer *v)
|
|
|
|
|
{
|
|
|
|
|
const bool isHex = s.compare(0, 2, "0x") == 0;
|
|
|
|
|
std::istringstream str(isHex ? s.substr(2, s.size() - 2) : s);
|
|
|
|
|
if (isHex)
|
|
|
|
|
str >> std::hex;
|
|
|
|
|
str >> *v;
|
|
|
|
|
return !str.fail();
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-09 17:04:43 +01:00
|
|
|
// Read an integer from a wstring as '10' or '0xA'
|
|
|
|
|
template <class Integer>
|
|
|
|
|
bool integerFromWString(const std::wstring &s, Integer *v)
|
|
|
|
|
{
|
|
|
|
|
const bool isHex = s.compare(0, 2, L"0x") == 0;
|
|
|
|
|
std::wistringstream str(isHex ? s.substr(2, s.size() - 2) : s);
|
|
|
|
|
if (isHex)
|
|
|
|
|
str >> std::hex;
|
|
|
|
|
str >> *v;
|
|
|
|
|
return !str.fail();
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-18 13:55:52 +01:00
|
|
|
void replace(std::wstring &s, wchar_t before, wchar_t after);
|
|
|
|
|
|
|
|
|
|
// Stream a string onto a char stream doing backslash & octal escaping
|
|
|
|
|
// suitable for GDBMI usable as 'str << gdbmiStringFormat(wstring)'
|
|
|
|
|
class gdbmiStringFormat {
|
|
|
|
|
public:
|
|
|
|
|
explicit gdbmiStringFormat(const std::string &s) : m_s(s) {}
|
|
|
|
|
|
|
|
|
|
void format(std::ostream &) const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const std::string &m_s;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Stream a wstring onto a char stream doing backslash & octal escaping
|
|
|
|
|
// suitable for GDBMI usable as 'str << gdbmiWStringFormat(wstring)'
|
|
|
|
|
class gdbmiWStringFormat {
|
|
|
|
|
public:
|
|
|
|
|
explicit gdbmiWStringFormat(const std::wstring &w) : m_w(w) {}
|
|
|
|
|
|
|
|
|
|
void format(std::ostream &) const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const std::wstring &m_w;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
inline std::ostream &operator<<(std::ostream &str, const gdbmiStringFormat &sf)
|
|
|
|
|
{
|
|
|
|
|
sf.format(str);
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline std::ostream &operator<<(std::ostream &str, const gdbmiWStringFormat &wsf)
|
|
|
|
|
{
|
|
|
|
|
wsf.format(str);
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string wStringToGdbmiString(const std::wstring &w);
|
|
|
|
|
std::string wStringToString(const std::wstring &w);
|
2010-11-25 17:17:06 +01:00
|
|
|
std::wstring stringToWString(const std::string &w);
|
2010-11-18 13:55:52 +01:00
|
|
|
|
2010-12-09 17:04:43 +01:00
|
|
|
// String from hex "414A" -> "AJ".
|
|
|
|
|
std::string stringFromHex(const char *begin, const char *end);
|
|
|
|
|
std::wstring dataToHexW(const unsigned char *begin, const unsigned char *end);
|
2010-12-10 10:47:51 +01:00
|
|
|
// Create readable hex: '0xAA 0xBB'..
|
|
|
|
|
std::wstring dataToReadableHexW(const unsigned char *begin, const unsigned char *end);
|
2010-12-09 17:04:43 +01:00
|
|
|
|
2010-11-18 13:55:52 +01:00
|
|
|
// Format a map as a GDBMI hash {key="value",..}
|
|
|
|
|
void formatGdbmiHash(std::ostream &os, const std::map<std::string, std::string> &);
|
|
|
|
|
|
|
|
|
|
#endif // SPLIT_H
|