/* PIP - Platform Independent Primitives PIIODevice wrapper around PIString Ivan Pelipenko peri4ko@yandex.ru This program 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 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ #include "piiostring.h" //! \class PIIOString piiostring.h //! \details //! \~english //! This class allow you to use PIString as PIIODevice, e.g. to pass it to PIConfig. //! //! \~russian //! Этот класс позволяет использовать PIString в качестве PIIODevice, например, //! для передачи в PIConfig. //! 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(&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; } ssize_t PIIOString::readDevice(void * read_to, ssize_t max_size) { if (!canRead() || !str || max_size <= 0) return -1; PIString rs = str->mid(pos, max_size); pos += max_size; if (pos > str->size_s()) pos = str->size_s(); const char * cc = rs.data(); int ret = strlen(cc); memcpy(read_to, cc, ret); return ret; } ssize_t PIIOString::writeDevice(const void * data, ssize_t 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 strlen((const char *)data); } 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.size_s(); } bool PIIOString::openDevice() { pos = 0; return (str != 0); }