home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rexx / rexxfunc / ezrxfunc / c / rxsutils.c < prev   
C/C++ Source or Header  |  1993-07-28  |  7KB  |  331 lines

  1. /*
  2.  * rxsutils.c -- REXX String utilities.  Various utility functions to do
  3.  *               string to type conversions and back.  You don't need to
  4.  *               use these, but they may be useful to you.
  5.  */
  6.  
  7. #include "standard.h"
  8.  
  9. #define INCL_DOS
  10. #include <os2.h>
  11. #include <rexxsaa.h>
  12.  
  13. #include "rxsutils.h"
  14.  
  15. #define MAX_NUM_STRING_LEN  20
  16.  
  17. /*
  18.  * CopyResult -- Copies a string into a result, allocating space for it
  19.  *               if necessary.  If you pass it an RXSTRING with a non-null
  20.  *               buffer and a non-zero length, it will try to copy the
  21.  *               data into that buffer.  Otherwise is uses DosAllocMem
  22.  *               to allocate a new one.
  23.  */
  24.  
  25. void CopyResult( char *src, ULONG len, PRXSTRING dest )
  26.   {
  27.     void *mem = NULL;
  28.  
  29.     if( !dest ) goto leave;
  30.  
  31.     if( ( !src || len == 0 ) && dest->strptr != NULL ){
  32.         SetNullRXString( dest );
  33.     } else if( dest->strptr != NULL && len <= dest->strlength ){
  34.         memset( dest->strptr, 0, dest->strlength );
  35.         memcpy( dest->strptr, src, len );
  36.         dest->strlength = len;
  37.     } else {
  38.         /* OK, at this point we've determined the buffer is too small
  39.            for use to use, so we'll allocate a new one */
  40.  
  41.         SetNullRXString( dest );
  42.  
  43.         if( DosAllocMem( &mem, len + 1, PAG_COMMIT | PAG_WRITE | PAG_READ ) )
  44.             goto leave;
  45.  
  46.         dest->strptr    = mem;
  47.         dest->strlength = len;
  48.  
  49.         memset( dest->strptr, 0, len + 1 );
  50.         memcpy( dest->strptr, src, len );
  51.     }
  52.  
  53.   leave:
  54.     return;
  55.   }
  56.  
  57. /*
  58.  * SetNullRXString -- Sets an RXSTRING to a null string.
  59.  */
  60.  
  61. void SetNullRXString( PRXSTRING str )
  62.   {
  63.     if( str ){
  64.         if( str->strptr ){
  65.             str->strptr[0] = NULLCHAR;
  66.         }
  67.  
  68.         str->strlength = 0;
  69.     }
  70.   }
  71.  
  72. /*
  73.  * The following functions were defined for use with WATCOM C.  They may
  74.  * work on other systems, but first you have to remove the #ifdef below
  75.  */
  76.  
  77. #ifdef __WATCOMC__
  78.  
  79. /*
  80.  * AllocNumSpace -- Allocate enough memory for a number.
  81.  */
  82.  
  83. static BOOL AllocNumSpace( PRXSTRING str )
  84.   {
  85.     if( !str ) goto error;
  86.  
  87.     if( str->strptr == NULL || str->strlength < MAX_NUM_STRING_LEN + 1 ){
  88.         if( DosAllocMem( (PPVOID) &str->strptr, MAX_NUM_STRING_LEN + 1,
  89.                          PAG_COMMIT | PAG_WRITE | PAG_READ ) != 0 ) goto error;
  90.     }
  91.  
  92.     memset( str->strptr, 0, MAX_NUM_STRING_LEN + 1 );
  93.     str->strlength = MAX_NUM_STRING_LEN;
  94.  
  95.     return( TRUE );
  96.  
  97.   error:
  98.     return( FALSE );
  99.   }
  100.  
  101. /*
  102.  * RXStringToULong
  103.  */
  104.  
  105. BOOL RXStringToULong( PRXSTRING str, PULONG val )
  106.   {
  107.     char *end;
  108.  
  109.     if( !str || !val || str->strptr == NULL || str->strlength == 0 ) goto error;
  110.  
  111.     errno = 0;
  112.     *val  = strtoul( str->strptr, &end, 10 );
  113.     if( errno != 0 ) goto error;
  114.  
  115.     while( isspace( *end ) ){
  116.         ++end;
  117.     }
  118.  
  119.     if( *end != NULLCHAR ) goto error;
  120.  
  121.     return( TRUE );
  122.  
  123.   error:
  124.     return( FALSE );
  125.   }
  126.  
  127. /*
  128.  * ULongToRXString
  129.  */
  130.  
  131. BOOL ULongToRXString( ULONG val, PRXSTRING str )
  132.   {
  133.     if( !AllocNumSpace( str ) ) goto error;
  134.  
  135.     str->strlength = sprintf( str->strptr, "%lu", val );
  136.  
  137.     return( TRUE );
  138.  
  139.   error:
  140.     return( FALSE );
  141.   }
  142.  
  143. /*
  144.  * RXStringToLong
  145.  */
  146.  
  147. BOOL RXStringToLong( PRXSTRING str, PLONG val )
  148.   {
  149.     char *end;
  150.  
  151.     if( !str || !val || str->strptr == NULL || str->strlength == 0 ) goto error;
  152.  
  153.     errno = 0;
  154.     *val  = strtol( str->strptr, &end, 10 );
  155.     if( errno != 0 ) goto error;
  156.  
  157.     while( isspace( *end ) ){
  158.         ++end;
  159.     }
  160.  
  161.     if( *end != NULLCHAR ) goto error;
  162.  
  163.     return( TRUE );
  164.  
  165.   error:
  166.     return( FALSE );
  167.   }
  168.  
  169. /*
  170.  * LongToRXString
  171.  */
  172.  
  173. BOOL LongToRXString( LONG val, PRXSTRING str )
  174.   {
  175.     if( !AllocNumSpace( str ) ) goto error;
  176.  
  177.     str->strlength = sprintf( str->strptr, "%ld", val );
  178.  
  179.     return( TRUE );
  180.  
  181.   error:
  182.     return( FALSE );
  183.   }
  184.  
  185. /*
  186.  * RXStringToUShort
  187.  */
  188.  
  189. BOOL RXStringToUShort( PRXSTRING str, PUSHORT val )
  190.   {
  191.     long l;
  192.     char *end;
  193.  
  194.     if( !str || !val || str->strptr == NULL || str->strlength == 0 ) goto error;
  195.  
  196.     *val  = 0;
  197.     errno = 0;
  198.     l     = strtol( str->strptr, &end, 10 );
  199.     if( errno != 0 ) goto error;
  200.  
  201.     while( isspace( *end ) ){
  202.         ++end;
  203.     }
  204.  
  205.     if( *end != NULLCHAR ) goto error;
  206.  
  207.     if( l < 0 || l > 65535 ) goto error;
  208.  
  209.     *val = (USHORT) l;
  210.  
  211.     return( TRUE );
  212.  
  213.   error:
  214.     return( FALSE );
  215.   }
  216.  
  217. /*
  218.  * UShortToRXString
  219.  */
  220.  
  221. BOOL UShortToRXString( USHORT val, PRXSTRING str )
  222.   {
  223.     if( !AllocNumSpace( str ) ) goto error;
  224.  
  225.     str->strlength = sprintf( str->strptr, "%u", val );
  226.  
  227.     return( TRUE );
  228.  
  229.   error:
  230.     return( FALSE );
  231.   }
  232.  
  233. /*
  234.  * RXStringToShort
  235.  */
  236.  
  237. BOOL RXStringToShort( PRXSTRING str, PSHORT val )
  238.   {
  239.     long  l;
  240.     char *end;
  241.  
  242.     if( !str || !val || str->strptr == NULL || str->strlength == 0 ) goto error;
  243.  
  244.     *val  = 0;
  245.     errno = 0;
  246.     l     = strtol( str->strptr, &end, 10 );
  247.     if( errno != 0 ) goto error;
  248.  
  249.     while( isspace( *end ) ){
  250.         ++end;
  251.     }
  252.  
  253.     if( *end != NULLCHAR ) goto error;
  254.  
  255.     if( l < -32768 || l > 32767 ) goto error;
  256.  
  257.     *val = (SHORT) l;
  258.  
  259.     return( TRUE );
  260.  
  261.   error:
  262.     return( FALSE );
  263.   }
  264.  
  265. /*
  266.  * ShortToRXString
  267.  */
  268.  
  269. BOOL ShortToRXString( SHORT val, PRXSTRING str )
  270.   {
  271.     if( !AllocNumSpace( str ) ) goto error;
  272.  
  273.     str->strlength = sprintf( str->strptr, "%d", val );
  274.  
  275.     return( TRUE );
  276.  
  277.   error:
  278.     return( FALSE );
  279.   }
  280.  
  281. /*
  282.  * RXStringToBoolean -- Sets an RXSTRING to a boolean value ("0" or "1").
  283.  */
  284.  
  285. BOOL BooleanToRXString( PRXSTRING str, BOOL val )
  286.   {
  287.     if( !AllocNumSpace( str ) ) goto error;
  288.  
  289.     if( str && str->strptr ){
  290.         strcpy( str->strptr, ( val ? "1" : "0" ) );
  291.         str->strlength = 1;
  292.     }
  293.  
  294.     return( TRUE );
  295.  
  296. error:
  297.     return( FALSE );
  298.   }
  299.  
  300. /*
  301.  * RXStringToBoolean -- Returns 1, 0 or -1 (for error).
  302.  */
  303.  
  304. BOOL RXStringToBoolean( PRXSTRING str )
  305.   {
  306.     char ch;
  307.  
  308.     if( !str || !str->strptr || str->strlength == 0 ) goto error;
  309.  
  310.     if( str->strlength == 1 ){
  311.         ch = str->strptr[0];
  312.  
  313.         if( ch == '1' || ch == 't' || ch == 'T' ){
  314.             return( 1 );
  315.         } else if( ch == '0' || ch == 'f' || ch == 'F' ){
  316.             return( 0 );
  317.         }
  318.     } else if( str->strlength == 4 &&
  319.                stricmp( str->strptr, "TRUE" ) == 0 ){
  320.         return( 1 );
  321.     } else if( str->strlength == 5 &&
  322.                stricmp( str->strptr, "FALSE" ) == 0 ){
  323.         return( 0 );
  324.     }
  325.  
  326.   error:
  327.     return( -1 );
  328.   }
  329.  
  330. #endif
  331.