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

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

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