| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
/****************************************************************************/ |
| 6 |
/* */ |
| 7 |
/* File: misc.c */ |
| 8 |
/* */ |
| 9 |
/* Purpose: miscellaneous routines */ |
| 10 |
/* */ |
| 11 |
/* Author: Klaus Johannsen */ |
| 12 |
/* Institut fuer Computeranwendungen */ |
| 13 |
/* Universitaet Stuttgart */ |
| 14 |
/* Pfaffenwaldring 27 */ |
| 15 |
/* 70569 Stuttgart */ |
| 16 |
/* internet: [email protected] */ |
| 17 |
/* */ |
| 18 |
/* History: 08.12.94 begin, ug3-version */ |
| 19 |
/* */ |
| 20 |
/* Revision: 07.09.95 */ |
| 21 |
/* */ |
| 22 |
/****************************************************************************/ |
| 23 |
|
| 24 |
/****************************************************************************/ |
| 25 |
/* */ |
| 26 |
/* include files */ |
| 27 |
/* system include files */ |
| 28 |
/* application include files */ |
| 29 |
/* */ |
| 30 |
/****************************************************************************/ |
| 31 |
|
| 32 |
#include <config.h> |
| 33 |
#include <cmath> |
| 34 |
#include <cassert> |
| 35 |
#include <cstdlib> |
| 36 |
#include <cstring> |
| 37 |
#include <ctype.h> |
| 38 |
#include <cstddef> |
| 39 |
#include <cstdio> |
| 40 |
#include <climits> |
| 41 |
|
| 42 |
#include "ugtypes.h" |
| 43 |
#include "architecture.h" |
| 44 |
#include "misc.h" |
| 45 |
#include "heaps.h" |
| 46 |
|
| 47 |
USING_UG_NAMESPACE |
| 48 |
|
| 49 |
/****************************************************************************/ |
| 50 |
/* */ |
| 51 |
/* defines in the following order */ |
| 52 |
/* */ |
| 53 |
/* compile time constants defining static data size (i.e. arrays) */ |
| 54 |
/* other constants */ |
| 55 |
/* macros */ |
| 56 |
/* */ |
| 57 |
/****************************************************************************/ |
| 58 |
|
| 59 |
/****************************************************************************/ |
| 60 |
/* */ |
| 61 |
/* data structures used in this source file (exported data structures are */ |
| 62 |
/* in the corresponding include file!) */ |
| 63 |
/* */ |
| 64 |
/****************************************************************************/ |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
/****************************************************************************/ |
| 69 |
/* */ |
| 70 |
/* definition of exported global variables */ |
| 71 |
/* */ |
| 72 |
/****************************************************************************/ |
| 73 |
|
| 74 |
#ifndef ModelP |
| 75 |
int PPIF::me = 0; /* to have in the serial case this variable as a dummy */ |
| 76 |
int PPIF::master = 0; /* to have in the serial case this variable as a dummy */ |
| 77 |
int PPIF::procs = 1; /* to have in the serial case this variable as a dummy */ |
| 78 |
int NS_PREFIX _proclist_ = -1; /* to have in the serial case this variable as a dummy */ |
| 79 |
int NS_PREFIX _partition_ = 0; /* to have in the serial case this variable as a dummy */ |
| 80 |
#endif |
| 81 |
|
| 82 |
/****************************************************************************/ |
| 83 |
/****************************************************************************/ |
| 84 |
/****************************************************************************/ |
| 85 |
/**** ****/ |
| 86 |
/**** general routines ****/ |
| 87 |
/**** ****/ |
| 88 |
/****************************************************************************/ |
| 89 |
/****************************************************************************/ |
| 90 |
/****************************************************************************/ |
| 91 |
|
| 92 |
#define FMTBUFFSIZE 1031 |
| 93 |
|
| 94 |
static char newfmt[FMTBUFFSIZE]; |
| 95 |
|
| 96 |
/****************************************************************************/ |
| 97 |
/** \brief Expand (make explicit) charset-ranges in scanf |
| 98 |
|
| 99 |
This function expands (make explicit) charset-ranges in scanf. |
| 100 |
For example '%<number>[...a-d...]' --> '%<number>[...abcd...]'. |
| 101 |
|
| 102 |
\return |
| 103 |
.n new pointer to char |
| 104 |
*/ |
| 105 |
/****************************************************************************/ |
| 106 |
char * NS_PREFIX expandfmt (const char *fmt) |
| 107 |
{ |
| 108 |
const char *pos; |
| 109 |
char *newpos; |
| 110 |
char leftchar,rightchar; |
| 111 |
int newlen; |
| 112 |
|
| 113 |
/* calculate min size of newfmt */ |
| 114 |
newlen = strlen(fmt); |
| 115 |
assert (newlen<FMTBUFFSIZE-1); |
| 116 |
|
| 117 |
pos = fmt; |
| 118 |
newpos = newfmt; |
| 119 |
|
| 120 |
/* scan fmt for %<number>[ */ |
| 121 |
while (*pos!='\0') |
| 122 |
{ |
| 123 |
/* copy til '%' */ |
| 124 |
while (*pos!='%' && *pos!='\0') |
| 125 |
*(newpos++) = *(pos++); |
| 126 |
|
| 127 |
if (*pos=='\0') |
| 128 |
break; |
| 129 |
|
| 130 |
*(newpos++) = *(pos++); |
| 131 |
|
| 132 |
/* copy til !isdigit */ |
| 133 |
while (isdigit(*pos) && *pos!='\0') |
| 134 |
*(newpos++) = *(pos++); |
| 135 |
|
| 136 |
if (*pos=='\0') |
| 137 |
break; |
| 138 |
|
| 139 |
if (*pos!='[') |
| 140 |
continue; |
| 141 |
|
| 142 |
*(newpos++) = *(pos++); |
| 143 |
|
| 144 |
/* ']' following '[' is included in the charset */ |
| 145 |
if ((*pos)==']') |
| 146 |
*(newpos++) = *(pos++); |
| 147 |
|
| 148 |
/* '^]' following '[' is included in the charset */ |
| 149 |
else if ((*pos)=='^' && (*(pos+1))==']') |
| 150 |
{ |
| 151 |
*(newpos++) = *(pos++); |
| 152 |
*(newpos++) = *(pos++); |
| 153 |
} |
| 154 |
|
| 155 |
while (*pos!=']' && *pos!='\0') |
| 156 |
{ |
| 157 |
/* now we are inside the charset '[...]': */ |
| 158 |
|
| 159 |
/* treat character ranges indicated by '-' */ |
| 160 |
while (*pos!='-' && *pos!=']' && *pos!='\0') |
| 161 |
*(newpos++) = *(pos++); |
| 162 |
|
| 163 |
if (*pos=='\0') |
| 164 |
break; |
| 165 |
|
| 166 |
if ((*pos)==']') |
| 167 |
continue; |
| 168 |
|
| 169 |
/* gotya: is left char < right char? */ |
| 170 |
leftchar = *(pos-1); |
| 171 |
rightchar = *(pos+1); |
| 172 |
|
| 173 |
if (leftchar=='[' || rightchar==']') |
| 174 |
{ |
| 175 |
*(newpos++) = *(pos++); |
| 176 |
continue; |
| 177 |
} |
| 178 |
|
| 179 |
if (leftchar>=rightchar) |
| 180 |
{ |
| 181 |
*(newpos++) = *(pos++); |
| 182 |
continue; |
| 183 |
} |
| 184 |
|
| 185 |
if (leftchar+1==rightchar) |
| 186 |
{ |
| 187 |
/* for example '...b-c...' */ |
| 188 |
pos++; |
| 189 |
continue; |
| 190 |
} |
| 191 |
|
| 192 |
/* calc new size and expand range */ |
| 193 |
newlen += rightchar-leftchar-2; |
| 194 |
assert (newlen<FMTBUFFSIZE-1); |
| 195 |
|
| 196 |
leftchar++; |
| 197 |
pos++; |
| 198 |
|
| 199 |
while (leftchar<rightchar) |
| 200 |
{ |
| 201 |
if (leftchar=='^' || leftchar==']') |
| 202 |
{ |
| 203 |
leftchar++; |
| 204 |
continue; |
| 205 |
} |
| 206 |
*(newpos++) = leftchar++; |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
*newpos = '\0'; |
| 212 |
|
| 213 |
return (newfmt); |
| 214 |
} |
| 215 |
|
| 216 |
/****************************************************************************/ |
| 217 |
/** \brief Split a string into tokens each of maximal length 'n+1' |
| 218 |
|
| 219 |
\param str - pointer to char (const) |
| 220 |
\param sep - pointer to char (const) |
| 221 |
\param n - integer, number of chars in token |
| 222 |
\param token - pointer to char |
| 223 |
|
| 224 |
This function splits a string into tokens each of maximal length 'n+1'. |
| 225 |
A pointer to the next char following the token (its a sep) is returned. |
| 226 |
NB: possibly check whether the returned char is a sep. |
| 227 |
If not: the token was to long and only the first n chars where copied! |
| 228 |
|
| 229 |
\return |
| 230 |
.n pointer to token |
| 231 |
.n NULL if token larger than n. |
| 232 |
*/ |
| 233 |
/****************************************************************************/ |
| 234 |
|
| 235 |
const char * NS_PREFIX strntok (const char *str, const char *sep, int n, char *token) |
| 236 |
{ |
| 237 |
int i; |
| 238 |
|
| 239 |
/* scan while current char is a separator */ |
| 240 |
while ((*str!='\0') && (strchr(sep,*str)!=NULL)) str++; |
| 241 |
|
| 242 |
/* copy into token */ |
| 243 |
for (i=0; i<n; i++,str++) |
| 244 |
if ((*str!='\0') && (strchr(sep,*str)==NULL)) |
| 245 |
token[i] = *str; |
| 246 |
else |
| 247 |
break; |
| 248 |
|
| 249 |
if (strchr(sep,*str)==NULL) |
| 250 |
return (NULL); /* ERROR: token too long! */ |
| 251 |
|
| 252 |
/* 0-terminate string */ |
| 253 |
token[i] = '\0'; |
| 254 |
|
| 255 |
return (str); |
| 256 |
} |