code format
This commit is contained in:
@@ -1,23 +1,24 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Abstract input/output device
|
||||
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||
PIP - Platform Independent Primitives
|
||||
Abstract input/output device
|
||||
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@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 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.
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "piiodevice.h"
|
||||
|
||||
#include "piconfig.h"
|
||||
#include "piconnection.h"
|
||||
#include "pipropertystorage.h"
|
||||
@@ -235,8 +236,7 @@ bool PIIODevice::isThreadedWrite() const {
|
||||
|
||||
|
||||
void PIIODevice::startThreadedWrite() {
|
||||
if (!write_thread.isRunning())
|
||||
write_thread.startOnce();
|
||||
if (!write_thread.isRunning()) write_thread.startOnce();
|
||||
}
|
||||
|
||||
|
||||
@@ -313,11 +313,13 @@ void PIIODevice::_init() {
|
||||
#else
|
||||
threaded_read_buffer_size = 4096;
|
||||
#endif
|
||||
read_thread .setName("__S__.PIIODevice.read_thread" );
|
||||
read_thread.setName("__S__.PIIODevice.read_thread");
|
||||
write_thread.setName("__S__.PIIODevice.write_thread");
|
||||
CONNECT(void, &write_thread, started, this, write_func);
|
||||
CONNECTL(&read_thread, started, [this](){if (!isOpened()) open();});
|
||||
read_thread.setSlot([this](void*){read_func();});
|
||||
CONNECTL(&read_thread, started, [this]() {
|
||||
if (!isOpened()) open();
|
||||
});
|
||||
read_thread.setSlot([this](void *) { read_func(); });
|
||||
}
|
||||
|
||||
|
||||
@@ -339,8 +341,7 @@ void PIIODevice::write_func() {
|
||||
PIIODevice * PIIODevice::newDeviceByPrefix(const char * prefix) {
|
||||
if (!prefix) return nullptr;
|
||||
auto fi = fabrics().value(prefix);
|
||||
if (fi.fabricator)
|
||||
return fi.fabricator();
|
||||
if (fi.fabricator) return fi.fabricator();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -364,10 +365,10 @@ void PIIODevice::read_func() {
|
||||
ssize_t readed_ = read(buffer_tr.data(), buffer_tr.size_s());
|
||||
if (readed_ <= 0) {
|
||||
piMSleep(10);
|
||||
//cout << readed_ << ", " << errno << ", " << errorString() << endl;
|
||||
// cout << readed_ << ", " << errno << ", " << errorString() << endl;
|
||||
return;
|
||||
}
|
||||
//piCoutObj << "readed" << readed_;// << ", " << errno << ", " << errorString();
|
||||
// piCoutObj << "readed" << readed_;// << ", " << errno << ", " << errorString();
|
||||
threadedRead(buffer_tr.data(), readed_);
|
||||
threadedReadEvent(buffer_tr.data(), readed_);
|
||||
}
|
||||
@@ -381,8 +382,10 @@ PIByteArray PIIODevice::readForTime(double timeout_ms) {
|
||||
tm.reset();
|
||||
while (tm.elapsed_m() < timeout_ms) {
|
||||
ret = read(td, threaded_read_buffer_size);
|
||||
if (ret <= 0) piMinSleep();
|
||||
else str.append(td, ret);
|
||||
if (ret <= 0)
|
||||
piMinSleep();
|
||||
else
|
||||
str.append(td, ret);
|
||||
}
|
||||
delete[] td;
|
||||
return str;
|
||||
@@ -442,8 +445,10 @@ bool PIIODevice::configure(const PIString & config_file, const PIString & sectio
|
||||
if (!conf.isOpened()) return false;
|
||||
bool ex = true;
|
||||
PIConfig::Entry em;
|
||||
if (section.isEmpty()) em = conf.rootEntry();
|
||||
else em = conf.getValue(section, PIString(), &ex);
|
||||
if (section.isEmpty())
|
||||
em = conf.rootEntry();
|
||||
else
|
||||
em = conf.getValue(section, PIString(), &ex);
|
||||
if (!ex) return false;
|
||||
PIConfig::Entry * ep = 0;
|
||||
if (parent_section) ep = em.parent();
|
||||
@@ -470,7 +475,7 @@ PIString PIIODevice::constructFullPath() const {
|
||||
|
||||
void PIIODevice::configureFromFullPath(const PIString & full_path) {
|
||||
PIString fp;
|
||||
DeviceMode dm = ReadWrite;
|
||||
DeviceMode dm = ReadWrite;
|
||||
DeviceOptions op = 0;
|
||||
splitFullPath(full_path, &fp, &dm, &op);
|
||||
setMode(dm);
|
||||
@@ -481,8 +486,8 @@ void PIIODevice::configureFromFullPath(const PIString & full_path) {
|
||||
|
||||
PIVariantTypes::IODevice PIIODevice::constructVariant() const {
|
||||
PIVariantTypes::IODevice ret;
|
||||
ret.prefix = fullPathPrefix();
|
||||
ret.mode = mode();
|
||||
ret.prefix = fullPathPrefix();
|
||||
ret.mode = mode();
|
||||
ret.options = options();
|
||||
ret.set(constructVariantDevice());
|
||||
return ret;
|
||||
@@ -497,22 +502,25 @@ void PIIODevice::configureFromVariant(const PIVariantTypes::IODevice & d) {
|
||||
|
||||
|
||||
void PIIODevice::splitFullPath(PIString fpwm, PIString * full_path, DeviceMode * mode, DeviceOptions * opts) {
|
||||
int dm = 0;
|
||||
int dm = 0;
|
||||
DeviceOptions op = 0;
|
||||
if (fpwm.find('(') > 0 && fpwm.find(')') > 0) {
|
||||
PIString dms(fpwm.right(fpwm.length() - fpwm.findLast('(')).takeRange('(', ')').trim().toLowerCase().removeAll(' '));
|
||||
PIStringList opts(dms.split(","));
|
||||
piForeachC (PIString & o, opts) {
|
||||
//piCout << dms;
|
||||
piForeachC(PIString & o, opts) {
|
||||
// piCout << dms;
|
||||
if (o == PIStringAscii("r") || o == PIStringAscii("ro") || o == PIStringAscii("read") || o == PIStringAscii("readonly"))
|
||||
dm |= ReadOnly;
|
||||
if (o == PIStringAscii("w") || o == PIStringAscii("wo") || o == PIStringAscii("write") || o == PIStringAscii("writeonly"))
|
||||
dm |= WriteOnly;
|
||||
if (o == PIStringAscii("br") || o == PIStringAscii("blockr") || o == PIStringAscii("blockread") || o == PIStringAscii("blockingread"))
|
||||
if (o == PIStringAscii("br") || o == PIStringAscii("blockr") || o == PIStringAscii("blockread") ||
|
||||
o == PIStringAscii("blockingread"))
|
||||
op |= BlockingRead;
|
||||
if (o == PIStringAscii("bw") || o == PIStringAscii("blockw") || o == PIStringAscii("blockwrite") || o == PIStringAscii("blockingwrite"))
|
||||
if (o == PIStringAscii("bw") || o == PIStringAscii("blockw") || o == PIStringAscii("blockwrite") ||
|
||||
o == PIStringAscii("blockingwrite"))
|
||||
op |= BlockingWrite;
|
||||
if (o == PIStringAscii("brw") || o == PIStringAscii("bwr") || o == PIStringAscii("blockrw") || o == PIStringAscii("blockwr") || o == PIStringAscii("blockreadrite") || o == PIStringAscii("blockingreadwrite"))
|
||||
if (o == PIStringAscii("brw") || o == PIStringAscii("bwr") || o == PIStringAscii("blockrw") || o == PIStringAscii("blockwr") ||
|
||||
o == PIStringAscii("blockreadrite") || o == PIStringAscii("blockingreadwrite"))
|
||||
op |= BlockingRead | BlockingWrite;
|
||||
}
|
||||
fpwm.cutRight(fpwm.length() - fpwm.findLast('(')).trim();
|
||||
@@ -542,12 +550,12 @@ PIStringList PIIODevice::availableClasses() {
|
||||
|
||||
void PIIODevice::registerDevice(PIConstChars prefix, PIConstChars classname, PIIODevice * (*fabric)()) {
|
||||
if (prefix.isEmpty()) return;
|
||||
//printf("registerDevice %s %d %d\n", prefix, p.isEmpty(), fabrics().size());
|
||||
// printf("registerDevice %s %d %d\n", prefix, p.isEmpty(), fabrics().size());
|
||||
if (!fabrics().contains(prefix)) {
|
||||
FabricInfo fi;
|
||||
fi.prefix = prefix;
|
||||
fi.classname = classname;
|
||||
fi.fabricator = fabric;
|
||||
fi.prefix = prefix;
|
||||
fi.classname = classname;
|
||||
fi.fabricator = fabric;
|
||||
fabrics()[prefix] = fi;
|
||||
}
|
||||
}
|
||||
@@ -557,10 +565,26 @@ PIString PIIODevice::fullPathOptions() const {
|
||||
if (mode_ == ReadWrite && options_ == 0) return PIString();
|
||||
PIString ret(" (");
|
||||
bool f = true;
|
||||
if (mode_ == ReadOnly) {if (!f) ret += ","; f = false; ret += "ro";}
|
||||
if (mode_ == WriteOnly) {if (!f) ret += ","; f = false; ret += "wo";}
|
||||
if (options_[BlockingRead]) {if (!f) ret += ","; f = false; ret += "br";}
|
||||
if (options_[BlockingWrite]) {if (!f) ret += ","; f = false; ret += "bw";}
|
||||
if (mode_ == ReadOnly) {
|
||||
if (!f) ret += ",";
|
||||
f = false;
|
||||
ret += "ro";
|
||||
}
|
||||
if (mode_ == WriteOnly) {
|
||||
if (!f) ret += ",";
|
||||
f = false;
|
||||
ret += "wo";
|
||||
}
|
||||
if (options_[BlockingRead]) {
|
||||
if (!f) ret += ",";
|
||||
f = false;
|
||||
ret += "br";
|
||||
}
|
||||
if (options_[BlockingWrite]) {
|
||||
if (!f) ret += ",";
|
||||
f = false;
|
||||
ret += "bw";
|
||||
}
|
||||
return ret + ")";
|
||||
}
|
||||
|
||||
@@ -599,7 +623,7 @@ PIString PIIODevice::normalizeFullPath(const PIString & full_path) {
|
||||
}
|
||||
nfp_mutex.unlock();
|
||||
PIIODevice * d = createFromFullPath(full_path);
|
||||
//piCout << "normalizeFullPath" << d;
|
||||
// piCout << "normalizeFullPath" << d;
|
||||
if (d == 0) return PIString();
|
||||
ret = d->constructFullPath();
|
||||
delete d;
|
||||
@@ -619,8 +643,8 @@ PIMap<PIConstChars, PIIODevice::FabricInfo> & PIIODevice::fabrics() {
|
||||
}
|
||||
|
||||
|
||||
bool PIIODevice::threadedRead(const uchar *readed, ssize_t size) {
|
||||
// piCout << "iodevice threaded read";
|
||||
bool PIIODevice::threadedRead(const uchar * readed, ssize_t size) {
|
||||
// piCout << "iodevice threaded read";
|
||||
if (func_read) return func_read(readed, size, ret_data_);
|
||||
return true;
|
||||
}
|
||||
@@ -636,5 +660,3 @@ PIPropertyStorage PIIODevice::constructVariantDevice() const {
|
||||
void PIIODevice::configureFromVariantDevice(const PIPropertyStorage & d) {
|
||||
setPath(d.propertyValueByName("path").toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user