Annotation of freem/src/objects.c, revision 1.6

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

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