| 1 |
/* |
| 2 |
* Copyright (c) 2021-2025 Symas Corporation |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions are |
| 6 |
* met: |
| 7 |
* |
| 8 |
* * Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* * Redistributions in binary form must reproduce the above |
| 11 |
* copyright notice, this list of conditions and the following disclaimer |
| 12 |
* in the documentation and/or other materials provided with the |
| 13 |
* distribution. |
| 14 |
* * Neither the name of the Symas Corporation nor the names of its |
| 15 |
* contributors may be used to endorse or promote products derived from |
| 16 |
* this software without specific prior written permission. |
| 17 |
* |
| 18 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
|
| 31 |
/* COBOL intrinsic functions. |
| 32 |
* |
| 33 |
* In general, the parameters to these functions are cblc_field_t pointers |
| 34 |
* along with an offset, size, and for some functions the "allflags", which |
| 35 |
* indicate that the variable is a table that was referenced as TABL(ALL) |
| 36 |
*/ |
| 37 |
|
| 38 |
#include <langinfo.h> |
| 39 |
|
| 40 |
#include <cctype> |
| 41 |
#include <cmath> |
| 42 |
#include <cstring> |
| 43 |
#include <ctime> |
| 44 |
|
| 45 |
#include <algorithm> |
| 46 |
#include <vector> |
| 47 |
|
| 48 |
#include "config.h" |
| 49 |
#include "libgcobol-fp.h" |
| 50 |
|
| 51 |
#include "ec.h" |
| 52 |
#include "common-defs.h" |
| 53 |
#include "io.h" |
| 54 |
#include "gcobolio.h" |
| 55 |
#include "libgcobol.h" |
| 56 |
#include "charmaps.h" |
| 57 |
|
| 58 |
|
| 59 |
#pragma GCC diagnostic ignored "-Wformat-truncation" |
| 60 |
|
| 61 |
#define JD_OF_1601_01_02 2305812.5 |
| 62 |
|
| 63 |
#define WEIRD_TRANSCENDENT_RETURN_VALUE GCOB_FP128_LITERAL (0.0) |
| 64 |
#define NO_RDIGITS (0) |
| 65 |
|
| 66 |
struct cobol_tm |
| 67 |
{ |
| 68 |
int YYYY; // 1601-9999 |
| 69 |
int MM; // 01-12 |
| 70 |
int DD; // 01-28,29,30,31 |
| 71 |
int hh; // 00-23 |
| 72 |
int mm; // 00-59 |
| 73 |
int ss; // 00-59 |
| 74 |
int nanoseconds; // 0 through 999,999,999 |
| 75 |
int tz_offset; // +/- 1359 |
| 76 |
int week_of_year; // 01 - 52,53 |
| 77 |
int day_of_year; // 001-365, 366 |
| 78 |
int day_of_week; // 0-6; 0 being Monday |
| 79 |
int days_in_year; // 365,366 |
| 80 |
int weeks_in_year; // 52,53 |
| 81 |
int ZZZZ; // Alternate year, when Jan 4 is Mon, Tue, or Wednesday |
| 82 |
}; |
| 83 |
|
| 84 |
static int is_leap_year(int); |
| 85 |
|
| 86 |
typedef char * PCHAR; |
| 87 |
|
| 88 |
static void |
| 89 |
trim_trailing_spaces(PCHAR left, PCHAR &right) |
| 90 |
{ |
| 91 |
while( right > left ) |
| 92 |
{ |
| 93 |
if( *(right-1) != internal_space ) |
| 94 |
{ |
| 95 |
break; |
| 96 |
} |
| 97 |
right -= 1; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
static bool |
| 102 |
is_zulu_format(PCHAR left, PCHAR &right) |
| 103 |
{ |
| 104 |
bool retval = false; |
| 105 |
if( right > left ) |
| 106 |
{ |
| 107 |
retval = std::toupper((unsigned char)*(right-1)) == internal_Z; |
| 108 |
} |
| 109 |
return retval; |
| 110 |
} |
| 111 |
|
| 112 |
static double |
| 113 |
YMD_to_JD(int Y, int M, int D) |
| 114 |
{ |
| 115 |
// Calculates the Julian Day |
| 116 |
|
| 117 |
if( M <= 2 ) |
| 118 |
{ |
| 119 |
Y -= 1 ; |
| 120 |
M += 12; |
| 121 |
} |
| 122 |
double A = floor(Y/100.); |
| 123 |
double B = 2. - A + floor(A/4.); |
| 124 |
|
| 125 |
double JD; |
| 126 |
JD = floor(365.25 * double(Y + 4716) + floor((30.6001 * double(M+1)))) + D + B -1524.5 ; |
| 127 |
|
| 128 |
return JD; |
| 129 |
} |
| 130 |
|
| 131 |
static void |
| 132 |
JD_to_YMD(int &YY, int &MM, int &DD, double JD) |
| 133 |
{ |
| 134 |
JD += 0.5; |
| 135 |
double Z = floor(JD); |
| 136 |
double F = JD - Z; |
| 137 |
double A; |
| 138 |
if( Z < 2299161.0 ) |
| 139 |
{ |
| 140 |
A = Z; |
| 141 |
} |
| 142 |
else |
| 143 |
{ |
| 144 |
double alpha = floor( (Z-1867216.25) / 36524.25 ) ; |
| 145 |
A = Z + 1.0 + alpha - floor(alpha/4.0); |
| 146 |
} |
| 147 |
double B = A + 1524; |
| 148 |
double C = floor( (B - 122.1)/365.25 ); |
| 149 |
double D = floor( 365.25 * C ); |
| 150 |
double E = floor( (B-D)/30.6001 ); |
| 151 |
|
| 152 |
DD = (int)( B - D - floor(30.6001 * E) + F ); |
| 153 |
MM = (int)( E < 14 ? E - 1 : E - 13 ); |
| 154 |
YY = (int)( MM > 2 ? C - 4716 : C - 4715 ); |
| 155 |
} |
| 156 |
|
| 157 |
static int |
| 158 |
JD_to_DOW(double JD) |
| 159 |
{ |
| 160 |
// Converts a Julian Day to 0 through 6, where |
| 161 |
// 0 is Monday. |
| 162 |
// 2415020.50000 is noon on 1900-01-01, which was a Monday |
| 163 |
return ((int)(JD-0.5)+1)%7; |
| 164 |
} |
| 165 |
|
| 166 |
#define DATE_STRING_BUFFER_SIZE 23 |
| 167 |
|
| 168 |
static |
| 169 |
char * |
| 170 |
timespec_to_string(char *retval, struct cbl_timespec &tp) |
| 171 |
{ |
| 172 |
/* |
| 173 |
Returns a 21-character string: |
| 174 |
|
| 175 |
1 - 4 Four numeric digits of the year in the Gregorian calendar |
| 176 |
5 - 6 Two numeric digits of the month of the year, in the range 01 through 12 |
| 177 |
7 - 8 Two numeric digits of the day of the month, in the range 01 through 31 |
| 178 |
9 - 10 Two numeric digits of the hours past midnight, in the range 00 through 23 |
| 179 |
11 - 12 Two numeric digits of the minutes past the hour, in the range 00 through 59 |
| 180 |
13 - 14 Two numeric digits of the seconds past the minute, in the range 00 through 59 |
| 181 |
15 - 16 Two numeric digits of the hundredths of a second past the second, in the range |
| 182 |
17 Either the character '-' or the character '+'. |
| 183 |
18 - 19 If character position 17 is '-', two numeric digits are returned in the range 00 |
| 184 |
through 12 indicating the number of hours that the reported time is behind |
| 185 |
Greenwich mean time. |
| 186 |
|
| 187 |
If character position 17 is '+', two numeric digits are |
| 188 |
returned in the range 00 through 13 indicating the number of hours that the |
| 189 |
reported time is ahead of Greenwich mean time. If character position 17 is '0', the |
| 190 |
value 00 is returned. |
| 191 |
20 - 21 Two numeric digits are returned in the range 00 through 59 indicating the number |
| 192 |
of additional minutes that the reported time is ahead of or behind Greenwich |
| 193 |
mean time, depending on whether character position 17 |
| 194 |
*/ |
| 195 |
|
| 196 |
const int size_of_buffer = DATE_STRING_BUFFER_SIZE; |
| 197 |
const int offset_to_hundredths = 14; |
| 198 |
const long nanoseconds_to_hundredths = 10000000; |
| 199 |
|
| 200 |
// Convert the nanosecond fraction to hundredths of a second: |
| 201 |
char achCentiseconds[3]; |
| 202 |
snprintf(achCentiseconds, 3, "%2.2ld", (tp.tv_nsec/nanoseconds_to_hundredths) ); |
| 203 |
|
| 204 |
// Convert the epoch seconds to broken-down time: |
| 205 |
struct tm tm = {}; |
| 206 |
|
| 207 |
if( false ) |
| 208 |
{ |
| 209 |
// With a forced date/time, eliminate local influences |
| 210 |
gmtime_r(&tp.tv_sec, &tm); |
| 211 |
} |
| 212 |
else |
| 213 |
{ |
| 214 |
localtime_r(&tp.tv_sec, &tm); |
| 215 |
} |
| 216 |
|
| 217 |
// Format the time as per COBOL specifications, leaving two spaces for the |
| 218 |
// hundredths of seconds: |
| 219 |
strftime(retval, size_of_buffer, "%Y%m%d%H%M%S %z", &tm); |
| 220 |
|
| 221 |
// Copy the 100ths into place: |
| 222 |
memcpy(retval+offset_to_hundredths, achCentiseconds, 2); |
| 223 |
|
| 224 |
return retval; |
| 225 |
} |
| 226 |
|
| 227 |
static |
| 228 |
void |
| 229 |
string_to_dest(cblc_field_t *dest, const char *psz) |
| 230 |
{ |
| 231 |
size_t dest_length = dest->capacity; |
| 232 |
size_t source_length = strlen(psz); |
| 233 |
size_t length = std::min(dest_length, source_length); |
| 234 |
memset(dest->data, internal_space, dest_length); |
| 235 |
memcpy(dest->data, psz, length); |
| 236 |
} |
| 237 |
|
| 238 |
struct input_state |
| 239 |
{ |
| 240 |
size_t nsubscript; |
| 241 |
bool *subscript_alls; |
| 242 |
size_t *subscripts; |
| 243 |
size_t *subscript_limits; |
| 244 |
bool done; |
| 245 |
|
| 246 |
void allocate(size_t N) |
| 247 |
{ |
| 248 |
nsubscript = N; |
| 249 |
if(N) |
| 250 |
{ |
| 251 |
subscript_alls = static_cast<bool *>(malloc(nsubscript)); |
| 252 |
subscripts = static_cast<size_t *>(malloc(nsubscript)); |
| 253 |
subscript_limits = static_cast<size_t *>(malloc(nsubscript)); |
| 254 |
massert(subscript_alls); |
| 255 |
massert(subscripts); |
| 256 |
massert(subscript_limits); |
| 257 |
} |
| 258 |
done = false; |
| 259 |
} |
| 260 |
void deallocate() |
| 261 |
{ |
| 262 |
if(nsubscript) |
| 263 |
{ |
| 264 |
free(subscript_alls); |
| 265 |
free(subscripts); |
| 266 |
free(subscript_limits); |
| 267 |
} |
| 268 |
} |
| 269 |
}; |
| 270 |
|
| 271 |
struct refer_state_for_all |
| 272 |
{ |
| 273 |
size_t nflags; |
| 274 |
size_t coefficients [MAXIMUM_TABLE_DIMENSIONS]; |
| 275 |
size_t capacities [MAXIMUM_TABLE_DIMENSIONS]; |
| 276 |
size_t limits [MAXIMUM_TABLE_DIMENSIONS]; |
| 277 |
}; |
| 278 |
|
| 279 |
static |
| 280 |
void |
| 281 |
build_refer_state_for_all( refer_state_for_all &state, |
| 282 |
cblc_field_t *field, |
| 283 |
int flags) |
| 284 |
{ |
| 285 |
memset(&state, 0, sizeof(refer_state_for_all) ); |
| 286 |
if( flags & REFER_T_ALL_FLAGS_MASK ) |
| 287 |
{ |
| 288 |
// At this point, refer points to the very first element of |
| 289 |
// an array specification that includes at least one ALL subscript. At |
| 290 |
// this time, those ALLs were calculated as if they had been replaced |
| 291 |
// with one. |
| 292 |
|
| 293 |
// We are going to walk the reference up to its ultimate parent, picking |
| 294 |
// up what we need along the way. |
| 295 |
|
| 296 |
size_t current_bit = 1; |
| 297 |
size_t current_index = 0; |
| 298 |
cblc_field_t *current_sizer = field; |
| 299 |
while( current_sizer ) |
| 300 |
{ |
| 301 |
while( current_sizer && !current_sizer->occurs_upper ) |
| 302 |
{ |
| 303 |
// current_sizer isn't a table, which isn't unusual. |
| 304 |
current_sizer = current_sizer->parent; |
| 305 |
} |
| 306 |
|
| 307 |
if( !current_sizer ) |
| 308 |
{ |
| 309 |
// We have found all of the elements in this data description |
| 310 |
// that have OCCURS clauses |
| 311 |
break; |
| 312 |
} |
| 313 |
|
| 314 |
// We are sitting on an occurs clause: |
| 315 |
|
| 316 |
if( current_bit & flags ) |
| 317 |
{ |
| 318 |
// It is an ALL subscript: |
| 319 |
state.nflags += 1; |
| 320 |
state.coefficients[current_index] = 1; |
| 321 |
state.capacities[current_index] = current_sizer->capacity; |
| 322 |
state.limits[current_index] = current_sizer->occurs_upper; |
| 323 |
current_index += 1 ; |
| 324 |
} |
| 325 |
|
| 326 |
current_bit <<= 1; |
| 327 |
current_sizer = current_sizer->parent; |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
static |
| 333 |
bool |
| 334 |
update_refer_state_for_all( refer_state_for_all &state, |
| 335 |
cblc_field_t *field) |
| 336 |
{ |
| 337 |
bool retval = false; // Means there is nothing left |
| 338 |
|
| 339 |
for(size_t i=0; i<state.nflags; i++) |
| 340 |
{ |
| 341 |
state.coefficients[i] += 1; |
| 342 |
field->data += state.capacities[i]; |
| 343 |
if( state.coefficients[i] <= state.limits[i] ) |
| 344 |
{ |
| 345 |
// This coefficient is within range: |
| 346 |
retval = true; |
| 347 |
break; |
| 348 |
} |
| 349 |
|
| 350 |
// We have used up this coefficient. |
| 351 |
|
| 352 |
// Remove the effects of incrementing this coefficient: |
| 353 |
field->data -= state.limits[i] * state.capacities[i]; |
| 354 |
// Reset the coefficient back to one: |
| 355 |
state.coefficients[i] = 1; |
| 356 |
|
| 357 |
// And continue on to the next coefficient. |
| 358 |
} |
| 359 |
|
| 360 |
return retval; |
| 361 |
} |
| 362 |
|
| 363 |
static |
| 364 |
int |
| 365 |
year_to_yyyy(int arg1, int arg2, int arg3) |
| 366 |
{ |
| 367 |
// See ISO/IEC 2014-1989 section 15.93 for a detailed description of the |
| 368 |
// sliding window calculation |
| 369 |
int max_year = arg2 + arg3; |
| 370 |
int retval; |
| 371 |
if( max_year % 100 >= arg1 ) |
| 372 |
{ |
| 373 |
retval = arg1 + 100 * (max_year/100); |
| 374 |
} |
| 375 |
else |
| 376 |
{ |
| 377 |
retval = arg1 + 100 * (max_year/100 - 1); |
| 378 |
} |
| 379 |
return retval; |
| 380 |
} |
| 381 |
|
| 382 |
static |
| 383 |
double |
| 384 |
get_value_as_double_from_qualified_field( const cblc_field_t *input, |
| 385 |
size_t input_o, |
| 386 |
size_t input_s) |
| 387 |
{ |
| 388 |
double retval; |
| 389 |
int rdigits; |
| 390 |
|
| 391 |
switch( input->type ) |
| 392 |
{ |
| 393 |
case FldFloat: |
| 394 |
fprintf(stderr, "get_value_as_double_from_qualified_field(): Hey!" |
| 395 |
" We got an unexpected float in intrinsic.cc!\n"); |
| 396 |
exit(1); |
| 397 |
break; |
| 398 |
|
| 399 |
default: |
| 400 |
retval = __gg__binary_value_from_qualified_field(&rdigits, |
| 401 |
input, |
| 402 |
input_o, |
| 403 |
input_s); |
| 404 |
for(int i=0; i<rdigits; i++) |
| 405 |
{ |
| 406 |
retval /= 10.0; |
| 407 |
} |
| 408 |
break; |
| 409 |
} |
| 410 |
|
| 411 |
return retval; |
| 412 |
} |
| 413 |
|
| 414 |
static |
| 415 |
GCOB_FP128 kahan_summation(size_t ncount, |
| 416 |
cblc_field_t **source, |
| 417 |
const size_t *source_o, |
| 418 |
const size_t *source_s, |
| 419 |
const int *flags, |
| 420 |
size_t *k_count) |
| 421 |
{ |
| 422 |
// We use compensated addition. Look up Kahan summation. |
| 423 |
|
| 424 |
// In the Kahan summation algorithm, the C value accumulates small errors |
| 425 |
// Algebraically, it should be zero. So, "volatile" is an attempt to prevent |
| 426 |
// an aggressive optimizing compiler from just making it go away. |
| 427 |
|
| 428 |
*k_count = 0; |
| 429 |
GCOB_FP128 sum = 0; |
| 430 |
volatile GCOB_FP128 kahan_c = 0; |
| 431 |
GCOB_FP128 input; |
| 432 |
GCOB_FP128 y; |
| 433 |
GCOB_FP128 t; |
| 434 |
|
| 435 |
for(size_t i=0; i<ncount; i++) |
| 436 |
{ |
| 437 |
refer_state_for_all state; |
| 438 |
build_refer_state_for_all(state, source[i], flags[i]); |
| 439 |
|
| 440 |
for(;;) |
| 441 |
{ |
| 442 |
input = __gg__float128_from_qualified_field(source[i], |
| 443 |
source_o[i], |
| 444 |
source_s[i]); |
| 445 |
y = input - kahan_c; |
| 446 |
t = sum + y; |
| 447 |
kahan_c = (t - sum) - y ; |
| 448 |
sum = t; |
| 449 |
*k_count += 1; |
| 450 |
if( !update_refer_state_for_all(state, source[i]) ) |
| 451 |
{ |
| 452 |
// There is nothing left to do. |
| 453 |
break; |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
return sum; |
| 458 |
} |
| 459 |
|
| 460 |
static |
| 461 |
GCOB_FP128 |
| 462 |
variance( size_t ncount, |
| 463 |
cblc_field_t **source, |
| 464 |
const size_t *source_o, |
| 465 |
const size_t *source_s, |
| 466 |
const int *flags) |
| 467 |
{ |
| 468 |
// In order to avoid catastrophic cancellation, we are going to use an |
| 469 |
// algorithm that is a bit wasteful of time, but is described as particularly |
| 470 |
// robust. |
| 471 |
|
| 472 |
GCOB_FP128 retval = 0; |
| 473 |
if( ncount ) |
| 474 |
{ |
| 475 |
// First, we calculate the mean of the input variables, which we will use |
| 476 |
// as an offset in the second stage: |
| 477 |
size_t k_count; |
| 478 |
GCOB_FP128 offset = kahan_summation( ncount, |
| 479 |
source, |
| 480 |
source_o, |
| 481 |
source_s, |
| 482 |
flags, |
| 483 |
&k_count); |
| 484 |
offset /= k_count; |
| 485 |
|
| 486 |
// Next, we use Welford's algorithm on the residuals: |
| 487 |
|
| 488 |
size_t count = 0; |
| 489 |
GCOB_FP128 mean = 0; |
| 490 |
GCOB_FP128 M2 = 0; |
| 491 |
GCOB_FP128 delta; |
| 492 |
GCOB_FP128 delta2; |
| 493 |
GCOB_FP128 newValue; |
| 494 |
|
| 495 |
for(size_t i=0; i<ncount; i++) |
| 496 |
{ |
| 497 |
refer_state_for_all state; |
| 498 |
build_refer_state_for_all(state, source[i], flags[i]); |
| 499 |
|
| 500 |
for(;;) |
| 501 |
{ |
| 502 |
newValue = __gg__float128_from_qualified_field(source[i], |
| 503 |
source_o[i], |
| 504 |
source_s[i]); |
| 505 |
newValue -= offset; |
| 506 |
|
| 507 |
count += 1; |
| 508 |
delta = newValue - mean ; |
| 509 |
mean += delta / count; |
| 510 |
delta2 = newValue - mean; |
| 511 |
M2 += delta * delta2; |
| 512 |
if( !update_refer_state_for_all(state, source[i]) ) |
| 513 |
{ |
| 514 |
// There is nothing left to do. |
| 515 |
break; |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
retval = M2 / count; |
| 521 |
} |
| 522 |
return retval; |
| 523 |
} |
| 524 |
|
| 525 |
static |
| 526 |
void |
| 527 |
get_all_time( char *stime, |
| 528 |
const struct cobol_tm &ctm) |
| 529 |
{ |
| 530 |
// This routine represents a universal source for all output formatted date |
| 531 |
// and formatted time functions. Messeth not with the format, for all the |
| 532 |
// calling routines are counting on it. |
| 533 |
// |
| 534 |
// 1111111111222222222233333333334 |
| 535 |
// 01234567890123456789012345678901234567890 |
| 536 |
// Returns YYYYMMDDThhmmss.sssssssss+hhmmWwwdDDDZZZZ |
| 537 |
// |
| 538 |
// YYYY is the year |
| 539 |
// MM is the month |
| 540 |
// DD is the day of the month |
| 541 |
// hh is the hour |
| 542 |
// mm is the minute |
| 543 |
// ss is the second |
| 544 |
// .sssssssss are the nanoseconds |
| 545 |
// +hhmm is the offset from UTC |
| 546 |
// Www is the COBOL-style week number. (See the comments and the spec) |
| 547 |
// d is the COBOL-style day-of-week 1-7, 1 being Monday |
| 548 |
// DDD is the COBOL-style day-of-year, 001 being Jan 01 |
| 549 |
// ZZZZ is the alternate year. Sometimes the first one, two, or three |
| 550 |
// days of January show up in the final week of the prior year. |
| 551 |
|
| 552 |
sprintf(stime, |
| 553 |
"%4.4d%2.2d%2.2dT" // YYYYMMSS |
| 554 |
"%2.2d%2.2d%2.2d" // hhmmss |
| 555 |
".%9.9d" // .sssssssss |
| 556 |
"%c%2.2d%2.2d" // +hhmm |
| 557 |
"W%2.2d" // Www |
| 558 |
"%1d" // DOW [1-7], 1 for Monday |
| 559 |
"%3.3d" // DDD day of year, 001 - 365,366 |
| 560 |
"%4.4d", // ZZZZ Year for YYYY-Www-D |
| 561 |
ctm.YYYY, |
| 562 |
ctm.MM, |
| 563 |
ctm.DD, |
| 564 |
ctm.hh, |
| 565 |
ctm.mm, |
| 566 |
ctm.ss, |
| 567 |
ctm.nanoseconds, |
| 568 |
ctm.tz_offset < 0 ? '-' : '+', |
| 569 |
abs(ctm.tz_offset) / 60, |
| 570 |
abs(ctm.tz_offset) % 60, |
| 571 |
ctm.week_of_year, |
| 572 |
ctm.day_of_week+1, |
| 573 |
ctm.day_of_year, |
| 574 |
ctm.ZZZZ); |
| 575 |
// We might be operating in EBCDIC: |
| 576 |
ascii_to_internal_str(stime, strlen(stime)); |
| 577 |
} |
| 578 |
|
| 579 |
static |
| 580 |
int |
| 581 |
is_leap_year(int yyyy) |
| 582 |
{ |
| 583 |
static const unsigned char leap_year_bits[50] = |
| 584 |
{ |
| 585 |
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, |
| 586 |
0x11, 0x11, 0x01, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, |
| 587 |
0x11, 0x11, 0x11, 0x11, 0x11, 0x10, 0x11, 0x11, 0x11, 0x11, |
| 588 |
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x01, 0x11, 0x11, |
| 589 |
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, |
| 590 |
}; |
| 591 |
|
| 592 |
static const unsigned char mask[8] = |
| 593 |
{ 1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80 }; |
| 594 |
|
| 595 |
int days_in_year; |
| 596 |
|
| 597 |
int year_in_cycle = yyyy % 400; |
| 598 |
|
| 599 |
if( leap_year_bits[year_in_cycle/8] & mask[year_in_cycle & 0x07] ) |
| 600 |
{ |
| 601 |
days_in_year = 366; |
| 602 |
} |
| 603 |
else |
| 604 |
{ |
| 605 |
days_in_year = 365; |
| 606 |
} |
| 607 |
|
| 608 |
return days_in_year; |
| 609 |
} |
| 610 |
|
| 611 |
static |
| 612 |
int |
| 613 |
weeks_in_year(int YYYY) |
| 614 |
{ |
| 615 |
double Jan4 = YMD_to_JD(YYYY, 1, 4); |
| 616 |
int dow = JD_to_DOW(Jan4); |
| 617 |
if( dow == 6 ) |
| 618 |
{ |
| 619 |
return 53; |
| 620 |
} |
| 621 |
if( dow == 5 && is_leap_year(YYYY) == 366 ) |
| 622 |
{ |
| 623 |
return 53; |
| 624 |
} |
| 625 |
return 52; |
| 626 |
} |
| 627 |
|
| 628 |
static |
| 629 |
void |
| 630 |
populate_ctm_from_tm(struct cobol_tm &ctm, const struct tm &tm) |
| 631 |
{ |
| 632 |
ctm.YYYY = tm.tm_year + 1900; |
| 633 |
ctm.MM = tm.tm_mon + 1; |
| 634 |
ctm.DD = tm.tm_mday ; |
| 635 |
ctm.hh = tm.tm_hour; |
| 636 |
ctm.mm = tm.tm_min; |
| 637 |
ctm.ss = tm.tm_sec; |
| 638 |
ctm.days_in_year = is_leap_year(ctm.YYYY); |
| 639 |
ctm.weeks_in_year = weeks_in_year(ctm.YYYY); |
| 640 |
|
| 641 |
double JD = YMD_to_JD(ctm.YYYY, ctm.MM, ctm.DD); |
| 642 |
ctm.day_of_week = JD_to_DOW(JD); |
| 643 |
|
| 644 |
double JD_Jan4 = YMD_to_JD(ctm.YYYY, 1, 4); |
| 645 |
ctm.day_of_year = (int)(JD - (JD_Jan4-4)); |
| 646 |
|
| 647 |
int dow_Jan4 = JD_to_DOW(JD_Jan4); |
| 648 |
double adjusted_starting_date = JD_Jan4 - dow_Jan4; |
| 649 |
|
| 650 |
int adjusted_days = (int)(JD - adjusted_starting_date); |
| 651 |
if( adjusted_days >= 0 ) |
| 652 |
{ |
| 653 |
int week_of_year = adjusted_days/7 + 1; |
| 654 |
if(week_of_year > ctm.weeks_in_year) |
| 655 |
{ |
| 656 |
ctm.ZZZZ = ctm.YYYY+1; |
| 657 |
ctm.week_of_year = 1; |
| 658 |
} |
| 659 |
else |
| 660 |
{ |
| 661 |
ctm.ZZZZ = ctm.YYYY; |
| 662 |
ctm.week_of_year = week_of_year; |
| 663 |
} |
| 664 |
} |
| 665 |
else |
| 666 |
{ |
| 667 |
ctm.ZZZZ = ctm.YYYY - 1; |
| 668 |
ctm.week_of_year = weeks_in_year(ctm.ZZZZ); |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
static |
| 673 |
void |
| 674 |
populate_ctm_from_JD(struct cobol_tm &ctm, double JD ) |
| 675 |
{ |
| 676 |
// Extract the year, month, and day |
| 677 |
int Y; |
| 678 |
int M; |
| 679 |
int D; |
| 680 |
JD += JD_OF_1601_01_02; |
| 681 |
JD_to_YMD(Y, M, D, JD); |
| 682 |
|
| 683 |
struct tm tm = {}; |
| 684 |
tm.tm_mday = D; |
| 685 |
tm.tm_mon = M-1; |
| 686 |
tm.tm_year = Y-1900; |
| 687 |
populate_ctm_from_tm(ctm, tm); |
| 688 |
} |
| 689 |
|
| 690 |
static |
| 691 |
void |
| 692 |
populate_ctm_from_date( struct cobol_tm &ctm, |
| 693 |
const cblc_field_t *pdate, |
| 694 |
size_t pdate_offset, |
| 695 |
size_t pdate_size) |
| 696 |
{ |
| 697 |
// Get the date as an integer |
| 698 |
int rdigits; |
| 699 |
double JD = (double)__gg__binary_value_from_qualified_field(&rdigits, |
| 700 |
pdate, |
| 701 |
pdate_offset, |
| 702 |
pdate_size); |
| 703 |
populate_ctm_from_JD(ctm, JD); |
| 704 |
} |
| 705 |
|
| 706 |
static |
| 707 |
void |
| 708 |
populate_ctm_from_double_time(struct cobol_tm &ctm, double time) |
| 709 |
{ |
| 710 |
// Get hours, minutes, and seconds |
| 711 |
double intpart; |
| 712 |
double fracpart = modf(time, &intpart); |
| 713 |
|
| 714 |
int hour = (int)intpart; |
| 715 |
int second = hour % 60; |
| 716 |
int minute = (hour / 60) % 60; |
| 717 |
hour = hour / 3600; |
| 718 |
ctm.ss = second; |
| 719 |
ctm.mm = minute; |
| 720 |
ctm.hh = hour; |
| 721 |
ctm.nanoseconds = (int)(fracpart * 1000000000 + 0.5); |
| 722 |
} |
| 723 |
|
| 724 |
static |
| 725 |
void |
| 726 |
populate_ctm_from_time( struct cobol_tm &ctm, |
| 727 |
const cblc_field_t *ptime, |
| 728 |
size_t ptime_o, |
| 729 |
size_t ptime_s, |
| 730 |
const cblc_field_t *poffset, |
| 731 |
size_t poffset_o, |
| 732 |
size_t poffset_s) |
| 733 |
{ |
| 734 |
double time = get_value_as_double_from_qualified_field( ptime, |
| 735 |
ptime_o, |
| 736 |
ptime_s); |
| 737 |
populate_ctm_from_double_time(ctm, time); |
| 738 |
|
| 739 |
if( poffset ) |
| 740 |
{ |
| 741 |
int rdigits; |
| 742 |
int value = (int)__gg__binary_value_from_qualified_field(&rdigits, |
| 743 |
poffset, |
| 744 |
poffset_o, |
| 745 |
poffset_s); |
| 746 |
if( rdigits ) |
| 747 |
{ |
| 748 |
value /= __gg__power_of_ten(rdigits); |
| 749 |
rdigits = 0; |
| 750 |
} |
| 751 |
ctm.tz_offset = value; |
| 752 |
if( abs(value) >= 1440 ) |
| 753 |
{ |
| 754 |
exception_raise(ec_argument_function_e); |
| 755 |
} |
| 756 |
} |
| 757 |
else |
| 758 |
{ |
| 759 |
ctm.tz_offset = 0; |
| 760 |
} |
| 761 |
} |
| 762 |
|
| 763 |
static void |
| 764 |
convert_to_zulu(cobol_tm &ctm) |
| 765 |
{ |
| 766 |
// Get the Julian Day |
| 767 |
double JD = YMD_to_JD(ctm.YYYY, |
| 768 |
ctm.MM, |
| 769 |
ctm.DD); |
| 770 |
// Get the time in seconds past midnight |
| 771 |
double seconds_past_midnight = ctm.hh * 3600 |
| 772 |
+ ctm.mm * 60 |
| 773 |
+ ctm.ss; |
| 774 |
// Subtract the UTC offset, which is given in minutes |
| 775 |
seconds_past_midnight -= ctm.tz_offset * 60; |
| 776 |
if( seconds_past_midnight < 0 ) |
| 777 |
{ |
| 778 |
JD -= 1; |
| 779 |
seconds_past_midnight += 86400; |
| 780 |
} |
| 781 |
else if( seconds_past_midnight >= 86400 ) |
| 782 |
{ |
| 783 |
JD += 1; |
| 784 |
seconds_past_midnight -= 86400; |
| 785 |
} |
| 786 |
JD -= JD_OF_1601_01_02; |
| 787 |
populate_ctm_from_JD(ctm, JD); |
| 788 |
populate_ctm_from_double_time(ctm, seconds_past_midnight); |
| 789 |
if( ctm.YYYY < 1601 ) |
| 790 |
{ |
| 791 |
ctm.YYYY = ctm.MM = ctm.DD = 0; |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
static |
| 796 |
void |
| 797 |
ftime_replace(char *dest, |
| 798 |
char const * const dest_end, |
| 799 |
char const * source, |
| 800 |
char const * const source_end, |
| 801 |
char const * const ftime) |
| 802 |
{ |
| 803 |
// This routine is highly dependent on the source format being correct. |
| 804 |
int ncount; |
| 805 |
const char *src; |
| 806 |
bool saw_decimal_point = false; |
| 807 |
bool saw_plus_sign = false; |
| 808 |
char decimal_point = __gg__get_decimal_point(); |
| 809 |
static const int OFFSET_TO_YYYY = 0; |
| 810 |
static const int OFFSET_TO_MM = 4; |
| 811 |
static const int OFFSET_TO_DD = 6; |
| 812 |
static const int OFFSET_TO_HOUR = 9; |
| 813 |
static const int OFFSET_TO_MINUTE = 11; |
| 814 |
static const int OFFSET_TO_SECOND = 13; |
| 815 |
static const int OFFSET_TO_FRACTION = 16; |
| 816 |
static const int OFFSET_TO_OFFSET = 25; |
| 817 |
static const int OFFSET_TO_OFFSET_HOUR = 26; |
| 818 |
static const int OFFSET_TO_OFFSET_MINUTE = 28; |
| 819 |
static const int OFFSET_TO_WEEK = 30; |
| 820 |
static const int OFFSET_TO_DOW = 33; |
| 821 |
static const int OFFSET_TO_DOY = 34; |
| 822 |
static const int OFFSET_TO_ZZZZ = 37; |
| 823 |
while( source < source_end && dest < dest_end ) |
| 824 |
{ |
| 825 |
char fchar = *source; |
| 826 |
if( fchar == internal_Y ) |
| 827 |
{ |
| 828 |
// This can only be a YYYY |
| 829 |
// But, we have a choice. If there is a 'W' in the format, then we |
| 830 |
// need to use ZZZZ rather than YYYY: |
| 831 |
src = ftime + OFFSET_TO_YYYY; |
| 832 |
const char *p = source; |
| 833 |
while(p < source_end) |
| 834 |
{ |
| 835 |
if( *p++ == internal_W ) |
| 836 |
{ |
| 837 |
src = ftime + OFFSET_TO_ZZZZ; |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
ncount = 4; |
| 842 |
} |
| 843 |
else if( fchar == internal_M ) |
| 844 |
{ |
| 845 |
// This can only be a MM |
| 846 |
ncount = 2; |
| 847 |
src = ftime + OFFSET_TO_MM; |
| 848 |
} |
| 849 |
else if( fchar == internal_D ) |
| 850 |
{ |
| 851 |
// It can be a D, DD or DDD |
| 852 |
if( source[2] == internal_D ) |
| 853 |
{ |
| 854 |
ncount = 3; |
| 855 |
src = ftime + OFFSET_TO_DOY; |
| 856 |
} |
| 857 |
else if( source[1] == internal_D ) |
| 858 |
{ |
| 859 |
ncount = 2; |
| 860 |
src = ftime + OFFSET_TO_DD; |
| 861 |
} |
| 862 |
else |
| 863 |
{ |
| 864 |
ncount = 1; |
| 865 |
src = ftime + OFFSET_TO_DOW; |
| 866 |
} |
| 867 |
} |
| 868 |
else if( fchar == internal_plus ) |
| 869 |
{ |
| 870 |
saw_plus_sign = true; |
| 871 |
ncount = 1; |
| 872 |
src = ftime + OFFSET_TO_OFFSET; |
| 873 |
} |
| 874 |
else if( fchar == internal_h ) |
| 875 |
{ |
| 876 |
ncount = 2; |
| 877 |
if(saw_plus_sign) |
| 878 |
{ |
| 879 |
src = ftime + OFFSET_TO_OFFSET_HOUR; |
| 880 |
} |
| 881 |
else |
| 882 |
{ |
| 883 |
src = ftime + OFFSET_TO_HOUR; |
| 884 |
} |
| 885 |
} |
| 886 |
else if( fchar == internal_m ) |
| 887 |
{ |
| 888 |
ncount = 2; |
| 889 |
if(saw_plus_sign) |
| 890 |
{ |
| 891 |
src = ftime + OFFSET_TO_OFFSET_MINUTE; |
| 892 |
} |
| 893 |
else |
| 894 |
{ |
| 895 |
src = ftime + OFFSET_TO_MINUTE; |
| 896 |
} |
| 897 |
} |
| 898 |
else if( fchar == decimal_point ) |
| 899 |
{ |
| 900 |
saw_decimal_point = true; |
| 901 |
ncount = 1; |
| 902 |
src = source; |
| 903 |
} |
| 904 |
else if( fchar == internal_s ) |
| 905 |
{ |
| 906 |
if(saw_decimal_point) |
| 907 |
{ |
| 908 |
// There can be a variable number of fractional 's' |
| 909 |
ncount = -1; |
| 910 |
src = ftime + OFFSET_TO_FRACTION; |
| 911 |
} |
| 912 |
else |
| 913 |
{ |
| 914 |
ncount = 2; |
| 915 |
src = ftime + OFFSET_TO_SECOND; |
| 916 |
} |
| 917 |
} |
| 918 |
else if( fchar == internal_W ) |
| 919 |
{ |
| 920 |
ncount = 3; |
| 921 |
src = ftime + OFFSET_TO_WEEK; |
| 922 |
} |
| 923 |
else |
| 924 |
{ |
| 925 |
ncount = 1; |
| 926 |
src = source; |
| 927 |
} |
| 928 |
|
| 929 |
// Copy over the ncount characters to dest |
| 930 |
if( ncount == -1 ) |
| 931 |
{ |
| 932 |
// This indicates special processing for a variable number of 's' |
| 933 |
// characters |
| 934 |
while(*source == 's' && dest < dest_end) |
| 935 |
{ |
| 936 |
source += 1; |
| 937 |
*dest++ = *src++; |
| 938 |
} |
| 939 |
} |
| 940 |
else |
| 941 |
{ |
| 942 |
source += ncount; |
| 943 |
while(ncount-- && dest < dest_end) |
| 944 |
{ |
| 945 |
*dest++ = *src++; |
| 946 |
} |
| 947 |
} |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
// |
| 952 |
// |
| 953 |
// Beyond this point, we are implementing phase 2 of intrinsics. These routines |
| 954 |
// are intended to be "better" than the ones above. In an ideal world, |
| 955 |
// eventually all of the above routines will migrate down here, and this comment |
| 956 |
// will be removed. Bob Dubner, 2023-01-18 |
| 957 |
|
| 958 |
// Although not, of course, necessary, these routines are being placed in |
| 959 |
// alphabetical order by the COBOL function name: |
| 960 |
|
| 961 |
extern "C" |
| 962 |
void |
| 963 |
__gg__abs(cblc_field_t *dest, |
| 964 |
const cblc_field_t *source, |
| 965 |
size_t source_offset, |
| 966 |
size_t source_size) |
| 967 |
{ |
| 968 |
// FUNCTION ABS |
| 969 |
GCOB_FP128 value; |
| 970 |
value = __gg__float128_from_qualified_field(source, |
| 971 |
source_offset, |
| 972 |
source_size); |
| 973 |
if( value < 0 ) |
| 974 |
{ |
| 975 |
value = -value; |
| 976 |
} |
| 977 |
__gg__float128_to_field(dest, |
| 978 |
value, |
| 979 |
truncation_e, |
| 980 |
NULL); |
| 981 |
} |
| 982 |
|
| 983 |
extern "C" |
| 984 |
void |
| 985 |
__gg__acos( cblc_field_t *dest, |
| 986 |
const cblc_field_t *source, |
| 987 |
size_t source_offset, |
| 988 |
size_t source_size) |
| 989 |
{ |
| 990 |
// FUNCTION ACOS |
| 991 |
GCOB_FP128 value; |
| 992 |
value = __gg__float128_from_qualified_field(source, source_offset, source_size); |
| 993 |
|
| 994 |
if( value < GCOB_FP128_LITERAL(-1.00) || value > GCOB_FP128_LITERAL(+1.00) ) |
| 995 |
{ |
| 996 |
exception_raise(ec_argument_function_e); |
| 997 |
value = WEIRD_TRANSCENDENT_RETURN_VALUE; |
| 998 |
} |
| 999 |
else |
| 1000 |
{ |
| 1001 |
value = FP128_FUNC(acos)(value); |
| 1002 |
} |
| 1003 |
|
| 1004 |
__gg__float128_to_field( dest, |
| 1005 |
value, |
| 1006 |
truncation_e, |
| 1007 |
NULL); |
| 1008 |
} |
| 1009 |
|
| 1010 |
extern "C" |
| 1011 |
void |
| 1012 |
__gg__annuity(cblc_field_t *dest, |
| 1013 |
const cblc_field_t *arg1, |
| 1014 |
size_t arg1_offset, |
| 1015 |
size_t arg1_size, |
| 1016 |
const cblc_field_t *arg2, |
| 1017 |
size_t arg2_offset, |
| 1018 |
size_t arg2_size) |
| 1019 |
{ |
| 1020 |
// FUNCTION ANNUITY |
| 1021 |
|
| 1022 |
GCOB_FP128 retval = 0; |
| 1023 |
|
| 1024 |
GCOB_FP128 val1 = FP128_FUNC(fabs)(__gg__float128_from_qualified_field(arg1, |
| 1025 |
arg1_offset, |
| 1026 |
arg1_size)); |
| 1027 |
GCOB_FP128 val2 = FP128_FUNC(fabs)(__gg__float128_from_qualified_field(arg2, |
| 1028 |
arg2_offset, |
| 1029 |
arg2_size)); |
| 1030 |
if( val2 > 0) |
| 1031 |
{ |
| 1032 |
if( val1 < 0 ) |
| 1033 |
{ |
| 1034 |
exception_raise(ec_argument_function_e); |
| 1035 |
} |
| 1036 |
else if( val1 == 0 ) |
| 1037 |
{ |
| 1038 |
retval = 1/val2; |
| 1039 |
} |
| 1040 |
else |
| 1041 |
{ |
| 1042 |
retval = val1 / (1- FP128_FUNC(pow)( (1+val1), -val2 )); |
| 1043 |
} |
| 1044 |
} |
| 1045 |
else |
| 1046 |
{ |
| 1047 |
exception_raise(ec_argument_function_e); |
| 1048 |
} |
| 1049 |
__gg__float128_to_field(dest, |
| 1050 |
retval, |
| 1051 |
truncation_e, |
| 1052 |
NULL); |
| 1053 |
} |
| 1054 |
|
| 1055 |
extern "C" |
| 1056 |
void |
| 1057 |
__gg__asin( cblc_field_t *dest, |
| 1058 |
const cblc_field_t *source, |
| 1059 |
size_t source_offset, |
| 1060 |
size_t source_size) |
| 1061 |
{ |
| 1062 |
// FUNCTION ASIN |
| 1063 |
|
| 1064 |
GCOB_FP128 value; |
| 1065 |
value = __gg__float128_from_qualified_field(source, |
| 1066 |
source_offset, |
| 1067 |
source_size); |
| 1068 |
|
| 1069 |
if( value < GCOB_FP128_LITERAL(-1.0) || value > GCOB_FP128_LITERAL(+1.00) ) |
| 1070 |
{ |
| 1071 |
exception_raise(ec_argument_function_e); |
| 1072 |
value = WEIRD_TRANSCENDENT_RETURN_VALUE; |
| 1073 |
} |
| 1074 |
else |
| 1075 |
{ |
| 1076 |
value = FP128_FUNC(asin)(value); |
| 1077 |
} |
| 1078 |
|
| 1079 |
__gg__float128_to_field( dest, |
| 1080 |
value, |
| 1081 |
truncation_e, |
| 1082 |
NULL); |
| 1083 |
} |
| 1084 |
|
| 1085 |
extern "C" |
| 1086 |
void |
| 1087 |
__gg__atan( cblc_field_t *dest, |
| 1088 |
const cblc_field_t *source, |
| 1089 |
size_t source_offset, |
| 1090 |
size_t source_size) |
| 1091 |
{ |
| 1092 |
// FUNCTION ATAN |
| 1093 |
|
| 1094 |
GCOB_FP128 value; |
| 1095 |
value = __gg__float128_from_qualified_field(source, |
| 1096 |
source_offset, |
| 1097 |
source_size); |
| 1098 |
|
| 1099 |
value = FP128_FUNC(atan)(value); |
| 1100 |
|
| 1101 |
__gg__float128_to_field( dest, |
| 1102 |
value, |
| 1103 |
truncation_e, |
| 1104 |
NULL); |
| 1105 |
} |
| 1106 |
|
| 1107 |
extern "C" |
| 1108 |
void |
| 1109 |
__gg__byte_length(cblc_field_t *dest, |
| 1110 |
const cblc_field_t */*source*/, |
| 1111 |
size_t /*source_offset*/, |
| 1112 |
size_t source_size) |
| 1113 |
{ |
| 1114 |
// FUNCTION BYTE-LENGTH |
| 1115 |
__int128 value = source_size; |
| 1116 |
__gg__int128_to_field(dest, |
| 1117 |
value, |
| 1118 |
NO_RDIGITS, |
| 1119 |
truncation_e, |
| 1120 |
NULL); |
| 1121 |
} |
| 1122 |
|
| 1123 |
extern "C" |
| 1124 |
void |
| 1125 |
__gg__char( cblc_field_t *dest, |
| 1126 |
const cblc_field_t *source, |
| 1127 |
size_t source_offset, |
| 1128 |
size_t source_size) |
| 1129 |
{ |
| 1130 |
int rdigits; |
| 1131 |
|
| 1132 |
// The CHAR function takes an integer, the ordinal position. It |
| 1133 |
// returns a single-character string, which is the character at that |
| 1134 |
// ordinal position. |
| 1135 |
|
| 1136 |
// 'A', with the ascii value of 65, is at the ordinal position 66. |
| 1137 |
|
| 1138 |
int ordinal = (int)(__gg__binary_value_from_qualified_field(&rdigits, |
| 1139 |
source, |
| 1140 |
source_offset, |
| 1141 |
source_size)); |
| 1142 |
ordinal /= __gg__power_of_ten(rdigits); |
| 1143 |
int ch = ordinal-1; |
| 1144 |
memset(dest->data, internal_space, dest->capacity); |
| 1145 |
dest->data[0] = ch; |
| 1146 |
} |
| 1147 |
|
| 1148 |
extern "C" |
| 1149 |
void |
| 1150 |
__gg__combined_datetime(cblc_field_t *dest, |
| 1151 |
const cblc_field_t *arg1, |
| 1152 |
size_t arg1_offset, |
| 1153 |
size_t arg1_size, |
| 1154 |
const cblc_field_t *arg2, |
| 1155 |
size_t arg2_offset, |
| 1156 |
size_t arg2_size) |
| 1157 |
{ |
| 1158 |
int rdigits; |
| 1159 |
|
| 1160 |
__int128 val1 = (int)(__gg__binary_value_from_qualified_field(&rdigits, |
| 1161 |
arg1, |
| 1162 |
arg1_offset, |
| 1163 |
arg1_size)); |
| 1164 |
__int128 val2 = (int)(__gg__binary_value_from_qualified_field(&rdigits, |
| 1165 |
arg2, |
| 1166 |
arg2_offset, |
| 1167 |
arg2_size)); |
| 1168 |
__int128 value = val1 * 1000000 + val2; |
| 1169 |
__gg__int128_to_field(dest, |
| 1170 |
value, |
| 1171 |
6, |
| 1172 |
truncation_e, |
| 1173 |
NULL); |
| 1174 |
} |
| 1175 |
|
| 1176 |
extern "C" |
| 1177 |
void |
| 1178 |
__gg__concat( cblc_field_t *dest, |
| 1179 |
size_t ncount) |
| 1180 |
{ |
| 1181 |
size_t bytes = 0; |
| 1182 |
size_t offset = 0; |
| 1183 |
for(size_t i=0; i<ncount; i++) |
| 1184 |
{ |
| 1185 |
bytes += __gg__treeplet_1s[i]; |
| 1186 |
} |
| 1187 |
__gg__adjust_dest_size(dest, bytes); |
| 1188 |
for(size_t i=0; i<ncount; i++) |
| 1189 |
{ |
| 1190 |
memcpy( dest->data + offset, |
| 1191 |
__gg__treeplet_1f[i]->data + __gg__treeplet_1o[i], |
| 1192 |
__gg__treeplet_1s[i]); |
| 1193 |
offset += __gg__treeplet_1s[i]; |
| 1194 |
} |
| 1195 |
} |
| 1196 |
|
| 1197 |
extern "C" |
| 1198 |
void |
| 1199 |
__gg__cos(cblc_field_t *dest, |
| 1200 |
const cblc_field_t *source, |
| 1201 |
size_t source_offset, |
| 1202 |
size_t source_size) |
| 1203 |
{ |
| 1204 |
// FUNCTION COS |
| 1205 |
|
| 1206 |
GCOB_FP128 value = __gg__float128_from_qualified_field(source, |
| 1207 |
source_offset, |
| 1208 |
source_size); |
| 1209 |
value = FP128_FUNC(cos)(value); |
| 1210 |
__gg__float128_to_field(dest, |
| 1211 |
value, |
| 1212 |
truncation_e, |
| 1213 |
NULL); |
| 1214 |
} |
| 1215 |
|
| 1216 |
extern "C" |
| 1217 |
void |
| 1218 |
__gg__current_date(cblc_field_t *dest) |
| 1219 |
{ |
| 1220 |
// FUNCTION CURRENT-DATE |
| 1221 |
struct cbl_timespec tp = {}; |
| 1222 |
__gg__clock_gettime(&tp); // time_t tv_sec; long tv_nsec |
| 1223 |
|
| 1224 |
char retval[DATE_STRING_BUFFER_SIZE]; |
| 1225 |
timespec_to_string(retval, tp); |
| 1226 |
ascii_to_internal_str(retval, strlen(retval)); |
| 1227 |
string_to_dest(dest, retval); |
| 1228 |
} |
| 1229 |
|
| 1230 |
extern "C" |
| 1231 |
void |
| 1232 |
__gg__seconds_past_midnight(cblc_field_t *dest) |
| 1233 |
{ |
| 1234 |
// SECONDS-PAST-MIDNIGHT |
| 1235 |
struct cbl_timespec tp = {}; |
| 1236 |
struct tm tm; |
| 1237 |
__int128 retval=0; |
| 1238 |
|
| 1239 |
__gg__clock_gettime(&tp); // time_t tv_sec; long tv_nsec |
| 1240 |
localtime_r(&tp.tv_sec, &tm); |
| 1241 |
|
| 1242 |
retval += tm.tm_hour; |
| 1243 |
retval *= 60; |
| 1244 |
retval += tm.tm_min; |
| 1245 |
retval *= 60; |
| 1246 |
retval += tm.tm_sec; |
| 1247 |
retval *= 1000000000; |
| 1248 |
retval += tp.tv_nsec; |
| 1249 |
__gg__int128_to_field(dest, |
| 1250 |
retval, |
| 1251 |
9, |
| 1252 |
truncation_e, |
| 1253 |
NULL); |
| 1254 |
} |
| 1255 |
|
| 1256 |
extern "C" |
| 1257 |
void |
| 1258 |
__gg__date_of_integer(cblc_field_t *dest, |
| 1259 |
const cblc_field_t *source, |
| 1260 |
size_t source_offset, |
| 1261 |
size_t source_size) |
| 1262 |
{ |
| 1263 |
// FUNCTION DATE-OF-INTEGER |
| 1264 |
int rdigits; |
| 1265 |
double JD = (double)__gg__binary_value_from_qualified_field(&rdigits, |
| 1266 |
source, |
| 1267 |
source_offset, |
| 1268 |
source_size); |
| 1269 |
JD += JD_OF_1601_01_02; |
| 1270 |
int Y; |
| 1271 |
int M; |
| 1272 |
int D; |
| 1273 |
JD_to_YMD(Y, M, D, JD); |
| 1274 |
int retval = Y*10000 + M*100 + D; |
| 1275 |
__gg__int128_to_field(dest, |
| 1276 |
retval, |
| 1277 |
NO_RDIGITS, |
| 1278 |
truncation_e, |
| 1279 |
NULL); |
| 1280 |
} |
| 1281 |
|
| 1282 |
extern "C" |
| 1283 |
void |
| 1284 |
__gg__date_to_yyyymmdd( cblc_field_t *dest, |
| 1285 |
const cblc_field_t *par1, |
| 1286 |
size_t par1_o, |
| 1287 |
size_t par1_s, |
| 1288 |
const cblc_field_t *par2, |
| 1289 |
size_t par2_o, |
| 1290 |
size_t par2_s, |
| 1291 |
const cblc_field_t *par3, |
| 1292 |
size_t par3_o, |
| 1293 |
size_t par3_s) |
| 1294 |
{ |
| 1295 |
// FUNCTION DATE-TO-YYYYMMDD |
| 1296 |
// See the discussion in ISO/IEC 2014-1989 Section 15.20 |
| 1297 |
int rdigits; |
| 1298 |
int arg1 = (int)__gg__binary_value_from_qualified_field(&rdigits, par1, par1_o, par1_s); |
| 1299 |
int arg2 = (int)__gg__binary_value_from_qualified_field(&rdigits, par2, par2_o, par2_s ); |
| 1300 |
int arg3 = (int)__gg__binary_value_from_qualified_field(&rdigits, par3, par3_o, par3_s); |
| 1301 |
|
| 1302 |
int yy = arg1/10000; |
| 1303 |
int mmdd = arg1%10000; |
| 1304 |
|
| 1305 |
int retval = year_to_yyyy(yy, arg2, arg3) * 10000 + mmdd; |
| 1306 |
__gg__int128_to_field(dest, |
| 1307 |
retval, |
| 1308 |
NO_RDIGITS, |
| 1309 |
truncation_e, |
| 1310 |
NULL); |
| 1311 |
} |
| 1312 |
|
| 1313 |
extern "C" |
| 1314 |
void |
| 1315 |
__gg__day_of_integer( cblc_field_t *dest, |
| 1316 |
const cblc_field_t *source, |
| 1317 |
size_t source_offset, |
| 1318 |
size_t source_size) |
| 1319 |
{ |
| 1320 |
// FUNCTION DAY-OF_INTEGER |
| 1321 |
int rdigits; |
| 1322 |
double JD = (double)__gg__binary_value_from_qualified_field(&rdigits, |
| 1323 |
source, |
| 1324 |
source_offset, |
| 1325 |
source_size); |
| 1326 |
JD += JD_OF_1601_01_02; |
| 1327 |
int Y; |
| 1328 |
int M; |
| 1329 |
int D; |
| 1330 |
JD_to_YMD(Y, M, D, JD); |
| 1331 |
|
| 1332 |
double start_of_year = YMD_to_JD(Y, 1, 1); |
| 1333 |
|
| 1334 |
__int128 retval = Y * 1000 + int(JD - start_of_year) + 1; |
| 1335 |
__gg__int128_to_field(dest, |
| 1336 |
retval, |
| 1337 |
NO_RDIGITS, |
| 1338 |
truncation_e, |
| 1339 |
NULL); |
| 1340 |
} |
| 1341 |
|
| 1342 |
extern "C" |
| 1343 |
void |
| 1344 |
__gg__day_to_yyyyddd( cblc_field_t *dest, |
| 1345 |
const cblc_field_t *par1, |
| 1346 |
size_t par1_o, |
| 1347 |
size_t par1_s, |
| 1348 |
const cblc_field_t *par2, |
| 1349 |
size_t par2_o, |
| 1350 |
size_t par2_s, |
| 1351 |
const cblc_field_t *par3, |
| 1352 |
size_t par3_o, |
| 1353 |
size_t par3_s) |
| 1354 |
{ |
| 1355 |
// FUNCTION DAY-TO-YYYYDDD |
| 1356 |
// See the discussion in ISO/IEC 2014-1989 Section 15.20 |
| 1357 |
int rdigits; |
| 1358 |
int arg1 = (int)__gg__binary_value_from_qualified_field(&rdigits, par1, par1_o, par1_s); |
| 1359 |
int arg2 = (int)__gg__binary_value_from_qualified_field(&rdigits, par2, par2_o, par2_s ); |
| 1360 |
int arg3 = (int)__gg__binary_value_from_qualified_field(&rdigits, par3, par3_o, par3_s); |
| 1361 |
|
| 1362 |
int yy = arg1/1000; |
| 1363 |
int ddd = arg1%1000; |
| 1364 |
|
| 1365 |
int retval = year_to_yyyy(yy, arg2, arg3) * 1000 + ddd; |
| 1366 |
|
| 1367 |
__gg__int128_to_field(dest, |
| 1368 |
retval, |
| 1369 |
NO_RDIGITS, |
| 1370 |
truncation_e, |
| 1371 |
NULL); |
| 1372 |
} |
| 1373 |
|
| 1374 |
extern "C" |
| 1375 |
void |
| 1376 |
__gg__e(cblc_field_t *dest) |
| 1377 |
{ |
| 1378 |
// FUNCTION E |
| 1379 |
static GCOB_FP128 e |
| 1380 |
= GCOB_FP128_LITERAL(2.7182818284590452353602874713526624977572); |
| 1381 |
__gg__float128_to_field(dest, |
| 1382 |
e, |
| 1383 |
truncation_e, |
| 1384 |
NULL); |
| 1385 |
} |
| 1386 |
|
| 1387 |
extern "C" |
| 1388 |
void |
| 1389 |
__gg__exp(cblc_field_t *dest, |
| 1390 |
const cblc_field_t *source, |
| 1391 |
size_t source_offset, |
| 1392 |
size_t source_size) |
| 1393 |
{ |
| 1394 |
// FUNCTION EXP |
| 1395 |
|
| 1396 |
GCOB_FP128 value = __gg__float128_from_qualified_field(source, |
| 1397 |
source_offset, |
| 1398 |
source_size); |
| 1399 |
value = FP128_FUNC(exp)(value); |
| 1400 |
__gg__float128_to_field(dest, |
| 1401 |
value, |
| 1402 |
truncation_e, |
| 1403 |
NULL); |
| 1404 |
} |
| 1405 |
|
| 1406 |
extern "C" |
| 1407 |
void |
| 1408 |
__gg__exp10(cblc_field_t *dest, |
| 1409 |
const cblc_field_t *source, |
| 1410 |
size_t source_offset, |
| 1411 |
size_t source_size) |
| 1412 |
{ |
| 1413 |
// FUNCTION EXP10 |
| 1414 |
|
| 1415 |
GCOB_FP128 value = __gg__float128_from_qualified_field(source, |
| 1416 |
source_offset, |
| 1417 |
source_size); |
| 1418 |
value = FP128_FUNC(pow)(GCOB_FP128_LITERAL(10.0), value); |
| 1419 |
__gg__float128_to_field(dest, |
| 1420 |
value, |
| 1421 |
truncation_e, |
| 1422 |
NULL); |
| 1423 |
} |
| 1424 |
|
| 1425 |
extern "C" |
| 1426 |
void |
| 1427 |
__gg__factorial(cblc_field_t *dest, |
| 1428 |
const cblc_field_t *source, |
| 1429 |
size_t source_offset, |
| 1430 |
size_t source_size) |
| 1431 |
{ |
| 1432 |
// FUNCTION FACTORIAL |
| 1433 |
int rdigits; |
| 1434 |
int N = (int)__gg__binary_value_from_qualified_field( &rdigits, |
| 1435 |
source, |
| 1436 |
source_offset, |
| 1437 |
source_size); |
| 1438 |
while(rdigits--) |
| 1439 |
{ |
| 1440 |
N /= 10; |
| 1441 |
} |
| 1442 |
|
| 1443 |
__int128 retval = 1; |
| 1444 |
|
| 1445 |
while( N > 1 ) |
| 1446 |
{ |
| 1447 |
retval *= N--; |
| 1448 |
} |
| 1449 |
__gg__int128_to_field(dest, |
| 1450 |
retval, |
| 1451 |
NO_RDIGITS, |
| 1452 |
truncation_e, |
| 1453 |
NULL); |
| 1454 |
} |
| 1455 |
|
| 1456 |
extern "C" |
| 1457 |
void |
| 1458 |
__gg__formatted_current_date( cblc_field_t *dest, // Destination string |
| 1459 |
const cblc_field_t *input, // datetime format |
| 1460 |
size_t input_offset, |
| 1461 |
size_t input_size) |
| 1462 |
{ |
| 1463 |
// FUNCTION FORMATTED-CURRENT-DATE |
| 1464 |
|
| 1465 |
// Establish the destination, and set it to spaces |
| 1466 |
char *d = PTRCAST(char, dest->data); |
| 1467 |
const char *dend = d + dest->capacity; |
| 1468 |
memset(d, internal_space, dest->capacity); |
| 1469 |
|
| 1470 |
// Establish the formatting string: |
| 1471 |
const char *format = PTRCAST(char, (input->data+input_offset)); |
| 1472 |
const char *format_end = format + input_size; |
| 1473 |
|
| 1474 |
bool is_zulu = false; |
| 1475 |
|
| 1476 |
const char *p = format; |
| 1477 |
while( p < format_end ) |
| 1478 |
{ |
| 1479 |
int ch = *p++; |
| 1480 |
if( ch == internal_Z ) |
| 1481 |
{ |
| 1482 |
is_zulu = true; |
| 1483 |
break; |
| 1484 |
} |
| 1485 |
} |
| 1486 |
|
| 1487 |
struct cbl_timespec ts = {}; |
| 1488 |
__gg__clock_gettime(&ts); |
| 1489 |
|
| 1490 |
struct tm tm = {}; |
| 1491 |
#ifdef HAVE_STRUCT_TM_TM_ZONE |
| 1492 |
tm.tm_zone = "GMT"; |
| 1493 |
#endif |
| 1494 |
if( is_zulu ) |
| 1495 |
{ |
| 1496 |
gmtime_r(&ts.tv_sec, &tm); |
| 1497 |
} |
| 1498 |
else |
| 1499 |
{ |
| 1500 |
localtime_r(&ts.tv_sec, &tm); |
| 1501 |
} |
| 1502 |
|
| 1503 |
struct cobol_tm ctm = {}; |
| 1504 |
populate_ctm_from_tm(ctm, tm); |
| 1505 |
|
| 1506 |
ctm.nanoseconds = ts.tv_nsec; |
| 1507 |
|
| 1508 |
tzset(); |
| 1509 |
// Convert seconds west of UTC to minutes east of UTC |
| 1510 |
ctm.tz_offset = -timezone/60; |
| 1511 |
|
| 1512 |
char achftime[64]; |
| 1513 |
get_all_time(achftime, ctm); |
| 1514 |
ftime_replace(d, dend, format, format_end, achftime); |
| 1515 |
} |
| 1516 |
|
| 1517 |
extern "C" |
| 1518 |
void |
| 1519 |
__gg__formatted_date(cblc_field_t *dest, // Destination string |
| 1520 |
const cblc_field_t *arg1, // datetime format |
| 1521 |
size_t arg1_offset, |
| 1522 |
size_t arg1_size, |
| 1523 |
const cblc_field_t *arg2, // integer date |
| 1524 |
size_t arg2_offset, |
| 1525 |
size_t arg2_size) |
| 1526 |
{ |
| 1527 |
// FUNCTION FORMATTED-DATE |
| 1528 |
|
| 1529 |
// Establish the destination, and set it to spaces |
| 1530 |
char *d = PTRCAST(char, dest->data); |
| 1531 |
const char *dend = d + dest->capacity; |
| 1532 |
memset(d, internal_space, dest->capacity); |
| 1533 |
|
| 1534 |
// Establish the formatting string: |
| 1535 |
char *format = PTRCAST(char, (arg1->data+arg1_offset)); |
| 1536 |
const char *format_end = format + arg1_size; |
| 1537 |
|
| 1538 |
struct cobol_tm ctm = {}; |
| 1539 |
|
| 1540 |
populate_ctm_from_date(ctm, arg2, arg2_offset, arg2_size); |
| 1541 |
|
| 1542 |
char achftime[64]; |
| 1543 |
get_all_time(achftime, ctm); |
| 1544 |
if( __gg__exception_code ) |
| 1545 |
{ |
| 1546 |
memset(d, internal_space, dend-d); |
| 1547 |
} |
| 1548 |
else |
| 1549 |
{ |
| 1550 |
ftime_replace(d, dend, format, format_end, achftime); |
| 1551 |
__gg__adjust_dest_size(dest, format_end-format); |
| 1552 |
} |
| 1553 |
} |
| 1554 |
|
| 1555 |
extern "C" |
| 1556 |
void |
| 1557 |
__gg__formatted_datetime( cblc_field_t *dest, // Destination string |
| 1558 |
const cblc_field_t *par1, // datetime format |
| 1559 |
size_t par1_o, |
| 1560 |
size_t par1_s, |
| 1561 |
const cblc_field_t *par2, // integer date |
| 1562 |
size_t par2_o, |
| 1563 |
size_t par2_s, |
| 1564 |
const cblc_field_t *par3, // numeric time |
| 1565 |
size_t par3_o, |
| 1566 |
size_t par3_s, |
| 1567 |
const cblc_field_t *par4, // optional offset in seconds |
| 1568 |
size_t par4_o, |
| 1569 |
size_t par4_s |
| 1570 |
) |
| 1571 |
{ |
| 1572 |
// FUNCTION FORMATTED-DATETIME |
| 1573 |
|
| 1574 |
// Establish the destination, and set it to spaces |
| 1575 |
char *d = PTRCAST(char, (dest->data)); |
| 1576 |
const char *dend = d + dest->capacity; |
| 1577 |
memset(d, internal_space, dest->capacity); |
| 1578 |
|
| 1579 |
// Establish the formatting string: |
| 1580 |
char *format = PTRCAST(char, (par1->data+par1_o)); |
| 1581 |
char *format_end = format + par1_s; |
| 1582 |
trim_trailing_spaces(format, format_end); |
| 1583 |
bool is_zulu = is_zulu_format(format, format_end); |
| 1584 |
|
| 1585 |
struct cobol_tm ctm = {}; |
| 1586 |
|
| 1587 |
populate_ctm_from_date(ctm, par2, par2_o, par2_s); |
| 1588 |
populate_ctm_from_time( ctm, |
| 1589 |
par3, par3_o, par3_s, |
| 1590 |
par4, par4_o, par4_s); |
| 1591 |
|
| 1592 |
if( is_zulu ) |
| 1593 |
{ |
| 1594 |
convert_to_zulu(ctm); |
| 1595 |
} |
| 1596 |
|
| 1597 |
char achftime[64]; |
| 1598 |
get_all_time(achftime, ctm); |
| 1599 |
if( __gg__exception_code ) |
| 1600 |
{ |
| 1601 |
memset(d, internal_space, dend-d); |
| 1602 |
} |
| 1603 |
else |
| 1604 |
{ |
| 1605 |
ftime_replace(d, dend, format, format_end, achftime); |
| 1606 |
__gg__adjust_dest_size(dest, format_end-format); |
| 1607 |
} |
| 1608 |
} |
| 1609 |
|
| 1610 |
extern "C" |
| 1611 |
void |
| 1612 |
__gg__formatted_time( cblc_field_t *dest,// Destination string |
| 1613 |
const cblc_field_t *par1, // datetime format |
| 1614 |
size_t par1_o, |
| 1615 |
size_t par1_s, |
| 1616 |
const cblc_field_t *par2,// numeric time |
| 1617 |
size_t par2_o, |
| 1618 |
size_t par2_s, |
| 1619 |
const cblc_field_t *par4, // optional offset in seconds |
| 1620 |
size_t par4_o, |
| 1621 |
size_t par4_s) |
| 1622 |
|
| 1623 |
{ |
| 1624 |
// FUNCTION FORMATTED-TIME |
| 1625 |
|
| 1626 |
// Establish the destination, and set it to spaces |
| 1627 |
char *d = PTRCAST(char, dest->data); |
| 1628 |
const char *dend = d + dest->capacity; |
| 1629 |
memset(d, internal_space, dest->capacity); |
| 1630 |
|
| 1631 |
// Establish the formatting string: |
| 1632 |
char *format = PTRCAST(char, (par1->data+par1_o)); |
| 1633 |
char *format_end = format + par1_s; |
| 1634 |
trim_trailing_spaces(format, format_end); |
| 1635 |
bool is_zulu = is_zulu_format(format, format_end); |
| 1636 |
|
| 1637 |
struct cobol_tm ctm = {}; |
| 1638 |
populate_ctm_from_time( ctm, |
| 1639 |
par2, |
| 1640 |
par2_o, |
| 1641 |
par2_s, |
| 1642 |
par4, |
| 1643 |
par4_o, |
| 1644 |
par4_s); |
| 1645 |
|
| 1646 |
if( is_zulu ) |
| 1647 |
{ |
| 1648 |
convert_to_zulu(ctm); |
| 1649 |
} |
| 1650 |
|
| 1651 |
char achftime[64]; |
| 1652 |
get_all_time(achftime, ctm); |
| 1653 |
if( __gg__exception_code ) |
| 1654 |
{ |
| 1655 |
memset(d, internal_space, dend-d); |
| 1656 |
} |
| 1657 |
else |
| 1658 |
{ |
| 1659 |
ftime_replace(d, dend, format, format_end, achftime); |
| 1660 |
__gg__adjust_dest_size(dest, format_end-format); |
| 1661 |
} |
| 1662 |
} |
| 1663 |
|
| 1664 |
extern "C" |
| 1665 |
void |
| 1666 |
__gg__integer(cblc_field_t *dest, |
| 1667 |
const cblc_field_t *source, |
| 1668 |
size_t source_offset, |
| 1669 |
size_t source_size) |
| 1670 |
{ |
| 1671 |
// FUNCTION INTEGER |
| 1672 |
GCOB_FP128 value = __gg__float128_from_qualified_field(source, |
| 1673 |
source_offset, |
| 1674 |
source_size); |
| 1675 |
value = FP128_FUNC(floor)(value); |
| 1676 |
__gg__float128_to_field(dest, |
| 1677 |
value, |
| 1678 |
truncation_e, |
| 1679 |
NULL); |
| 1680 |
} |
| 1681 |
|
| 1682 |
extern "C" |
| 1683 |
void |
| 1684 |
__gg__integer_of_date(cblc_field_t *dest, |
| 1685 |
const cblc_field_t *source, |
| 1686 |
size_t source_offset, |
| 1687 |
size_t source_size) |
| 1688 |
{ |
| 1689 |
// FUNCTION INTEGER-OF-DATE |
| 1690 |
int rdigits; |
| 1691 |
long argument_1 = (long)(__gg__binary_value_from_qualified_field(&rdigits, |
| 1692 |
source, |
| 1693 |
source_offset, |
| 1694 |
source_size)); |
| 1695 |
|
| 1696 |
int retval = 0; |
| 1697 |
static const int max_days[13] = {0, 31, 28, 31, 30, 31, 30, |
| 1698 |
31, 31, 30, 31, 30, 31}; |
| 1699 |
|
| 1700 |
int year = (long)argument_1/10000; |
| 1701 |
int month = (long)argument_1/100 % 100; |
| 1702 |
int day = (long)argument_1 % 100; |
| 1703 |
|
| 1704 |
// We need to check for validity in the proleptic Gregorian calendar. |
| 1705 |
|
| 1706 |
int max_day = 0; |
| 1707 |
if( month >= 1 && month <= 12 ) |
| 1708 |
{ |
| 1709 |
max_day = max_days[month]; |
| 1710 |
} |
| 1711 |
if( max_day == 28 && (((year%4) == 0 && ((year)%100) != 0) || ((year%400) == 0) )) |
| 1712 |
{ |
| 1713 |
// Year is divisible by four, but is not divisible by 100, so this |
| 1714 |
// is a leap year. |
| 1715 |
max_day += 1; |
| 1716 |
} |
| 1717 |
if( day < 1 || day > max_day ) |
| 1718 |
{ |
| 1719 |
max_day = 0; |
| 1720 |
} |
| 1721 |
if( max_day && year >= 1601 && year <= 9999 ) |
| 1722 |
{ |
| 1723 |
// It's a valid Y/M/D: |
| 1724 |
double JD = YMD_to_JD(year, month, day); |
| 1725 |
|
| 1726 |
// Offset result so that 1601-01-01 comes back as the first day of |
| 1727 |
// the Gregorian Calendar |
| 1728 |
retval = (int)(JD - JD_OF_1601_01_02); |
| 1729 |
} |
| 1730 |
__gg__int128_to_field(dest, |
| 1731 |
retval, |
| 1732 |
NO_RDIGITS, |
| 1733 |
truncation_e, |
| 1734 |
NULL); |
| 1735 |
} |
| 1736 |
|
| 1737 |
extern "C" |
| 1738 |
void |
| 1739 |
__gg__integer_of_day( cblc_field_t *dest, |
| 1740 |
const cblc_field_t *source, |
| 1741 |
size_t source_offset, |
| 1742 |
size_t source_size) |
| 1743 |
{ |
| 1744 |
// FUNCTION INTEGER-OF-DAY |
| 1745 |
// Convert YYYYDDD to "integer date" |
| 1746 |
int rdigits; |
| 1747 |
int yyyyddd = (int)__gg__binary_value_from_qualified_field( &rdigits, |
| 1748 |
source, |
| 1749 |
source_offset, |
| 1750 |
source_size); |
| 1751 |
int yyyy = yyyyddd / 1000; |
| 1752 |
int ddd = yyyyddd % 1000; |
| 1753 |
|
| 1754 |
double JD = YMD_to_JD(yyyy, 1, 0) + ddd; |
| 1755 |
int retval = (int)(JD - JD_OF_1601_01_02); |
| 1756 |
|
| 1757 |
__gg__int128_to_field(dest, |
| 1758 |
retval, |
| 1759 |
NO_RDIGITS, |
| 1760 |
truncation_e, |
| 1761 |
NULL); |
| 1762 |
} |
| 1763 |
|
| 1764 |
extern "C" |
| 1765 |
void |
| 1766 |
__gg__integer_part( cblc_field_t *dest, |
| 1767 |
const cblc_field_t *source, |
| 1768 |
size_t source_offset, |
| 1769 |
size_t source_size) |
| 1770 |
{ |
| 1771 |
// FUNCTION INTEGER-PART |
| 1772 |
GCOB_FP128 value = __gg__float128_from_qualified_field(source, |
| 1773 |
source_offset, |
| 1774 |
source_size); |
| 1775 |
GCOB_FP128 retval = FP128_FUNC(floor)(FP128_FUNC(fabs)(value)); |
| 1776 |
|
| 1777 |
if( value < 0 ) |
| 1778 |
{ |
| 1779 |
retval = -retval; |
| 1780 |
} |
| 1781 |
__gg__float128_to_field(dest, |
| 1782 |
retval, |
| 1783 |
truncation_e, |
| 1784 |
NULL); |
| 1785 |
} |
| 1786 |
|
| 1787 |
extern "C" |
| 1788 |
void |
| 1789 |
__gg__fraction_part(cblc_field_t *dest, |
| 1790 |
const cblc_field_t *source, |
| 1791 |
size_t source_offset, |
| 1792 |
size_t source_size) |
| 1793 |
{ |
| 1794 |
// FUNCTION INTEGER-PART |
| 1795 |
GCOB_FP128 value = __gg__float128_from_qualified_field(source, |
| 1796 |
source_offset, |
| 1797 |
source_size); |
| 1798 |
bool is_negative = false; |
| 1799 |
if( value < 0 ) |
| 1800 |
{ |
| 1801 |
is_negative = true; |
| 1802 |
value = -value; |
| 1803 |
} |
| 1804 |
|
| 1805 |
GCOB_FP128 retval = value - FP128_FUNC(floor)(value); |
| 1806 |
|
| 1807 |
if( is_negative ) |
| 1808 |
{ |
| 1809 |
retval = -retval; |
| 1810 |
} |
| 1811 |
__gg__float128_to_field(dest, |
| 1812 |
retval, |
| 1813 |
truncation_e, |
| 1814 |
NULL); |
| 1815 |
} |
| 1816 |
|
| 1817 |
extern "C" |
| 1818 |
void |
| 1819 |
__gg__log(cblc_field_t *dest, |
| 1820 |
const cblc_field_t *source, |
| 1821 |
size_t source_offset, |
| 1822 |
size_t source_size) |
| 1823 |
{ |
| 1824 |
// FUNCTION LOG |
| 1825 |
GCOB_FP128 value = __gg__float128_from_qualified_field(source, |
| 1826 |
source_offset, |
| 1827 |
source_size); |
| 1828 |
if( value <= 0.00 ) |
| 1829 |
{ |
| 1830 |
exception_raise(ec_argument_function_e); |
| 1831 |
} |
| 1832 |
else |
| 1833 |
{ |
| 1834 |
GCOB_FP128 retval = FP128_FUNC(log)(value); |
| 1835 |
__gg__float128_to_field(dest, |
| 1836 |
retval, |
| 1837 |
truncation_e, |
| 1838 |
NULL); |
| 1839 |
} |
| 1840 |
} |
| 1841 |
|
| 1842 |
extern "C" |
| 1843 |
void |
| 1844 |
__gg__log10(cblc_field_t *dest, |
| 1845 |
const cblc_field_t *source, |
| 1846 |
size_t source_offset, |
| 1847 |
size_t source_size) |
| 1848 |
{ |
| 1849 |
// FUNCTION LOG10 |
| 1850 |
GCOB_FP128 value = __gg__float128_from_qualified_field(source, |
| 1851 |
source_offset, |
| 1852 |
source_size); |
| 1853 |
if( value <= 0.00 ) |
| 1854 |
{ |
| 1855 |
exception_raise(ec_argument_function_e); |
| 1856 |
} |
| 1857 |
else |
| 1858 |
{ |
| 1859 |
GCOB_FP128 retval = FP128_FUNC(log10)(value); |
| 1860 |
__gg__float128_to_field(dest, |
| 1861 |
retval, |
| 1862 |
truncation_e, |
| 1863 |
NULL); |
| 1864 |
} |
| 1865 |
} |
| 1866 |
|
| 1867 |
extern "C" |
| 1868 |
void |
| 1869 |
__gg__max(cblc_field_t *dest, |
| 1870 |
size_t ncount) |
| 1871 |
{ |
| 1872 |
// FUNCTION MAX |
| 1873 |
|
| 1874 |
if( ( __gg__treeplet_1f[0]->type == FldAlphanumeric |
| 1875 |
|| __gg__treeplet_1f[0]->type == FldLiteralA) ) |
| 1876 |
{ |
| 1877 |
cblc_field_t *best_field ; |
| 1878 |
unsigned char *best_location = nullptr ; |
| 1879 |
size_t best_length = 0 ; |
| 1880 |
int best_attr ; |
| 1881 |
int best_flags ; |
| 1882 |
|
| 1883 |
bool first_time = true; |
| 1884 |
assert(ncount); |
| 1885 |
for(size_t i=0; i<ncount; i++) |
| 1886 |
{ |
| 1887 |
refer_state_for_all state; |
| 1888 |
|
| 1889 |
build_refer_state_for_all(state, __gg__treeplet_1f[i], __gg__fourplet_flags[i]); |
| 1890 |
|
| 1891 |
for(;;) |
| 1892 |
{ |
| 1893 |
if( first_time ) |
| 1894 |
{ |
| 1895 |
first_time = false; |
| 1896 |
best_field = __gg__treeplet_1f[i]; |
| 1897 |
best_location = __gg__treeplet_1f[i]->data + __gg__treeplet_1o[i]; |
| 1898 |
best_length = __gg__treeplet_1s[i]; |
| 1899 |
best_attr = __gg__treeplet_1f[i]->attr; |
| 1900 |
best_flags = __gg__fourplet_flags[i]; |
| 1901 |
} |
| 1902 |
else |
| 1903 |
{ |
| 1904 |
cblc_field_t *candidate_field = __gg__treeplet_1f[i]; |
| 1905 |
unsigned char *candidate_location = __gg__treeplet_1f[i]->data + __gg__treeplet_1o[i]; |
| 1906 |
size_t candidate_length = __gg__treeplet_1s[i]; |
| 1907 |
int candidate_attr = __gg__treeplet_1f[i]->attr; |
| 1908 |
int candidate_flags = __gg__fourplet_flags[i]; |
| 1909 |
|
| 1910 |
int compare_result = __gg__compare_2( |
| 1911 |
candidate_field, |
| 1912 |
candidate_location, |
| 1913 |
candidate_length, |
| 1914 |
candidate_attr, |
| 1915 |
candidate_flags, |
| 1916 |
best_field, |
| 1917 |
best_location, |
| 1918 |
best_length, |
| 1919 |
best_attr, |
| 1920 |
best_flags, |
| 1921 |
0); |
| 1922 |
if( compare_result >= 0 ) |
| 1923 |
{ |
| 1924 |
best_field = candidate_field ; |
| 1925 |
best_location = candidate_location ; |
| 1926 |
best_length = candidate_length ; |
| 1927 |
best_attr = candidate_attr ; |
| 1928 |
best_flags = candidate_flags ; |
| 1929 |
} |
| 1930 |
} |
| 1931 |
if( !update_refer_state_for_all(state, __gg__treeplet_1f[i]) ) |
| 1932 |
{ |
| 1933 |
// There is nothing left to do. |
| 1934 |
break; |
| 1935 |
} |
| 1936 |
} |
| 1937 |
} |
| 1938 |
|
| 1939 |
|
| 1940 |
__gg__adjust_dest_size(dest, best_length); |
| 1941 |
dest->type = FldAlphanumeric; |
| 1942 |
assert(best_location); |
| 1943 |
memcpy(dest->data, best_location, best_length); |
| 1944 |
} |
| 1945 |
else |
| 1946 |
{ |
| 1947 |
GCOB_FP128 retval; |
| 1948 |
bool first_time = true; |
| 1949 |
assert(ncount); |
| 1950 |
for(size_t i=0; i<ncount; i++) |
| 1951 |
{ |
| 1952 |
refer_state_for_all state; |
| 1953 |
build_refer_state_for_all(state, __gg__treeplet_1f[i], __gg__fourplet_flags[i]); |
| 1954 |
|
| 1955 |
for(;;) |
| 1956 |
{ |
| 1957 |
if( first_time ) |
| 1958 |
{ |
| 1959 |
first_time = false; |
| 1960 |
retval = __gg__float128_from_qualified_field(__gg__treeplet_1f[i], __gg__treeplet_1o[i], __gg__treeplet_1s[i]); |
| 1961 |
} |
| 1962 |
else |
| 1963 |
{ |
| 1964 |
GCOB_FP128 candidate = __gg__float128_from_qualified_field(__gg__treeplet_1f[i], __gg__treeplet_1o[i], __gg__treeplet_1s[i]); |
| 1965 |
if( candidate >= retval ) |
| 1966 |
{ |
| 1967 |
retval = candidate; |
| 1968 |
} |
| 1969 |
} |
| 1970 |
if( !update_refer_state_for_all(state, __gg__treeplet_1f[i]) ) |
| 1971 |
{ |
| 1972 |
// There is nothing left to do for that input. |
| 1973 |
break; |
| 1974 |
} |
| 1975 |
} |
| 1976 |
} |
| 1977 |
__gg__float128_to_field(dest, |
| 1978 |
retval, |
| 1979 |
truncation_e, |
| 1980 |
NULL); |
| 1981 |
} |
| 1982 |
} |
| 1983 |
|
| 1984 |
extern "C" |
| 1985 |
void |
| 1986 |
__gg__lower_case( cblc_field_t *dest, |
| 1987 |
const cblc_field_t *input, |
| 1988 |
size_t input_offset, |
| 1989 |
size_t input_size) |
| 1990 |
{ |
| 1991 |
size_t dest_length = dest->capacity; |
| 1992 |
size_t source_length = input_size; |
| 1993 |
memset(dest->data, internal_space, dest_length); |
| 1994 |
memcpy(dest->data, input->data+input_offset, std::min(dest_length, source_length)); |
| 1995 |
internal_to_ascii( PTRCAST(char, dest->data), dest_length); |
| 1996 |
std::transform(dest->data, dest->data + dest_length, dest->data, |
| 1997 |
[](unsigned char c) { return std::tolower(c); }); |
| 1998 |
ascii_to_internal_str( PTRCAST(char, dest->data), dest_length); |
| 1999 |
} |
| 2000 |
|
| 2001 |
extern "C" |
| 2002 |
void |
| 2003 |
__gg__mean( cblc_field_t *dest, |
| 2004 |
size_t ninputs) |
| 2005 |
{ |
| 2006 |
// FUNCTION MEAN |
| 2007 |
size_t k_count; |
| 2008 |
GCOB_FP128 sum = kahan_summation(ninputs, |
| 2009 |
__gg__treeplet_1f, |
| 2010 |
__gg__treeplet_1o, |
| 2011 |
__gg__treeplet_1s, |
| 2012 |
__gg__fourplet_flags, |
| 2013 |
&k_count); |
| 2014 |
sum /= k_count; |
| 2015 |
__gg__float128_to_field(dest, |
| 2016 |
sum, |
| 2017 |
truncation_e, |
| 2018 |
NULL); |
| 2019 |
} |
| 2020 |
|
| 2021 |
extern "C" |
| 2022 |
void |
| 2023 |
__gg__median( cblc_field_t *dest, |
| 2024 |
size_t ncount) |
| 2025 |
{ |
| 2026 |
// FUNCTION MEDIAN |
| 2027 |
|
| 2028 |
// This is wasteful, because it allocates N values in order to sort them. It |
| 2029 |
// is also an O(NlogN) solution, when there are O(N) solutions available. |
| 2030 |
|
| 2031 |
// It has the merit of being very simple. |
| 2032 |
|
| 2033 |
// The future beckons, but not today. |
| 2034 |
|
| 2035 |
size_t list_size = 1; |
| 2036 |
|
| 2037 |
GCOB_FP128 *the_list = static_cast<GCOB_FP128 *>(malloc(list_size *sizeof(GCOB_FP128))); |
| 2038 |
massert(the_list); |
| 2039 |
size_t k_count = 0; |
| 2040 |
assert(ncount); |
| 2041 |
for(size_t i=0; i<ncount; i++) |
| 2042 |
{ |
| 2043 |
refer_state_for_all state; |
| 2044 |
build_refer_state_for_all(state, __gg__treeplet_1f[i], __gg__fourplet_flags[i]); |
| 2045 |
|
| 2046 |
for(;;) |
| 2047 |
{ |
| 2048 |
if(k_count >= list_size) |
| 2049 |
{ |
| 2050 |
list_size *= 2; |
| 2051 |
the_list = PTRCAST(GCOB_FP128, realloc(the_list, list_size *sizeof(GCOB_FP128))); |
| 2052 |
massert(the_list); |
| 2053 |
} |
| 2054 |
|
| 2055 |
assert(the_list); |
| 2056 |
the_list[k_count] = __gg__float128_from_qualified_field(__gg__treeplet_1f[i], |
| 2057 |
__gg__treeplet_1o[i], |
| 2058 |
__gg__treeplet_1s[i]); |
| 2059 |
k_count += 1; |
| 2060 |
if( !update_refer_state_for_all(state, __gg__treeplet_1f[i]) ) |
| 2061 |
{ |
| 2062 |
// There is nothing left to do. |
| 2063 |
break; |
| 2064 |
} |
| 2065 |
} |
| 2066 |
} |
| 2067 |
std::sort(the_list, the_list+k_count); |
| 2068 |
|
| 2069 |
GCOB_FP128 retval; |
| 2070 |
size_t i=k_count/2; |
| 2071 |
if( k_count & 1 ) |
| 2072 |
{ |
| 2073 |
retval = the_list[i]; |
| 2074 |
} |
| 2075 |
else |
| 2076 |
{ |
| 2077 |
retval = (the_list[i-1] + the_list[i])/2.0; |
| 2078 |
} |
| 2079 |
__gg__float128_to_field(dest, |
| 2080 |
retval, |
| 2081 |
truncation_e, |
| 2082 |
NULL); |
| 2083 |
free(the_list); |
| 2084 |
} |
| 2085 |
|
| 2086 |
extern "C" |
| 2087 |
void |
| 2088 |
__gg__midrange( cblc_field_t *dest, |
| 2089 |
size_t ncount) |
| 2090 |
{ |
| 2091 |
// FUNCTION MIDRANGE |
| 2092 |
GCOB_FP128 val; |
| 2093 |
GCOB_FP128 min=0; |
| 2094 |
GCOB_FP128 max=0; |
| 2095 |
bool first_time = true; |
| 2096 |
assert(ncount); |
| 2097 |
for(size_t i=0; i<ncount; i++) |
| 2098 |
{ |
| 2099 |
refer_state_for_all state; |
| 2100 |
build_refer_state_for_all(state, __gg__treeplet_1f[i], __gg__fourplet_flags[i]); |
| 2101 |
for(;;) |
| 2102 |
{ |
| 2103 |
val = __gg__float128_from_qualified_field(__gg__treeplet_1f[i], |
| 2104 |
__gg__treeplet_1o[i], |
| 2105 |
__gg__treeplet_1s[i]); |
| 2106 |
if( first_time ) |
| 2107 |
{ |
| 2108 |
first_time = false; |
| 2109 |
min = val; |
| 2110 |
max = val; |
| 2111 |
} |
| 2112 |
min = std::min(min, val); |
| 2113 |
max = std::max(max, val); |
| 2114 |
if( !update_refer_state_for_all(state, __gg__treeplet_1f[i]) ) |
| 2115 |
{ |
| 2116 |
// There is nothing left to do for that input. |
| 2117 |
break; |
| 2118 |
} |
| 2119 |
} |
| 2120 |
} |
| 2121 |
GCOB_FP128 retval = (min + max)/2.0; |
| 2122 |
__gg__float128_to_field(dest, |
| 2123 |
retval, |
| 2124 |
truncation_e, |
| 2125 |
NULL); |
| 2126 |
} |
| 2127 |
|
| 2128 |
extern "C" |
| 2129 |
void |
| 2130 |
__gg__min(cblc_field_t *dest, |
| 2131 |
size_t ncount) |
| 2132 |
{ |
| 2133 |
// FUNCTION MIN |
| 2134 |
|
| 2135 |
if( ( __gg__treeplet_1f[0]->type == FldAlphanumeric |
| 2136 |
|| __gg__treeplet_1f[0]->type == FldLiteralA) ) |
| 2137 |
{ |
| 2138 |
cblc_field_t *best_field ; |
| 2139 |
unsigned char *best_location = nullptr ; |
| 2140 |
size_t best_length = 0 ; |
| 2141 |
int best_attr ; |
| 2142 |
int best_flags ; |
| 2143 |
|
| 2144 |
bool first_time = true; |
| 2145 |
assert(ncount); |
| 2146 |
for(size_t i=0; i<ncount; i++) |
| 2147 |
{ |
| 2148 |
refer_state_for_all state; |
| 2149 |
|
| 2150 |
build_refer_state_for_all(state, __gg__treeplet_1f[i], __gg__fourplet_flags[i]); |
| 2151 |
|
| 2152 |
for(;;) |
| 2153 |
{ |
| 2154 |
if( first_time ) |
| 2155 |
{ |
| 2156 |
first_time = false; |
| 2157 |
best_field = __gg__treeplet_1f[i]; |
| 2158 |
best_location = __gg__treeplet_1f[i]->data + __gg__treeplet_1o[i]; |
| 2159 |
best_length = __gg__treeplet_1s[i]; |
| 2160 |
best_attr = __gg__treeplet_1f[i]->attr; |
| 2161 |
best_flags = __gg__fourplet_flags[i]; |
| 2162 |
} |
| 2163 |
else |
| 2164 |
{ |
| 2165 |
cblc_field_t *candidate_field = __gg__treeplet_1f[i]; |
| 2166 |
unsigned char *candidate_location = __gg__treeplet_1f[i]->data + __gg__treeplet_1o[i]; |
| 2167 |
size_t candidate_length = __gg__treeplet_1s[i]; |
| 2168 |
int candidate_attr = __gg__treeplet_1f[i]->attr; |
| 2169 |
int candidate_flags = __gg__fourplet_flags[i]; |
| 2170 |
|
| 2171 |
int compare_result = __gg__compare_2( |
| 2172 |
candidate_field, |
| 2173 |
candidate_location, |
| 2174 |
candidate_length, |
| 2175 |
candidate_attr, |
| 2176 |
candidate_flags, |
| 2177 |
best_field, |
| 2178 |
best_location, |
| 2179 |
best_length, |
| 2180 |
best_attr, |
| 2181 |
best_flags, |
| 2182 |
0); |
| 2183 |
if( compare_result < 0 ) |
| 2184 |
{ |
| 2185 |
best_field = candidate_field ; |
| 2186 |
best_location = candidate_location ; |
| 2187 |
best_length = candidate_length ; |
| 2188 |
best_attr = candidate_attr ; |
| 2189 |
best_flags = candidate_flags ; |
| 2190 |
} |
| 2191 |
} |
| 2192 |
if( !update_refer_state_for_all(state, __gg__treeplet_1f[i]) ) |
| 2193 |
{ |
| 2194 |
// There is nothing left to do. |
| 2195 |
break; |
| 2196 |
} |
| 2197 |
} |
| 2198 |
} |
| 2199 |
|
| 2200 |
__gg__adjust_dest_size(dest, best_length); |
| 2201 |
dest->type = FldAlphanumeric; |
| 2202 |
assert(best_location); |
| 2203 |
memcpy(dest->data, best_location, best_length); |
| 2204 |
} |
| 2205 |
else |
| 2206 |
{ |
| 2207 |
GCOB_FP128 retval; |
| 2208 |
bool first_time = true; |
| 2209 |
assert(ncount); |
| 2210 |
for(size_t i=0; i<ncount; i++) |
| 2211 |
{ |
| 2212 |
refer_state_for_all state; |
| 2213 |
build_refer_state_for_all(state, __gg__treeplet_1f[i], __gg__fourplet_flags[i]); |
| 2214 |
|
| 2215 |
for(;;) |
| 2216 |
{ |
| 2217 |
if( first_time ) |
| 2218 |
{ |
| 2219 |
first_time = false; |
| 2220 |
retval = __gg__float128_from_qualified_field(__gg__treeplet_1f[i], __gg__treeplet_1o[i], __gg__treeplet_1s[i]); |
| 2221 |
} |
| 2222 |
else |
| 2223 |
{ |
| 2224 |
GCOB_FP128 candidate = __gg__float128_from_qualified_field(__gg__treeplet_1f[i], __gg__treeplet_1o[i], __gg__treeplet_1s[i]); |
| 2225 |
if( candidate < retval ) |
| 2226 |
{ |
| 2227 |
retval = candidate; |
| 2228 |
} |
| 2229 |
} |
| 2230 |
if( !update_refer_state_for_all(state, __gg__treeplet_1f[i]) ) |
| 2231 |
{ |
| 2232 |
// There is nothing left to do for that input. |
| 2233 |
break; |
| 2234 |
} |
| 2235 |
} |
| 2236 |
} |
| 2237 |
__gg__float128_to_field(dest, |
| 2238 |
retval, |
| 2239 |
truncation_e, |
| 2240 |
NULL); |
| 2241 |
} |
| 2242 |
} |
| 2243 |
|
| 2244 |
extern "C" |
| 2245 |
void |
| 2246 |
__gg__mod(cblc_field_t *dest, |
| 2247 |
cblc_field_t *source1, |
| 2248 |
size_t source1_offset, |
| 2249 |
size_t source1_size, |
| 2250 |
cblc_field_t *source2, |
| 2251 |
size_t source2_offset, |
| 2252 |
size_t source2_size) |
| 2253 |
{ |
| 2254 |
// FUNCTION MOD |
| 2255 |
__int128 arg1 = __gg__integer_from_qualified_field(source1, |
| 2256 |
source1_offset, |
| 2257 |
source1_size); |
| 2258 |
__int128 arg2 = __gg__integer_from_qualified_field(source2, |
| 2259 |
source2_offset, |
| 2260 |
source2_size); |
| 2261 |
__int128 retval; |
| 2262 |
|
| 2263 |
if( arg2 == 0 ) |
| 2264 |
{ |
| 2265 |
exception_raise(ec_argument_function_e); |
| 2266 |
retval = 0; |
| 2267 |
} |
| 2268 |
else |
| 2269 |
{ |
| 2270 |
int sign_of_div = arg1 >= 0 ? 1 : -1 ; |
| 2271 |
sign_of_div *= arg2 >= 0 ? 1 : -1 ; |
| 2272 |
|
| 2273 |
__int128 div = ( arg1 / arg2 ) ; |
| 2274 |
if( sign_of_div < 0 ) |
| 2275 |
{ |
| 2276 |
div -= 1; |
| 2277 |
} |
| 2278 |
|
| 2279 |
retval = arg1 - arg2 * div ; |
| 2280 |
} |
| 2281 |
|
| 2282 |
__gg__int128_to_field(dest, |
| 2283 |
retval, |
| 2284 |
NO_RDIGITS, |
| 2285 |
truncation_e, |
| 2286 |
NULL); |
| 2287 |
} |
| 2288 |
|
| 2289 |
static int |
| 2290 |
numval( cblc_field_t *dest, |
| 2291 |
const cblc_field_t *input, |
| 2292 |
size_t input_offset, |
| 2293 |
size_t input_size) |
| 2294 |
{ |
| 2295 |
// Returns the one-based character position of a bad character |
| 2296 |
// returns zero if it is okay |
| 2297 |
|
| 2298 |
const char *p = PTRCAST(char, (input->data + input_offset)); |
| 2299 |
const char *pend = p + input_size; |
| 2300 |
|
| 2301 |
int errpos = 0; |
| 2302 |
__int128 retval = 0; |
| 2303 |
int retval_rdigits = 0; |
| 2304 |
|
| 2305 |
bool saw_digit= false; |
| 2306 |
char decimal_point = ascii_to_internal(__gg__get_decimal_point()); |
| 2307 |
bool in_fraction = false; |
| 2308 |
bool leading_sign = false; |
| 2309 |
bool is_negative = false; |
| 2310 |
enum |
| 2311 |
{ |
| 2312 |
SPACE1, |
| 2313 |
SPACE2, |
| 2314 |
DIGITS, |
| 2315 |
SPACE3, |
| 2316 |
SPACE4, |
| 2317 |
} state = SPACE1; |
| 2318 |
|
| 2319 |
if( input_size == 0 ) |
| 2320 |
{ |
| 2321 |
errpos = 1; |
| 2322 |
goto done; |
| 2323 |
} |
| 2324 |
while( p < pend ) |
| 2325 |
{ |
| 2326 |
unsigned char ch = *p++; |
| 2327 |
errpos += 1; |
| 2328 |
switch( state ) |
| 2329 |
{ |
| 2330 |
case SPACE1: |
| 2331 |
// We tolerate spaces, and expect to end with a sign, digit, |
| 2332 |
// or decimal point: |
| 2333 |
if( ch == internal_space ) |
| 2334 |
{ |
| 2335 |
continue; |
| 2336 |
} |
| 2337 |
if( ch == internal_plus ) |
| 2338 |
{ |
| 2339 |
leading_sign = true; |
| 2340 |
state = SPACE2; |
| 2341 |
break; |
| 2342 |
} |
| 2343 |
if( ch == internal_minus ) |
| 2344 |
{ |
| 2345 |
leading_sign = true; |
| 2346 |
is_negative = true; |
| 2347 |
state = SPACE2; |
| 2348 |
break; |
| 2349 |
} |
| 2350 |
if( ch >= internal_0 && ch <= internal_9 ) |
| 2351 |
{ |
| 2352 |
saw_digit = true; |
| 2353 |
retval = ch & 0xF; |
| 2354 |
state = DIGITS; |
| 2355 |
break; |
| 2356 |
} |
| 2357 |
if( ch == decimal_point ) |
| 2358 |
{ |
| 2359 |
in_fraction = true; |
| 2360 |
state = DIGITS; |
| 2361 |
break; |
| 2362 |
} |
| 2363 |
// This is a bad character; errpos is correct |
| 2364 |
goto done; |
| 2365 |
break; |
| 2366 |
|
| 2367 |
case SPACE2: |
| 2368 |
// We tolerate spaces, and expect to end with a digit or decimal point: |
| 2369 |
if( ch == internal_space ) |
| 2370 |
{ |
| 2371 |
break; |
| 2372 |
} |
| 2373 |
if( ch >= internal_0 && ch <= internal_9 ) |
| 2374 |
{ |
| 2375 |
saw_digit = true; |
| 2376 |
retval = ch & 0xF; |
| 2377 |
state = DIGITS; |
| 2378 |
break; |
| 2379 |
} |
| 2380 |
if( ch == decimal_point ) |
| 2381 |
{ |
| 2382 |
in_fraction = true; |
| 2383 |
state = DIGITS; |
| 2384 |
break; |
| 2385 |
} |
| 2386 |
// This is a bad character; errpos is correct |
| 2387 |
goto done; |
| 2388 |
break; |
| 2389 |
|
| 2390 |
case DIGITS: |
| 2391 |
// We tolerate digits. We tolerate one decimal point. We expect to |
| 2392 |
// end with a space, a sign, "DB" or "CR", or the the end of the string |
| 2393 |
// It's a bit complicated |
| 2394 |
|
| 2395 |
if( ch >= internal_0 && ch <= internal_9 ) |
| 2396 |
{ |
| 2397 |
saw_digit = true; |
| 2398 |
retval *= 10; |
| 2399 |
retval += ch & 0xF; |
| 2400 |
if( in_fraction ) |
| 2401 |
{ |
| 2402 |
retval_rdigits += 1; |
| 2403 |
} |
| 2404 |
break; |
| 2405 |
} |
| 2406 |
if( ch == decimal_point && in_fraction ) |
| 2407 |
{ |
| 2408 |
// Only one decimal is allowed |
| 2409 |
goto done; |
| 2410 |
} |
| 2411 |
if( ch == decimal_point ) |
| 2412 |
{ |
| 2413 |
in_fraction = true; |
| 2414 |
break; |
| 2415 |
} |
| 2416 |
if( ch == internal_space ) |
| 2417 |
{ |
| 2418 |
state = SPACE3; |
| 2419 |
break; |
| 2420 |
} |
| 2421 |
if( ch == internal_plus && leading_sign) |
| 2422 |
{ |
| 2423 |
// We are allowed leading or trailing signs, but not both |
| 2424 |
goto done; |
| 2425 |
} |
| 2426 |
if( ch == internal_minus && leading_sign) |
| 2427 |
{ |
| 2428 |
// We are allowed leading or trailing signs, but not both |
| 2429 |
goto done; |
| 2430 |
} |
| 2431 |
if( ch == internal_plus ) |
| 2432 |
{ |
| 2433 |
state = SPACE4; |
| 2434 |
break; |
| 2435 |
} |
| 2436 |
if( ch == internal_minus ) |
| 2437 |
{ |
| 2438 |
is_negative = true; |
| 2439 |
state = SPACE4; |
| 2440 |
break; |
| 2441 |
} |
| 2442 |
if( std::tolower(ch) == 'd' ) |
| 2443 |
{ |
| 2444 |
if( leading_sign ) |
| 2445 |
{ |
| 2446 |
goto done; |
| 2447 |
} |
| 2448 |
ch = *p++; |
| 2449 |
errpos += 1; |
| 2450 |
if( p > pend || std::tolower(ch) != 'b' ) |
| 2451 |
{ |
| 2452 |
goto done; |
| 2453 |
} |
| 2454 |
is_negative = true; |
| 2455 |
state = SPACE4; |
| 2456 |
break; |
| 2457 |
} |
| 2458 |
if( std::tolower(ch) == 'c' ) |
| 2459 |
{ |
| 2460 |
if( leading_sign ) |
| 2461 |
{ |
| 2462 |
goto done; |
| 2463 |
} |
| 2464 |
ch = *p++; |
| 2465 |
errpos += 1; |
| 2466 |
if( p > pend || std::tolower(ch) != 'r' ) |
| 2467 |
{ |
| 2468 |
goto done; |
| 2469 |
} |
| 2470 |
is_negative = true; |
| 2471 |
state = SPACE4; |
| 2472 |
break; |
| 2473 |
} |
| 2474 |
// This is a bad character; errpos is correct |
| 2475 |
goto done; |
| 2476 |
break; |
| 2477 |
|
| 2478 |
case SPACE3: |
| 2479 |
// We tolerate spaces, or we end with a sign: |
| 2480 |
if( ch == internal_space ) |
| 2481 |
{ |
| 2482 |
break; |
| 2483 |
} |
| 2484 |
if( ch == internal_plus && leading_sign) |
| 2485 |
{ |
| 2486 |
// We are allowed leading or trailing signs, but not both |
| 2487 |
goto done; |
| 2488 |
} |
| 2489 |
if( ch == internal_minus && leading_sign) |
| 2490 |
{ |
| 2491 |
// We are allowed leading or trailing signs, but not both |
| 2492 |
goto done; |
| 2493 |
} |
| 2494 |
if( ch == internal_plus ) |
| 2495 |
{ |
| 2496 |
state = SPACE4; |
| 2497 |
break; |
| 2498 |
} |
| 2499 |
if( ch == internal_minus ) |
| 2500 |
{ |
| 2501 |
is_negative = true; |
| 2502 |
state = SPACE4; |
| 2503 |
break; |
| 2504 |
} |
| 2505 |
if( std::tolower(ch) == 'd' ) |
| 2506 |
{ |
| 2507 |
if( leading_sign ) |
| 2508 |
{ |
| 2509 |
goto done; |
| 2510 |
} |
| 2511 |
ch = *p++; |
| 2512 |
errpos += 1; |
| 2513 |
if( p > pend || std::tolower(ch) != 'b' ) |
| 2514 |
{ |
| 2515 |
goto done; |
| 2516 |
} |
| 2517 |
is_negative = true; |
| 2518 |
state = SPACE4; |
| 2519 |
break; |
| 2520 |
} |
| 2521 |
if( std::tolower(ch) == 'c' ) |
| 2522 |
{ |
| 2523 |
if( leading_sign ) |
| 2524 |
{ |
| 2525 |
goto done; |
| 2526 |
} |
| 2527 |
ch = *p++; |
| 2528 |
errpos += 1; |
| 2529 |
if( p > pend || std::tolower(ch) != 'r' ) |
| 2530 |
{ |
| 2531 |
goto done; |
| 2532 |
} |
| 2533 |
is_negative = true; |
| 2534 |
state = SPACE4; |
| 2535 |
break; |
| 2536 |
} |
| 2537 |
goto done; |
| 2538 |
break; |
| 2539 |
case SPACE4: |
| 2540 |
if( ch == internal_space ) |
| 2541 |
{ |
| 2542 |
break; |
| 2543 |
} |
| 2544 |
goto done; |
| 2545 |
break; |
| 2546 |
} |
| 2547 |
} |
| 2548 |
if( saw_digit ) |
| 2549 |
{ |
| 2550 |
errpos = 0; |
| 2551 |
} |
| 2552 |
else if( p == pend ) |
| 2553 |
{ |
| 2554 |
// If we got to the end without seeing adigit, we need to bump the |
| 2555 |
// error pointer: |
| 2556 |
errpos += 1; |
| 2557 |
} |
| 2558 |
|
| 2559 |
done: |
| 2560 |
if(errpos) |
| 2561 |
{ |
| 2562 |
retval = 0; |
| 2563 |
} |
| 2564 |
if( is_negative ) |
| 2565 |
{ |
| 2566 |
retval = -retval; |
| 2567 |
} |
| 2568 |
if(dest) |
| 2569 |
{ |
| 2570 |
__gg__int128_to_field(dest, |
| 2571 |
retval, |
| 2572 |
retval_rdigits, |
| 2573 |
truncation_e, |
| 2574 |
NULL); |
| 2575 |
} |
| 2576 |
return errpos; |
| 2577 |
} |
| 2578 |
|
| 2579 |
static |
| 2580 |
int |
| 2581 |
numval_c( cblc_field_t *dest, |
| 2582 |
const cblc_field_t *src, |
| 2583 |
size_t src_offset, |
| 2584 |
size_t src_size, |
| 2585 |
const cblc_field_t *crcy, |
| 2586 |
size_t crcy_offset, |
| 2587 |
size_t crcy_size |
| 2588 |
) |
| 2589 |
{ |
| 2590 |
size_t errcode = 0; |
| 2591 |
|
| 2592 |
char *pstart = PTRCAST(char, (src->data+src_offset)); |
| 2593 |
char *pend = pstart + src_size; |
| 2594 |
char *p = pstart; |
| 2595 |
|
| 2596 |
GCOB_FP128 retval = 0; |
| 2597 |
int sign = 0; |
| 2598 |
int rdigits = 0; |
| 2599 |
int rdigit_bump = 0; |
| 2600 |
unsigned char decimal_point = ascii_to_internal(__gg__get_decimal_point()); |
| 2601 |
unsigned char decimal_separator = ascii_to_internal(__gg__get_decimal_separator()); |
| 2602 |
|
| 2603 |
char *currency_start; |
| 2604 |
char *currency_end; |
| 2605 |
if( crcy ) |
| 2606 |
{ |
| 2607 |
currency_start = PTRCAST(char, (crcy->data+crcy_offset)); |
| 2608 |
currency_end = currency_start + crcy_size; |
| 2609 |
} |
| 2610 |
else |
| 2611 |
{ |
| 2612 |
currency_start = __gg__get_default_currency_string(); |
| 2613 |
currency_end = currency_start + strlen(currency_start); |
| 2614 |
} |
| 2615 |
char *pcurrency = currency_start; |
| 2616 |
// Trim off spaces from the currency: |
| 2617 |
while( *pcurrency == internal_space && pcurrency < currency_end ) |
| 2618 |
{ |
| 2619 |
pcurrency += 1; |
| 2620 |
} |
| 2621 |
|
| 2622 |
while( *(currency_end-1) == internal_space && currency_end > currency_start ) |
| 2623 |
{ |
| 2624 |
currency_end -= 1; |
| 2625 |
} |
| 2626 |
|
| 2627 |
// We will do this as a state machine: |
| 2628 |
|
| 2629 |
enum |
| 2630 |
{ |
| 2631 |
first_space, |
| 2632 |
first_sign, |
| 2633 |
second_space, |
| 2634 |
currency, |
| 2635 |
before_digits, |
| 2636 |
digits, |
| 2637 |
after_digits, |
| 2638 |
second_sign, |
| 2639 |
final_space, |
| 2640 |
} state = first_space; |
| 2641 |
|
| 2642 |
while( p < pend ) |
| 2643 |
{ |
| 2644 |
unsigned char ch = *p++; |
| 2645 |
switch( state ) |
| 2646 |
{ |
| 2647 |
case first_space : |
| 2648 |
// Eat up spaces, if any, and then dispatch on the first non-space: |
| 2649 |
if( ch != internal_space ) |
| 2650 |
{ |
| 2651 |
// ch can now be a plus, a minus, a digit, or the first character |
| 2652 |
// of the currency string |
| 2653 |
if( ch == internal_plus || ch == internal_minus ) |
| 2654 |
{ |
| 2655 |
state = first_sign; |
| 2656 |
// Decrement to pointer in order to pick up the character again |
| 2657 |
p -= 1; |
| 2658 |
} |
| 2659 |
else if( ch == *pcurrency ) |
| 2660 |
{ |
| 2661 |
state = currency; |
| 2662 |
p -= 1; |
| 2663 |
} |
| 2664 |
else if( (ch >= internal_0 && ch <= internal_9) |
| 2665 |
|| ch == decimal_point ) |
| 2666 |
{ |
| 2667 |
state = digits; |
| 2668 |
p -= 1; |
| 2669 |
} |
| 2670 |
else |
| 2671 |
{ |
| 2672 |
// We have a bad character. Set the errcode to be the position of |
| 2673 |
// the bad character, and adjust p to break out of the loop. |
| 2674 |
// Set the state so that the default error processing is suppressed |
| 2675 |
state = final_space; |
| 2676 |
errcode = p - pstart; |
| 2677 |
p = pend; |
| 2678 |
} |
| 2679 |
} |
| 2680 |
break; |
| 2681 |
|
| 2682 |
case first_sign : |
| 2683 |
// We know the character is a plus or a minus: |
| 2684 |
if( ch == internal_plus ) |
| 2685 |
{ |
| 2686 |
sign = 1; |
| 2687 |
state = second_space; |
| 2688 |
} |
| 2689 |
else |
| 2690 |
{ |
| 2691 |
sign = -1; |
| 2692 |
state = second_space; |
| 2693 |
} |
| 2694 |
break; |
| 2695 |
|
| 2696 |
case second_space : |
| 2697 |
// Eat up spaces, if any. This segment has to end with a currency or |
| 2698 |
// a digit: |
| 2699 |
if( ch != internal_space ) |
| 2700 |
{ |
| 2701 |
if( ch == *pcurrency ) |
| 2702 |
{ |
| 2703 |
state = currency; |
| 2704 |
p -= 1; |
| 2705 |
} |
| 2706 |
else if( (ch >= internal_0 && ch <= internal_9) |
| 2707 |
|| ch == decimal_point ) |
| 2708 |
{ |
| 2709 |
state = digits; |
| 2710 |
p -= 1; |
| 2711 |
} |
| 2712 |
else |
| 2713 |
{ |
| 2714 |
// We have a bad character. Set the errcode to be the position of |
| 2715 |
// the bad character, and adjust p to break out of the loop. |
| 2716 |
state = final_space; |
| 2717 |
errcode = p - pstart; |
| 2718 |
p = pend; |
| 2719 |
} |
| 2720 |
} |
| 2721 |
break; |
| 2722 |
|
| 2723 |
case currency : |
| 2724 |
// At this point, the only valid character is the next character |
| 2725 |
// in the currency string: |
| 2726 |
if( pcurrency >= currency_end ) |
| 2727 |
{ |
| 2728 |
// Hey! Look at us! We got through the whole currency string. |
| 2729 |
state = before_digits; |
| 2730 |
p -= 1; |
| 2731 |
} |
| 2732 |
else if( ch == *pcurrency++) |
| 2733 |
{ |
| 2734 |
// We are still marching through the currency |
| 2735 |
} |
| 2736 |
else |
| 2737 |
{ |
| 2738 |
// We have a bad character: |
| 2739 |
errcode = p - pstart; |
| 2740 |
state = final_space; |
| 2741 |
p = pend; |
| 2742 |
} |
| 2743 |
break; |
| 2744 |
|
| 2745 |
case before_digits : |
| 2746 |
// Eat up spaces, if any. This segment has to end with a digit |
| 2747 |
if( ch != internal_space ) |
| 2748 |
{ |
| 2749 |
if( (ch >= internal_0 && ch <= internal_9) |
| 2750 |
|| ch == decimal_point ) |
| 2751 |
{ |
| 2752 |
state = digits; |
| 2753 |
p -= 1; |
| 2754 |
} |
| 2755 |
else |
| 2756 |
{ |
| 2757 |
// We have a bad character. Set the errcode to be the position of |
| 2758 |
// the bad character, and adjust p to break out of the loop. |
| 2759 |
state = final_space; |
| 2760 |
errcode = p - pstart; |
| 2761 |
p = pend; |
| 2762 |
} |
| 2763 |
} |
| 2764 |
break; |
| 2765 |
|
| 2766 |
case digits : |
| 2767 |
// The only thing allowed here are digits, decimal points, and |
| 2768 |
// decimal separators |
| 2769 |
if( ch >= internal_0 && ch <= internal_9 ) |
| 2770 |
{ |
| 2771 |
// We have a digit. |
| 2772 |
rdigits += rdigit_bump; |
| 2773 |
retval *= 10; |
| 2774 |
retval += ch & 0x0F; |
| 2775 |
} |
| 2776 |
else if( ch == decimal_point && rdigit_bump) |
| 2777 |
{ |
| 2778 |
// We have a second decimal_point, which is against the rules |
| 2779 |
errcode = p - pstart; |
| 2780 |
state = final_space; |
| 2781 |
p = pend; |
| 2782 |
} |
| 2783 |
else if( ch == decimal_separator ) |
| 2784 |
{ |
| 2785 |
// Commas are ignored |
| 2786 |
} |
| 2787 |
else if( ch == decimal_point ) |
| 2788 |
{ |
| 2789 |
rdigit_bump = 1; |
| 2790 |
} |
| 2791 |
else |
| 2792 |
{ |
| 2793 |
// We have something that isn't a digit or decimal point or decimal |
| 2794 |
// separator: |
| 2795 |
state = after_digits; |
| 2796 |
p -= 1; |
| 2797 |
} |
| 2798 |
break; |
| 2799 |
|
| 2800 |
case after_digits : |
| 2801 |
// after digits, the only valid things are spaces, plus, minus, D, or C |
| 2802 |
if( ch != internal_space ) |
| 2803 |
{ |
| 2804 |
if( ch == internal_plus |
| 2805 |
|| ch == internal_minus |
| 2806 |
|| ch == internal_D |
| 2807 |
|| ch == internal_d |
| 2808 |
|| ch == internal_C |
| 2809 |
|| ch == internal_c ) |
| 2810 |
{ |
| 2811 |
state = second_sign; |
| 2812 |
p -= 1; |
| 2813 |
} |
| 2814 |
} |
| 2815 |
break; |
| 2816 |
|
| 2817 |
case second_sign : |
| 2818 |
if( sign ) |
| 2819 |
{ |
| 2820 |
// A second sign isn't allowed |
| 2821 |
errcode = p - pstart; |
| 2822 |
p = pend; |
| 2823 |
} |
| 2824 |
if( ch == internal_plus ) |
| 2825 |
{ |
| 2826 |
sign = 1; |
| 2827 |
} |
| 2828 |
else if( ch == internal_minus ) |
| 2829 |
{ |
| 2830 |
sign = -1; |
| 2831 |
} |
| 2832 |
else if( (ch == internal_D || ch == internal_d) |
| 2833 |
&& p < pend |
| 2834 |
&& (*p == internal_B || *p == internal_b) ) |
| 2835 |
{ |
| 2836 |
sign = -1; |
| 2837 |
p += 1; |
| 2838 |
} |
| 2839 |
else if( (ch == internal_C || ch == internal_c) |
| 2840 |
&& p < pend |
| 2841 |
&& (*p == internal_R || *p == internal_r) ) |
| 2842 |
{ |
| 2843 |
sign = -1; |
| 2844 |
p += 1; |
| 2845 |
} |
| 2846 |
state = final_space; |
| 2847 |
break; |
| 2848 |
|
| 2849 |
case final_space : |
| 2850 |
// There should be only spaces until the end |
| 2851 |
if( ch == internal_space ) |
| 2852 |
{ |
| 2853 |
continue; |
| 2854 |
} |
| 2855 |
// We have a non-space where there should be only space |
| 2856 |
state = final_space; |
| 2857 |
errcode = p - pstart; |
| 2858 |
p = pend; |
| 2859 |
break; |
| 2860 |
} |
| 2861 |
} |
| 2862 |
if( sign == 0 ) |
| 2863 |
{ |
| 2864 |
sign = 1; |
| 2865 |
} |
| 2866 |
retval *= sign; |
| 2867 |
|
| 2868 |
if( state != after_digits && state != final_space && state != digits ) |
| 2869 |
{ |
| 2870 |
// We broke out of the loop too soon: |
| 2871 |
errcode = pend - pstart + 1; |
| 2872 |
} |
| 2873 |
|
| 2874 |
if( dest ) |
| 2875 |
{ |
| 2876 |
retval /= __gg__power_of_ten(rdigits); |
| 2877 |
__gg__float128_to_field(dest, |
| 2878 |
retval, |
| 2879 |
truncation_e, |
| 2880 |
NULL); |
| 2881 |
} |
| 2882 |
return (int)errcode; |
| 2883 |
} |
| 2884 |
|
| 2885 |
extern "C" |
| 2886 |
void |
| 2887 |
__gg__numval( cblc_field_t *dest, |
| 2888 |
const cblc_field_t *source, |
| 2889 |
size_t source_offset, |
| 2890 |
size_t source_size) |
| 2891 |
{ |
| 2892 |
int errpos = numval(dest, source, source_offset, source_size); |
| 2893 |
if( errpos ) |
| 2894 |
{ |
| 2895 |
exception_raise(ec_argument_function_e); |
| 2896 |
} |
| 2897 |
} |
| 2898 |
|
| 2899 |
extern "C" |
| 2900 |
void |
| 2901 |
__gg__test_numval(cblc_field_t *dest, |
| 2902 |
const cblc_field_t *source, |
| 2903 |
size_t source_offset, |
| 2904 |
size_t source_size) |
| 2905 |
{ |
| 2906 |
int retval = numval(NULL, source, source_offset, source_size); |
| 2907 |
__gg__int128_to_field(dest, |
| 2908 |
retval, |
| 2909 |
NO_RDIGITS, |
| 2910 |
truncation_e, |
| 2911 |
NULL); |
| 2912 |
} |
| 2913 |
|
| 2914 |
extern "C" |
| 2915 |
void |
| 2916 |
__gg__numval_c( cblc_field_t *dest, |
| 2917 |
const cblc_field_t *src, |
| 2918 |
size_t src_offset, |
| 2919 |
size_t src_size, |
| 2920 |
const cblc_field_t *crcy, |
| 2921 |
size_t crcy_offset, |
| 2922 |
size_t crcy_size |
| 2923 |
) |
| 2924 |
{ |
| 2925 |
numval_c( dest, |
| 2926 |
src, |
| 2927 |
src_offset, |
| 2928 |
src_size, |
| 2929 |
crcy, |
| 2930 |
crcy_offset, |
| 2931 |
crcy_size); |
| 2932 |
} |
| 2933 |
|
| 2934 |
extern "C" |
| 2935 |
void |
| 2936 |
__gg__test_numval_c(cblc_field_t *dest, |
| 2937 |
const cblc_field_t *src, |
| 2938 |
size_t src_offset, |
| 2939 |
size_t src_size, |
| 2940 |
const cblc_field_t *crcy, |
| 2941 |
size_t crcy_offset, |
| 2942 |
size_t crcy_size |
| 2943 |
) |
| 2944 |
{ |
| 2945 |
int retval = numval_c(NULL, |
| 2946 |
src, |
| 2947 |
src_offset, |
| 2948 |
src_size, |
| 2949 |
crcy, |
| 2950 |
crcy_offset, |
| 2951 |
crcy_size); |
| 2952 |
__gg__int128_to_field(dest, |
| 2953 |
retval, |
| 2954 |
NO_RDIGITS, |
| 2955 |
truncation_e, |
| 2956 |
NULL); |
| 2957 |
} |
| 2958 |
|
| 2959 |
extern "C" |
| 2960 |
void |
| 2961 |
__gg__ord(cblc_field_t *dest, |
| 2962 |
const cblc_field_t *input, |
| 2963 |
size_t input_offset, |
| 2964 |
size_t /*input_size*/) |
| 2965 |
{ |
| 2966 |
// We get our input in internal_character form. |
| 2967 |
const char *arg = PTRCAST(char, (input->data + input_offset)); |
| 2968 |
|
| 2969 |
// The ORD function takes a single-character string and returns the |
| 2970 |
// ordinal position of that character. |
| 2971 |
|
| 2972 |
// In ASCII mode, an A is 0x41, so we return 0x42 |
| 2973 |
// In EBCDIC mode, an A is 0xC1, so we return 0xC2 |
| 2974 |
|
| 2975 |
size_t retval = (arg[0]&0xFF) + 1; |
| 2976 |
__gg__int128_to_field(dest, |
| 2977 |
retval, |
| 2978 |
NO_RDIGITS, |
| 2979 |
truncation_e, |
| 2980 |
NULL); |
| 2981 |
} |
| 2982 |
|
| 2983 |
extern "C" |
| 2984 |
void |
| 2985 |
__gg__ord_min(cblc_field_t *dest, |
| 2986 |
size_t ninputs) |
| 2987 |
{ |
| 2988 |
// Sets dest to the one-based ordinal position of the first occurrence |
| 2989 |
// of the biggest element in the list of refs[] |
| 2990 |
|
| 2991 |
int retval = -1; |
| 2992 |
int running_position = -1; |
| 2993 |
|
| 2994 |
cblc_field_t *best; |
| 2995 |
unsigned char *best_location; |
| 2996 |
size_t best_length; |
| 2997 |
int best_attr; |
| 2998 |
int best_flags; |
| 2999 |
|
| 3000 |
unsigned char *candidate_location; |
| 3001 |
size_t candidate_length; |
| 3002 |
int candidate_attr; |
| 3003 |
int candidate_flags; |
| 3004 |
|
| 3005 |
for( size_t i=0; i<ninputs; i++ ) |
| 3006 |
{ |
| 3007 |
refer_state_for_all state; |
| 3008 |
|
| 3009 |
build_refer_state_for_all(state, __gg__treeplet_1f[i], __gg__fourplet_flags[i]); |
| 3010 |
for(;;) |
| 3011 |
{ |
| 3012 |
running_position += 1; |
| 3013 |
if( retval == -1) |
| 3014 |
{ |
| 3015 |
// We have to initialize the comparisons: |
| 3016 |
retval = running_position; |
| 3017 |
best = __gg__treeplet_1f[i]; |
| 3018 |
best_location = __gg__treeplet_1f[i]->data + __gg__treeplet_1o[i]; |
| 3019 |
best_length = __gg__treeplet_1s[i]; |
| 3020 |
best_attr = __gg__treeplet_1f[i]->attr; |
| 3021 |
best_flags = __gg__fourplet_flags[i]; |
| 3022 |
} |
| 3023 |
else |
| 3024 |
{ |
| 3025 |
// We need to save the current adjustments, because __gg__compare |
| 3026 |
// is free to modify .location |
| 3027 |
candidate_location = __gg__treeplet_1f[i]->data + __gg__treeplet_1o[i]; |
| 3028 |
candidate_length = __gg__treeplet_1s[i]; |
| 3029 |
candidate_attr = __gg__treeplet_1f[i]->attr; |
| 3030 |
candidate_flags = __gg__fourplet_flags[i]; |
| 3031 |
|
| 3032 |
int compare_result = |
| 3033 |
__gg__compare_2( |
| 3034 |
__gg__treeplet_1f[i], |
| 3035 |
candidate_location, |
| 3036 |
candidate_length, |
| 3037 |
candidate_attr, |
| 3038 |
candidate_flags, |
| 3039 |
best, |
| 3040 |
best_location, |
| 3041 |
best_length, |
| 3042 |
best_attr, |
| 3043 |
best_flags, |
| 3044 |
0); |
| 3045 |
if( compare_result < 0 ) |
| 3046 |
{ |
| 3047 |
retval = running_position; |
| 3048 |
best = __gg__treeplet_1f[i]; |
| 3049 |
best_location = candidate_location; |
| 3050 |
best_length = candidate_length; |
| 3051 |
best_attr = candidate_attr; |
| 3052 |
best_flags = candidate_flags; |
| 3053 |
} |
| 3054 |
} |
| 3055 |
if( !update_refer_state_for_all(state, __gg__treeplet_1f[i]) ) |
| 3056 |
{ |
| 3057 |
// There is nothing left to do for that input. |
| 3058 |
break; |
| 3059 |
} |
| 3060 |
} |
| 3061 |
} |
| 3062 |
|
| 3063 |
retval += 1; |
| 3064 |
__gg__int128_to_field(dest, |
| 3065 |
retval, |
| 3066 |
NO_RDIGITS, |
| 3067 |
truncation_e, |
| 3068 |
NULL); |
| 3069 |
} |
| 3070 |
|
| 3071 |
extern "C" |
| 3072 |
void |
| 3073 |
__gg__ord_max(cblc_field_t *dest, |
| 3074 |
size_t ninputs) |
| 3075 |
{ |
| 3076 |
// Sets dest to the one-based ordinal position of the first occurrence |
| 3077 |
// of the biggest element in the list of refs[] |
| 3078 |
|
| 3079 |
int retval = -1; |
| 3080 |
int running_position = -1; |
| 3081 |
|
| 3082 |
cblc_field_t *best; |
| 3083 |
unsigned char *best_location; |
| 3084 |
size_t best_length; |
| 3085 |
int best_attr; |
| 3086 |
int best_flags; |
| 3087 |
|
| 3088 |
unsigned char *candidate_location; |
| 3089 |
size_t candidate_length; |
| 3090 |
int candidate_attr; |
| 3091 |
int candidate_flags; |
| 3092 |
|
| 3093 |
for( size_t i=0; i<ninputs; i++ ) |
| 3094 |
{ |
| 3095 |
refer_state_for_all state; |
| 3096 |
|
| 3097 |
build_refer_state_for_all(state, __gg__treeplet_1f[i], __gg__fourplet_flags[i]); |
| 3098 |
for(;;) |
| 3099 |
{ |
| 3100 |
running_position += 1; |
| 3101 |
if( retval == -1) |
| 3102 |
{ |
| 3103 |
// We have to initialize the comparisons: |
| 3104 |
retval = running_position; |
| 3105 |
best = __gg__treeplet_1f[i]; |
| 3106 |
best_location = __gg__treeplet_1f[i]->data + __gg__treeplet_1o[i]; |
| 3107 |
best_length = __gg__treeplet_1s[i]; |
| 3108 |
best_attr = __gg__treeplet_1f[i]->attr; |
| 3109 |
best_flags = __gg__fourplet_flags[i]; |
| 3110 |
} |
| 3111 |
else |
| 3112 |
{ |
| 3113 |
// We need to save the current adjustments, because __gg__compare |
| 3114 |
// is free to modify .location |
| 3115 |
candidate_location = __gg__treeplet_1f[i]->data + __gg__treeplet_1o[i]; |
| 3116 |
candidate_length = __gg__treeplet_1s[i]; |
| 3117 |
candidate_attr = __gg__treeplet_1f[i]->attr; |
| 3118 |
candidate_flags = __gg__fourplet_flags[i]; |
| 3119 |
|
| 3120 |
int compare_result = |
| 3121 |
__gg__compare_2( |
| 3122 |
__gg__treeplet_1f[i], |
| 3123 |
candidate_location, |
| 3124 |
candidate_length, |
| 3125 |
candidate_attr, |
| 3126 |
candidate_flags, |
| 3127 |
best, |
| 3128 |
best_location, |
| 3129 |
best_length, |
| 3130 |
best_attr, |
| 3131 |
best_flags, |
| 3132 |
0); |
| 3133 |
if( compare_result > 0 ) |
| 3134 |
{ |
| 3135 |
retval = running_position; |
| 3136 |
best = __gg__treeplet_1f[i]; |
| 3137 |
best_location = candidate_location; |
| 3138 |
best_length = candidate_length; |
| 3139 |
best_attr = candidate_attr; |
| 3140 |
best_flags = candidate_flags; |
| 3141 |
} |
| 3142 |
} |
| 3143 |
if( !update_refer_state_for_all(state, __gg__treeplet_1f[i]) ) |
| 3144 |
{ |
| 3145 |
// There is nothing left to do for that input. |
| 3146 |
break; |
| 3147 |
} |
| 3148 |
} |
| 3149 |
} |
| 3150 |
|
| 3151 |
retval += 1; // Make the result one-based, as per COBOL specification |
| 3152 |
__gg__int128_to_field(dest, |
| 3153 |
retval, |
| 3154 |
NO_RDIGITS, |
| 3155 |
truncation_e, |
| 3156 |
NULL); |
| 3157 |
} |
| 3158 |
|
| 3159 |
extern "C" |
| 3160 |
void |
| 3161 |
__gg__pi(cblc_field_t *dest) |
| 3162 |
{ |
| 3163 |
// FUNCTION PI |
| 3164 |
|
| 3165 |
static GCOB_FP128 pi |
| 3166 |
= GCOB_FP128_LITERAL(3.141592653589793238462643383279502884); |
| 3167 |
__gg__float128_to_field(dest, |
| 3168 |
pi, |
| 3169 |
truncation_e, |
| 3170 |
NULL); |
| 3171 |
} |
| 3172 |
|
| 3173 |
extern "C" |
| 3174 |
void |
| 3175 |
__gg__present_value(cblc_field_t *dest, |
| 3176 |
size_t ncount) |
| 3177 |
{ |
| 3178 |
GCOB_FP128 discount = 0;; |
| 3179 |
GCOB_FP128 denom = 1; |
| 3180 |
|
| 3181 |
GCOB_FP128 retval = 0; |
| 3182 |
bool first_time = true; |
| 3183 |
for(size_t i=0; i<ncount; i++) |
| 3184 |
{ |
| 3185 |
refer_state_for_all state; |
| 3186 |
build_refer_state_for_all(state, __gg__treeplet_1f[i], __gg__fourplet_flags[i]); |
| 3187 |
for(;;) |
| 3188 |
{ |
| 3189 |
if(first_time) |
| 3190 |
{ |
| 3191 |
first_time = false; |
| 3192 |
GCOB_FP128 arg1 = __gg__float128_from_qualified_field(__gg__treeplet_1f[i], |
| 3193 |
__gg__treeplet_1o[i], |
| 3194 |
__gg__treeplet_1s[i]); |
| 3195 |
if( arg1 <= GCOB_FP128_LITERAL(-1.0) ) |
| 3196 |
{ |
| 3197 |
exception_raise(ec_argument_function_e); |
| 3198 |
break; |
| 3199 |
} |
| 3200 |
discount = GCOB_FP128_LITERAL(1.0) / (GCOB_FP128_LITERAL(1.0) + arg1); |
| 3201 |
} |
| 3202 |
else |
| 3203 |
{ |
| 3204 |
GCOB_FP128 arg = __gg__float128_from_qualified_field(__gg__treeplet_1f[i], |
| 3205 |
__gg__treeplet_1o[i], |
| 3206 |
__gg__treeplet_1s[i]); |
| 3207 |
denom *= discount; |
| 3208 |
retval += arg * denom; |
| 3209 |
} |
| 3210 |
if( !update_refer_state_for_all(state, __gg__treeplet_1f[i]) ) |
| 3211 |
{ |
| 3212 |
// There is nothing left to do for that input. |
| 3213 |
break; |
| 3214 |
} |
| 3215 |
} |
| 3216 |
} |
| 3217 |
__gg__float128_to_field(dest, |
| 3218 |
retval, |
| 3219 |
truncation_e, |
| 3220 |
NULL); |
| 3221 |
} |
| 3222 |
|
| 3223 |
extern "C" |
| 3224 |
void |
| 3225 |
__gg__range(cblc_field_t *dest, |
| 3226 |
size_t ncount) |
| 3227 |
{ |
| 3228 |
// FUNCTION RANGE |
| 3229 |
bool first_time = true; |
| 3230 |
GCOB_FP128 val; |
| 3231 |
GCOB_FP128 min; |
| 3232 |
GCOB_FP128 max; |
| 3233 |
|
| 3234 |
assert(ncount > 0); |
| 3235 |
for(size_t i=0; i<ncount; i++) |
| 3236 |
{ |
| 3237 |
refer_state_for_all state; |
| 3238 |
build_refer_state_for_all(state, __gg__treeplet_1f[i], __gg__fourplet_flags[i]); |
| 3239 |
for(;;) |
| 3240 |
{ |
| 3241 |
val = __gg__float128_from_qualified_field(__gg__treeplet_1f[i], |
| 3242 |
__gg__treeplet_1o[i], |
| 3243 |
__gg__treeplet_1s[i]); |
| 3244 |
if( first_time ) |
| 3245 |
{ |
| 3246 |
first_time = false; |
| 3247 |
min = val; |
| 3248 |
max = val; |
| 3249 |
} |
| 3250 |
min = std::min(min, val); |
| 3251 |
max = std::max(max, val); |
| 3252 |
if( !update_refer_state_for_all(state, __gg__treeplet_1f[i]) ) |
| 3253 |
{ |
| 3254 |
// There is nothing left to do. |
| 3255 |
break; |
| 3256 |
} |
| 3257 |
} |
| 3258 |
} |
| 3259 |
|
| 3260 |
GCOB_FP128 retval = max - min; |
| 3261 |
__gg__float128_to_field(dest, |
| 3262 |
retval, |
| 3263 |
truncation_e, |
| 3264 |
NULL); |
| 3265 |
} |
| 3266 |
|
| 3267 |
extern "C" |
| 3268 |
void |
| 3269 |
__gg__rem(cblc_field_t *dest, |
| 3270 |
const cblc_field_t *par1, |
| 3271 |
size_t par1_offset, |
| 3272 |
size_t par1_size, |
| 3273 |
const cblc_field_t *par2, |
| 3274 |
size_t par2_offset, |
| 3275 |
size_t par2_size) |
| 3276 |
{ |
| 3277 |
// FUNCTION REM |
| 3278 |
// |
| 3279 |
// Note: REM takes two NUMERICS, not necessarily integers |
| 3280 |
|
| 3281 |
// The ISO spec says: |
| 3282 |
// ((argument-1) – ((argument-2) * FUNCTION INTEGER-PART ((argument-1) / (argument-2)))) |
| 3283 |
|
| 3284 |
GCOB_FP128 arg1 = __gg__float128_from_qualified_field( par1, |
| 3285 |
par1_offset, |
| 3286 |
par1_size); |
| 3287 |
GCOB_FP128 arg2 = __gg__float128_from_qualified_field( par2, |
| 3288 |
par2_offset, |
| 3289 |
par2_size); |
| 3290 |
|
| 3291 |
GCOB_FP128 intpart; |
| 3292 |
GCOB_FP128 retval; |
| 3293 |
if( arg2 == 0 ) |
| 3294 |
{ |
| 3295 |
exception_raise(ec_argument_function_e); |
| 3296 |
retval = 0; |
| 3297 |
} |
| 3298 |
else |
| 3299 |
{ |
| 3300 |
FP128_FUNC(modf)(arg1 / arg2, &intpart); |
| 3301 |
retval = arg1 - arg2 * intpart; |
| 3302 |
} |
| 3303 |
|
| 3304 |
__gg__float128_to_field(dest, |
| 3305 |
retval, |
| 3306 |
truncation_e, |
| 3307 |
NULL); |
| 3308 |
} |
| 3309 |
|
| 3310 |
extern "C" |
| 3311 |
void |
| 3312 |
__gg__trim( cblc_field_t *dest, |
| 3313 |
const cblc_field_t *arg1, |
| 3314 |
size_t arg1_offset, |
| 3315 |
size_t arg1_size, |
| 3316 |
const cblc_field_t *arg2, |
| 3317 |
size_t arg2_offset, |
| 3318 |
size_t arg2_size) |
| 3319 |
{ |
| 3320 |
int rdigits; |
| 3321 |
__int128 type = __gg__binary_value_from_qualified_field(&rdigits, |
| 3322 |
arg2, |
| 3323 |
arg2_offset, |
| 3324 |
arg2_size); |
| 3325 |
//static const int BOTH = 0; |
| 3326 |
static const int LEADING = 1; // Remove leading spaces |
| 3327 |
static const int TRAILING = 2; // Remove trailing spaces |
| 3328 |
|
| 3329 |
if( dest->type != FldAlphanumeric || |
| 3330 |
!(dest->attr & intermediate_e) ) |
| 3331 |
{ |
| 3332 |
fprintf(stderr, |
| 3333 |
"We expect the target of a FUNCTION TIME to " |
| 3334 |
"be an intermediate alphanumeric\n"); |
| 3335 |
abort(); |
| 3336 |
} |
| 3337 |
dest->capacity = dest->offset; |
| 3338 |
|
| 3339 |
// No matter what, we want to find the leftmost non-space and the |
| 3340 |
// rightmost non-space: |
| 3341 |
|
| 3342 |
char *left = PTRCAST(char, (arg1->data+arg1_offset)); |
| 3343 |
char *right = left + arg1_size-1; |
| 3344 |
|
| 3345 |
// Find left and right: the first and last non-spaces |
| 3346 |
while( left <= right ) |
| 3347 |
{ |
| 3348 |
if( *left != internal_space && *right != internal_space ) |
| 3349 |
{ |
| 3350 |
break; |
| 3351 |
} |
| 3352 |
if( *left == internal_space ) |
| 3353 |
{ |
| 3354 |
left += 1; |
| 3355 |
} |
| 3356 |
if( *right == internal_space ) |
| 3357 |
{ |
| 3358 |
right -= 1; |
| 3359 |
} |
| 3360 |
} |
| 3361 |
if( type == LEADING ) |
| 3362 |
{ |
| 3363 |
// We want to leave any trailing spaces, so we return 'right' to its |
| 3364 |
// original value: |
| 3365 |
right = PTRCAST(char, (arg1->data+arg1_offset)) + arg1_size-1; |
| 3366 |
} |
| 3367 |
else if( type == TRAILING ) |
| 3368 |
{ |
| 3369 |
// We want to leave any leading spaces, so we return 'left' to its |
| 3370 |
// original value: |
| 3371 |
left = PTRCAST(char, (arg1->data+arg1_offset)); |
| 3372 |
} |
| 3373 |
|
| 3374 |
if( left > right ) |
| 3375 |
{ |
| 3376 |
// When the arg1 input string was empty, we want left to be right+1. |
| 3377 |
// The left/right loop can sometimes end up with left equal to right+2. |
| 3378 |
// That needs to be fixed: |
| 3379 |
left = right+1; |
| 3380 |
} |
| 3381 |
|
| 3382 |
size_t ncount = right+1 - left; |
| 3383 |
__gg__adjust_dest_size(dest, ncount); |
| 3384 |
|
| 3385 |
// Because it's a temporary, we are weakly confident that we can change |
| 3386 |
// the capacity to match what we want. At this writing, we aren't 100% |
| 3387 |
// sure of the implications of the run-time capacity not matching what the |
| 3388 |
// compiler believes the capacity to be at compile-time. But we obviously |
| 3389 |
// think it'll be okay. |
| 3390 |
|
| 3391 |
char *dest_left = PTRCAST(char, dest->data); |
| 3392 |
char *dest_right = dest_left + dest->capacity - 1; |
| 3393 |
const char *dest_end = dest_left + dest->capacity; |
| 3394 |
|
| 3395 |
while( dest_left <= dest_right && left <= right ) |
| 3396 |
{ |
| 3397 |
*dest_left++ = *left++; |
| 3398 |
} |
| 3399 |
while(dest_left < dest_end) |
| 3400 |
{ |
| 3401 |
*dest_left++ = internal_space; |
| 3402 |
} |
| 3403 |
} |
| 3404 |
|
| 3405 |
#if HAVE_INITSTATE_R && HAVE_SRANDOM_R && HAVE_RANDOM_R |
| 3406 |
static struct random_data *buf = NULL; |
| 3407 |
static char *state = NULL; |
| 3408 |
static const size_t state_len = 256; |
| 3409 |
#else |
| 3410 |
static unsigned seed = 0; |
| 3411 |
#endif |
| 3412 |
|
| 3413 |
extern "C" |
| 3414 |
void |
| 3415 |
__gg__random( cblc_field_t *dest, |
| 3416 |
const cblc_field_t *input, |
| 3417 |
size_t input_offset, |
| 3418 |
size_t input_size) |
| 3419 |
{ |
| 3420 |
int32_t retval_31; |
| 3421 |
int rdigits; |
| 3422 |
#if HAVE_INITSTATE_R && HAVE_SRANDOM_R && HAVE_RANDOM_R |
| 3423 |
// This creates a thread-safe pseudo-random number generator |
| 3424 |
// using input as the seed |
| 3425 |
|
| 3426 |
// The return value is between zero and not quite one |
| 3427 |
|
| 3428 |
if( !buf ) |
| 3429 |
{ |
| 3430 |
// This is the very first time through |
| 3431 |
buf = (random_data *)malloc(sizeof(struct random_data)); |
| 3432 |
buf->state = NULL; |
| 3433 |
state = (char *)malloc(state_len); |
| 3434 |
|
| 3435 |
struct cbl_timespec ts; |
| 3436 |
__gg__clock_gettime(&ts); |
| 3437 |
initstate_r( ts.tv_nsec, state, state_len, buf); |
| 3438 |
} |
| 3439 |
int seed = (int)__gg__binary_value_from_qualified_field(&rdigits, |
| 3440 |
input, |
| 3441 |
input_offset, |
| 3442 |
input_size); |
| 3443 |
srandom_r(seed, buf); |
| 3444 |
|
| 3445 |
random_r(buf, &retval_31); |
| 3446 |
#else |
| 3447 |
seed = (unsigned)__gg__binary_value_from_qualified_field(&rdigits, |
| 3448 |
input, |
| 3449 |
input_offset, |
| 3450 |
input_size); |
| 3451 |
srandom (seed); |
| 3452 |
retval_31 = random (); |
| 3453 |
#endif |
| 3454 |
// We are going to convert this to a value between zero and not quite one: |
| 3455 |
double retval = double(retval_31) / double(0x80000000UL); |
| 3456 |
__gg__double_to_target( dest, |
| 3457 |
retval, |
| 3458 |
truncation_e); |
| 3459 |
} |
| 3460 |
|
| 3461 |
extern "C" |
| 3462 |
void |
| 3463 |
__gg__random_next(cblc_field_t *dest) |
| 3464 |
{ |
| 3465 |
int32_t retval_31; |
| 3466 |
#if HAVE_INITSTATE_R && HAVE_SRANDOM_R && HAVE_RANDOM_R |
| 3467 |
// The return value is between zero and not quite one |
| 3468 |
|
| 3469 |
if( !buf ) |
| 3470 |
{ |
| 3471 |
// This is the very first time through |
| 3472 |
buf = (random_data *)malloc(sizeof(struct random_data)); |
| 3473 |
buf->state = NULL; |
| 3474 |
state = (char *)malloc(state_len); |
| 3475 |
struct cbl_timespec ts; |
| 3476 |
__gg__clock_gettime(&ts); |
| 3477 |
initstate_r( ts.tv_nsec, state, state_len, buf); |
| 3478 |
} |
| 3479 |
random_r(buf, &retval_31); |
| 3480 |
#else |
| 3481 |
retval_31 = random (); |
| 3482 |
#endif |
| 3483 |
// We are going to convert this to a value between zero and not quite one: |
| 3484 |
double retval = double(retval_31) / double(0x80000000UL); |
| 3485 |
__gg__double_to_target( dest, |
| 3486 |
retval, |
| 3487 |
truncation_e); |
| 3488 |
} |
| 3489 |
|
| 3490 |
extern "C" |
| 3491 |
void |
| 3492 |
__gg__reverse(cblc_field_t *dest, |
| 3493 |
const cblc_field_t *input, |
| 3494 |
size_t input_offset, |
| 3495 |
size_t input_size) |
| 3496 |
{ |
| 3497 |
size_t dest_length = dest->capacity; |
| 3498 |
size_t source_length = input_size; |
| 3499 |
size_t length = std::min(dest_length, source_length); |
| 3500 |
memset(dest->data, internal_space, dest_length); |
| 3501 |
for(size_t i=0; i<length; i++) |
| 3502 |
{ |
| 3503 |
dest->data[i] = (input->data+input_offset)[source_length-1-i]; |
| 3504 |
} |
| 3505 |
if( (dest->attr & intermediate_e) ) |
| 3506 |
{ |
| 3507 |
dest->capacity = std::min(dest_length, source_length); |
| 3508 |
} |
| 3509 |
} |
| 3510 |
|
| 3511 |
extern "C" |
| 3512 |
void |
| 3513 |
__gg__sign( cblc_field_t *dest, |
| 3514 |
const cblc_field_t *source, |
| 3515 |
size_t source_offset, |
| 3516 |
size_t source_size) |
| 3517 |
{ |
| 3518 |
// FUNCTION SIGN |
| 3519 |
|
| 3520 |
GCOB_FP128 value = __gg__float128_from_qualified_field(source, |
| 3521 |
source_offset, |
| 3522 |
source_size); |
| 3523 |
|
| 3524 |
int retval; |
| 3525 |
if(value > 0) |
| 3526 |
{ |
| 3527 |
retval = 1; |
| 3528 |
} |
| 3529 |
else if(value < 0) |
| 3530 |
{ |
| 3531 |
retval = -1; |
| 3532 |
} |
| 3533 |
else |
| 3534 |
{ |
| 3535 |
retval = 0; |
| 3536 |
} |
| 3537 |
__gg__int128_to_field(dest, |
| 3538 |
retval, |
| 3539 |
NO_RDIGITS, |
| 3540 |
truncation_e, |
| 3541 |
NULL); |
| 3542 |
} |
| 3543 |
|
| 3544 |
extern "C" |
| 3545 |
void |
| 3546 |
__gg__sin(cblc_field_t *dest, |
| 3547 |
const cblc_field_t *source, |
| 3548 |
size_t source_offset, |
| 3549 |
size_t source_size) |
| 3550 |
{ |
| 3551 |
// FUNCTION SIN |
| 3552 |
|
| 3553 |
GCOB_FP128 value = __gg__float128_from_qualified_field(source, |
| 3554 |
source_offset, |
| 3555 |
source_size); |
| 3556 |
|
| 3557 |
value = FP128_FUNC(sin)(value); |
| 3558 |
|
| 3559 |
__gg__float128_to_field(dest, |
| 3560 |
value, |
| 3561 |
truncation_e, |
| 3562 |
NULL); |
| 3563 |
} |
| 3564 |
|
| 3565 |
extern "C" |
| 3566 |
void |
| 3567 |
__gg__sqrt( cblc_field_t *dest, |
| 3568 |
const cblc_field_t *source, |
| 3569 |
size_t source_offset, |
| 3570 |
size_t source_size) |
| 3571 |
{ |
| 3572 |
// FUNCTION SQRT |
| 3573 |
|
| 3574 |
GCOB_FP128 value = __gg__float128_from_qualified_field(source, |
| 3575 |
source_offset, |
| 3576 |
source_size); |
| 3577 |
|
| 3578 |
if( value < GCOB_FP128_LITERAL(0.0) ) |
| 3579 |
{ |
| 3580 |
exception_raise(ec_argument_function_e); |
| 3581 |
} |
| 3582 |
else |
| 3583 |
{ |
| 3584 |
value = FP128_FUNC(sqrt)(value); |
| 3585 |
} |
| 3586 |
|
| 3587 |
__gg__float128_to_field(dest, |
| 3588 |
value, |
| 3589 |
truncation_e, |
| 3590 |
NULL); |
| 3591 |
} |
| 3592 |
|
| 3593 |
extern "C" |
| 3594 |
void |
| 3595 |
__gg__standard_deviation( cblc_field_t *dest, |
| 3596 |
size_t ninputs) |
| 3597 |
{ |
| 3598 |
// FUNCTION STANDARD-DEVIATION |
| 3599 |
GCOB_FP128 retval = variance(ninputs, |
| 3600 |
__gg__treeplet_1f, |
| 3601 |
__gg__treeplet_1o, |
| 3602 |
__gg__treeplet_1s, |
| 3603 |
__gg__fourplet_flags); |
| 3604 |
retval = FP128_FUNC(sqrt)(retval); |
| 3605 |
|
| 3606 |
__gg__float128_to_field(dest, |
| 3607 |
retval, |
| 3608 |
truncation_e, |
| 3609 |
NULL); |
| 3610 |
} |
| 3611 |
|
| 3612 |
extern "C" |
| 3613 |
void |
| 3614 |
__gg__sum(cblc_field_t *dest, |
| 3615 |
size_t ninputs) |
| 3616 |
{ |
| 3617 |
// FUNCTION SUM |
| 3618 |
size_t k_count; |
| 3619 |
GCOB_FP128 sum = kahan_summation(ninputs, |
| 3620 |
__gg__treeplet_1f, |
| 3621 |
__gg__treeplet_1o, |
| 3622 |
__gg__treeplet_1s, |
| 3623 |
__gg__fourplet_flags, |
| 3624 |
&k_count); |
| 3625 |
__gg__float128_to_field(dest, |
| 3626 |
sum, |
| 3627 |
truncation_e, |
| 3628 |
NULL); |
| 3629 |
} |
| 3630 |
|
| 3631 |
extern "C" |
| 3632 |
void |
| 3633 |
__gg__tan(cblc_field_t *dest, |
| 3634 |
const cblc_field_t *source, |
| 3635 |
size_t source_offset, |
| 3636 |
size_t source_size) |
| 3637 |
{ |
| 3638 |
// FUNCTION TAN |
| 3639 |
|
| 3640 |
GCOB_FP128 value = __gg__float128_from_qualified_field(source, |
| 3641 |
source_offset, |
| 3642 |
source_size); |
| 3643 |
value = FP128_FUNC(tan)(value); |
| 3644 |
__gg__float128_to_field(dest, |
| 3645 |
value, |
| 3646 |
truncation_e, |
| 3647 |
NULL); |
| 3648 |
} |
| 3649 |
|
| 3650 |
extern "C" |
| 3651 |
void |
| 3652 |
__gg__test_date_yyyymmdd( cblc_field_t *dest, |
| 3653 |
const cblc_field_t *source, |
| 3654 |
size_t source_offset, |
| 3655 |
size_t source_size) |
| 3656 |
{ |
| 3657 |
int rdigits; |
| 3658 |
int yyyymmdd = (int)__gg__binary_value_from_qualified_field(&rdigits, |
| 3659 |
source, |
| 3660 |
source_offset, |
| 3661 |
source_size); |
| 3662 |
int retval; |
| 3663 |
int mmdd = yyyymmdd % 10000; |
| 3664 |
int mm = mmdd / 100; |
| 3665 |
if( yyyymmdd < 16010000 || yyyymmdd > 99999999 ) |
| 3666 |
{ |
| 3667 |
retval = 1; |
| 3668 |
} |
| 3669 |
else if( mm < 1 || mm > 12 ) |
| 3670 |
{ |
| 3671 |
retval = 2; |
| 3672 |
} |
| 3673 |
else |
| 3674 |
{ |
| 3675 |
int dd = yyyymmdd % 100; |
| 3676 |
int yyyy = yyyymmdd / 10000; |
| 3677 |
int jy; |
| 3678 |
int jm; |
| 3679 |
int jd; |
| 3680 |
double JD; |
| 3681 |
|
| 3682 |
// If there is something wrong with the number of days per month for a |
| 3683 |
// given year, the Julian Date conversion won't reverse properly. |
| 3684 |
// For example, January 32 will come back as February 1 |
| 3685 |
JD = YMD_to_JD(yyyy, mm, dd); |
| 3686 |
JD_to_YMD(jy, jm, jd, JD); |
| 3687 |
if( jd == dd && jm == mm && jy == yyyy ) |
| 3688 |
{ |
| 3689 |
retval = 0; |
| 3690 |
} |
| 3691 |
else |
| 3692 |
{ |
| 3693 |
retval = 3; |
| 3694 |
} |
| 3695 |
} |
| 3696 |
__gg__int128_to_field(dest, |
| 3697 |
retval, |
| 3698 |
NO_RDIGITS, |
| 3699 |
truncation_e, |
| 3700 |
NULL); |
| 3701 |
} |
| 3702 |
|
| 3703 |
extern "C" |
| 3704 |
void |
| 3705 |
__gg__test_day_yyyyddd( cblc_field_t *dest, |
| 3706 |
const cblc_field_t *source, |
| 3707 |
size_t source_offset, |
| 3708 |
size_t source_size) |
| 3709 |
{ |
| 3710 |
int rdigits; |
| 3711 |
int yyyyddd = (int)__gg__binary_value_from_qualified_field(&rdigits, |
| 3712 |
source, |
| 3713 |
source_offset, |
| 3714 |
source_size); |
| 3715 |
int retval; |
| 3716 |
int ddd = yyyyddd % 1000; |
| 3717 |
int yyyy = yyyyddd / 1000; |
| 3718 |
int days_in_year; |
| 3719 |
|
| 3720 |
days_in_year = is_leap_year(yyyy); |
| 3721 |
|
| 3722 |
if( yyyyddd < 1601000 || yyyyddd > 9999999 ) |
| 3723 |
{ |
| 3724 |
retval = 1; |
| 3725 |
} |
| 3726 |
else if( ddd < 1 || ddd > days_in_year) |
| 3727 |
{ |
| 3728 |
retval = 2; |
| 3729 |
} |
| 3730 |
else |
| 3731 |
{ |
| 3732 |
retval = 0; |
| 3733 |
} |
| 3734 |
__gg__int128_to_field(dest, |
| 3735 |
retval, |
| 3736 |
NO_RDIGITS, |
| 3737 |
truncation_e, |
| 3738 |
NULL); |
| 3739 |
} |
| 3740 |
|
| 3741 |
extern "C" |
| 3742 |
void |
| 3743 |
__gg__upper_case( cblc_field_t *dest, |
| 3744 |
const cblc_field_t *input, |
| 3745 |
size_t input_offset, |
| 3746 |
size_t input_size) |
| 3747 |
{ |
| 3748 |
size_t dest_length = dest->capacity; |
| 3749 |
size_t source_length = input_size; |
| 3750 |
memset(dest->data, internal_space, dest_length); |
| 3751 |
memcpy(dest->data, input->data+input_offset, std::min(dest_length, source_length)); |
| 3752 |
internal_to_ascii( PTRCAST(char, dest->data), dest_length); |
| 3753 |
std::transform(dest->data, dest->data + dest_length, dest->data, |
| 3754 |
[](unsigned char c) { return std::toupper(c); }); |
| 3755 |
ascii_to_internal_str( PTRCAST(char, dest->data), dest_length); |
| 3756 |
} |
| 3757 |
|
| 3758 |
extern "C" |
| 3759 |
void |
| 3760 |
__gg__variance( cblc_field_t *dest, |
| 3761 |
size_t ncount) |
| 3762 |
{ |
| 3763 |
// FUNCTION VARIANCE |
| 3764 |
GCOB_FP128 retval = variance(ncount, |
| 3765 |
__gg__treeplet_1f, |
| 3766 |
__gg__treeplet_1o, |
| 3767 |
__gg__treeplet_1s, |
| 3768 |
__gg__fourplet_flags); |
| 3769 |
__gg__float128_to_field(dest, |
| 3770 |
retval, |
| 3771 |
truncation_e, |
| 3772 |
NULL); |
| 3773 |
} |
| 3774 |
|
| 3775 |
extern "C" |
| 3776 |
void |
| 3777 |
__gg__when_compiled(cblc_field_t *dest, size_t tv_sec, long tv_nsec) |
| 3778 |
{ |
| 3779 |
struct cbl_timespec tp = {}; |
| 3780 |
tp.tv_sec = tv_sec; |
| 3781 |
tp.tv_nsec = tv_nsec; |
| 3782 |
char retval[DATE_STRING_BUFFER_SIZE]; |
| 3783 |
timespec_to_string(retval, tp); |
| 3784 |
ascii_to_internal_str(retval, strlen(retval)); |
| 3785 |
string_to_dest(dest, retval); |
| 3786 |
} |
| 3787 |
|
| 3788 |
extern "C" |
| 3789 |
void |
| 3790 |
__gg__year_to_yyyy( cblc_field_t *dest, |
| 3791 |
const cblc_field_t *par1, |
| 3792 |
size_t par1_o, |
| 3793 |
size_t par1_s, |
| 3794 |
const cblc_field_t *par2, |
| 3795 |
size_t par2_o, |
| 3796 |
size_t par2_s, |
| 3797 |
const cblc_field_t *par3, |
| 3798 |
size_t par3_o, |
| 3799 |
size_t par3_s) |
| 3800 |
{ |
| 3801 |
// FUNCTION YEAR_TO_YYYY |
| 3802 |
int rdigits; |
| 3803 |
int yy = (int)__gg__binary_value_from_qualified_field(&rdigits, par1, par1_o, par1_s); |
| 3804 |
int arg2 = (int)__gg__binary_value_from_qualified_field(&rdigits, par2, par2_o, par2_s ); |
| 3805 |
int arg3 = (int)__gg__binary_value_from_qualified_field(&rdigits, par3, par3_o, par3_s); |
| 3806 |
|
| 3807 |
int retval = year_to_yyyy(yy, arg2, arg3); |
| 3808 |
|
| 3809 |
__gg__int128_to_field(dest, |
| 3810 |
retval, |
| 3811 |
NO_RDIGITS, |
| 3812 |
truncation_e, |
| 3813 |
NULL); |
| 3814 |
} |
| 3815 |
|
| 3816 |
static |
| 3817 |
int |
| 3818 |
gets_int(int ndigits, const char *p, const char *pend, int *digits) |
| 3819 |
{ |
| 3820 |
// This routine returns the value of the integer at p. If there is something |
| 3821 |
// wrong with the integer, it returns a negative number, the value being the |
| 3822 |
// position (starting at 1) where the problem is. |
| 3823 |
int retval = 0; |
| 3824 |
memset(digits, 0xFF, ndigits * sizeof(int)); |
| 3825 |
for(int i=1; i<=ndigits; i++) |
| 3826 |
{ |
| 3827 |
if(p >= pend) |
| 3828 |
{ |
| 3829 |
// We ran out of input too soon |
| 3830 |
retval = -i; |
| 3831 |
break; |
| 3832 |
} |
| 3833 |
int ch = *p++; |
| 3834 |
if( ch < internal_0 || ch > internal_9 ) |
| 3835 |
{ |
| 3836 |
// This isn't a digit zero through nine |
| 3837 |
retval = -i; |
| 3838 |
break; |
| 3839 |
} |
| 3840 |
retval *= 10; |
| 3841 |
retval += ch & 0xF; |
| 3842 |
digits[i-1] = ch & 0xF; |
| 3843 |
} |
| 3844 |
return retval; |
| 3845 |
} |
| 3846 |
|
| 3847 |
static |
| 3848 |
int |
| 3849 |
gets_year(const char *p, const char *pend, struct cobol_tm &ctm) |
| 3850 |
{ |
| 3851 |
// Populates ctm.YYYY, ctm.days_in_year, and ctm.weeks_in_year, which are |
| 3852 |
// all determined by the YYYY value. |
| 3853 |
|
| 3854 |
// Returns 0 if successful, and returns the ordinal position of the character |
| 3855 |
// where a four-character range with a year value of 1601 became impossible. |
| 3856 |
|
| 3857 |
int retval = 0; |
| 3858 |
int digits[4]; |
| 3859 |
int YYYY = gets_int(4, p, pend, digits); |
| 3860 |
|
| 3861 |
if( digits[0] == -1 || digits[0] == 0 ) |
| 3862 |
{ |
| 3863 |
return 1; |
| 3864 |
} |
| 3865 |
if( digits[1] == -1 ) |
| 3866 |
{ |
| 3867 |
return 2; |
| 3868 |
} |
| 3869 |
if( digits[2] == -1 ) |
| 3870 |
{ |
| 3871 |
return 3; |
| 3872 |
} |
| 3873 |
if( digits[3] == -1 ) |
| 3874 |
{ |
| 3875 |
return 4; |
| 3876 |
} |
| 3877 |
|
| 3878 |
if( YYYY >= 0 ) |
| 3879 |
{ |
| 3880 |
// The year has to be > 1000 |
| 3881 |
if( YYYY < 1000 ) |
| 3882 |
{ |
| 3883 |
// We fail on the initial zero |
| 3884 |
retval = 1; |
| 3885 |
} |
| 3886 |
else if( YYYY < 1600 ) |
| 3887 |
{ |
| 3888 |
// We fail on the second digit |
| 3889 |
retval = 2; |
| 3890 |
} |
| 3891 |
else if( YYYY == 1600 ) |
| 3892 |
{ |
| 3893 |
// We fail on the fourth digit |
| 3894 |
retval = 4; |
| 3895 |
} |
| 3896 |
else |
| 3897 |
{ |
| 3898 |
// The year is a good value |
| 3899 |
ctm.YYYY = YYYY; |
| 3900 |
ctm.days_in_year = is_leap_year(YYYY); |
| 3901 |
ctm.weeks_in_year = weeks_in_year(YYYY); |
| 3902 |
} |
| 3903 |
} |
| 3904 |
else |
| 3905 |
{ |
| 3906 |
retval = -YYYY; |
| 3907 |
} |
| 3908 |
return retval; |
| 3909 |
} |
| 3910 |
|
| 3911 |
static |
| 3912 |
int |
| 3913 |
gets_month(const char *p, const char *pend, struct cobol_tm &ctm) |
| 3914 |
{ |
| 3915 |
// Populates ctm.MM |
| 3916 |
|
| 3917 |
// Returns either zero, or else the ordinal position of where the input |
| 3918 |
// processing failed. |
| 3919 |
|
| 3920 |
int digits[2]; |
| 3921 |
int retval = 0; |
| 3922 |
int MM = gets_int(2, p, pend, digits); |
| 3923 |
|
| 3924 |
if( digits[0] == -1 || digits[0] > 1) |
| 3925 |
{ |
| 3926 |
return 1; |
| 3927 |
} |
| 3928 |
if( digits[1] == -1 ) |
| 3929 |
{ |
| 3930 |
return 2; |
| 3931 |
} |
| 3932 |
if( MM >= 0 ) |
| 3933 |
{ |
| 3934 |
if( MM == 0 ) |
| 3935 |
{ |
| 3936 |
// We know the month was wrong at the second zero |
| 3937 |
retval = 2; |
| 3938 |
} |
| 3939 |
if( MM >= 20 ) |
| 3940 |
{ |
| 3941 |
// We know the month was wrong at the first digit |
| 3942 |
retval = 1; |
| 3943 |
} |
| 3944 |
else if( MM > 12 ) |
| 3945 |
{ |
| 3946 |
// We are betweem 13 and 19, so it was the second digit |
| 3947 |
retval = 2; |
| 3948 |
} |
| 3949 |
ctm.MM = MM; |
| 3950 |
} |
| 3951 |
else |
| 3952 |
{ |
| 3953 |
retval = -MM; |
| 3954 |
} |
| 3955 |
return retval; |
| 3956 |
} |
| 3957 |
|
| 3958 |
static |
| 3959 |
int |
| 3960 |
gets_day(const char *p, const char *pend, struct cobol_tm &ctm) |
| 3961 |
{ |
| 3962 |
// Populates ctm.DD, ctm.day_of_week, ctm.week_of_year, ctm.day_of_week |
| 3963 |
|
| 3964 |
// The assumption is that YYYY and MM were populated before arriving here |
| 3965 |
|
| 3966 |
int digits[2]; |
| 3967 |
int retval = 0; |
| 3968 |
int DD = gets_int(2, p, pend, digits); |
| 3969 |
|
| 3970 |
if( digits[0] == -1 || digits[0] > 3) |
| 3971 |
{ |
| 3972 |
return 1; |
| 3973 |
} |
| 3974 |
if( digits[1] == -1 ) |
| 3975 |
{ |
| 3976 |
return 2; |
| 3977 |
} |
| 3978 |
if( DD >= 0 ) |
| 3979 |
{ |
| 3980 |
if( DD == 0) |
| 3981 |
{ |
| 3982 |
// If zero, we know we failed at the second '0' in "00" |
| 3983 |
retval = 2; |
| 3984 |
} |
| 3985 |
else if( DD >= 40) |
| 3986 |
{ |
| 3987 |
// 40 or more, then we knew there was trouble at the first digit |
| 3988 |
retval = 1; |
| 3989 |
} |
| 3990 |
else if(ctm.MM == 2 && DD >=30) |
| 3991 |
{ |
| 3992 |
// It's February, so if we see 3x we know on the 3 that we are in |
| 3993 |
// error: |
| 3994 |
retval = 1; |
| 3995 |
} |
| 3996 |
else |
| 3997 |
{ |
| 3998 |
static const int month_days[13] = {-1,31,28,31,30,31,30,31,31,30,31,30,31}; |
| 3999 |
int days_in_month = month_days[ctm.MM]; |
| 4000 |
if( ctm.MM == 2 && ctm.days_in_year == 366 ) |
| 4001 |
{ |
| 4002 |
days_in_month = 29; |
| 4003 |
} |
| 4004 |
|
| 4005 |
if( DD > days_in_month ) |
| 4006 |
{ |
| 4007 |
retval = 2; |
| 4008 |
} |
| 4009 |
else |
| 4010 |
{ |
| 4011 |
// We have a good YYYY-MM-DD |
| 4012 |
ctm.DD = DD; |
| 4013 |
double JD = YMD_to_JD(ctm.YYYY, ctm.MM, DD); |
| 4014 |
double JD_Jan0 = YMD_to_JD(ctm.YYYY, 1, 0); |
| 4015 |
ctm.day_of_year = (int)(JD - JD_Jan0); |
| 4016 |
ctm.day_of_week = JD_to_DOW(JD); |
| 4017 |
} |
| 4018 |
} |
| 4019 |
} |
| 4020 |
else |
| 4021 |
{ |
| 4022 |
retval = -DD; |
| 4023 |
} |
| 4024 |
return retval; |
| 4025 |
} |
| 4026 |
|
| 4027 |
static |
| 4028 |
int |
| 4029 |
gets_day_of_week(const char *p, const char *pend, struct cobol_tm &ctm) |
| 4030 |
{ |
| 4031 |
// This is just a simple D, for day-of-week. The COBOL spec is that |
| 4032 |
// it be 1 to 7, 1 being Monday |
| 4033 |
int digits[1]; |
| 4034 |
int day_of_week = gets_int(1, p, pend, digits); |
| 4035 |
if( day_of_week<0 || day_of_week >7) |
| 4036 |
{ |
| 4037 |
// The single character at source is no good: |
| 4038 |
return 1; |
| 4039 |
} |
| 4040 |
ctm.day_of_week = day_of_week; |
| 4041 |
|
| 4042 |
// It is a value 1 through 7. Convert it to 1 through 6: |
| 4043 |
day_of_week -= 1; |
| 4044 |
|
| 4045 |
// Find the day-of-year using COBOL week logic: |
| 4046 |
double JD_Jan4 = YMD_to_JD(ctm.YYYY, 1, 4); |
| 4047 |
double JD_Jan0 = JD_Jan4 - 4; |
| 4048 |
int dow_Jan4 = JD_to_DOW(JD_Jan4); |
| 4049 |
double week_zero = JD_Jan4 - dow_Jan4; |
| 4050 |
double JD = week_zero + (ctm.week_of_year-1)*7 + day_of_week; |
| 4051 |
|
| 4052 |
int day_of_year = (int)(JD - JD_Jan0); |
| 4053 |
|
| 4054 |
// It's possible for the year/week/day_of_week to be |
| 4055 |
// before Jan 1. This is the case for 1900-12-31, as one example; that |
| 4056 |
// date gets converted to 1901-W01-01 |
| 4057 |
if( day_of_year <= 0 ) |
| 4058 |
{ |
| 4059 |
double JD_prior_year = YMD_to_JD(ctm.YYYY-1, 1, 0); |
| 4060 |
int day_of_prior_year = (int)(JD-JD_prior_year); |
| 4061 |
int days_in_prior_year = is_leap_year(ctm.YYYY-1); |
| 4062 |
if( day_of_prior_year > days_in_prior_year ) |
| 4063 |
{ |
| 4064 |
return 1; |
| 4065 |
} |
| 4066 |
ctm.ZZZZ = ctm.YYYY + 1; |
| 4067 |
day_of_year = day_of_prior_year; |
| 4068 |
} |
| 4069 |
|
| 4070 |
// Arriving here means we have a good JD, which means we can decompose it |
| 4071 |
JD_to_YMD(ctm.YYYY, ctm.MM, ctm.DD, JD); |
| 4072 |
ctm.day_of_year = day_of_year; |
| 4073 |
return 0; |
| 4074 |
} |
| 4075 |
|
| 4076 |
static |
| 4077 |
int |
| 4078 |
gets_day_of_year(const char *p, const char *pend, struct cobol_tm &ctm) |
| 4079 |
{ |
| 4080 |
// This is a three-digit day-of-year, 001 through 365,366 |
| 4081 |
int digits[3]; |
| 4082 |
int DDD = gets_int(3, p, pend, digits); |
| 4083 |
if( digits[0] == -1 || digits[0] > 3) |
| 4084 |
{ |
| 4085 |
return 1; |
| 4086 |
} |
| 4087 |
if( digits[1] == -1 ) |
| 4088 |
{ |
| 4089 |
return 2; |
| 4090 |
} |
| 4091 |
if( digits[2] == -1 ) |
| 4092 |
{ |
| 4093 |
return 3; |
| 4094 |
} |
| 4095 |
if( DDD < 0 ) |
| 4096 |
{ |
| 4097 |
return -DDD; |
| 4098 |
} |
| 4099 |
|
| 4100 |
if( DDD == 0 ) |
| 4101 |
{ |
| 4102 |
// We know we went wrong at the third '0' in "000" |
| 4103 |
return 3; |
| 4104 |
} |
| 4105 |
if( DDD >= 400 ) |
| 4106 |
{ |
| 4107 |
// We know we went wrong at the first digit |
| 4108 |
return 1; |
| 4109 |
} |
| 4110 |
if( DDD >= 370 ) |
| 4111 |
{ |
| 4112 |
// We know we went wrong at the second digit |
| 4113 |
return 2; |
| 4114 |
} |
| 4115 |
if( DDD > ctm.days_in_year ) |
| 4116 |
{ |
| 4117 |
// We know we went wrong at the third digit |
| 4118 |
return 3; |
| 4119 |
} |
| 4120 |
// We know that DDD is a good value between 1 and ctm.days_in_year |
| 4121 |
ctm.day_of_year = DDD; |
| 4122 |
|
| 4123 |
double JD_Jan0 = YMD_to_JD(ctm.YYYY, 1, 0); |
| 4124 |
double JD = JD_Jan0 + DDD; |
| 4125 |
JD_to_YMD(ctm.YYYY, |
| 4126 |
ctm.MM, |
| 4127 |
ctm.DD, |
| 4128 |
JD); |
| 4129 |
ctm.day_of_week = JD_to_DOW(JD); |
| 4130 |
return 0; |
| 4131 |
} |
| 4132 |
|
| 4133 |
static |
| 4134 |
int |
| 4135 |
gets_week(const char *p, const char *pend, struct cobol_tm &ctm) |
| 4136 |
{ |
| 4137 |
// This is a two-digit value, 01 through 52,53 |
| 4138 |
int digits[2]; |
| 4139 |
int ww = gets_int(2, p, pend, digits); |
| 4140 |
if( digits[0] == -1 || digits[0] > 5 ) |
| 4141 |
{ |
| 4142 |
return 1; |
| 4143 |
} |
| 4144 |
if( digits[1] == -1 ) |
| 4145 |
{ |
| 4146 |
return 2; |
| 4147 |
} |
| 4148 |
if( ww < 0 ) |
| 4149 |
{ |
| 4150 |
return -ww; |
| 4151 |
} |
| 4152 |
|
| 4153 |
if( ww == 0 ) |
| 4154 |
{ |
| 4155 |
// We know we went wrong at the second '0' in "00" |
| 4156 |
return 2; |
| 4157 |
} |
| 4158 |
if( ww >= 60 ) |
| 4159 |
{ |
| 4160 |
// We know we went wrong at the first digit |
| 4161 |
return 1; |
| 4162 |
} |
| 4163 |
if( ww > ctm.weeks_in_year ) |
| 4164 |
{ |
| 4165 |
// We know we went wrong at the second digit |
| 4166 |
return 2; |
| 4167 |
} |
| 4168 |
// We know that ww is a good value for this year. |
| 4169 |
ctm.week_of_year = ww; |
| 4170 |
return 0; |
| 4171 |
} |
| 4172 |
|
| 4173 |
static |
| 4174 |
int |
| 4175 |
gets_hours( const char *p, |
| 4176 |
const char *pend, |
| 4177 |
struct cobol_tm &ctm, |
| 4178 |
bool in_offset) |
| 4179 |
{ |
| 4180 |
// This is a two-digit value, 01 through 23 |
| 4181 |
int digits[2]; |
| 4182 |
int hh = gets_int(2, p, pend, digits); |
| 4183 |
|
| 4184 |
if( digits[0] == -1 || digits[0] > 2 ) |
| 4185 |
{ |
| 4186 |
return 1; |
| 4187 |
} |
| 4188 |
if( digits[1] == -1 ) |
| 4189 |
{ |
| 4190 |
return 2; |
| 4191 |
} |
| 4192 |
|
| 4193 |
if( hh < 0 ) |
| 4194 |
{ |
| 4195 |
return -hh; |
| 4196 |
} |
| 4197 |
|
| 4198 |
if( hh >= 30 ) |
| 4199 |
{ |
| 4200 |
// We know we went wrong at the first digit |
| 4201 |
return 1; |
| 4202 |
} |
| 4203 |
|
| 4204 |
if( hh >= 24 ) |
| 4205 |
{ |
| 4206 |
// We know we went wrong at the first digit |
| 4207 |
return 2; |
| 4208 |
} |
| 4209 |
|
| 4210 |
if( in_offset ) |
| 4211 |
{ |
| 4212 |
ctm.tz_offset = 60*hh; |
| 4213 |
} |
| 4214 |
else |
| 4215 |
{ |
| 4216 |
ctm.hh = hh; |
| 4217 |
} |
| 4218 |
return 0; |
| 4219 |
} |
| 4220 |
|
| 4221 |
static |
| 4222 |
int |
| 4223 |
gets_minutes( const char *p, |
| 4224 |
const char *pend, |
| 4225 |
struct cobol_tm &ctm, |
| 4226 |
bool in_offset) |
| 4227 |
{ |
| 4228 |
// This is a two-digit value, 01 through 59 |
| 4229 |
int digits[2]; |
| 4230 |
int mm = gets_int(2, p, pend, digits); |
| 4231 |
if( digits[0] == -1 || digits[0] > 5 ) |
| 4232 |
{ |
| 4233 |
return 1; |
| 4234 |
} |
| 4235 |
if( digits[1] == -1 ) |
| 4236 |
{ |
| 4237 |
return 2; |
| 4238 |
} |
| 4239 |
|
| 4240 |
if( mm < 0 ) |
| 4241 |
{ |
| 4242 |
return -mm; |
| 4243 |
} |
| 4244 |
|
| 4245 |
if( mm >= 60 ) |
| 4246 |
{ |
| 4247 |
// We know we went wrong at the first digit |
| 4248 |
return 1; |
| 4249 |
} |
| 4250 |
|
| 4251 |
if( in_offset ) |
| 4252 |
{ |
| 4253 |
ctm.tz_offset += mm; |
| 4254 |
} |
| 4255 |
else |
| 4256 |
{ |
| 4257 |
ctm.mm = mm; |
| 4258 |
} |
| 4259 |
return 0; |
| 4260 |
} |
| 4261 |
|
| 4262 |
static |
| 4263 |
int |
| 4264 |
gets_seconds(const char *p, const char *pend, struct cobol_tm &ctm) |
| 4265 |
{ |
| 4266 |
// This is a two-digit value, 01 through 59 |
| 4267 |
int digits[2]; |
| 4268 |
int ss = gets_int(2, p, pend, digits); |
| 4269 |
if( digits[0] == -1 || digits[0] > 5 ) |
| 4270 |
{ |
| 4271 |
return 1; |
| 4272 |
} |
| 4273 |
if( digits[1] == -1 ) |
| 4274 |
{ |
| 4275 |
return 2; |
| 4276 |
} |
| 4277 |
if( ss < 0 ) |
| 4278 |
{ |
| 4279 |
return -ss; |
| 4280 |
} |
| 4281 |
|
| 4282 |
if( ss >= 60 ) |
| 4283 |
{ |
| 4284 |
// We know we went wrong at the first digit |
| 4285 |
return 1; |
| 4286 |
} |
| 4287 |
|
| 4288 |
ctm.ss = ss; |
| 4289 |
return 0; |
| 4290 |
} |
| 4291 |
|
| 4292 |
static |
| 4293 |
int |
| 4294 |
gets_nanoseconds( const char *f, |
| 4295 |
const char *f_end, |
| 4296 |
const char *p, |
| 4297 |
const char *pend, |
| 4298 |
struct cobol_tm &ctm) |
| 4299 |
{ |
| 4300 |
// Because nanoseconds digits to the right of the decimal point can vary from |
| 4301 |
// one digit to our implementation-specific limit of nine characters, this |
| 4302 |
// routine is slightly different. If there is an error, that causes a |
| 4303 |
// positive return value. A negative return value contains the number of |
| 4304 |
// digits we processed |
| 4305 |
|
| 4306 |
int errpos = 0; |
| 4307 |
int ncount = 0; |
| 4308 |
int nanoseconds = 0; |
| 4309 |
|
| 4310 |
const char *pinit = p; |
| 4311 |
while( f < f_end && *f == internal_s && p < pend ) |
| 4312 |
{ |
| 4313 |
f += 1; |
| 4314 |
int ch = *p++; |
| 4315 |
errpos += 1; |
| 4316 |
|
| 4317 |
if( ch < internal_0 || ch > internal_9 ) |
| 4318 |
{ |
| 4319 |
// Let our caller know we see a bad character |
| 4320 |
return errpos; |
| 4321 |
} |
| 4322 |
|
| 4323 |
if(ncount < 9) |
| 4324 |
{ |
| 4325 |
nanoseconds *= 10; |
| 4326 |
nanoseconds += ch & 0x0F; |
| 4327 |
} |
| 4328 |
ncount += 1; |
| 4329 |
} |
| 4330 |
while(ncount++ < 9) |
| 4331 |
{ |
| 4332 |
nanoseconds *= 10; |
| 4333 |
} |
| 4334 |
ctm.nanoseconds = nanoseconds; |
| 4335 |
|
| 4336 |
return -((int)(p - pinit)); |
| 4337 |
} |
| 4338 |
|
| 4339 |
static |
| 4340 |
int |
| 4341 |
fill_cobol_tm(cobol_tm &ctm, |
| 4342 |
const cblc_field_t *par1, |
| 4343 |
size_t par1_offset, |
| 4344 |
size_t par1_size, |
| 4345 |
const cblc_field_t *par2, |
| 4346 |
size_t par2_offset, |
| 4347 |
size_t par2_size) |
| 4348 |
{ |
| 4349 |
// Establish the formatting string: |
| 4350 |
char *format = PTRCAST(char, (par1->data+par1_offset)); |
| 4351 |
char *format_end = format + par1_size; |
| 4352 |
|
| 4353 |
// Establish the string to be checked: |
| 4354 |
char *source = PTRCAST(char, (par2->data+par2_offset)); |
| 4355 |
char *source_end = source + par2_size; |
| 4356 |
|
| 4357 |
// Let's eliminate trailing spaces... |
| 4358 |
trim_trailing_spaces(format, format_end); |
| 4359 |
trim_trailing_spaces(source, source_end); |
| 4360 |
|
| 4361 |
bool in_offset = false; |
| 4362 |
bool in_nanoseconds = false; |
| 4363 |
|
| 4364 |
char decimal_point = __gg__get_decimal_point(); |
| 4365 |
|
| 4366 |
// We keep constant track of the current error location. |
| 4367 |
int retval = 1; |
| 4368 |
int errpos; |
| 4369 |
|
| 4370 |
// At this juncture, we expect both the format and the source to have valid |
| 4371 |
// data. If they don't, it's because the source is too short, and thus |
| 4372 |
// retval is the failure point. |
| 4373 |
int bump; |
| 4374 |
while( format < format_end && source < source_end ) |
| 4375 |
{ |
| 4376 |
char ch = *format; |
| 4377 |
|
| 4378 |
if( ch == internal_T |
| 4379 |
|| ch == internal_colon |
| 4380 |
|| ch == internal_minus |
| 4381 |
|| ch == internal_W) |
| 4382 |
{ |
| 4383 |
// These are just formatting characters. They need to be duplicated, |
| 4384 |
// but are otherwise ignored. |
| 4385 |
if( *source != ch ) |
| 4386 |
{ |
| 4387 |
break; |
| 4388 |
} |
| 4389 |
bump = 1; |
| 4390 |
goto proceed; |
| 4391 |
} |
| 4392 |
|
| 4393 |
if( ch == internal_plus ) |
| 4394 |
{ |
| 4395 |
// This flags a following hhmm offset. It needs to match a '+' or '-' |
| 4396 |
if( *source != internal_plus |
| 4397 |
&& *source != internal_minus |
| 4398 |
&& *source != internal_zero) |
| 4399 |
{ |
| 4400 |
break; |
| 4401 |
} |
| 4402 |
if( *source == internal_zero ) |
| 4403 |
{ |
| 4404 |
// The next four characters have to be zeroes |
| 4405 |
if( source[1] != internal_zero ) |
| 4406 |
{ |
| 4407 |
retval += 1; |
| 4408 |
break; |
| 4409 |
} |
| 4410 |
if( source[2] != internal_zero ) |
| 4411 |
{ |
| 4412 |
retval += 2; |
| 4413 |
break; |
| 4414 |
} |
| 4415 |
if( source[3] != internal_zero ) |
| 4416 |
{ |
| 4417 |
retval += 3; |
| 4418 |
break; |
| 4419 |
} |
| 4420 |
if( source[4] != internal_zero ) |
| 4421 |
{ |
| 4422 |
retval += 4; |
| 4423 |
break; |
| 4424 |
} |
| 4425 |
} |
| 4426 |
|
| 4427 |
in_offset = true; |
| 4428 |
bump = 1; |
| 4429 |
goto proceed; |
| 4430 |
} |
| 4431 |
|
| 4432 |
if( ch == decimal_point ) |
| 4433 |
{ |
| 4434 |
// This indicates we are starting to process fractional seconds |
| 4435 |
if( *source != decimal_point ) |
| 4436 |
{ |
| 4437 |
break; |
| 4438 |
} |
| 4439 |
in_nanoseconds = true; |
| 4440 |
bump = 1; |
| 4441 |
goto proceed; |
| 4442 |
} |
| 4443 |
|
| 4444 |
if( ch == internal_Y ) |
| 4445 |
{ |
| 4446 |
errpos = gets_year(source, source_end, ctm); |
| 4447 |
if( errpos > 0 ) |
| 4448 |
{ |
| 4449 |
retval += errpos - 1; |
| 4450 |
break; |
| 4451 |
} |
| 4452 |
bump = 4; |
| 4453 |
goto proceed; |
| 4454 |
} |
| 4455 |
|
| 4456 |
if( ch == internal_M ) |
| 4457 |
{ |
| 4458 |
errpos = gets_month(source, source_end, ctm); |
| 4459 |
if( errpos > 0 ) |
| 4460 |
{ |
| 4461 |
retval += errpos - 1; |
| 4462 |
break; |
| 4463 |
} |
| 4464 |
bump = 2; |
| 4465 |
goto proceed; |
| 4466 |
} |
| 4467 |
|
| 4468 |
if( ch == internal_D ) |
| 4469 |
{ |
| 4470 |
// We have three possibilities: DDD, DD, and D |
| 4471 |
if( format[1] != internal_D ) |
| 4472 |
{ |
| 4473 |
// A singleton 'D' is a day-of-week |
| 4474 |
errpos = gets_day_of_week(source, source_end, ctm); |
| 4475 |
if( errpos > 0) |
| 4476 |
{ |
| 4477 |
retval += errpos - 1; |
| 4478 |
break; |
| 4479 |
} |
| 4480 |
bump = 1; |
| 4481 |
} |
| 4482 |
else if( format[2] != internal_D ) |
| 4483 |
{ |
| 4484 |
// This is DD, for day-of-month |
| 4485 |
errpos = gets_day(source, source_end, ctm); |
| 4486 |
if( errpos > 0) |
| 4487 |
{ |
| 4488 |
retval += errpos - 1; |
| 4489 |
break; |
| 4490 |
} |
| 4491 |
bump = 2; |
| 4492 |
} |
| 4493 |
else |
| 4494 |
{ |
| 4495 |
// Arriving here means that it is DDD, for day-of-year |
| 4496 |
// This is DD, for day-of-month |
| 4497 |
errpos = gets_day_of_year(source, source_end, ctm); |
| 4498 |
if( errpos > 0) |
| 4499 |
{ |
| 4500 |
retval += errpos - 1; |
| 4501 |
break; |
| 4502 |
} |
| 4503 |
bump = 3; |
| 4504 |
} |
| 4505 |
goto proceed; |
| 4506 |
} |
| 4507 |
|
| 4508 |
if( ch == internal_w ) |
| 4509 |
{ |
| 4510 |
errpos = gets_week(source, source_end, ctm); |
| 4511 |
if( errpos > 0 ) |
| 4512 |
{ |
| 4513 |
retval += errpos - 1; |
| 4514 |
break; |
| 4515 |
} |
| 4516 |
bump = 2; |
| 4517 |
goto proceed; |
| 4518 |
} |
| 4519 |
|
| 4520 |
if( ch == internal_h ) |
| 4521 |
{ |
| 4522 |
errpos = gets_hours(source, source_end, ctm, in_offset); |
| 4523 |
if( errpos > 0 ) |
| 4524 |
{ |
| 4525 |
retval += errpos - 1; |
| 4526 |
break; |
| 4527 |
} |
| 4528 |
bump = 2; |
| 4529 |
goto proceed; |
| 4530 |
} |
| 4531 |
|
| 4532 |
if( ch == internal_m ) |
| 4533 |
{ |
| 4534 |
errpos = gets_minutes(source, source_end, ctm, in_offset); |
| 4535 |
if( errpos > 0 ) |
| 4536 |
{ |
| 4537 |
retval += errpos - 1; |
| 4538 |
break; |
| 4539 |
} |
| 4540 |
bump = 2; |
| 4541 |
goto proceed; |
| 4542 |
} |
| 4543 |
|
| 4544 |
if( ch == internal_s && !in_nanoseconds ) |
| 4545 |
{ |
| 4546 |
errpos = gets_seconds(source, source_end, ctm); |
| 4547 |
if( errpos > 0 ) |
| 4548 |
{ |
| 4549 |
retval += errpos - 1; |
| 4550 |
break; |
| 4551 |
} |
| 4552 |
bump = 2; |
| 4553 |
goto proceed; |
| 4554 |
} |
| 4555 |
|
| 4556 |
if( ch == internal_s && in_nanoseconds ) |
| 4557 |
{ |
| 4558 |
// Peel off digits to the right of the decimal point one at a time |
| 4559 |
errpos = gets_nanoseconds(format, format_end, source, source_end, ctm); |
| 4560 |
if( errpos > 0 ) |
| 4561 |
{ |
| 4562 |
retval += errpos - 1; |
| 4563 |
break; |
| 4564 |
} |
| 4565 |
bump = -errpos; |
| 4566 |
goto proceed; |
| 4567 |
} |
| 4568 |
|
| 4569 |
if( ch == internal_Z || ch == internal_z ) |
| 4570 |
{ |
| 4571 |
// This has to be the end of the road |
| 4572 |
if( std::toupper((unsigned char)source[0]) != 'Z' ) |
| 4573 |
{ |
| 4574 |
retval += 0; |
| 4575 |
break; |
| 4576 |
} |
| 4577 |
|
| 4578 |
convert_to_zulu(ctm); |
| 4579 |
bump = 1; |
| 4580 |
goto proceed; |
| 4581 |
} |
| 4582 |
|
| 4583 |
assert(false); |
| 4584 |
|
| 4585 |
proceed: |
| 4586 |
retval += bump; |
| 4587 |
format += bump; |
| 4588 |
source += bump; |
| 4589 |
} |
| 4590 |
|
| 4591 |
if( format >= format_end && source >= source_end) |
| 4592 |
{ |
| 4593 |
// This means we processed the entire format string without seeing an error |
| 4594 |
retval = 0; |
| 4595 |
|
| 4596 |
// Otherwise, either the format or source was too short |
| 4597 |
} |
| 4598 |
return retval; |
| 4599 |
} |
| 4600 |
|
| 4601 |
extern "C" |
| 4602 |
void |
| 4603 |
__gg__test_formatted_datetime(cblc_field_t *dest, |
| 4604 |
const cblc_field_t *arg1, |
| 4605 |
size_t arg1_offset, |
| 4606 |
size_t arg1_size, |
| 4607 |
const cblc_field_t *arg2, |
| 4608 |
size_t arg2_offset, |
| 4609 |
size_t arg2_size) |
| 4610 |
|
| 4611 |
{ |
| 4612 |
struct cobol_tm ctm = {}; |
| 4613 |
|
| 4614 |
int retval = fill_cobol_tm( ctm, |
| 4615 |
arg1, arg1_offset, arg1_size, |
| 4616 |
arg2, arg2_offset, arg2_size); |
| 4617 |
__gg__int128_to_field(dest, |
| 4618 |
retval, |
| 4619 |
NO_RDIGITS, |
| 4620 |
truncation_e, |
| 4621 |
NULL); |
| 4622 |
} |
| 4623 |
|
| 4624 |
extern "C" |
| 4625 |
void |
| 4626 |
__gg__integer_of_formatted_date(cblc_field_t *dest, |
| 4627 |
const cblc_field_t *arg1, |
| 4628 |
size_t arg1_offset, |
| 4629 |
size_t arg1_size, |
| 4630 |
const cblc_field_t *arg2, |
| 4631 |
size_t arg2_offset, |
| 4632 |
size_t arg2_size) |
| 4633 |
{ |
| 4634 |
struct cobol_tm ctm = {}; |
| 4635 |
|
| 4636 |
int retval = fill_cobol_tm( ctm, |
| 4637 |
arg1, arg1_offset, arg1_size, |
| 4638 |
arg2, arg2_offset, arg2_size); |
| 4639 |
if(retval) |
| 4640 |
{ |
| 4641 |
retval = 0; // Indicates there was a problem with the input data |
| 4642 |
} |
| 4643 |
else |
| 4644 |
{ |
| 4645 |
double JD = YMD_to_JD(ctm.YYYY, ctm.MM, ctm.DD); |
| 4646 |
|
| 4647 |
// Offset result so that 1601-01-01 comes back as the first day of |
| 4648 |
// the Gregorian Calendar |
| 4649 |
retval = (int)(JD - JD_OF_1601_01_02); |
| 4650 |
} |
| 4651 |
|
| 4652 |
__gg__int128_to_field(dest, |
| 4653 |
retval, |
| 4654 |
NO_RDIGITS, |
| 4655 |
truncation_e, |
| 4656 |
NULL); |
| 4657 |
} |
| 4658 |
|
| 4659 |
extern "C" |
| 4660 |
void |
| 4661 |
__gg__seconds_from_formatted_time(cblc_field_t *dest, |
| 4662 |
const cblc_field_t *arg1, |
| 4663 |
size_t arg1_offset, |
| 4664 |
size_t arg1_size, |
| 4665 |
const cblc_field_t *arg2, |
| 4666 |
size_t arg2_offset, |
| 4667 |
size_t arg2_size) |
| 4668 |
{ |
| 4669 |
struct cobol_tm ctm = {}; |
| 4670 |
|
| 4671 |
double retval = fill_cobol_tm( ctm, |
| 4672 |
arg1, arg1_offset, arg1_size, |
| 4673 |
arg2, arg2_offset, arg2_size); |
| 4674 |
if(retval > 0) |
| 4675 |
{ |
| 4676 |
retval = 0; // Indicates there was a problem with the input data |
| 4677 |
} |
| 4678 |
else |
| 4679 |
{ |
| 4680 |
retval = (double)(ctm.hh * 3600 + ctm.mm * 60 + ctm.ss) + ctm.nanoseconds/1000000000.; |
| 4681 |
} |
| 4682 |
__gg__double_to_target( dest, |
| 4683 |
retval, |
| 4684 |
truncation_e); |
| 4685 |
} |
| 4686 |
|
| 4687 |
extern "C" |
| 4688 |
void |
| 4689 |
__gg__hex_of(cblc_field_t *dest, |
| 4690 |
const cblc_field_t *field, |
| 4691 |
size_t field_offset, |
| 4692 |
size_t field_size) |
| 4693 |
{ |
| 4694 |
static const char hex[17] = "0123456789ABCDEF"; |
| 4695 |
size_t bytes = field_size; |
| 4696 |
__gg__adjust_dest_size(dest, 2*bytes); |
| 4697 |
for(size_t i=0; i<bytes; i++) |
| 4698 |
{ |
| 4699 |
unsigned char byte = (field->data+field_offset)[i]; |
| 4700 |
dest->data[2*i] = ascii_to_internal(hex[byte>>4]); |
| 4701 |
dest->data[2*i+1] = ascii_to_internal(hex[byte&0xF]); |
| 4702 |
} |
| 4703 |
} |
| 4704 |
|
| 4705 |
extern "C" |
| 4706 |
void |
| 4707 |
__gg__highest_algebraic(cblc_field_t *dest, |
| 4708 |
const cblc_field_t *var, |
| 4709 |
size_t, |
| 4710 |
size_t) |
| 4711 |
{ |
| 4712 |
__int128 result = 0; |
| 4713 |
__int128 result_rdigits = 0; |
| 4714 |
|
| 4715 |
if( var->attr & scaled_e ) |
| 4716 |
{ |
| 4717 |
result = __gg__power_of_ten(var->digits) - 1; |
| 4718 |
if( var->rdigits<0 ) |
| 4719 |
{ |
| 4720 |
result *= __gg__power_of_ten(-var->rdigits); |
| 4721 |
} |
| 4722 |
else |
| 4723 |
{ |
| 4724 |
result_rdigits = var->digits + var->rdigits; |
| 4725 |
} |
| 4726 |
} |
| 4727 |
else if( var->digits == 0 ) |
| 4728 |
{ |
| 4729 |
result = (1<<(var->capacity*8)) -1 ; |
| 4730 |
if( var->attr & signable_e ) |
| 4731 |
{ |
| 4732 |
result >>=1 ; |
| 4733 |
} |
| 4734 |
} |
| 4735 |
else |
| 4736 |
{ |
| 4737 |
result_rdigits = var->rdigits; |
| 4738 |
result = __gg__power_of_ten(var->digits) - 1; |
| 4739 |
} |
| 4740 |
__gg__int128_to_field(dest, |
| 4741 |
result, |
| 4742 |
result_rdigits, |
| 4743 |
truncation_e, |
| 4744 |
NULL); |
| 4745 |
} |
| 4746 |
|
| 4747 |
extern "C" |
| 4748 |
void |
| 4749 |
__gg__lowest_algebraic( cblc_field_t *dest, |
| 4750 |
const cblc_field_t *var, |
| 4751 |
size_t, |
| 4752 |
size_t) |
| 4753 |
{ |
| 4754 |
__int128 result = 0; |
| 4755 |
__int128 result_rdigits = 0; |
| 4756 |
|
| 4757 |
if( var->attr & scaled_e ) |
| 4758 |
{ |
| 4759 |
result = __gg__power_of_ten(var->digits) - 1; |
| 4760 |
if( var->rdigits<0 ) |
| 4761 |
{ |
| 4762 |
result *= __gg__power_of_ten(-var->rdigits); |
| 4763 |
} |
| 4764 |
else |
| 4765 |
{ |
| 4766 |
result_rdigits = var->digits + var->rdigits; |
| 4767 |
} |
| 4768 |
if( var->attr & signable_e ) |
| 4769 |
{ |
| 4770 |
result = -result; |
| 4771 |
} |
| 4772 |
else |
| 4773 |
{ |
| 4774 |
result = 0; |
| 4775 |
} |
| 4776 |
} |
| 4777 |
else if( var->digits == 0 ) |
| 4778 |
{ |
| 4779 |
result = (1<<(var->capacity*8)) -1 ; |
| 4780 |
if( var->attr & signable_e ) |
| 4781 |
{ |
| 4782 |
result >>=1 ; |
| 4783 |
result += 1; |
| 4784 |
result = -result; |
| 4785 |
} |
| 4786 |
else |
| 4787 |
{ |
| 4788 |
result = 0; |
| 4789 |
} |
| 4790 |
} |
| 4791 |
else |
| 4792 |
{ |
| 4793 |
result_rdigits = var->rdigits; |
| 4794 |
result = __gg__power_of_ten(var->digits) - 1; |
| 4795 |
if( var->attr & signable_e ) |
| 4796 |
{ |
| 4797 |
result = -result; |
| 4798 |
} |
| 4799 |
else |
| 4800 |
{ |
| 4801 |
result = 0; |
| 4802 |
} |
| 4803 |
} |
| 4804 |
__gg__int128_to_field(dest, |
| 4805 |
result, |
| 4806 |
result_rdigits, |
| 4807 |
truncation_e, |
| 4808 |
NULL); |
| 4809 |
} |
| 4810 |
|
| 4811 |
static int |
| 4812 |
floating_format_tester(char const * const f, char const * const f_end) |
| 4813 |
{ |
| 4814 |
int retval = -1; |
| 4815 |
char decimal_point = __gg__get_decimal_point(); |
| 4816 |
|
| 4817 |
enum |
| 4818 |
{ |
| 4819 |
SPACE1, |
| 4820 |
SPACE2, |
| 4821 |
DIGITS1, |
| 4822 |
DIGITS2, |
| 4823 |
SPACE3, |
| 4824 |
SPACE4, |
| 4825 |
SPACE5, |
| 4826 |
DIGITS3, |
| 4827 |
SPACE6, |
| 4828 |
} state = SPACE1; |
| 4829 |
ssize_t index = 0; |
| 4830 |
while(index < f_end - f) |
| 4831 |
{ |
| 4832 |
char ch = f[index]; |
| 4833 |
switch(state) |
| 4834 |
{ |
| 4835 |
case SPACE1: |
| 4836 |
if( ch == internal_space ) |
| 4837 |
{ |
| 4838 |
// Just keep looking |
| 4839 |
break; |
| 4840 |
} |
| 4841 |
if( ch == internal_minus |
| 4842 |
|| ch == internal_plus) |
| 4843 |
{ |
| 4844 |
state = SPACE2; |
| 4845 |
break; |
| 4846 |
} |
| 4847 |
if( ch >= internal_0 && ch <= internal_9 ) |
| 4848 |
{ |
| 4849 |
state = DIGITS1; |
| 4850 |
break; |
| 4851 |
} |
| 4852 |
if( decimal_point ) |
| 4853 |
{ |
| 4854 |
state = DIGITS2; |
| 4855 |
break; |
| 4856 |
} |
| 4857 |
// Disallowed character |
| 4858 |
retval = index; |
| 4859 |
break; |
| 4860 |
|
| 4861 |
case SPACE2: |
| 4862 |
if( ch == internal_space ) |
| 4863 |
{ |
| 4864 |
break; |
| 4865 |
} |
| 4866 |
if( ch >= internal_0 && ch <= internal_9 ) |
| 4867 |
{ |
| 4868 |
state = DIGITS1; |
| 4869 |
break; |
| 4870 |
} |
| 4871 |
if( ch == decimal_point ) |
| 4872 |
{ |
| 4873 |
state = DIGITS2; |
| 4874 |
break; |
| 4875 |
} |
| 4876 |
retval = index; |
| 4877 |
break; |
| 4878 |
|
| 4879 |
case DIGITS1: |
| 4880 |
if( ch >= internal_0 && ch <= internal_9 ) |
| 4881 |
{ |
| 4882 |
break; |
| 4883 |
} |
| 4884 |
if( ch == decimal_point ) |
| 4885 |
{ |
| 4886 |
state = DIGITS2; |
| 4887 |
break; |
| 4888 |
} |
| 4889 |
if( ch == internal_space ) |
| 4890 |
{ |
| 4891 |
state = SPACE3; |
| 4892 |
break; |
| 4893 |
} |
| 4894 |
retval = index; |
| 4895 |
break; |
| 4896 |
|
| 4897 |
case DIGITS2: |
| 4898 |
if( ch >= internal_0 && ch <= internal_9 ) |
| 4899 |
{ |
| 4900 |
break; |
| 4901 |
} |
| 4902 |
if( ch == internal_space ) |
| 4903 |
{ |
| 4904 |
state = SPACE3; |
| 4905 |
break; |
| 4906 |
} |
| 4907 |
if( ch == internal_E || ch == internal_e ) |
| 4908 |
{ |
| 4909 |
state = SPACE4; |
| 4910 |
break; |
| 4911 |
} |
| 4912 |
retval = index; |
| 4913 |
break; |
| 4914 |
|
| 4915 |
case SPACE3: |
| 4916 |
if( ch == internal_space ) |
| 4917 |
{ |
| 4918 |
break; |
| 4919 |
} |
| 4920 |
if( ch >= internal_0 && ch <= internal_9 ) |
| 4921 |
{ |
| 4922 |
retval = index; |
| 4923 |
break; |
| 4924 |
} |
| 4925 |
if( ch == internal_E || ch == internal_e ) |
| 4926 |
{ |
| 4927 |
state = SPACE4; |
| 4928 |
break; |
| 4929 |
} |
| 4930 |
retval = index; |
| 4931 |
break; |
| 4932 |
|
| 4933 |
case SPACE4: |
| 4934 |
if( ch == internal_space ) |
| 4935 |
{ |
| 4936 |
break; |
| 4937 |
} |
| 4938 |
if( ch == internal_minus || ch == internal_plus ) |
| 4939 |
{ |
| 4940 |
state = SPACE5; |
| 4941 |
break; |
| 4942 |
} |
| 4943 |
if( ch >= internal_0 && ch <= internal_9 ) |
| 4944 |
{ |
| 4945 |
state = DIGITS3; |
| 4946 |
break; |
| 4947 |
} |
| 4948 |
retval = index; |
| 4949 |
break; |
| 4950 |
|
| 4951 |
case SPACE5: |
| 4952 |
if( ch == internal_space ) |
| 4953 |
{ |
| 4954 |
break; |
| 4955 |
} |
| 4956 |
if( ch >= internal_0 && ch <= internal_9 ) |
| 4957 |
{ |
| 4958 |
state = DIGITS3; |
| 4959 |
break; |
| 4960 |
} |
| 4961 |
retval = index; |
| 4962 |
break; |
| 4963 |
|
| 4964 |
case DIGITS3: |
| 4965 |
if( ch >= internal_0 && ch <= internal_9 ) |
| 4966 |
{ |
| 4967 |
break; |
| 4968 |
} |
| 4969 |
if( ch == internal_space ) |
| 4970 |
{ |
| 4971 |
state = SPACE6; |
| 4972 |
break; |
| 4973 |
} |
| 4974 |
retval = index; |
| 4975 |
break; |
| 4976 |
|
| 4977 |
case SPACE6: |
| 4978 |
if( ch == internal_space ) |
| 4979 |
{ |
| 4980 |
break; |
| 4981 |
} |
| 4982 |
retval = index; |
| 4983 |
break; |
| 4984 |
} |
| 4985 |
|
| 4986 |
if( retval > -1 ) |
| 4987 |
{ |
| 4988 |
break; |
| 4989 |
} |
| 4990 |
index += 1; |
| 4991 |
} |
| 4992 |
|
| 4993 |
retval += 1; |
| 4994 |
return retval; |
| 4995 |
} |
| 4996 |
|
| 4997 |
extern "C" |
| 4998 |
void |
| 4999 |
__gg__numval_f( cblc_field_t *dest, |
| 5000 |
const cblc_field_t *source, |
| 5001 |
size_t source_offset, |
| 5002 |
size_t source_size) |
| 5003 |
{ |
| 5004 |
GCOB_FP128 value = 0; |
| 5005 |
const char *data = PTRCAST(char, (source->data + source_offset)); |
| 5006 |
const char *data_end = data + source_size; |
| 5007 |
|
| 5008 |
int error = floating_format_tester(data, data_end); |
| 5009 |
|
| 5010 |
if( error || source_size >= 256 ) |
| 5011 |
{ |
| 5012 |
exception_raise(ec_argument_function_e); |
| 5013 |
} |
| 5014 |
else |
| 5015 |
{ |
| 5016 |
// Get rid of any spaces in the string |
| 5017 |
char ach[256]; |
| 5018 |
char *p = ach; |
| 5019 |
while( data < data_end ) |
| 5020 |
{ |
| 5021 |
char ch = *data++; |
| 5022 |
if( ch != internal_space ) |
| 5023 |
{ |
| 5024 |
*p++ = ch; |
| 5025 |
} |
| 5026 |
} |
| 5027 |
*p++ = '\0'; |
| 5028 |
value = strtofp128(ach, NULL); |
| 5029 |
} |
| 5030 |
__gg__float128_to_field(dest, |
| 5031 |
value, |
| 5032 |
truncation_e, |
| 5033 |
NULL); |
| 5034 |
} |
| 5035 |
|
| 5036 |
extern "C" |
| 5037 |
void |
| 5038 |
__gg__test_numval_f(cblc_field_t *dest, |
| 5039 |
const cblc_field_t *source, |
| 5040 |
size_t source_offset, |
| 5041 |
size_t source_size) |
| 5042 |
{ |
| 5043 |
const char *data = PTRCAST(char, (source->data + source_offset)); |
| 5044 |
const char *data_end = data + source_size; |
| 5045 |
|
| 5046 |
int error = floating_format_tester(data, data_end); |
| 5047 |
|
| 5048 |
__gg__int128_to_field(dest, |
| 5049 |
error, |
| 5050 |
NO_RDIGITS, |
| 5051 |
truncation_e, |
| 5052 |
NULL); |
| 5053 |
} |
| 5054 |
|
| 5055 |
static bool |
| 5056 |
ismatch(const char *a1, const char *a2, const char *b1, const char *b2) |
| 5057 |
{ |
| 5058 |
bool retval = true; |
| 5059 |
while( a1 < a2 && b1 < b2 ) |
| 5060 |
{ |
| 5061 |
if( *a1++ != *b1++ ) |
| 5062 |
{ |
| 5063 |
retval = false; |
| 5064 |
} |
| 5065 |
} |
| 5066 |
return retval; |
| 5067 |
} |
| 5068 |
|
| 5069 |
static bool |
| 5070 |
iscasematch(const char *a1, const char *a2, const char *b1, const char *b2) |
| 5071 |
{ |
| 5072 |
bool retval = true; |
| 5073 |
while( a1 < a2 && b1 < b2 ) |
| 5074 |
{ |
| 5075 |
if( std::tolower((unsigned char)*a1++) != std::tolower((unsigned char)*b1++) ) |
| 5076 |
{ |
| 5077 |
retval = false; |
| 5078 |
} |
| 5079 |
} |
| 5080 |
return retval; |
| 5081 |
} |
| 5082 |
|
| 5083 |
static |
| 5084 |
const char * |
| 5085 |
strstr( const char *haystack, |
| 5086 |
const char *haystack_e, |
| 5087 |
const char *needle, |
| 5088 |
const char *needle_e) |
| 5089 |
{ |
| 5090 |
const char *retval = NULL; |
| 5091 |
const char *pend = haystack_e - (needle_e - needle); |
| 5092 |
while( haystack <= pend ) |
| 5093 |
{ |
| 5094 |
if(ismatch(haystack, haystack_e, needle, needle_e)) |
| 5095 |
{ |
| 5096 |
retval = haystack; |
| 5097 |
break; |
| 5098 |
} |
| 5099 |
haystack += 1; |
| 5100 |
} |
| 5101 |
return retval; |
| 5102 |
} |
| 5103 |
|
| 5104 |
static |
| 5105 |
const char * |
| 5106 |
strcasestr( const char *haystack, |
| 5107 |
const char *haystack_e, |
| 5108 |
const char *needle, |
| 5109 |
const char *needle_e) |
| 5110 |
{ |
| 5111 |
const char *retval = NULL; |
| 5112 |
const char *pend = haystack_e - (needle_e - needle); |
| 5113 |
while( haystack <= pend ) |
| 5114 |
{ |
| 5115 |
if(iscasematch(haystack, haystack_e, needle, needle_e)) |
| 5116 |
{ |
| 5117 |
retval = haystack; |
| 5118 |
break; |
| 5119 |
} |
| 5120 |
haystack += 1; |
| 5121 |
} |
| 5122 |
return retval; |
| 5123 |
} |
| 5124 |
|
| 5125 |
static |
| 5126 |
const char * |
| 5127 |
strlaststr( const char *haystack, |
| 5128 |
const char *haystack_e, |
| 5129 |
const char *needle, |
| 5130 |
const char *needle_e) |
| 5131 |
{ |
| 5132 |
const char *retval = NULL; |
| 5133 |
const char *pend = haystack_e - (needle_e - needle); |
| 5134 |
while( haystack <= pend ) |
| 5135 |
{ |
| 5136 |
if(ismatch(haystack, haystack_e, needle, needle_e)) |
| 5137 |
{ |
| 5138 |
retval = haystack; |
| 5139 |
} |
| 5140 |
haystack += 1; |
| 5141 |
} |
| 5142 |
return retval; |
| 5143 |
} |
| 5144 |
|
| 5145 |
static |
| 5146 |
const char * |
| 5147 |
strcaselaststr( const char *haystack, |
| 5148 |
const char *haystack_e, |
| 5149 |
const char *needle, |
| 5150 |
const char *needle_e) |
| 5151 |
{ |
| 5152 |
const char *retval = NULL; |
| 5153 |
const char *pend = haystack_e - (needle_e - needle); |
| 5154 |
while( haystack <= pend ) |
| 5155 |
{ |
| 5156 |
if(iscasematch(haystack, haystack_e, needle, needle_e)) |
| 5157 |
{ |
| 5158 |
retval = haystack; |
| 5159 |
} |
| 5160 |
haystack += 1; |
| 5161 |
} |
| 5162 |
return retval; |
| 5163 |
} |
| 5164 |
|
| 5165 |
|
| 5166 |
extern "C" |
| 5167 |
void |
| 5168 |
__gg__substitute( cblc_field_t *dest, |
| 5169 |
const cblc_field_t *arg1_f, |
| 5170 |
size_t arg1_o, |
| 5171 |
size_t arg1_s, |
| 5172 |
size_t N, |
| 5173 |
const uint8_t *control) |
| 5174 |
{ |
| 5175 |
// arg2 is the Group 1 triplet. |
| 5176 |
// arg3 is the Group 2 triplet |
| 5177 |
cblc_field_t **arg2_f = __gg__treeplet_1f; |
| 5178 |
size_t *arg2_o = __gg__treeplet_1o; |
| 5179 |
size_t *arg2_s = __gg__treeplet_1s; |
| 5180 |
cblc_field_t **arg3_f = __gg__treeplet_2f; |
| 5181 |
const size_t *arg3_o = __gg__treeplet_2o; |
| 5182 |
const size_t *arg3_s = __gg__treeplet_2s; |
| 5183 |
|
| 5184 |
ssize_t retval_size; |
| 5185 |
retval_size = 256; |
| 5186 |
char *retval = static_cast<char *>(malloc(retval_size)); |
| 5187 |
massert(retval); |
| 5188 |
*retval = '\0'; |
| 5189 |
|
| 5190 |
const char *haystack = PTRCAST(char, (arg1_f->data + arg1_o)); |
| 5191 |
const char *haystack_e = haystack + arg1_s; |
| 5192 |
|
| 5193 |
ssize_t outdex = 0; |
| 5194 |
|
| 5195 |
const char **pflasts = static_cast<const char **>(malloc(N * sizeof(char *))); |
| 5196 |
massert(pflasts); |
| 5197 |
|
| 5198 |
if( arg1_s == 0 ) |
| 5199 |
{ |
| 5200 |
exception_raise(ec_argument_function_e); |
| 5201 |
goto bugout; |
| 5202 |
} |
| 5203 |
|
| 5204 |
for( size_t i=0; i<N; i++ ) |
| 5205 |
{ |
| 5206 |
if( arg2_s[i] == 0 ) |
| 5207 |
{ |
| 5208 |
exception_raise(ec_argument_function_e); |
| 5209 |
goto bugout; |
| 5210 |
} |
| 5211 |
if( control[i] & substitute_anycase_e ) |
| 5212 |
{ |
| 5213 |
if( control[i] & substitute_first_e ) |
| 5214 |
{ |
| 5215 |
pflasts[i] = strcasestr(haystack, |
| 5216 |
haystack_e, |
| 5217 |
PTRCAST(char, (arg2_f[i]->data+arg2_o[i])), |
| 5218 |
PTRCAST(char, (arg2_f[i]->data+arg2_o[i])) + arg2_s[i]); |
| 5219 |
} |
| 5220 |
else if( control[i] & substitute_last_e) |
| 5221 |
{ |
| 5222 |
pflasts[i] = strcaselaststr(haystack, |
| 5223 |
haystack_e, |
| 5224 |
PTRCAST(char, (arg2_f[i]->data+arg2_o[i])), |
| 5225 |
PTRCAST(char, (arg2_f[i]->data+arg2_o[i])) + arg2_s[i]); |
| 5226 |
} |
| 5227 |
else |
| 5228 |
{ |
| 5229 |
pflasts[i] = NULL; |
| 5230 |
} |
| 5231 |
} |
| 5232 |
else |
| 5233 |
{ |
| 5234 |
if( control[i] & substitute_first_e ) |
| 5235 |
{ |
| 5236 |
pflasts[i] = strstr(haystack, |
| 5237 |
haystack_e, |
| 5238 |
PTRCAST(char, (arg2_f[i]->data+arg2_o[i])), |
| 5239 |
PTRCAST(char, (arg2_f[i]->data+arg2_o[i])) + arg2_s[i]); |
| 5240 |
} |
| 5241 |
else if( control[i] & substitute_last_e) |
| 5242 |
{ |
| 5243 |
pflasts[i] = strlaststr(haystack, |
| 5244 |
haystack_e, |
| 5245 |
PTRCAST(char, (arg2_f[i]->data+arg2_o[i])), |
| 5246 |
PTRCAST(char, (arg2_f[i]->data+arg2_o[i])) + arg2_s[i]); |
| 5247 |
} |
| 5248 |
else |
| 5249 |
{ |
| 5250 |
pflasts[i] = NULL; |
| 5251 |
} |
| 5252 |
} |
| 5253 |
} |
| 5254 |
|
| 5255 |
while( haystack < haystack_e ) |
| 5256 |
{ |
| 5257 |
bool did_something = false; |
| 5258 |
for( size_t i=0; i<N; i++ ) |
| 5259 |
{ |
| 5260 |
// Let's make sure that there is enough room in the case that we add this |
| 5261 |
// arg |
| 5262 |
while( outdex - (ssize_t)arg2_s[i] + (ssize_t)arg3_s[i] |
| 5263 |
> retval_size ) |
| 5264 |
{ |
| 5265 |
retval_size *= 2; |
| 5266 |
retval = static_cast<char *>(realloc(retval, retval_size)); |
| 5267 |
massert(retval); |
| 5268 |
} |
| 5269 |
|
| 5270 |
// We checked earlier for FIRST/LAST matches |
| 5271 |
bool matched = pflasts[i] == haystack; |
| 5272 |
if( !matched ) |
| 5273 |
{ |
| 5274 |
// It didn't match. But if it was flagged as FIRST or LAST, we need |
| 5275 |
// to skip it |
| 5276 |
|
| 5277 |
if( control[i] & (substitute_first_e|substitute_last_e) ) |
| 5278 |
{ |
| 5279 |
continue; |
| 5280 |
} |
| 5281 |
|
| 5282 |
const char *needle = PTRCAST(char, arg2_f[i]->data+arg2_o[i]); |
| 5283 |
const char *needle_e = PTRCAST(char, arg2_f[i]->data+arg2_o[i]) + arg2_s[i]; |
| 5284 |
matched = (control[i] & substitute_anycase_e) && iscasematch( |
| 5285 |
haystack, |
| 5286 |
haystack_e, |
| 5287 |
needle, |
| 5288 |
needle_e); |
| 5289 |
if( !matched ) |
| 5290 |
{ |
| 5291 |
matched = !(control[i] & substitute_anycase_e) && ismatch(haystack, |
| 5292 |
haystack_e, |
| 5293 |
needle, |
| 5294 |
needle_e) ; |
| 5295 |
} |
| 5296 |
} |
| 5297 |
if( matched ) |
| 5298 |
{ |
| 5299 |
haystack += arg2_s[i]; |
| 5300 |
memcpy(retval + outdex, arg3_f[i]->data + arg3_o[i], arg3_s[i]); |
| 5301 |
outdex += arg3_s[i]; |
| 5302 |
did_something = true; |
| 5303 |
break; |
| 5304 |
} |
| 5305 |
} |
| 5306 |
if( !did_something ) |
| 5307 |
{ |
| 5308 |
while( outdex + 1 > retval_size ) |
| 5309 |
{ |
| 5310 |
retval_size *= 2; |
| 5311 |
retval = static_cast<char *>(realloc(retval, retval_size)); |
| 5312 |
massert(retval); |
| 5313 |
} |
| 5314 |
retval[outdex++] = *haystack++; |
| 5315 |
} |
| 5316 |
} |
| 5317 |
|
| 5318 |
bugout: |
| 5319 |
__gg__adjust_dest_size(dest, outdex); |
| 5320 |
memcpy(dest->data, retval, outdex); |
| 5321 |
|
| 5322 |
free(pflasts); |
| 5323 |
free(retval); |
| 5324 |
} |
| 5325 |
|
| 5326 |
extern "C" |
| 5327 |
void |
| 5328 |
__gg__locale_compare( cblc_field_t *dest, |
| 5329 |
const cblc_field_t *arg1, |
| 5330 |
size_t arg1_o, |
| 5331 |
size_t arg1_s, |
| 5332 |
const cblc_field_t *arg2, |
| 5333 |
size_t arg2_o, |
| 5334 |
size_t arg2_s, |
| 5335 |
const cblc_field_t *arg_locale, |
| 5336 |
size_t /*arg_locale_o*/, |
| 5337 |
size_t /*arg_locale_s*/ |
| 5338 |
) |
| 5339 |
{ |
| 5340 |
char achretval[2] = "?"; |
| 5341 |
|
| 5342 |
if( arg_locale ) |
| 5343 |
{ |
| 5344 |
// We don't yet know what to do with a locale |
| 5345 |
exception_raise(ec_locale_missing_e); |
| 5346 |
} |
| 5347 |
else |
| 5348 |
{ |
| 5349 |
// Default locale |
| 5350 |
achretval[0] = '='; |
| 5351 |
size_t length = std::min(arg1_s, arg2_s); |
| 5352 |
for(size_t i=0; i<length; i++ ) |
| 5353 |
{ |
| 5354 |
if( (arg1->data+arg1_o)[i] < (arg2->data+arg2_o)[i] ) |
| 5355 |
{ |
| 5356 |
achretval[0] = '<'; |
| 5357 |
break; |
| 5358 |
} |
| 5359 |
if( (arg1->data+arg1_o)[i] > (arg2->data+arg2_o)[i] ) |
| 5360 |
{ |
| 5361 |
achretval[0] = '>'; |
| 5362 |
break; |
| 5363 |
} |
| 5364 |
} |
| 5365 |
if( achretval[0] == '=' ) |
| 5366 |
{ |
| 5367 |
if( arg1_s < arg2_s ) |
| 5368 |
{ |
| 5369 |
achretval[0] = '<'; |
| 5370 |
} |
| 5371 |
else if( arg1_s > arg2_s ) |
| 5372 |
{ |
| 5373 |
achretval[0] = '>'; |
| 5374 |
} |
| 5375 |
} |
| 5376 |
} |
| 5377 |
|
| 5378 |
__gg__adjust_dest_size(dest, 1); |
| 5379 |
ascii_to_internal_str(achretval, 1); |
| 5380 |
dest->data[0] = *achretval; |
| 5381 |
} |
| 5382 |
|
| 5383 |
extern "C" |
| 5384 |
void |
| 5385 |
__gg__locale_date(cblc_field_t *dest, |
| 5386 |
const cblc_field_t *arg1, |
| 5387 |
size_t arg1_o, |
| 5388 |
size_t /*arg1_s*/, |
| 5389 |
const cblc_field_t *arg_locale, |
| 5390 |
size_t /*arg_locale_o*/, |
| 5391 |
size_t /*arg_locale_s*/) |
| 5392 |
{ |
| 5393 |
char ach[256] = " "; |
| 5394 |
|
| 5395 |
if( arg_locale ) |
| 5396 |
{ |
| 5397 |
// We don't yet know what to do with a locale |
| 5398 |
exception_raise(ec_locale_missing_e); |
| 5399 |
} |
| 5400 |
else |
| 5401 |
{ |
| 5402 |
// Default locale |
| 5403 |
tm tm; |
| 5404 |
memcpy(ach, arg1->data+arg1_o, 8); |
| 5405 |
ach[8] = '\0'; |
| 5406 |
long ymd = atoi(ach); |
| 5407 |
tm.tm_year = ymd/10000 - 1900; |
| 5408 |
tm.tm_mon = ymd/100 % 100; |
| 5409 |
tm.tm_mday = ymd % 100; |
| 5410 |
strcpy(ach, nl_langinfo(D_FMT)); |
| 5411 |
strftime(ach, sizeof(ach), nl_langinfo(D_FMT), &tm); |
| 5412 |
} |
| 5413 |
|
| 5414 |
__gg__adjust_dest_size(dest, strlen(ach)); |
| 5415 |
ascii_to_internal_str(ach, strlen(ach)); |
| 5416 |
memcpy(dest->data, ach, strlen(ach)); |
| 5417 |
} |
| 5418 |
|
| 5419 |
extern "C" |
| 5420 |
void |
| 5421 |
__gg__locale_time(cblc_field_t *dest, |
| 5422 |
const cblc_field_t *arg1, |
| 5423 |
size_t arg1_o, |
| 5424 |
size_t /*arg1_s*/, |
| 5425 |
const cblc_field_t *arg_locale, |
| 5426 |
size_t /*arg_locale_o*/, |
| 5427 |
size_t /*arg_locale_s*/) |
| 5428 |
|
| 5429 |
{ |
| 5430 |
char ach[256] = " "; |
| 5431 |
|
| 5432 |
if( arg_locale) |
| 5433 |
{ |
| 5434 |
// We don't yet know what to do with a locale |
| 5435 |
exception_raise(ec_locale_missing_e); |
| 5436 |
} |
| 5437 |
else |
| 5438 |
{ |
| 5439 |
// Default locale |
| 5440 |
tm tm = {}; |
| 5441 |
memcpy(ach, arg1->data+arg1_o, 8); |
| 5442 |
ach[8] = '\0'; |
| 5443 |
long hms = atoi(ach); |
| 5444 |
tm.tm_hour = hms/10000; |
| 5445 |
tm.tm_min = hms/100 % 100; |
| 5446 |
tm.tm_sec = hms % 100; |
| 5447 |
strftime(ach, sizeof(ach), nl_langinfo(T_FMT), &tm); |
| 5448 |
} |
| 5449 |
|
| 5450 |
__gg__adjust_dest_size(dest, strlen(ach)); |
| 5451 |
ascii_to_internal_str(ach, strlen(ach)); |
| 5452 |
memcpy(dest->data, ach, strlen(ach)); |
| 5453 |
} |
| 5454 |
|
| 5455 |
extern "C" |
| 5456 |
void |
| 5457 |
__gg__locale_time_from_seconds( cblc_field_t *dest, |
| 5458 |
const cblc_field_t *arg1, |
| 5459 |
size_t arg1_o, |
| 5460 |
size_t arg1_s, |
| 5461 |
const cblc_field_t *arg_locale, |
| 5462 |
size_t /*arg_locale_o*/, |
| 5463 |
size_t /*arg_locale_s*/) |
| 5464 |
{ |
| 5465 |
char ach[256] = " "; |
| 5466 |
|
| 5467 |
if( arg_locale ) |
| 5468 |
{ |
| 5469 |
// We don't yet know what to do with a locale |
| 5470 |
exception_raise(ec_locale_missing_e); |
| 5471 |
} |
| 5472 |
else |
| 5473 |
{ |
| 5474 |
// Default locale |
| 5475 |
tm tm = {}; |
| 5476 |
|
| 5477 |
int rdigits=0; |
| 5478 |
long seconds = (long)__gg__binary_value_from_qualified_field(&rdigits, |
| 5479 |
arg1, |
| 5480 |
arg1_o, |
| 5481 |
arg1_s); |
| 5482 |
tm.tm_hour = seconds/3600; |
| 5483 |
tm.tm_min = ((seconds%3600) / 60) % 100; |
| 5484 |
tm.tm_sec = seconds % 100; |
| 5485 |
strftime(ach, sizeof(ach), nl_langinfo(T_FMT), &tm); |
| 5486 |
} |
| 5487 |
|
| 5488 |
__gg__adjust_dest_size(dest, strlen(ach)); |
| 5489 |
ascii_to_internal_str(ach, strlen(ach)); |
| 5490 |
memcpy(dest->data, ach, strlen(ach)); |
| 5491 |
} |