home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / rxasyn20.zip / RXVPFUNC.C < prev    next >
Text File  |  1994-12-27  |  16KB  |  332 lines

  1. /*****************************************************************************/
  2. /*                                                                           */
  3. /*  MODULE         RXVPFUNC.C                                                */
  4. /*                                                                           */
  5. /*  VERSION        Version 2.0 - 26th Dec 1994                               */
  6. /*                                                                           */
  7. /*  COPYRIGHT      Copyright (c) 1993, 1994 by Crucial Applications          */
  8. /*                             All rights reserved.                          */
  9. /*                                                                           */
  10. /*  DESCRIPTION    Rexx Variable Pool Functions                              */
  11. /*                                                                           */
  12. /*  FUNCTIONS                                                                */
  13. /*                                                                           */
  14. /*    InitVar      - Initialise rexx variable structure for fetch            */
  15. /*    FetchVar     - Fetches the specified variables from the variable pool  */
  16. /*    EditVar      - Modify value in specified rexx variable for update      */
  17. /*    UpdateVar    - Updates the specified variables into the variable pool  */
  18. /*    FreeVar      - Free rexx variable memory allocated by rexx             */
  19. /*                                                                           */
  20. /*    NewRexxValue - Create the specified rexx variable                      */
  21. /*    GetRexxValue - Get the value of a specified rexx variable              */
  22. /*    SetRexxValue - Set the value of a specified rexx variable              */
  23. /*                                                                           */
  24. /*    NewStemValue - Create the specified rexx stem variable                 */
  25. /*    GetStemValue - Get the value of a specified rexx stem variable         */
  26. /*    SetStemValue - Set the value of a specified rexx stem variable         */
  27. /*                                                                           */
  28. /*****************************************************************************/
  29.  
  30. /*********************************************************************/
  31. /* Includes needed by this module                                    */
  32. /*********************************************************************/
  33.  
  34. #define  _MT
  35. #define  _DLL
  36. #define  INCL_DOS
  37. #define  INCL_ERRORS
  38. #define  INCL_REXXSAA
  39.  
  40. #include <os2.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <stdlib.h>
  44. #include <rexxsaa.h>
  45.  
  46. #include "miscfunc.h"
  47. #include "rxvpfunc.h"
  48.  
  49. /*********************************************************************/
  50. /* Function:  InitVar( &rexxvar, &nextvar, varname )                 */
  51. /*                                                                   */
  52. /* Purpose:   Initialises Rexx variable with the specified name in   */
  53. /*            preparation for querying the variable pool.            */
  54. /*********************************************************************/
  55. VOID InitVar(
  56.         PSHVBLOCK  pRexxVar,
  57.         PSHVBLOCK  pNextPtr,
  58.         PSZ        VarName )
  59. {
  60.    pRexxVar->shvnext            = pNextPtr;
  61.    pRexxVar->shvname.strptr     = VarName;
  62.    pRexxVar->shvname.strlength  = strlen(VarName);
  63.    pRexxVar->shvvalue.strptr    = NULL;
  64.    pRexxVar->shvvalue.strlength = 0;
  65.    return;
  66. }
  67.  
  68. /*********************************************************************/
  69. /* Function:  FetchVar( &rexxvar )                                   */
  70. /*                                                                   */
  71. /* Purpose:   Fetches the Rexx variable(s) from the variable pool.   */
  72. /*            Rexx allocates the returned values, we free it later.  */
  73. /*********************************************************************/
  74. APIRET FetchVar( PSHVBLOCK  pRexxVar )
  75. {
  76.    APIRET    rc;                       /* Function return code       */
  77.    APIRET    rxrc;                     /* RexxVariablePool return    */
  78.    PSHVBLOCK pNextPtr;
  79.    pNextPtr = pRexxVar;
  80.    while( pNextPtr != NULL ) {
  81.       pNextPtr->shvcode     = RXSHV_SYFET;
  82.       pNextPtr->shvret      = (UCHAR) 0;
  83.       pNextPtr->shvnamelen  = pNextPtr->shvname.strlength;
  84.       pNextPtr->shvvaluelen = pNextPtr->shvvalue.strlength;
  85.       pNextPtr = pNextPtr->shvnext;
  86.    }
  87.    rxrc = RexxVariablePool( pRexxVar ); /* GET the rexx variable     */
  88.    if ( rxrc == RXSHV_OK    ) {
  89.       rc = NO_ERROR;                   /* all okay                   */
  90.    } else if ( rxrc & RXSHV_MEMFL ) {
  91.       rc = ERROR_NOT_ENOUGH_MEMORY;    /* no memory                  */
  92.    } else {
  93.       rc = ERROR_INVALID_PARAMETER;    /* other failure              */
  94.    }
  95.    return( rc );
  96. }
  97.  
  98. /*********************************************************************/
  99. /* Function:  EditVar( &rexxvar, type, &value )                      */
  100. /*                                                                   */
  101. /* Purpose:   Modifies the Rexx variable with the specified value in */
  102. /*            preparation for return to the variable pool.           */
  103. /*********************************************************************/
  104. VOID EditVar(
  105.         PSHVBLOCK  pRexxVar,
  106.         USHORT     VarType,
  107.         PVOID      VarValue )
  108. {
  109.    Val2Str( VarValue, pRexxVar->shvvalue.strptr, VarType );
  110.    pRexxVar->shvvalue.strlength = strlen( pRexxVar->shvvalue.strptr );
  111.    return;
  112. }
  113.  
  114. /*********************************************************************/
  115. /* Function:  UpdateVar( &rexxvar )                                  */
  116. /*                                                                   */
  117. /* Purpose:   Updates the Rexx variable(s) into the variable pool.   */
  118. /*********************************************************************/
  119. APIRET UpdateVar( PSHVBLOCK  pRexxVar )
  120. {
  121.    APIRET    rc;                       /* Function return code       */
  122.    APIRET    rxrc;                     /* RexxVariablePool return    */
  123.    PSHVBLOCK pNextPtr;
  124.    pNextPtr = pRexxVar;
  125.    while( pNextPtr != NULL ) {
  126.       pNextPtr->shvcode     = RXSHV_SYSET;
  127.       pNextPtr->shvret      = (UCHAR) 0;
  128.       pNextPtr->shvvaluelen = pNextPtr->shvvalue.strlength;
  129.       pNextPtr = pNextPtr->shvnext;
  130.    }
  131.    rxrc = RexxVariablePool( pRexxVar ); /* GET the rexx variable     */
  132.    if ( rxrc == RXSHV_OK    ) {
  133.       rc = NO_ERROR;                   /* all okay                   */
  134.    } else if ( rxrc == RXSHV_NEWV ) {
  135.       rc = NO_ERROR;                   /* all new variables          */
  136.    } else if ( rxrc & RXSHV_MEMFL ) {
  137.       rc = ERROR_NOT_ENOUGH_MEMORY;    /* no memory                  */
  138.    } else {
  139.       rc = ERROR_INVALID_PARAMETER;    /* other failure              */
  140.    }
  141.    return( rc );
  142. }
  143.  
  144. /*********************************************************************/
  145. /* Function:  FreeVar( &rexxvar )                                    */
  146. /*                                                                   */
  147. /* Purpose:   Frees the memory allocated to the Rexx variable value  */
  148. /*            which is allocated when fetching the variable(s) from  */
  149. /*            the variable pool.                                     */
  150. /*********************************************************************/
  151. VOID FreeVar( PSHVBLOCK  pRexxVar )
  152. {
  153.    PSHVBLOCK pNextPtr;
  154.    pNextPtr = pRexxVar;
  155.    while( pNextPtr != NULL ) {
  156.       if ( pNextPtr->shvvalue.strptr != NULL ) {
  157.          DosFreeMem( pNextPtr->shvvalue.strptr );
  158.          pNextPtr->shvvalue.strlength = 0;
  159.          pNextPtr->shvvaluelen = 0;
  160.       }
  161.       pNextPtr = pNextPtr->shvnext;
  162.    }
  163.    return;
  164. }
  165.  
  166. /*********************************************************************/
  167. /* Function:  rc = NewRexxValue( name, type, &value )                */
  168. /*                                                                   */
  169. /* Purpose:   Initialises rexx variable with the specified name then */
  170. /*            updates the rexx variable with the specified value and */
  171. /*            creates it in the variable pool.                       */
  172. /*                                                                   */
  173. /* Notes:     Currently only handles normal (256byte) values.        */
  174. /*********************************************************************/
  175. APIRET NewRexxValue(
  176.           PSZ       VarName,
  177.           USHORT    VarType,
  178.           PVOID     VarValue )
  179. {
  180.    APIRET   rc;                        /* Function return code       */
  181.    SHVBLOCK RexxVar;                   /* Rexx variable structure    */
  182.    CHAR     VarBuff[RXAUTOBUFLEN];     /* Holder for value           */
  183.    VarBuff[0] = EOSTR_CH;
  184.    InitVar( &RexxVar, NULL, VarName ); /* Initialise for fetch       */
  185.    RexxVar.shvvalue.strptr    = VarBuff;
  186.    RexxVar.shvvalue.strlength = RXAUTOBUFLEN;
  187.    EditVar( &RexxVar, VarType, VarValue );
  188.    rc = UpdateVar( &RexxVar );         /* Create variable            */
  189.    return( rc );
  190. }
  191.  
  192. /*********************************************************************/
  193. /* Function:  rc = GetRexxValue( name, type, &value )                */
  194. /*                                                                   */
  195. /* Purpose:   Initialises rexx variable with the specified name and  */
  196. /*            fetches its contents from the rexx variable pool then  */
  197. /*            frees the memory which was allocated by the variable   */
  198. /*            pool interface when fetching the variable from it.     */
  199. /*                                                                   */
  200. /*            The value returned is copied into the variable pointed */
  201. /*            to by the VarValue parameter, this memory must be      */
  202. /*            allocated by the caller and be of sufficient length to */
  203. /*            hold the returned value.                               */
  204. /*                                                                   */
  205. /*            The value type returned is determined by the VarType   */
  206. /*            parameter and must be a valid type for the Str2Num     */
  207. /*            function call.                                         */
  208. /*********************************************************************/
  209. APIRET GetRexxValue(
  210.           PSZ       VarName,
  211.           USHORT    VarType,
  212.           PVOID     VarValue )
  213. {
  214.    APIRET   rc;                        /* Function return code       */
  215.    SHVBLOCK RexxVar;                   /* Rexx variable structure    */
  216.    InitVar( &RexxVar, NULL, VarName ); /* Setup variable for fetch   */
  217.    rc = FetchVar( &RexxVar );          /* Fetch variable             */
  218.    if ( rc == NO_ERROR ) {
  219.       if ( !Str2Num( RexxVar.shvvalue.strptr, VarValue, VarType ) ) {
  220.          rc = ERROR_INVALID_PARAMETER;
  221.       }
  222.    }
  223.    FreeVar( &RexxVar );                      /* Free variable        */
  224.    return( rc );
  225. }
  226.  
  227. /*********************************************************************/
  228. /* Function:  rc = SetRexxValue( name, type, &value )                */
  229. /*                                                                   */
  230. /* Purpose:   Initialises rexx variable with the specified name and  */
  231. /*            fetches its contents from the rexx variable pool then  */
  232. /*            updates the rexx variable with the specified value and */
  233. /*            returns it to the variable pool and finally frees the  */
  234. /*            memory which was allocated by the rexx variable pool   */
  235. /*            interface when fetching the variable from the pool.    */
  236. /*********************************************************************/
  237. APIRET SetRexxValue(
  238.           PSZ       VarName,
  239.           USHORT    VarType,
  240.           PVOID     VarValue )
  241. {
  242.    APIRET   rc;                        /* Function return code       */
  243.    SHVBLOCK RexxVar;                   /* Rexx variable structure    */
  244.    InitVar( &RexxVar, NULL, VarName ); /* Initialise for fetch       */
  245.    rc = FetchVar( &RexxVar );          /* Fetch variable             */
  246.    if ( rc == NO_ERROR ) {
  247.       EditVar( &RexxVar, VarType, VarValue );/* Alter var contents   */
  248.       rc = UpdateVar( &RexxVar );      /* Update variable            */
  249.    }
  250.    FreeVar( &RexxVar );                      /* Free variable        */
  251.    return( rc );
  252. }
  253.  
  254. /*********************************************************************/
  255. /* Function:  rc = NewStemValue( stem, numb, type, &value )          */
  256. /*                                                                   */
  257. /* Purpose:   Constructs the variable name from the stem and number  */
  258. /*            specified then invokes NewRexxVar to create the value  */
  259. /*            of the variable in the variable pool.                  */
  260. /*                                                                   */
  261. /* Notes:     Currently only handles normal (256byte) names & values.*/
  262. /*********************************************************************/
  263. APIRET NewStemValue(
  264.           PSZ       VarStem,
  265.           USHORT    VarNumb,
  266.           USHORT    VarType,
  267.           PVOID     VarValue )
  268. {
  269.    CHAR     VarName[RXAUTOBUFLEN];     /* Buffer to hold stem name   */
  270.    VarName[0] = EOSTR_CH;
  271.    if ( VarStem[strlen(VarStem)-1] == '.' ) {
  272.       sprintf( VarName,  "%s%d", VarStem, VarNumb );
  273.    } else {
  274.       sprintf( VarName, "%s.%d", VarStem, VarNumb );
  275.    }
  276.    return( NewRexxValue( VarName, VarType, VarValue ) );
  277. }
  278.  
  279. /*********************************************************************/
  280. /* Function:  rc = GetStemValue( stem, numb, type, &value )          */
  281. /*                                                                   */
  282. /* Purpose:   Constructs the variable name from the stem and number  */
  283. /*            specified then invokes GetRexxVar to retrieve the      */
  284. /*            value of the variable from the variable pool.          */
  285. /*                                                                   */
  286. /* Notes:     Currently only handles normal (256byte) names.         */
  287. /*********************************************************************/
  288. APIRET GetStemValue(
  289.           PSZ       VarStem,
  290.           USHORT    VarNumb,
  291.           USHORT    VarType,
  292.           PVOID     VarValue )
  293. {
  294.    CHAR     VarName[RXAUTOBUFLEN];     /* Buffer to hold stem name   */
  295.    VarName[0] = EOSTR_CH;
  296.    if ( VarStem[strlen(VarStem)-1] == '.' ) {
  297.       sprintf( VarName,  "%s%d", VarStem, VarNumb );
  298.    } else {
  299.       sprintf( VarName, "%s.%d", VarStem, VarNumb );
  300.    }
  301.    return( GetRexxValue( VarName, VarType, VarValue ) );
  302. }
  303.  
  304. /*********************************************************************/
  305. /* Function:  rc = SetStemValue( stem, numb, type, &value )          */
  306. /*                                                                   */
  307. /* Purpose:   Constructs the variable name from the stem and number  */
  308. /*            specified then invokes SetRexxVar to update the value  */
  309. /*            of the variable in the variable pool.                  */
  310. /*                                                                   */
  311. /* Notes:     Currently only handles normal (256byte) names.         */
  312. /*********************************************************************/
  313. APIRET SetStemValue(
  314.           PSZ       VarStem,
  315.           USHORT    VarNumb,
  316.           USHORT    VarType,
  317.           PVOID     VarValue )
  318. {
  319.    CHAR     VarName[RXAUTOBUFLEN];     /* Buffer to hold stem name   */
  320.    VarName[0] = EOSTR_CH;
  321.    if ( VarStem[strlen(VarStem)-1] == '.' ) {
  322.       sprintf( VarName,  "%s%d", VarStem, VarNumb );
  323.    } else {
  324.       sprintf( VarName, "%s.%d", VarStem, VarNumb );
  325.    }
  326.    return( SetRexxValue( VarName, VarType, VarValue ) );
  327. }
  328.  
  329. /*********************************************************************/
  330. /* END MODULE                                                        */
  331. /*********************************************************************/
  332.