Debian Codegraph @

rexxbif.c

raw
1
2 #include <stdio.h>
3 #include <limits.h>
4 #include "rexxbif.h"
5
6 #ifdef EXTERNAL_TO_REGINA
7 #define exiterror __regina_exiterror
8 void DropStreng( streng *str )
9 {
10 if ( str )
11 free( str );
12 }
13
14 streng *MakeStreng( int num )
15 {
16 streng *out = (streng *)malloc( sizeof(streng) + num );
17 if ( out != NULL )
18 {
19 out->len = out->max = num;
20 if ( num )
21 {
22 memset( out->value, '$', num );
23 }
24 }
25 if ( out == NULL ) fprintf(stderr,"ERROR allocating RXSTRING\n");
26 return out;
27 }
28 void exiterror( int errorno, int suberrorno, ... )
29 {
30 }
31 #endif
32
33 int Rexx_x2d( const tsd_t *TSD, const streng *hex, int *error )
34 {
35 int dec=0,i;
36 char c;
37
38 if ( PSTRENGLEN(hex) == 0 )
39 {
40 *error = 1;
41 return -1;
42 }
43 for ( i = 0; i < PSTRENGLEN(hex); i++ )
44 {
45 dec <<= 4;
46 c = hex->value[i];
47 if ( c >='0'&& c<='9')
48 dec += c-'0';
49 else if ( c >= 'A' &&c <= 'F' )
50 dec += c-'A'+10;
51 else if( c >='a' && c <='f' )
52 dec += c-'a'+10;
53 else
54 {
55 *error = 1;
56 return -1;
57 }
58 }
59 *error = 0;
60 return dec;
61 }
62
63 streng *Rexx_right( const tsd_t *TSD, streng *str, int length, char padch )
64 {
65 streng *out = MAKESTRENG( length );
66 int i,j;
67 if ( out )
68 {
69 for ( j = 0; PSTRENGLEN(str) > j; j++) ;
70 for (i=length-1,j--;(i>=0)&&(j>=0);out->value[i--]=str->value[j--]) ;
71 for (;i>=0;out->value[i--]=padch) ;
72 out->len = length;
73 }
74 return out;
75 }
76
77 streng *Rexx_d2x( const tsd_t *TSD, int num )
78 {
79 streng *out;
80 out = MAKESTRENG( 100 ); /* use 100 as the largest length of a hex value we will ever get */
81 if ( out )
82 {
83 out->len = sprintf( PSTRENGVAL(out), "%X", num );
84 }
85 return out;
86 }
87
88 streng *Rexx_upper( const tsd_t *TSD, streng *str )
89 {
90 int i;
91
92 for ( i = 0; i < PSTRENGLEN( str ); i++ )
93 {
94 if ( islower( str->value[i] ) )
95 str->value[i] = (char)toupper( str->value[i] );
96 }
97 return str;
98 }