File:  [Coherent Logic Development] / freem / src / objects.c
Revision 1.6: download - view: text, annotated - select for diffs
Sun Apr 13 04:22:43 2025 UTC (3 months, 2 weeks ago) by snw
Branches: MAIN
CVS tags: HEAD
Fix snprintf calls

    1: /*
    2:  *   $Id: objects.c,v 1.6 2025/04/13 04:22:43 snw Exp $
    3:  *    implementation of OO support
    4:  *
    5:  *  
    6:  *   Author: Serena Willis <snw@coherent-logic.com>
    7:  *    Copyright (C) 1998 MUG Deutschland
    8:  *    Copyright (C) 2023, 2025 Coherent Logic Development LLC
    9:  *
   10:  *
   11:  *   This file is part of FreeM.
   12:  *
   13:  *   FreeM is free software: you can redistribute it and/or modify
   14:  *   it under the terms of the GNU Affero Public License as published by
   15:  *   the Free Software Foundation, either version 3 of the License, or
   16:  *   (at your option) any later version.
   17:  *
   18:  *   FreeM is distributed in the hope that it will be useful,
   19:  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   20:  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   21:  *   GNU Affero Public License for more details.
   22:  *
   23:  *   You should have received a copy of the GNU Affero Public License
   24:  *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
   25:  *
   26:  *   $Log: objects.c,v $
   27:  *   Revision 1.6  2025/04/13 04:22:43  snw
   28:  *   Fix snprintf calls
   29:  *
   30:  *   Revision 1.5  2025/04/10 01:24:38  snw
   31:  *   Remove C++ style comments
   32:  *
   33:  *   Revision 1.4  2025/04/09 19:52:02  snw
   34:  *   Eliminate as many warnings as possible while building with -Wall
   35:  *
   36:  *   Revision 1.3  2025/03/09 19:50:47  snw
   37:  *   Second phase of REUSE compliance and header reformat
   38:  *
   39:  *
   40:  * SPDX-FileCopyrightText:  (C) 2025 Coherent Logic Development LLC
   41:  * SPDX-License-Identifier: AGPL-3.0-or-later
   42:  **/
   43: #include <string.h>
   44: #include <stdlib.h>
   45: #include <stdio.h>
   46: #include <unistd.h>
   47: #include <ctype.h>
   48: 
   49: #include "mpsdef.h"
   50: #include "mref.h"
   51: #include "objects.h"
   52: 
   53: void obj_init(void)
   54: {
   55:     register int i;
   56: 
   57:     for (i = 0; i < MAX_PRIVATE_KEYS; i++) {
   58:         private_keys[i][0] = EOL;
   59:     }
   60: }
   61: 
   62: short obj_is_field_private(char *key)
   63: {
   64:     register int i;
   65: 
   66:     for (i = 0; i < MAX_PRIVATE_KEYS; i++) {
   67:         if (stcmp (private_keys[i], key) == 0) {
   68:             return TRUE;
   69:         }
   70:     }
   71: 
   72:     return FALSE;
   73: }
   74: 
   75: void obj_set_field_private(char *key)
   76: {
   77:     register int i;
   78: 
   79:     if (obj_is_field_private (key) == TRUE) return;
   80: 
   81:     for (i = 0; i < MAX_PRIVATE_KEYS; i++) {
   82:         if (private_keys[i][0] == EOL) {
   83:             stcpy (private_keys[i], key);
   84:             return;
   85:         }
   86:     }
   87: 
   88:     merr_raise (OBJPRIVOVFL);
   89:     return;
   90: }
   91: 
   92: void obj_set_field_public(char *key)
   93: {
   94:     register int i;
   95: 
   96:     for (i = 0; i < MAX_PRIVATE_KEYS; i++) {
   97:         if (stcmp (private_keys[i], key) == 0) {
   98:             private_keys[i][0] = EOL;
   99:             return;
  100:         }
  101:     }
  102: }
  103: 
  104: short obj_is_object(char *inst)
  105: {
  106:     char t_buf[255];
  107: 
  108:     return obj_get_attribute (inst, "CLASS", t_buf);
  109: }
  110: 
  111: short obj_instance_of(char *inst, char *class)
  112: {
  113:     char t_buf[255];
  114:     short res;
  115:     
  116:     res = obj_get_attribute (inst, "CLASS", t_buf);
  117: 
  118:     if ((res == FALSE) || (strcmp (t_buf, class) != 0)) {
  119:         return FALSE;
  120:     }
  121: 
  122:     return TRUE;    
  123: }
  124: 
  125: void obj_set_attribute(char *inst, char *attrib, char *value)
  126: {
  127:     char t_key[255];
  128:     char t_data[255];
  129: 
  130:     snprintf (t_key, sizeof (t_key) - 1, "^$OBJECT\202%s\202%s\201", inst, attrib);
  131:     snprintf (t_data, sizeof (t_data) - 1, "%s\201", value);
  132: 
  133:     symtab_bltin (set_sym, t_key, t_data);
  134: }
  135: 
  136: short obj_get_attribute(char *inst, char *attrib, char *buf)
  137: {
  138:     char t_key[255];
  139: 
  140:     snprintf (t_key, sizeof (t_key) - 1, "^$OBJECT\202%s\202%s\201", inst, attrib);
  141: 
  142:     symtab_bltin (get_sym, t_key, buf);
  143: 
  144:     if (merr () == UNDEF || merr () == M6) {
  145:         
  146:         if (strcmp (attrib, "CLASS") == 0) {
  147:             /* non-object variables always belong to the ^%STRING class */
  148:             snprintf (buf, 9, "^%%STRING");
  149: 
  150:             merr_clear ();
  151:             return TRUE;
  152:             
  153:         }
  154:         else {
  155:             merr_clear ();
  156:             return FALSE;
  157:         }
  158:     }
  159:     else {
  160:         stcnv_m2c (buf);
  161:         return TRUE;
  162:     }
  163:     
  164: }
  165: 
  166: void obj_destroy(char *inst)
  167: {
  168:     char t_key[256];
  169: 
  170:     snprintf (t_key, sizeof (t_key) - 1, "^$OBJECT\202\%s\201", inst);
  171:     symtab_bltin (kill_sym, t_key, " \201");
  172: }
  173: 
  174: void obj_create_symbols(char *objvar, char *class)
  175: {
  176:     char t_key[256];
  177:     char t_data[256];
  178: 
  179:     snprintf (t_key, sizeof (t_key) - 1, "%s\201", objvar);
  180:     snprintf (t_data, sizeof (t_data) - 1, " \201");
  181: 
  182:     symtab_bltin (set_sym, t_key, t_data);
  183: 
  184:     obj_set_attribute (objvar, "CLASS", class);
  185:         
  186: }
  187: 
  188: void obj_get_constructor(char *constructor, char *class, char *instvar)
  189: {
  190:     freem_ref_t inref;
  191:     freem_ref_t outref;
  192:     register int i;
  193:     
  194:     mref_init (&inref, MREF_RT_GLOBAL, "");
  195:     internal_to_mref (&inref, class);
  196:     mref_init (&outref, MREF_RT_GLOBAL, inref.name);
  197:     
  198:     strcpy (outref.name, inref.name);
  199:     outref.subscript_count = inref.subscript_count + 1;
  200: 
  201:     snprintf (outref.subscripts[0], STRLEN - 1, ".%s", instvar);
  202: 
  203:     for (i = 0; i < inref.subscript_count; i++) {
  204:         strcpy (outref.subscripts[i + 1], inref.subscripts[i]);
  205:     }
  206: 
  207: 
  208:     mref_to_external (&outref, constructor);
  209:     return;
  210: 
  211:     /*
  212:     for (i = 0; i < stlen (constructor); i++) {
  213: 
  214:         switch (constructor[i]) {
  215:             case '\202':
  216:                 if (argct == 0) {
  217:                     constructor[i] = '(';
  218:                     argct++;
  219:                 }
  220:                 else {
  221:                     constructor[i] = ',';
  222:                     argct++;
  223:                 }
  224:                 break;
  225:             case '\201':
  226:                 constructor[i] = ')';
  227:         }
  228:         
  229:     }
  230:     constructor[i] = ')';
  231: 
  232:     if (argct > 1) ierr = ARGLIST;
  233:     */
  234: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>