Commit 9cc136a6199f83dafe8ad01e99c82152c294c50a

Authored by Perry Werneck
1 parent 6bcd7cf1
Exists in develop

Adding field methods splitted from ctlr.c

Showing 1 changed file with 235 additions and 0 deletions   Show diff stats
src/core/field.c 0 → 100644
... ... @@ -0,0 +1,235 @@
  1 +/* SPDX-License-Identifier: LGPL-3.0-or-later */
  2 +
  3 +/*
  4 + * Copyright (C) 2023 Perry Werneck <perry.werneck@gmail.com>
  5 + *
  6 + * This program is free software: you can redistribute it and/or modify
  7 + * it under the terms of the GNU Lesser General Public License as published
  8 + * by the Free Software Foundation, either version 3 of the License, or
  9 + * (at your option) any later version.
  10 + *
  11 + * This program is distributed in the hope that it will be useful,
  12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + * GNU General Public License for more details.
  15 + *
  16 + * You should have received a copy of the GNU Lesser General Public License
  17 + * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18 + */
  19 +
  20 + #include <config.h>
  21 + #include <internals.h>
  22 + #include <lib3270.h>
  23 + #include "3270ds.h"
  24 +
  25 +/**
  26 + * @brief Get field address.
  27 + *
  28 + * @return Negative on error(sets errno) or field address.
  29 + *
  30 + */
  31 + LIB3270_EXPORT int lib3270_get_field_start(H3270 *hSession, int baddr) {
  32 + int sbaddr;
  33 +
  34 + if(check_online_session(hSession))
  35 + return - errno;
  36 +
  37 + if (!hSession->formatted)
  38 + return - (errno = ENOTSUP);
  39 +
  40 + if(baddr < 0)
  41 + baddr = hSession->cursor_addr;
  42 +
  43 + sbaddr = baddr;
  44 + do {
  45 + if(hSession->ea_buf[baddr].fa)
  46 + return baddr;
  47 + DEC_BA(baddr);
  48 + } while (baddr != sbaddr);
  49 +
  50 + return -1;
  51 +
  52 + }
  53 +
  54 + LIB3270_EXPORT int lib3270_is_formatted(const H3270 *hSession) {
  55 + if(check_online_session(hSession))
  56 + return 0;
  57 + return hSession->formatted ? 1 : 0;
  58 + }
  59 +
  60 + LIB3270_EXPORT int lib3270_get_field_len(H3270 *hSession, int baddr) {
  61 + int saddr;
  62 + int addr;
  63 + int width = 0;
  64 +
  65 + if(check_online_session(hSession))
  66 + return - errno;
  67 +
  68 + if (!hSession->formatted)
  69 + return - (errno = ENOTSUP);
  70 +
  71 + if(baddr < 0)
  72 + baddr = hSession->cursor_addr;
  73 +
  74 + addr = lib3270_field_addr(hSession,baddr);
  75 + if(addr < 0)
  76 + return addr;
  77 +
  78 + saddr = addr;
  79 + INC_BA(addr);
  80 + do {
  81 + if(hSession->ea_buf[addr].fa)
  82 + return width;
  83 + INC_BA(addr);
  84 + width++;
  85 + } while (addr != saddr);
  86 +
  87 + return -(errno = ENODATA);
  88 + }
  89 +
  90 + LIB3270_EXPORT int lib3270_field_addr(const H3270 *hSession, int baddr) {
  91 +
  92 + int sbaddr;
  93 +
  94 + if(!lib3270_is_connected(hSession))
  95 + return -(errno = ENOTCONN);
  96 +
  97 + if(!hSession->formatted)
  98 + return -(errno = ENOTSUP);
  99 +
  100 + if(baddr < 0)
  101 + baddr = lib3270_get_cursor_address(hSession);
  102 +
  103 + if( (unsigned int) baddr > lib3270_get_length(hSession))
  104 + return -(errno = EOVERFLOW);
  105 +
  106 + sbaddr = baddr;
  107 + do {
  108 + if(hSession->ea_buf[baddr].fa)
  109 + return baddr;
  110 + DEC_BA(baddr);
  111 + } while (baddr != sbaddr);
  112 +
  113 + return -(errno = ENODATA);
  114 + }
  115 +
  116 + LIB3270_EXPORT LIB3270_FIELD_ATTRIBUTE lib3270_get_field_attribute(H3270 *hSession, int baddr) {
  117 + int sbaddr;
  118 +
  119 + FAIL_IF_NOT_ONLINE(hSession);
  120 +
  121 + if(!hSession->formatted) {
  122 + errno = ENOTCONN;
  123 + return LIB3270_FIELD_ATTRIBUTE_INVALID;
  124 + }
  125 +
  126 + if(baddr < 0)
  127 + baddr = lib3270_get_cursor_address(hSession);
  128 +
  129 + sbaddr = baddr;
  130 + do {
  131 + if(hSession->ea_buf[baddr].fa)
  132 + return (LIB3270_FIELD_ATTRIBUTE) hSession->ea_buf[baddr].fa;
  133 +
  134 + DEC_BA(baddr);
  135 + } while (baddr != sbaddr);
  136 +
  137 + errno = EINVAL;
  138 + return LIB3270_FIELD_ATTRIBUTE_INVALID;
  139 +
  140 + }
  141 +
  142 +/**
  143 + * @brief Get the length of the field at given buffer address.
  144 + *
  145 + * @param hSession Session handle.
  146 + * @param addr Buffer address of the field.
  147 + *
  148 + * @return field length or negative if invalid or not connected (sets errno).
  149 + *
  150 + */
  151 + int lib3270_field_length(H3270 *hSession, int baddr) {
  152 + int saddr;
  153 + int addr;
  154 + int width = 0;
  155 +
  156 + addr = lib3270_field_addr(hSession,baddr);
  157 + if(addr < 0)
  158 + return addr;
  159 +
  160 + saddr = addr;
  161 + INC_BA(addr);
  162 + do {
  163 + if(hSession->ea_buf[addr].fa)
  164 + return width;
  165 + INC_BA(addr);
  166 + width++;
  167 + } while (addr != saddr);
  168 +
  169 + return -(errno = EINVAL);
  170 +
  171 + }
  172 +
  173 +/**
  174 + * @brief Find the field attribute for the given buffer address.
  175 + *
  176 + * @return Field attribute.
  177 + *
  178 + */
  179 + unsigned char get_field_attribute(H3270 *hSession, int baddr) {
  180 + baddr = lib3270_field_addr(hSession,baddr);
  181 + if(baddr < 0)
  182 + return 0;
  183 + return hSession->ea_buf[baddr].fa;
  184 + }
  185 +
  186 +/**
  187 + * @brief Find the next unprotected field.
  188 + *
  189 + * @param hSession Session handle.
  190 + * @param baddr0 Search start addr (-1 to use current cursor position).
  191 + *
  192 + * @return address following the unprotected attribute byte, or 0 if no nonzero-width unprotected field can be found, negative if failed.
  193 + *
  194 + */
  195 + LIB3270_EXPORT int lib3270_get_next_unprotected(H3270 *hSession, int baddr0) {
  196 + register int baddr, nbaddr;
  197 +
  198 + FAIL_IF_NOT_ONLINE(hSession);
  199 +
  200 + if(!hSession->formatted)
  201 + return -(errno = ENOTSUP);
  202 +
  203 + if(baddr0 < 0)
  204 + baddr0 = hSession->cursor_addr;
  205 +
  206 + nbaddr = baddr0;
  207 + do {
  208 + baddr = nbaddr;
  209 + INC_BA(nbaddr);
  210 + if(hSession->ea_buf[baddr].fa &&!FA_IS_PROTECTED(hSession->ea_buf[baddr].fa) &&!hSession->ea_buf[nbaddr].fa)
  211 + return nbaddr;
  212 + } while (nbaddr != baddr0);
  213 +
  214 + return 0;
  215 + }
  216 +
  217 + LIB3270_EXPORT int lib3270_get_is_protected_at(const H3270 *h, unsigned int row, unsigned int col) {
  218 + return lib3270_get_is_protected(h, lib3270_translate_to_address(h,row,col));
  219 + }
  220 +
  221 + LIB3270_EXPORT int lib3270_get_is_protected(const H3270 *hSession, int baddr) {
  222 +
  223 + FAIL_IF_NOT_ONLINE(hSession);
  224 +
  225 + if(baddr < 0)
  226 + baddr = hSession->cursor_addr;
  227 +
  228 + int faddr = lib3270_field_addr(hSession,baddr);
  229 +
  230 + return FA_IS_PROTECTED(hSession->ea_buf[faddr].fa) ? 1 : 0;
  231 + }
  232 +
  233 + LIB3270_EXPORT int lib3270_is_protected(H3270 *h, unsigned int baddr) {
  234 + return lib3270_get_is_protected(h, baddr);
  235 + }
... ...