103 lines
2.7 KiB
C++
103 lines
2.7 KiB
C++
/*
|
|
PIP - Platform Independent Primitives
|
|
PIIODevice wrapper around PIString
|
|
Copyright (C) 2019 Ivan Pelipenko peri4ko@yandex.ru
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "piiostring.h"
|
|
|
|
|
|
/*! \class PIIOString
|
|
* \brief PIIODevice wrapper around PIString
|
|
*
|
|
* \section PIIOString_sec0 Synopsis
|
|
* This class allow you to use PIString as PIIODevice and pass it to, e.g. PIConfig
|
|
*/
|
|
|
|
//REGISTER_DEVICE(PIIOString);
|
|
|
|
|
|
PIIOString::PIIOString(PIString * string, PIIODevice::DeviceMode mode) {
|
|
open(string, mode);
|
|
}
|
|
|
|
|
|
PIIOString::PIIOString(const PIString & string) {
|
|
open(string);
|
|
}
|
|
|
|
|
|
bool PIIOString::open(PIString * string, PIIODevice::DeviceMode mode) {
|
|
str = string;
|
|
return PIIODevice::open(mode);
|
|
}
|
|
|
|
|
|
bool PIIOString::open(const PIString & string) {
|
|
str = const_cast<PIString*>(&string);
|
|
return PIIODevice::open(PIIODevice::ReadOnly);
|
|
}
|
|
|
|
|
|
PIString PIIOString::readLine() {
|
|
if (!canRead() || !str) return PIString();
|
|
int np = pos;
|
|
while (++np < str->size_s())
|
|
if ((*str)[np] == '\n')
|
|
break;
|
|
PIString ret = str->mid(pos, np - pos);
|
|
pos = piMini(np + 1, str->size_s());
|
|
return ret;
|
|
}
|
|
|
|
|
|
int PIIOString::readDevice(void * read_to, int max_size) {
|
|
if (!canRead() || !str) return -1;
|
|
PIString rs = str->mid(pos, max_size);
|
|
pos += max_size;
|
|
if (pos > str->size_s()) pos = str->size_s();
|
|
int ret = rs.lengthAscii();
|
|
memcpy(read_to, rs.data(), rs.lengthAscii());
|
|
return ret;
|
|
}
|
|
|
|
|
|
int PIIOString::writeDevice(const void * data, int max_size) {
|
|
if (!canWrite() || !str) return -1;
|
|
//piCout << "write" << data;
|
|
if (pos > str->size_s()) pos = str->size_s();
|
|
PIString rs = PIString::fromUTF8((const char *)data);
|
|
if (rs.size_s() > max_size) rs.resize(max_size);
|
|
str->insert(pos, rs);
|
|
pos += rs.size_s();
|
|
return rs.lengthAscii();
|
|
}
|
|
|
|
|
|
int PIIOString::writeString(const PIString & string) {
|
|
if (!canWrite() || !str) return -1;
|
|
if (pos > str->size_s()) pos = str->size_s();
|
|
str->insert(pos, string);
|
|
pos += string.size_s();
|
|
return string.lengthAscii();
|
|
}
|
|
|
|
|
|
bool PIIOString::openDevice() {
|
|
pos = 0;
|
|
return (str != 0);
|
|
}
|