diff --git a/client/ipcclient.cbp b/client/ipcclient.cbp index 42b0fb4..caf6bac 100644 --- a/client/ipcclient.cbp +++ b/client/ipcclient.cbp @@ -45,6 +45,7 @@ + diff --git a/client/src/core/attribute.cc b/client/src/core/attribute.cc index 1992d8b..ed9c5cb 100644 --- a/client/src/core/attribute.cc +++ b/client/src/core/attribute.cc @@ -47,11 +47,52 @@ namespace TN3270 { - Attribute::Attribute(H3270 *hSession, Type type, void * worker) { + Attribute::Attribute(const Attribute &src) { - this->hSession = hSession; - this->type = type; - this->worker = worker; + this->type = src.type; + this->szData = src.szData; + this->get = src.get; + this->set = src.set; + + if(this->szData) { + this->data = new uint8_t[this->szData]; + memcpy(this->data,src.data,this->szData); + } else { + this->data = nullptr; + } + + } + + Attribute::Attribute(const Attribute *src) { + + this->type = src->type; + this->szData = src->szData; + + this->get = src->get; + this->set = src->set; + + if(this->szData) { + this->data = new uint8_t[this->szData]; + memcpy(this->data,src->data,this->szData); + } else { + this->data = nullptr; + } + + } + + Attribute::Attribute(Type type, size_t szWorker) { + + this->type = type; + this->szData = szWorker; + + if(this->szData) { + this->data = new uint8_t[this->szData]; + memset(this->data,0,this->szData); + } else { + this->data = nullptr; + } + + debug("worker=",((void *) this->data)," length=",szWorker); get.name = [](const void *worker) { return "unnamed"; @@ -95,6 +136,11 @@ } Attribute::~Attribute() { + + if(data) { + delete[] data; + } + } } diff --git a/client/src/core/linux/attribute.cc b/client/src/core/linux/attribute.cc new file mode 100644 index 0000000..3754aef --- /dev/null +++ b/client/src/core/linux/attribute.cc @@ -0,0 +1,74 @@ +/* + * "Software pw3270, desenvolvido com base nos códigos fontes do WC3270 e X3270 + * (Paul Mattes Paul.Mattes@usa.net), de emulação de terminal 3270 para acesso a + * aplicativos mainframe. Registro no INPI sob o nome G3270. + * + * Copyright (C) <2008> + * + * Este programa é software livre. Você pode redistribuí-lo e/ou modificá-lo sob + * os termos da GPL v.2 - Licença Pública Geral GNU, conforme publicado pela + * Free Software Foundation. + * + * Este programa é distribuído na expectativa de ser útil, mas SEM QUALQUER + * GARANTIA; sem mesmo a garantia implícita de COMERCIALIZAÇÃO ou de ADEQUAÇÃO + * A QUALQUER PROPÓSITO EM PARTICULAR. Consulte a Licença Pública Geral GNU para + * obter mais detalhes. + * + * Você deve ter recebido uma cópia da Licença Pública Geral GNU junto com este + * programa; se não, escreva para a Free Software Foundation, Inc., 51 Franklin + * St, Fifth Floor, Boston, MA 02110-1301 USA + * + * Este programa está nomeado como - e possui - linhas de código. + * + * Contatos: + * + * perry.werneck@gmail.com (Alexandre Perry de Souza Werneck) + * erico.mendonca@gmail.com (Erico Mascarenhas Mendonça) + * + */ + +/** + * @file src/os/linux/attribute.cc + * + * @brief Implements methods for static attribute management. + * + * @author perry.werneck@gmail.com + * + */ + + #include + #include + #include + +/*---[ Implement ]----------------------------------------------------------------------------------*/ + + std::vector TN3270::getAttributes() noexcept { + + std::vector attributes; + + for(auto prop = lib3270_get_int_properties_list(); prop->name; prop++) { + attributes.push_back((const LIB3270_PROPERTY *) prop); + } + + for(auto prop = lib3270_get_unsigned_properties_list(); prop->name; prop++) { + attributes.push_back((const LIB3270_PROPERTY *) prop); + } + + for(auto prop = lib3270_get_string_properties_list(); prop->name; prop++) { + attributes.push_back((const LIB3270_PROPERTY *) prop); + } + + for(auto prop = lib3270_get_toggle_list(); prop->name; prop++) { + attributes.push_back((const LIB3270_PROPERTY *) prop); + } + + for(auto prop = lib3270_get_boolean_properties_list(); prop->name; prop++) { + attributes.push_back((const LIB3270_PROPERTY *) prop); + } + + return attributes; + + } + + + diff --git a/client/src/include/lib3270/ipc.h b/client/src/include/lib3270/ipc.h index f1bcdae..a800796 100644 --- a/client/src/include/lib3270/ipc.h +++ b/client/src/include/lib3270/ipc.h @@ -86,6 +86,12 @@ */ TN3270_PUBLIC const char * getRevision(); + /** + * @brief Get list of attributes. + * + */ + TN3270_PUBLIC std::vector getAttributes() noexcept; + class TN3270_PUBLIC Event { public: @@ -259,14 +265,17 @@ Uint64 = 't' }; + private: Type type; ///< @brief Data type. - H3270 * hSession; ///< @brief TN3270 Session which "owns" this attribute. - void * worker; ///< @brief Internal worker. protected: + size_t szData; + uint8_t * data; ///< @brief Internal worker. + struct { std::function name; + std::function description; std::function asString; std::function asInt32; @@ -281,72 +290,76 @@ std::function asBoolean; } set; - Attribute(H3270 *hSession, Type type, void * worker); + Attribute(Type type, size_t szWorker = 0); public: + + Attribute(const Attribute &src); + Attribute(const Attribute *src); + ~Attribute(); - inline H3270 * getTN3270Session() const { - return this->hSession; + inline const char * getName() const { + return this->get.name(this->data); } - inline const char * getName() const { - return this->get.name(this->worker); + inline const char * getDescription() const { + return this->get.description(this->data); } inline std::string getString() const { - return get.asString(*this,worker); + return get.asString(*this,data); } inline int32_t getInt32() const { - return get.asInt32(*this,worker); + return get.asInt32(*this,data); } inline uint32_t getUint32() const { - return get.asUint32(*this,worker); + return get.asUint32(*this,data); } inline bool getBoolean() const { - return get.asBoolean(*this,worker); + return get.asBoolean(*this,data); } inline std::string toString() const { - return get.asString(*this, worker); + return get.asString(*this, data); } inline void setString(const char * value) { - set.asString(*this,worker,value); + set.asString(*this,data,value); } inline void setInt32(const int32_t value) { - set.asInt32(*this,worker,value); + set.asInt32(*this,data,value); } inline void setUint32(const uint32_t value) { - set.asUint32(*this,worker,value); + set.asUint32(*this,data,value); } inline void setBoolean(const bool value) { - set.asBoolean(*this,worker,value); + set.asBoolean(*this,data,value); } inline Attribute & operator=(const char * value) { - set.asString(*this,worker,value); + set.asString(*this,data,value); return *this; } inline Attribute & operator=(const int32_t value) { - set.asInt32(*this,worker,value); + set.asInt32(*this,data,value); return *this; } inline Attribute & operator=(const uint32_t value) { - set.asUint32(*this,worker,value); + set.asUint32(*this,data,value); return *this; } inline Attribute & operator=(const bool value) { - set.asBoolean(*this,worker,value); + set.asBoolean(*this,data,value); return *this; } diff --git a/client/src/session/local/attribute.cc b/client/src/session/local/attribute.cc index 9a37c0c..3fdc34a 100644 --- a/client/src/session/local/attribute.cc +++ b/client/src/session/local/attribute.cc @@ -46,23 +46,57 @@ namespace TN3270 { - // Signed int attribute - class IntAttribute : public Attribute { + // Class template + template + class TN3270_PRIVATE TemplateAttribute : public Attribute { + protected: + + struct Worker { + H3270 *hSession; + const T *methods; + }; + public: - IntAttribute(H3270 *hSession, const LIB3270_INT_PROPERTY *worker) : Attribute(hSession, Attribute::Int32, (void *) worker) { + + TemplateAttribute(H3270 *hSession, Attribute::Type type, const T *methods) : Attribute(type, sizeof(struct Worker)) { + + getWorker()->hSession = hSession; + getWorker()->methods = methods; + + debug("hSession=",hSession," methods=",methods); get.name = [](const void *worker) { - return ((const LIB3270_INT_PROPERTY *) worker)->name; + return ((const struct Worker *) worker)->methods->name; }; + get.description = [](const void *worker) { + return ((const struct Worker *) worker)->methods->description; + }; + + } + + inline struct Worker * getWorker() { + return (struct Worker *) this->data; + } + + }; + + + // Signed int attribute + class TN3270_PRIVATE IntAttribute : public TemplateAttribute { + public: + IntAttribute(H3270 *hSession, const LIB3270_INT_PROPERTY *worker) : TemplateAttribute(hSession, Attribute::Int32, worker) { + get.asString = [](const Attribute & attr, const void *worker) { return std::to_string(attr.getInt32()); }; get.asInt32 = [](const Attribute & attr, const void *worker) { + const struct Worker * w = (const struct Worker *) worker; + errno = 0; - int value = ((const LIB3270_INT_PROPERTY *) worker)->get(attr.getTN3270Session()); + int value = w->methods->get(w->hSession); if(errno != 0) { throw std::system_error(errno, std::system_category()); @@ -80,19 +114,52 @@ return (attr.getInt32() != 0); }; + } }; - // Unsigned int attribute - class UnsignedIntAttribute : public Attribute { + // Boolean attribute + class TN3270_PRIVATE BooleanAttribute : public TemplateAttribute { public: - UnsignedIntAttribute(H3270 *hSession, const LIB3270_UINT_PROPERTY *worker) : Attribute(hSession, Attribute::Uint32, (void *) worker) { + BooleanAttribute(H3270 *hSession, const LIB3270_INT_PROPERTY *worker) : TemplateAttribute(hSession, Attribute::Boolean, worker) { - get.name = [](const void *worker) { - return ((const LIB3270_UINT_PROPERTY *) worker)->name; + get.asString = [](const Attribute & attr, const void *worker) { + return attr.getInt32() ? "true" : "false"; + }; + + get.asInt32 = [](const Attribute & attr, const void *worker) { + + const struct Worker * w = (const struct Worker *) worker; + + errno = 0; + int value = w->methods->get(w->hSession); + + if(errno != 0) { + throw std::system_error(errno, std::system_category()); + } + + return (int32_t) value; + + }; + + get.asUint32 = [](const Attribute & attr, const void *worker) { + return (uint32_t) attr.getInt32(); + }; + + get.asBoolean = [](const Attribute & attr, const void *worker) { + return (attr.getInt32() != 0); }; + + } + + }; + + class TN3270_PRIVATE UnsignedIntAttribute : public TemplateAttribute { + public: + UnsignedIntAttribute(H3270 *hSession, const LIB3270_UINT_PROPERTY *worker) : TemplateAttribute(hSession, Attribute::Boolean, worker) { + get.asString = [](const Attribute & attr, const void *worker) { return std::to_string(attr.getUint32()); }; @@ -103,8 +170,10 @@ get.asUint32 = [](const Attribute & attr, const void *worker) { + const struct Worker * w = (const struct Worker *) worker; + errno = 0; - unsigned int value = ((const LIB3270_UINT_PROPERTY *) worker)->get(attr.getTN3270Session()); + unsigned int value = w->methods->get(w->hSession); if(errno != 0) { throw std::system_error(errno, std::system_category()); @@ -119,33 +188,28 @@ }; } - }; - // String attribute - class StringAttribute : public Attribute { + class TN3270_PRIVATE StringAttribute : public TemplateAttribute { public: - StringAttribute(H3270 *hSession, const LIB3270_STRING_PROPERTY *worker) : Attribute(hSession, Attribute::String, (void *) worker) { - - get.name = [](const void *worker) { - return ((const LIB3270_STRING_PROPERTY *) worker)->name; - }; + StringAttribute(H3270 *hSession, const LIB3270_STRING_PROPERTY *worker) : TemplateAttribute(hSession, Attribute::String, worker) { get.asString = [](const Attribute & attr, const void *worker) { - const char * str = ((const LIB3270_STRING_PROPERTY *) worker)->get(attr.getTN3270Session()); + const struct Worker * w = (const struct Worker *) worker; + const char * str = w->methods->get(w->hSession); if(str) { return string(str); } - throw std::system_error(errno, std::system_category()); }; get.asInt32 = [](const Attribute & attr, const void *worker) { - const char * str = ((const LIB3270_STRING_PROPERTY *) worker)->get(attr.getTN3270Session()); + const struct Worker * w = (const struct Worker *) worker; + const char * str = w->methods->get(w->hSession); if(str) { return (int32_t) atoi(str); @@ -156,17 +220,11 @@ } - }; - // Boolean attribute - class BooleanAttribute : public Attribute { + class TN3270_PRIVATE ToggleAttribute : public TemplateAttribute { public: - BooleanAttribute(H3270 *hSession, const LIB3270_INT_PROPERTY *worker) : Attribute(hSession, Attribute::Boolean, (void *) worker) { - - get.name = [](const void *worker) { - return ((const LIB3270_INT_PROPERTY *) worker)->name; - }; + ToggleAttribute(H3270 *hSession, const LIB3270_TOGGLE_ENTRY *worker) : TemplateAttribute(hSession, Attribute::Boolean, worker) { get.asString = [](const Attribute & attr, const void *worker) { return attr.getBoolean() ? "true" : "false"; @@ -174,39 +232,11 @@ get.asInt32 = [](const Attribute & attr, const void *worker) { - errno = 0; - int value = ((const LIB3270_INT_PROPERTY *) worker)->get(attr.getTN3270Session()); - - if(errno != 0) { - throw std::system_error(errno, std::system_category()); - } - - return (int32_t) value; - - }; - - } - - }; - - // Toggle attribute - class ToggleAttribute : public Attribute { - public: - ToggleAttribute(H3270 *hSession, const LIB3270_TOGGLE_ENTRY *worker) : Attribute(hSession, Attribute::Boolean, (void *) worker) { - - get.name = [](const void *worker) { - return ((const LIB3270_TOGGLE_ENTRY *) worker)->name; - }; - - get.asString = [](const Attribute & attr, const void *worker) { - return attr.getBoolean() ? "true" : "false"; - }; - - get.asInt32 = [](const Attribute & attr, const void *worker) { + const struct Worker * w = (const struct Worker *) worker; errno = 0; - int value = lib3270_get_toggle(attr.getTN3270Session(),((const LIB3270_TOGGLE_ENTRY *) worker)->id); + int value = lib3270_get_toggle(w->hSession,w->methods->id); if(errno != 0) { throw std::system_error(errno, std::system_category()); @@ -217,15 +247,16 @@ }; set.asInt32 = [](const Attribute & attr, const void *worker, const int32_t value) { - lib3270_set_toggle(attr.getTN3270Session(),((const LIB3270_TOGGLE_ENTRY *) worker)->id, (int) value); + const struct Worker * w = (const struct Worker *) worker; + lib3270_set_toggle(w->hSession,w->methods->id, (int) value); }; set.asBoolean = [](const Attribute & attr, const void *worker, const bool value) { - lib3270_set_toggle(attr.getTN3270Session(),((const LIB3270_TOGGLE_ENTRY *) worker)->id, (int) value); + const struct Worker * w = (const struct Worker *) worker; + lib3270_set_toggle(w->hSession,w->methods->id, (int) value); }; } - }; Attribute Local::Session::getAttribute(const char *name) const { diff --git a/client/src/testprogram/testprogram.cc b/client/src/testprogram/testprogram.cc index c670e31..2ccbfcb 100644 --- a/client/src/testprogram/testprogram.cc +++ b/client/src/testprogram/testprogram.cc @@ -39,6 +39,7 @@ #include #include #include + #include #include using namespace std; @@ -118,10 +119,11 @@ cout << "Version: " << host["version"] - << "\tRevision: " << host["Revision"] - << "\tConnected: " << host["Connected"] +// << "\tRevision: " << host["Revision"] +// << "\tConnected: " << host["Connected"] << std::endl; + /* host.connect(nullptr); cout @@ -152,6 +154,7 @@ host.wait(10); host.disconnect(); + */ } catch(const std::exception &e) { @@ -190,9 +193,17 @@ cout << "Session: " << session << endl; - // testHost(session); + //testHost(session); testAttributes(session); + /* + for(auto attribute : TN3270::getAttributes()) { + + cout << attribute->name << ":\t" << attribute->description << endl; + + } + */ + return 0; } -- libgit2 0.21.2