| 1 |
/* do not edit automatically generated by mc from FormatStrings. */ |
| 2 |
/* FormatStrings.mod provides a pseudo printf capability. |
| 3 |
|
| 4 |
Copyright (C) 2005-2025 Free Software Foundation, Inc. |
| 5 |
Contributed by Gaius Mulley <[email protected]>. |
| 6 |
|
| 7 |
This file is part of GNU Modula-2. |
| 8 |
|
| 9 |
GNU Modula-2 is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation; either version 3, or (at your option) |
| 12 |
any later version. |
| 13 |
|
| 14 |
GNU Modula-2 is distributed in the hope that it will be useful, but |
| 15 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
General Public License for more details. |
| 18 |
|
| 19 |
Under Section 7 of GPL version 3, you are granted additional |
| 20 |
permissions described in the GCC Runtime Library Exception, version |
| 21 |
3.1, as published by the Free Software Foundation. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License and |
| 24 |
a copy of the GCC Runtime Library Exception along with this program; |
| 25 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 26 |
<http://www.gnu.org/licenses/>. */ |
| 27 |
|
| 28 |
#include "config.h" |
| 29 |
#include "system.h" |
| 30 |
#include <stdbool.h> |
| 31 |
# if !defined (PROC_D) |
| 32 |
# define PROC_D |
| 33 |
typedef void (*PROC_t) (void); |
| 34 |
typedef struct { PROC_t proc; } PROC; |
| 35 |
# endif |
| 36 |
|
| 37 |
# if !defined (TRUE) |
| 38 |
# define TRUE (1==1) |
| 39 |
# endif |
| 40 |
|
| 41 |
# if !defined (FALSE) |
| 42 |
# define FALSE (1==0) |
| 43 |
# endif |
| 44 |
|
| 45 |
#if defined(__cplusplus) |
| 46 |
# undef NULL |
| 47 |
# define NULL 0 |
| 48 |
#endif |
| 49 |
#define _FormatStrings_C |
| 50 |
|
| 51 |
#include "GFormatStrings.h" |
| 52 |
# include "GDynamicStrings.h" |
| 53 |
# include "GStringConvert.h" |
| 54 |
# include "GSYSTEM.h" |
| 55 |
# include "GASCII.h" |
| 56 |
# include "GM2RTS.h" |
| 57 |
|
| 58 |
|
| 59 |
/* |
| 60 |
Sprintf0 - returns a String containing, s, after it has had its |
| 61 |
escape sequences translated. |
| 62 |
*/ |
| 63 |
|
| 64 |
extern "C" DynamicStrings_String FormatStrings_Sprintf0 (DynamicStrings_String fmt); |
| 65 |
|
| 66 |
/* |
| 67 |
Sprintf1 - returns a String containing, s, together with encapsulated |
| 68 |
entity, w. It only formats the first %s or %d with n. |
| 69 |
*/ |
| 70 |
|
| 71 |
extern "C" DynamicStrings_String FormatStrings_Sprintf1 (DynamicStrings_String fmt, const unsigned char *w_, unsigned int _w_high); |
| 72 |
|
| 73 |
/* |
| 74 |
Sprintf2 - returns a string, s, which has been formatted. |
| 75 |
*/ |
| 76 |
|
| 77 |
extern "C" DynamicStrings_String FormatStrings_Sprintf2 (DynamicStrings_String fmt, const unsigned char *w1_, unsigned int _w1_high, const unsigned char *w2_, unsigned int _w2_high); |
| 78 |
|
| 79 |
/* |
| 80 |
Sprintf3 - returns a string, s, which has been formatted. |
| 81 |
*/ |
| 82 |
|
| 83 |
extern "C" DynamicStrings_String FormatStrings_Sprintf3 (DynamicStrings_String fmt, const unsigned char *w1_, unsigned int _w1_high, const unsigned char *w2_, unsigned int _w2_high, const unsigned char *w3_, unsigned int _w3_high); |
| 84 |
|
| 85 |
/* |
| 86 |
Sprintf4 - returns a string, s, which has been formatted. |
| 87 |
*/ |
| 88 |
|
| 89 |
extern "C" DynamicStrings_String FormatStrings_Sprintf4 (DynamicStrings_String fmt, const unsigned char *w1_, unsigned int _w1_high, const unsigned char *w2_, unsigned int _w2_high, const unsigned char *w3_, unsigned int _w3_high, const unsigned char *w4_, unsigned int _w4_high); |
| 90 |
|
| 91 |
/* |
| 92 |
HandleEscape - translates \a, \b, \e, \f, |
| 93 |
, \r, \x[hex] \[octal] into |
| 94 |
their respective ascii codes. It also converts \[any] into |
| 95 |
a single [any] character. |
| 96 |
*/ |
| 97 |
|
| 98 |
extern "C" DynamicStrings_String FormatStrings_HandleEscape (DynamicStrings_String s); |
| 99 |
|
| 100 |
/* |
| 101 |
doDSdbEnter - |
| 102 |
*/ |
| 103 |
|
| 104 |
static void doDSdbEnter (void); |
| 105 |
|
| 106 |
/* |
| 107 |
doDSdbExit - |
| 108 |
*/ |
| 109 |
|
| 110 |
static void doDSdbExit (DynamicStrings_String s); |
| 111 |
|
| 112 |
/* |
| 113 |
DSdbEnter - |
| 114 |
*/ |
| 115 |
|
| 116 |
static void DSdbEnter (void); |
| 117 |
|
| 118 |
/* |
| 119 |
DSdbExit - |
| 120 |
*/ |
| 121 |
|
| 122 |
static void DSdbExit (DynamicStrings_String s); |
| 123 |
|
| 124 |
/* |
| 125 |
IsDigit - returns TRUE if ch lies in the range: 0..9 |
| 126 |
*/ |
| 127 |
|
| 128 |
static bool IsDigit (char ch); |
| 129 |
|
| 130 |
/* |
| 131 |
Cast - casts a := b |
| 132 |
*/ |
| 133 |
|
| 134 |
static void Cast (unsigned char *a, unsigned int _a_high, const unsigned char *b_, unsigned int _b_high); |
| 135 |
|
| 136 |
/* |
| 137 |
isHex - |
| 138 |
*/ |
| 139 |
|
| 140 |
static bool isHex (char ch); |
| 141 |
|
| 142 |
/* |
| 143 |
toHex - |
| 144 |
*/ |
| 145 |
|
| 146 |
static unsigned int toHex (char ch); |
| 147 |
|
| 148 |
/* |
| 149 |
toOct - |
| 150 |
*/ |
| 151 |
|
| 152 |
static unsigned int toOct (char ch); |
| 153 |
|
| 154 |
/* |
| 155 |
isOct - |
| 156 |
*/ |
| 157 |
|
| 158 |
static bool isOct (char ch); |
| 159 |
|
| 160 |
/* |
| 161 |
FormatString - returns a String containing, s, together with encapsulated |
| 162 |
entity, w. It only formats the first %s or %d or %u with n. |
| 163 |
A new string is returned. |
| 164 |
*/ |
| 165 |
|
| 166 |
static DynamicStrings_String FormatString (DynamicStrings_String fmt, int *startpos, DynamicStrings_String in, const unsigned char *w_, unsigned int _w_high); |
| 167 |
|
| 168 |
/* |
| 169 |
FormatString - returns a String containing, s, together with encapsulated |
| 170 |
entity, w. It only formats the first %s or %d or %u with n. |
| 171 |
A new string is returned. |
| 172 |
*/ |
| 173 |
|
| 174 |
static DynamicStrings_String PerformFormatString (DynamicStrings_String fmt, int *startpos, DynamicStrings_String in, const unsigned char *w_, unsigned int _w_high); |
| 175 |
|
| 176 |
/* |
| 177 |
Copy - copies, fmt[start:end] -> in and returns in. Providing that start >= 0. |
| 178 |
*/ |
| 179 |
|
| 180 |
static DynamicStrings_String Copy (DynamicStrings_String fmt, DynamicStrings_String in, int start, int end); |
| 181 |
|
| 182 |
/* |
| 183 |
HandlePercent - pre-condition: s, is a string. |
| 184 |
Post-condition: a new string is returned which is a copy of, |
| 185 |
s, except %% is transformed into %. |
| 186 |
*/ |
| 187 |
|
| 188 |
static DynamicStrings_String HandlePercent (DynamicStrings_String fmt, DynamicStrings_String s, int startpos); |
| 189 |
|
| 190 |
|
| 191 |
/* |
| 192 |
doDSdbEnter - |
| 193 |
*/ |
| 194 |
|
| 195 |
static void doDSdbEnter (void) |
| 196 |
{ |
| 197 |
DynamicStrings_PushAllocation (); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
/* |
| 202 |
doDSdbExit - |
| 203 |
*/ |
| 204 |
|
| 205 |
static void doDSdbExit (DynamicStrings_String s) |
| 206 |
{ |
| 207 |
s = DynamicStrings_PopAllocationExemption (true, s); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
/* |
| 212 |
DSdbEnter - |
| 213 |
*/ |
| 214 |
|
| 215 |
static void DSdbEnter (void) |
| 216 |
{ |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
/* |
| 221 |
DSdbExit - |
| 222 |
*/ |
| 223 |
|
| 224 |
static void DSdbExit (DynamicStrings_String s) |
| 225 |
{ |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
/* |
| 230 |
IsDigit - returns TRUE if ch lies in the range: 0..9 |
| 231 |
*/ |
| 232 |
|
| 233 |
static bool IsDigit (char ch) |
| 234 |
{ |
| 235 |
return (ch >= '0') && (ch <= '9'); |
| 236 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 237 |
__builtin_unreachable (); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
/* |
| 242 |
Cast - casts a := b |
| 243 |
*/ |
| 244 |
|
| 245 |
static void Cast (unsigned char *a, unsigned int _a_high, const unsigned char *b_, unsigned int _b_high) |
| 246 |
{ |
| 247 |
unsigned int i; |
| 248 |
unsigned char b[_b_high+1]; |
| 249 |
|
| 250 |
/* make a local copy of each unbounded array. */ |
| 251 |
memcpy (b, b_, _b_high+1); |
| 252 |
|
| 253 |
if (_a_high == _b_high) |
| 254 |
{ |
| 255 |
for (i=0; i<=_a_high; i++) |
| 256 |
{ |
| 257 |
const_cast<unsigned char *>(a)[i] = b[i]; |
| 258 |
} |
| 259 |
} |
| 260 |
else |
| 261 |
{ |
| 262 |
M2RTS_HALT (-1); |
| 263 |
__builtin_unreachable (); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
/* |
| 269 |
isHex - |
| 270 |
*/ |
| 271 |
|
| 272 |
static bool isHex (char ch) |
| 273 |
{ |
| 274 |
return (((ch >= '0') && (ch <= '9')) || ((ch >= 'A') && (ch <= 'F'))) || ((ch >= 'a') && (ch <= 'f')); |
| 275 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 276 |
__builtin_unreachable (); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
/* |
| 281 |
toHex - |
| 282 |
*/ |
| 283 |
|
| 284 |
static unsigned int toHex (char ch) |
| 285 |
{ |
| 286 |
if ((ch >= '0') && (ch <= '9')) |
| 287 |
{ |
| 288 |
return ((unsigned int) (ch))- ((unsigned int) ('0')); |
| 289 |
} |
| 290 |
else if ((ch >= 'A') && (ch <= 'F')) |
| 291 |
{ |
| 292 |
/* avoid dangling else. */ |
| 293 |
return ( ((unsigned int) (ch))- ((unsigned int) ('A')))+10; |
| 294 |
} |
| 295 |
else |
| 296 |
{ |
| 297 |
/* avoid dangling else. */ |
| 298 |
return ( ((unsigned int) (ch))- ((unsigned int) ('a')))+10; |
| 299 |
} |
| 300 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 301 |
__builtin_unreachable (); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
/* |
| 306 |
toOct - |
| 307 |
*/ |
| 308 |
|
| 309 |
static unsigned int toOct (char ch) |
| 310 |
{ |
| 311 |
return ((unsigned int) (ch))- ((unsigned int) ('0')); |
| 312 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 313 |
__builtin_unreachable (); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
/* |
| 318 |
isOct - |
| 319 |
*/ |
| 320 |
|
| 321 |
static bool isOct (char ch) |
| 322 |
{ |
| 323 |
return (ch >= '0') && (ch <= '8'); |
| 324 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 325 |
__builtin_unreachable (); |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
/* |
| 330 |
FormatString - returns a String containing, s, together with encapsulated |
| 331 |
entity, w. It only formats the first %s or %d or %u with n. |
| 332 |
A new string is returned. |
| 333 |
*/ |
| 334 |
|
| 335 |
static DynamicStrings_String FormatString (DynamicStrings_String fmt, int *startpos, DynamicStrings_String in, const unsigned char *w_, unsigned int _w_high) |
| 336 |
{ |
| 337 |
DynamicStrings_String s; |
| 338 |
unsigned char w[_w_high+1]; |
| 339 |
|
| 340 |
/* make a local copy of each unbounded array. */ |
| 341 |
memcpy (w, w_, _w_high+1); |
| 342 |
|
| 343 |
DSdbEnter (); |
| 344 |
if ((*startpos) >= 0) |
| 345 |
{ |
| 346 |
s = PerformFormatString (fmt, startpos, in, (const unsigned char *) w, _w_high); |
| 347 |
} |
| 348 |
else |
| 349 |
{ |
| 350 |
s = DynamicStrings_Dup (in); |
| 351 |
} |
| 352 |
DSdbExit (s); |
| 353 |
return s; |
| 354 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 355 |
__builtin_unreachable (); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
/* |
| 360 |
FormatString - returns a String containing, s, together with encapsulated |
| 361 |
entity, w. It only formats the first %s or %d or %u with n. |
| 362 |
A new string is returned. |
| 363 |
*/ |
| 364 |
|
| 365 |
static DynamicStrings_String PerformFormatString (DynamicStrings_String fmt, int *startpos, DynamicStrings_String in, const unsigned char *w_, unsigned int _w_high) |
| 366 |
{ |
| 367 |
bool left; |
| 368 |
unsigned int u; |
| 369 |
int c; |
| 370 |
int width; |
| 371 |
int nextperc; |
| 372 |
int afterperc; |
| 373 |
char leader; |
| 374 |
char ch; |
| 375 |
char ch2; |
| 376 |
DynamicStrings_String p; |
| 377 |
unsigned char w[_w_high+1]; |
| 378 |
|
| 379 |
/* make a local copy of each unbounded array. */ |
| 380 |
memcpy (w, w_, _w_high+1); |
| 381 |
|
| 382 |
while ((*startpos) >= 0) |
| 383 |
{ |
| 384 |
nextperc = DynamicStrings_Index (fmt, '%', static_cast<unsigned int> ((*startpos))); |
| 385 |
afterperc = nextperc; |
| 386 |
if (nextperc >= 0) |
| 387 |
{ |
| 388 |
afterperc += 1; |
| 389 |
if ((DynamicStrings_char (fmt, afterperc)) == '-') |
| 390 |
{ |
| 391 |
left = true; |
| 392 |
afterperc += 1; |
| 393 |
} |
| 394 |
else |
| 395 |
{ |
| 396 |
left = false; |
| 397 |
} |
| 398 |
ch = DynamicStrings_char (fmt, afterperc); |
| 399 |
if (ch == '0') |
| 400 |
{ |
| 401 |
leader = '0'; |
| 402 |
} |
| 403 |
else |
| 404 |
{ |
| 405 |
leader = ' '; |
| 406 |
} |
| 407 |
width = 0; |
| 408 |
while (IsDigit (ch)) |
| 409 |
{ |
| 410 |
width = (width*10)+((int ) ( ((unsigned int) (ch))- ((unsigned int) ('0')))); |
| 411 |
afterperc += 1; |
| 412 |
ch = DynamicStrings_char (fmt, afterperc); |
| 413 |
} |
| 414 |
if ((ch == 'c') || (ch == 's')) |
| 415 |
{ |
| 416 |
afterperc += 1; |
| 417 |
if (ch == 'c') |
| 418 |
{ |
| 419 |
ch2 = static_cast<char> (w[0]); |
| 420 |
p = DynamicStrings_ConCatChar (DynamicStrings_InitString ((const char *) "", 0), ch2); |
| 421 |
} |
| 422 |
else |
| 423 |
{ |
| 424 |
Cast ((unsigned char *) &p, (sizeof (p)-1), (const unsigned char *) w, _w_high); |
| 425 |
p = DynamicStrings_Dup (p); |
| 426 |
} |
| 427 |
if ((width > 0) && (((int ) (DynamicStrings_Length (p))) < width)) |
| 428 |
{ |
| 429 |
/* avoid gcc warning by using compound statement even if not strictly necessary. */ |
| 430 |
if (left) |
| 431 |
{ |
| 432 |
/* place trailing spaces after, p. */ |
| 433 |
p = DynamicStrings_ConCat (p, DynamicStrings_Mark (DynamicStrings_Mult (DynamicStrings_Mark (DynamicStrings_InitString ((const char *) " ", 1)), static_cast<unsigned int> (width-((int ) (DynamicStrings_Length (p))))))); |
| 434 |
} |
| 435 |
else |
| 436 |
{ |
| 437 |
/* padd string, p, with leading spaces. */ |
| 438 |
p = DynamicStrings_ConCat (DynamicStrings_Mult (DynamicStrings_Mark (DynamicStrings_InitString ((const char *) " ", 1)), static_cast<unsigned int> (width-((int ) (DynamicStrings_Length (p))))), DynamicStrings_Mark (p)); |
| 439 |
} |
| 440 |
} |
| 441 |
/* include string, p, into, in. */ |
| 442 |
if (nextperc > 0) |
| 443 |
{ |
| 444 |
in = DynamicStrings_ConCat (in, DynamicStrings_Slice (fmt, (*startpos), nextperc)); |
| 445 |
} |
| 446 |
in = DynamicStrings_ConCat (in, p); |
| 447 |
(*startpos) = afterperc; |
| 448 |
DSdbExit (static_cast<DynamicStrings_String> (NULL)); |
| 449 |
return in; |
| 450 |
} |
| 451 |
else if (ch == 'd') |
| 452 |
{ |
| 453 |
/* avoid dangling else. */ |
| 454 |
afterperc += 1; |
| 455 |
Cast ((unsigned char *) &c, (sizeof (c)-1), (const unsigned char *) w, _w_high); |
| 456 |
in = Copy (fmt, in, (*startpos), nextperc); |
| 457 |
in = DynamicStrings_ConCat (in, StringConvert_IntegerToString (c, static_cast<unsigned int> (width), leader, false, 10, false)); |
| 458 |
(*startpos) = afterperc; |
| 459 |
DSdbExit (static_cast<DynamicStrings_String> (NULL)); |
| 460 |
return in; |
| 461 |
} |
| 462 |
else if (ch == 'x') |
| 463 |
{ |
| 464 |
/* avoid dangling else. */ |
| 465 |
afterperc += 1; |
| 466 |
Cast ((unsigned char *) &u, (sizeof (u)-1), (const unsigned char *) w, _w_high); |
| 467 |
in = DynamicStrings_ConCat (in, DynamicStrings_Slice (fmt, (*startpos), nextperc)); |
| 468 |
in = DynamicStrings_ConCat (in, StringConvert_CardinalToString (u, static_cast<unsigned int> (width), leader, 16, true)); |
| 469 |
(*startpos) = afterperc; |
| 470 |
DSdbExit (static_cast<DynamicStrings_String> (NULL)); |
| 471 |
return in; |
| 472 |
} |
| 473 |
else if (ch == 'u') |
| 474 |
{ |
| 475 |
/* avoid dangling else. */ |
| 476 |
afterperc += 1; |
| 477 |
Cast ((unsigned char *) &u, (sizeof (u)-1), (const unsigned char *) w, _w_high); |
| 478 |
in = DynamicStrings_ConCat (in, DynamicStrings_Slice (fmt, (*startpos), nextperc)); |
| 479 |
in = DynamicStrings_ConCat (in, StringConvert_CardinalToString (u, static_cast<unsigned int> (width), leader, 10, false)); |
| 480 |
(*startpos) = afterperc; |
| 481 |
DSdbExit (static_cast<DynamicStrings_String> (NULL)); |
| 482 |
return in; |
| 483 |
} |
| 484 |
else |
| 485 |
{ |
| 486 |
/* avoid dangling else. */ |
| 487 |
afterperc += 1; |
| 488 |
/* copy format string. */ |
| 489 |
if (nextperc > 0) |
| 490 |
{ |
| 491 |
in = DynamicStrings_ConCat (in, DynamicStrings_Slice (fmt, (*startpos), nextperc)); |
| 492 |
} |
| 493 |
/* and the character after the %. */ |
| 494 |
in = DynamicStrings_ConCat (in, DynamicStrings_Mark (DynamicStrings_InitStringChar (ch))); |
| 495 |
} |
| 496 |
(*startpos) = afterperc; |
| 497 |
} |
| 498 |
else |
| 499 |
{ |
| 500 |
/* nothing to do. */ |
| 501 |
DSdbExit (static_cast<DynamicStrings_String> (NULL)); |
| 502 |
return in; |
| 503 |
} |
| 504 |
} |
| 505 |
DSdbExit (static_cast<DynamicStrings_String> (NULL)); |
| 506 |
return in; |
| 507 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 508 |
__builtin_unreachable (); |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
/* |
| 513 |
Copy - copies, fmt[start:end] -> in and returns in. Providing that start >= 0. |
| 514 |
*/ |
| 515 |
|
| 516 |
static DynamicStrings_String Copy (DynamicStrings_String fmt, DynamicStrings_String in, int start, int end) |
| 517 |
{ |
| 518 |
if (start >= 0) |
| 519 |
{ |
| 520 |
/* avoid gcc warning by using compound statement even if not strictly necessary. */ |
| 521 |
if (end > 0) |
| 522 |
{ |
| 523 |
in = DynamicStrings_ConCat (in, DynamicStrings_Mark (DynamicStrings_Slice (fmt, start, end))); |
| 524 |
} |
| 525 |
else if (end < 0) |
| 526 |
{ |
| 527 |
/* avoid dangling else. */ |
| 528 |
in = DynamicStrings_ConCat (in, DynamicStrings_Mark (DynamicStrings_Slice (fmt, start, 0))); |
| 529 |
} |
| 530 |
} |
| 531 |
return in; |
| 532 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 533 |
__builtin_unreachable (); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
/* |
| 538 |
HandlePercent - pre-condition: s, is a string. |
| 539 |
Post-condition: a new string is returned which is a copy of, |
| 540 |
s, except %% is transformed into %. |
| 541 |
*/ |
| 542 |
|
| 543 |
static DynamicStrings_String HandlePercent (DynamicStrings_String fmt, DynamicStrings_String s, int startpos) |
| 544 |
{ |
| 545 |
int prevpos; |
| 546 |
|
| 547 |
if ((startpos == ((int ) (DynamicStrings_Length (fmt)))) || (startpos < 0)) |
| 548 |
{ |
| 549 |
return s; |
| 550 |
} |
| 551 |
else |
| 552 |
{ |
| 553 |
prevpos = startpos; |
| 554 |
while ((startpos >= 0) && (prevpos < ((int ) (DynamicStrings_Length (fmt))))) |
| 555 |
{ |
| 556 |
startpos = DynamicStrings_Index (fmt, '%', static_cast<unsigned int> (startpos)); |
| 557 |
if (startpos >= prevpos) |
| 558 |
{ |
| 559 |
if (startpos > 0) |
| 560 |
{ |
| 561 |
s = DynamicStrings_ConCat (s, DynamicStrings_Mark (DynamicStrings_Slice (fmt, prevpos, startpos))); |
| 562 |
} |
| 563 |
startpos += 1; |
| 564 |
if ((DynamicStrings_char (fmt, startpos)) == '%') |
| 565 |
{ |
| 566 |
s = DynamicStrings_ConCatChar (s, '%'); |
| 567 |
startpos += 1; |
| 568 |
} |
| 569 |
prevpos = startpos; |
| 570 |
} |
| 571 |
} |
| 572 |
if (prevpos < ((int ) (DynamicStrings_Length (fmt)))) |
| 573 |
{ |
| 574 |
s = DynamicStrings_ConCat (s, DynamicStrings_Mark (DynamicStrings_Slice (fmt, prevpos, 0))); |
| 575 |
} |
| 576 |
return s; |
| 577 |
} |
| 578 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 579 |
__builtin_unreachable (); |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
/* |
| 584 |
Sprintf0 - returns a String containing, s, after it has had its |
| 585 |
escape sequences translated. |
| 586 |
*/ |
| 587 |
|
| 588 |
extern "C" DynamicStrings_String FormatStrings_Sprintf0 (DynamicStrings_String fmt) |
| 589 |
{ |
| 590 |
DynamicStrings_String s; |
| 591 |
|
| 592 |
DSdbEnter (); |
| 593 |
fmt = FormatStrings_HandleEscape (fmt); |
| 594 |
s = HandlePercent (fmt, DynamicStrings_InitString ((const char *) "", 0), 0); |
| 595 |
DSdbExit (s); |
| 596 |
return s; |
| 597 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 598 |
__builtin_unreachable (); |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
/* |
| 603 |
Sprintf1 - returns a String containing, s, together with encapsulated |
| 604 |
entity, w. It only formats the first %s or %d with n. |
| 605 |
*/ |
| 606 |
|
| 607 |
extern "C" DynamicStrings_String FormatStrings_Sprintf1 (DynamicStrings_String fmt, const unsigned char *w_, unsigned int _w_high) |
| 608 |
{ |
| 609 |
int i; |
| 610 |
DynamicStrings_String s; |
| 611 |
unsigned char w[_w_high+1]; |
| 612 |
|
| 613 |
/* make a local copy of each unbounded array. */ |
| 614 |
memcpy (w, w_, _w_high+1); |
| 615 |
|
| 616 |
DSdbEnter (); |
| 617 |
fmt = FormatStrings_HandleEscape (fmt); |
| 618 |
i = 0; |
| 619 |
s = FormatString (fmt, &i, DynamicStrings_InitString ((const char *) "", 0), (const unsigned char *) w, _w_high); |
| 620 |
s = HandlePercent (fmt, s, i); |
| 621 |
DSdbExit (s); |
| 622 |
return s; |
| 623 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 624 |
__builtin_unreachable (); |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
/* |
| 629 |
Sprintf2 - returns a string, s, which has been formatted. |
| 630 |
*/ |
| 631 |
|
| 632 |
extern "C" DynamicStrings_String FormatStrings_Sprintf2 (DynamicStrings_String fmt, const unsigned char *w1_, unsigned int _w1_high, const unsigned char *w2_, unsigned int _w2_high) |
| 633 |
{ |
| 634 |
int i; |
| 635 |
DynamicStrings_String s; |
| 636 |
unsigned char w1[_w1_high+1]; |
| 637 |
unsigned char w2[_w2_high+1]; |
| 638 |
|
| 639 |
/* make a local copy of each unbounded array. */ |
| 640 |
memcpy (w1, w1_, _w1_high+1); |
| 641 |
memcpy (w2, w2_, _w2_high+1); |
| 642 |
|
| 643 |
DSdbEnter (); |
| 644 |
fmt = FormatStrings_HandleEscape (fmt); |
| 645 |
i = 0; |
| 646 |
s = FormatString (fmt, &i, DynamicStrings_InitString ((const char *) "", 0), (const unsigned char *) w1, _w1_high); |
| 647 |
s = FormatString (fmt, &i, s, (const unsigned char *) w2, _w2_high); |
| 648 |
s = HandlePercent (fmt, s, i); |
| 649 |
DSdbExit (s); |
| 650 |
return s; |
| 651 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 652 |
__builtin_unreachable (); |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
/* |
| 657 |
Sprintf3 - returns a string, s, which has been formatted. |
| 658 |
*/ |
| 659 |
|
| 660 |
extern "C" DynamicStrings_String FormatStrings_Sprintf3 (DynamicStrings_String fmt, const unsigned char *w1_, unsigned int _w1_high, const unsigned char *w2_, unsigned int _w2_high, const unsigned char *w3_, unsigned int _w3_high) |
| 661 |
{ |
| 662 |
int i; |
| 663 |
DynamicStrings_String s; |
| 664 |
unsigned char w1[_w1_high+1]; |
| 665 |
unsigned char w2[_w2_high+1]; |
| 666 |
unsigned char w3[_w3_high+1]; |
| 667 |
|
| 668 |
/* make a local copy of each unbounded array. */ |
| 669 |
memcpy (w1, w1_, _w1_high+1); |
| 670 |
memcpy (w2, w2_, _w2_high+1); |
| 671 |
memcpy (w3, w3_, _w3_high+1); |
| 672 |
|
| 673 |
DSdbEnter (); |
| 674 |
fmt = FormatStrings_HandleEscape (fmt); |
| 675 |
i = 0; |
| 676 |
s = FormatString (fmt, &i, DynamicStrings_InitString ((const char *) "", 0), (const unsigned char *) w1, _w1_high); |
| 677 |
s = FormatString (fmt, &i, s, (const unsigned char *) w2, _w2_high); |
| 678 |
s = FormatString (fmt, &i, s, (const unsigned char *) w3, _w3_high); |
| 679 |
s = HandlePercent (fmt, s, i); |
| 680 |
DSdbExit (s); |
| 681 |
return s; |
| 682 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 683 |
__builtin_unreachable (); |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
/* |
| 688 |
Sprintf4 - returns a string, s, which has been formatted. |
| 689 |
*/ |
| 690 |
|
| 691 |
extern "C" DynamicStrings_String FormatStrings_Sprintf4 (DynamicStrings_String fmt, const unsigned char *w1_, unsigned int _w1_high, const unsigned char *w2_, unsigned int _w2_high, const unsigned char *w3_, unsigned int _w3_high, const unsigned char *w4_, unsigned int _w4_high) |
| 692 |
{ |
| 693 |
int i; |
| 694 |
DynamicStrings_String s; |
| 695 |
unsigned char w1[_w1_high+1]; |
| 696 |
unsigned char w2[_w2_high+1]; |
| 697 |
unsigned char w3[_w3_high+1]; |
| 698 |
unsigned char w4[_w4_high+1]; |
| 699 |
|
| 700 |
/* make a local copy of each unbounded array. */ |
| 701 |
memcpy (w1, w1_, _w1_high+1); |
| 702 |
memcpy (w2, w2_, _w2_high+1); |
| 703 |
memcpy (w3, w3_, _w3_high+1); |
| 704 |
memcpy (w4, w4_, _w4_high+1); |
| 705 |
|
| 706 |
DSdbEnter (); |
| 707 |
fmt = FormatStrings_HandleEscape (fmt); |
| 708 |
i = 0; |
| 709 |
s = FormatString (fmt, &i, DynamicStrings_InitString ((const char *) "", 0), (const unsigned char *) w1, _w1_high); |
| 710 |
s = FormatString (fmt, &i, s, (const unsigned char *) w2, _w2_high); |
| 711 |
s = FormatString (fmt, &i, s, (const unsigned char *) w3, _w3_high); |
| 712 |
s = FormatString (fmt, &i, s, (const unsigned char *) w4, _w4_high); |
| 713 |
s = HandlePercent (fmt, s, i); |
| 714 |
DSdbExit (s); |
| 715 |
return s; |
| 716 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 717 |
__builtin_unreachable (); |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
/* |
| 722 |
HandleEscape - translates \a, \b, \e, \f, |
| 723 |
, \r, \x[hex] \[octal] into |
| 724 |
their respective ascii codes. It also converts \[any] into |
| 725 |
a single [any] character. |
| 726 |
*/ |
| 727 |
|
| 728 |
extern "C" DynamicStrings_String FormatStrings_HandleEscape (DynamicStrings_String s) |
| 729 |
{ |
| 730 |
DynamicStrings_String d; |
| 731 |
int i; |
| 732 |
int j; |
| 733 |
char ch; |
| 734 |
unsigned char b; |
| 735 |
|
| 736 |
DSdbEnter (); |
| 737 |
d = DynamicStrings_InitString ((const char *) "", 0); |
| 738 |
i = DynamicStrings_Index (s, '\\', 0); |
| 739 |
j = 0; |
| 740 |
while (i >= 0) |
| 741 |
{ |
| 742 |
if (i > 0) |
| 743 |
{ |
| 744 |
/* initially i might be zero which means the end of the string, which is not what we want. */ |
| 745 |
d = DynamicStrings_ConCat (d, DynamicStrings_Slice (s, j, i)); |
| 746 |
} |
| 747 |
ch = DynamicStrings_char (s, i+1); |
| 748 |
if (ch == 'a') |
| 749 |
{ |
| 750 |
/* requires a bell. */ |
| 751 |
d = DynamicStrings_ConCat (d, DynamicStrings_Mark (DynamicStrings_InitStringChar (ASCII_bel))); |
| 752 |
} |
| 753 |
else if (ch == 'b') |
| 754 |
{ |
| 755 |
/* avoid dangling else. */ |
| 756 |
/* requires a backspace. */ |
| 757 |
d = DynamicStrings_ConCat (d, DynamicStrings_Mark (DynamicStrings_InitStringChar (ASCII_bs))); |
| 758 |
} |
| 759 |
else if (ch == 'e') |
| 760 |
{ |
| 761 |
/* avoid dangling else. */ |
| 762 |
/* requires a escape. */ |
| 763 |
d = DynamicStrings_ConCat (d, DynamicStrings_Mark (DynamicStrings_InitStringChar (ASCII_esc))); |
| 764 |
} |
| 765 |
else if (ch == 'f') |
| 766 |
{ |
| 767 |
/* avoid dangling else. */ |
| 768 |
/* requires a formfeed. */ |
| 769 |
d = DynamicStrings_ConCat (d, DynamicStrings_Mark (DynamicStrings_InitStringChar (ASCII_ff))); |
| 770 |
} |
| 771 |
else if (ch == 'n') |
| 772 |
{ |
| 773 |
/* avoid dangling else. */ |
| 774 |
/* requires a newline. */ |
| 775 |
d = DynamicStrings_ConCat (d, DynamicStrings_Mark (DynamicStrings_InitStringChar (ASCII_nl))); |
| 776 |
} |
| 777 |
else if (ch == 'r') |
| 778 |
{ |
| 779 |
/* avoid dangling else. */ |
| 780 |
/* requires a carriage return. */ |
| 781 |
d = DynamicStrings_ConCat (d, DynamicStrings_Mark (DynamicStrings_InitStringChar (ASCII_cr))); |
| 782 |
} |
| 783 |
else if (ch == 't') |
| 784 |
{ |
| 785 |
/* avoid dangling else. */ |
| 786 |
/* requires a tab. */ |
| 787 |
d = DynamicStrings_ConCat (d, DynamicStrings_Mark (DynamicStrings_InitStringChar (ASCII_tab))); |
| 788 |
} |
| 789 |
else if (ch == 'x') |
| 790 |
{ |
| 791 |
/* avoid dangling else. */ |
| 792 |
i += 1; |
| 793 |
if (isHex (DynamicStrings_char (s, i+1))) |
| 794 |
{ |
| 795 |
b = (unsigned char ) (toHex (DynamicStrings_char (s, i+1))); |
| 796 |
i += 1; |
| 797 |
if (isHex (DynamicStrings_char (s, i+1))) |
| 798 |
{ |
| 799 |
b = (unsigned char ) ((((unsigned int ) (b))*0x010)+(toHex (DynamicStrings_char (s, i+1)))); |
| 800 |
d = DynamicStrings_ConCat (d, DynamicStrings_Mark (DynamicStrings_InitStringChar ((char ) (b)))); |
| 801 |
} |
| 802 |
} |
| 803 |
} |
| 804 |
else if (isOct (ch)) |
| 805 |
{ |
| 806 |
/* avoid dangling else. */ |
| 807 |
b = (unsigned char ) (toOct (ch)); |
| 808 |
i += 1; |
| 809 |
if (isOct (DynamicStrings_char (s, i+1))) |
| 810 |
{ |
| 811 |
b = (unsigned char ) ((((unsigned int ) (b))*8)+(toOct (DynamicStrings_char (s, i+1)))); |
| 812 |
i += 1; |
| 813 |
if (isOct (DynamicStrings_char (s, i+1))) |
| 814 |
{ |
| 815 |
b = (unsigned char ) ((((unsigned int ) (b))*8)+(toOct (DynamicStrings_char (s, i+1)))); |
| 816 |
} |
| 817 |
} |
| 818 |
d = DynamicStrings_ConCat (d, DynamicStrings_Mark (DynamicStrings_InitStringChar ((char ) (b)))); |
| 819 |
} |
| 820 |
else |
| 821 |
{ |
| 822 |
/* avoid dangling else. */ |
| 823 |
/* copy escaped character. */ |
| 824 |
d = DynamicStrings_ConCat (d, DynamicStrings_Mark (DynamicStrings_InitStringChar (ch))); |
| 825 |
} |
| 826 |
i += 2; |
| 827 |
j = i; |
| 828 |
i = DynamicStrings_Index (s, '\\', (unsigned int ) (i)); |
| 829 |
} |
| 830 |
/* s := Assign(s, Mark(ConCat(d, Mark(Slice(s, j, 0))))) ; dont Mark(s) in the Slice as we Assign contents */ |
| 831 |
s = DynamicStrings_ConCat (d, DynamicStrings_Mark (DynamicStrings_Slice (DynamicStrings_Mark (s), j, 0))); |
| 832 |
DSdbExit (s); |
| 833 |
return s; |
| 834 |
/* static analysis guarentees a RETURN statement will be used before here. */ |
| 835 |
__builtin_unreachable (); |
| 836 |
} |
| 837 |
|
| 838 |
extern "C" void _M2_FormatStrings_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 839 |
{ |
| 840 |
} |
| 841 |
|
| 842 |
extern "C" void _M2_FormatStrings_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[]) |
| 843 |
{ |
| 844 |
} |