Annotation of freem/src/datatypes.c, revision 1.4
1.1 snw 1: /*
1.4 ! snw 2: * $Id: datatypes.c,v 1.3 2025/03/09 19:14:24 snw Exp $
1.1 snw 3: * supports the type system
4: *
5: *
1.2 snw 6: * Author: Serena Willis <snw@coherent-logic.com>
1.1 snw 7: * Copyright (C) 1998 MUG Deutschland
8: * Copyright (C) 2023 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: *
1.4 ! snw 26: * $Log: datatypes.c,v $
! 27: * Revision 1.3 2025/03/09 19:14:24 snw
! 28: * First 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:
35: #include <string.h>
36: #include <stdlib.h>
37: #include <stdio.h>
38: #include <unistd.h>
39: #include <ctype.h>
40:
41: #include "mpsdef.h"
42: #include "datatypes.h"
43:
44: short dt_get_type(char *type_string)
45: {
46: register int i;
47:
48: for (i = 0; i < strlen (type_string); i++) {
49: type_string[i] = toupper (type_string[i]);
50: }
51:
52: if ((strcmp (type_string, "STRING")) == 0) {
53: return DT_STRING;
54: }
55: else if ((strcmp (type_string, "BOOLEAN")) == 0) {
56: return DT_BOOLEAN;
57: }
58: else if ((strcmp (type_string, "COMPLEX")) == 0) {
59: return DT_COMPLEX;
60: }
61: else if ((strcmp (type_string, "INTEGER")) == 0) {
62: return DT_INTEGER;
63: }
64: else if ((strcmp (type_string, "MATRIX")) == 0) {
65: return DT_MATRIX;
66: }
67: else if ((strcmp (type_string, "NAME")) == 0) {
68: return DT_NAME;
69: }
70: else if ((strcmp (type_string, "REAL")) == 0) {
71: return DT_REAL;
72: }
73: else {
74: return DT_INVALID;
75: }
76: }
77:
78: void dt_get_typestr (char *buf, short datatype)
79: {
80: switch (datatype) {
81:
82: case DT_AUTO:
83: sprintf (buf, "AUTO");
84: break;
85:
86: case DT_STRING:
87: sprintf (buf, "STRING");
88: break;
89:
90: case DT_BOOLEAN:
91: sprintf (buf, "BOOLEAN");
92: break;
93:
94: case DT_INTEGER:
95: sprintf (buf, "INTEGER");
96: break;
97:
98: case DT_REAL:
99: sprintf (buf, "REAL");
100: break;
101:
102: case DT_NAME:
103: sprintf (buf, "NAME");
104: break;
105:
106: case DT_COMPLEX:
107: sprintf (buf, "COMPLEX");
108: break;
109:
110: case DT_MATRIX:
111: sprintf (buf, "MATRIX");
112: break;
113:
114: }
115: }
116:
117: short dt_check(short datatype, char *data, int arg_num)
118: {
119: short res;
120: char dtc_temp[10];
121:
122: switch (datatype) {
123:
124: case DT_AUTO:
125: case DT_STRING:
126: case DT_BOOLEAN:
127: res = TRUE;
128: break;
129:
130: case DT_COMPLEX:
131: res = dt_check_complex (data);
132: break;
133:
134: case DT_MATRIX:
135: res = dt_check_matrix (data);
136: break;
137:
138: case DT_INTEGER:
139: res = dt_check_integer (data);
140: break;
141:
142: case DT_REAL:
143: res = dt_check_real (data);
144: break;
145:
146: default:
147: res = FALSE;
148:
149: }
150:
151: if (res == FALSE) {
152: dt_get_typestr (dtc_temp, datatype);
153:
154: if (arg_num == 0) {
155: sprintf (err_suppl, "%s expected for extrinsic function return value\201", dtc_temp);
156: }
157: else {
158: sprintf (err_suppl, "%s expected in argument %d\201", dtc_temp, arg_num);
159: }
160: }
161:
162: return res;
163:
164: }
165:
166: short dt_check_complex(char *data)
167: {
168: register char ch;
169: char *cpx_real;
170: char *cpx_imaginary;
171:
172: int pctct = 0;
173:
174: while ((ch = *data++) != EOL) {
175: if (ch == '%') {
176: pctct++;
177: if (pctct > 1) return FALSE;
178: }
179: }
180:
181: cpx_real = strtok (data, "%");
182: cpx_imaginary = strtok (NULL, "%");
183:
184: if ((dt_check_real (cpx_real) == FALSE) || (dt_check_real (cpx_imaginary) == FALSE)) {
185: return FALSE;
186: }
187:
188: return TRUE;
189: }
190:
191: short dt_check_integer(char *data)
192: {
193: register char ch;
194: register int i;
195:
196: i = 0;
197:
198: while ((ch = *data++) != EOL) {
199:
200: if (((ch == '+') || (ch == '-')) && i > 0) return FALSE;
201:
202: if (!isdigit (ch)) return FALSE;
203:
204: i++;
205:
206: }
207:
208: return TRUE;
209: }
210:
211: short dt_check_matrix(char *data)
212: {
213: return FALSE;
214: }
215:
216: short dt_check_name(char *data)
217: {
218: return FALSE;
219: }
220:
221: short dt_check_real(char *data)
222: {
223: register char ch;
224: register int i;
225: int ptct;
226:
227: i = 0;
228: ptct = 0;
229:
230: while ((ch = *data++) != EOL) {
231:
232: if (((ch == '+') || (ch == '-')) && i > 0) return FALSE;
233:
234: if ((ch == '.') && (++ptct > 1)) return FALSE;
235:
236: if (!isdigit (ch)) return FALSE;
237:
238: i++;
239:
240: }
241:
242: return TRUE;
243: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>