/*
* $Id: datatypes.c,v 1.3 2025/03/09 19:14:24 snw Exp $
* supports the type system
*
*
* Author: Serena Willis <snw@coherent-logic.com>
* Copyright (C) 1998 MUG Deutschland
* Copyright (C) 2023 Coherent Logic Development LLC
*
*
* This file is part of FreeM.
*
* FreeM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero Public License for more details.
*
* You should have received a copy of the GNU Affero Public License
* along with FreeM. If not, see <https://www.gnu.org/licenses/>.
*
* $Log: datatypes.c,v $
* Revision 1.3 2025/03/09 19:14:24 snw
* First phase of REUSE compliance and header reformat
*
*
* SPDX-FileCopyrightText: (C) 2025 Coherent Logic Development LLC
* SPDX-License-Identifier: AGPL-3.0-or-later
**/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include "mpsdef.h"
#include "datatypes.h"
short dt_get_type(char *type_string)
{
register int i;
for (i = 0; i < strlen (type_string); i++) {
type_string[i] = toupper (type_string[i]);
}
if ((strcmp (type_string, "STRING")) == 0) {
return DT_STRING;
}
else if ((strcmp (type_string, "BOOLEAN")) == 0) {
return DT_BOOLEAN;
}
else if ((strcmp (type_string, "COMPLEX")) == 0) {
return DT_COMPLEX;
}
else if ((strcmp (type_string, "INTEGER")) == 0) {
return DT_INTEGER;
}
else if ((strcmp (type_string, "MATRIX")) == 0) {
return DT_MATRIX;
}
else if ((strcmp (type_string, "NAME")) == 0) {
return DT_NAME;
}
else if ((strcmp (type_string, "REAL")) == 0) {
return DT_REAL;
}
else {
return DT_INVALID;
}
}
void dt_get_typestr (char *buf, short datatype)
{
switch (datatype) {
case DT_AUTO:
sprintf (buf, "AUTO");
break;
case DT_STRING:
sprintf (buf, "STRING");
break;
case DT_BOOLEAN:
sprintf (buf, "BOOLEAN");
break;
case DT_INTEGER:
sprintf (buf, "INTEGER");
break;
case DT_REAL:
sprintf (buf, "REAL");
break;
case DT_NAME:
sprintf (buf, "NAME");
break;
case DT_COMPLEX:
sprintf (buf, "COMPLEX");
break;
case DT_MATRIX:
sprintf (buf, "MATRIX");
break;
}
}
short dt_check(short datatype, char *data, int arg_num)
{
short res;
char dtc_temp[10];
switch (datatype) {
case DT_AUTO:
case DT_STRING:
case DT_BOOLEAN:
res = TRUE;
break;
case DT_COMPLEX:
res = dt_check_complex (data);
break;
case DT_MATRIX:
res = dt_check_matrix (data);
break;
case DT_INTEGER:
res = dt_check_integer (data);
break;
case DT_REAL:
res = dt_check_real (data);
break;
default:
res = FALSE;
}
if (res == FALSE) {
dt_get_typestr (dtc_temp, datatype);
if (arg_num == 0) {
sprintf (err_suppl, "%s expected for extrinsic function return value\201", dtc_temp);
}
else {
sprintf (err_suppl, "%s expected in argument %d\201", dtc_temp, arg_num);
}
}
return res;
}
short dt_check_complex(char *data)
{
register char ch;
register int i;
char *cpx_real;
char *cpx_imaginary;
int pctct = 0;
while ((ch = *data++) != EOL) {
if (ch == '%') {
pctct++;
if (pctct > 1) return FALSE;
}
}
cpx_real = strtok (data, "%");
cpx_imaginary = strtok (NULL, "%");
if ((dt_check_real (cpx_real) == FALSE) || (dt_check_real (cpx_imaginary) == FALSE)) {
return FALSE;
}
return TRUE;
}
short dt_check_integer(char *data)
{
register char ch;
register int i;
i = 0;
while ((ch = *data++) != EOL) {
if (((ch == '+') || (ch == '-')) && i > 0) return FALSE;
if (!isdigit (ch)) return FALSE;
i++;
}
return TRUE;
}
short dt_check_matrix(char *data)
{
return FALSE;
}
short dt_check_name(char *data)
{
return FALSE;
}
short dt_check_real(char *data)
{
register char ch;
register int i;
int ptct;
i = 0;
ptct = 0;
while ((ch = *data++) != EOL) {
if (((ch == '+') || (ch == '-')) && i > 0) return FALSE;
if ((ch == '.') && (++ptct > 1)) return FALSE;
if (!isdigit (ch)) return FALSE;
i++;
}
return TRUE;
}
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>