home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / external / sharelib / c_only.pro next >
Encoding:
Text File  |  1997-07-08  |  6.0 KB  |  234 lines

  1. ;;    $Id: c_only.pro,v 1.2 1997/01/28 22:40:21 kirk Exp $
  2. ;;
  3. ;; NAME:
  4. ;;    c_only.pro
  5. ;;
  6. ;; PURPOSE:
  7. ;;    This C function is used to demonstrate how to pass IDL variables
  8. ;;    to a C function via the CALL_EXTERNAL function. 
  9. ;;
  10. ;; CATEGORY:
  11. ;;    Dynamic Link
  12. ;;
  13. ;; CALLING SEQUENCE:
  14. ;;      This function is called in IDL by using the following command
  15. ;;
  16. ;;    IDL> c_only
  17. ;;
  18. ;; INPUTS:
  19. ;;    None.
  20. ;;
  21. ;; OUTPUTS:
  22. ;;     None.
  23. ;;
  24. ;; SIDE EFFECTS:
  25. ;;    The value of IDL variables are written to standard output 
  26. ;;    before and after calls using CALL_EXTERNAL.
  27. ;;
  28. ;; RESTRICTIONS:
  29. ;;    This procedure assumes that all of the dynamic libraries are 
  30. ;;    present. If they are not they can be built using the provided
  31. ;;    Makefile (run make_callext_demo).
  32. ;;
  33. ;;
  34. ;; MODIFICATION HISTORY:
  35. ;;    Written October, 1993        KDB
  36. ;;
  37. ;;===========================================================================
  38.  
  39.     PRO C_ONLY
  40.  
  41. ;;     Different platforms have different filename extensions and entry
  42. ;;    point formats. Determine what type of machine we are on and set the 
  43. ;;    correct names.
  44.  
  45.     CASE !VERSION.ARCH OF 
  46.  
  47.        'sparc'    :BEGIN        ;Sun or Solaris
  48.  
  49.         LIB_EXT        = 'so'
  50.         ENTRY_PREFIX    = ''
  51.  
  52.         END
  53.  
  54.        'hp_pa'    :BEGIN        ;HP 
  55.  
  56.         LIB_EXT        = 'sl'
  57.         ENTRY_PREFIX    = ''
  58.  
  59.         END
  60.  
  61.        'hp9000s300'    :BEGIN        ;HP 9000s300 series
  62.  
  63.         LIB_EXT        = 'sl'
  64.         ENTRY_PREFIX    = '_'
  65.  
  66.         END
  67.  
  68.        'ibmr2'    :BEGIN        ;IBM RS6000
  69.  
  70.         LIB_EXT        = 'a'
  71.         ENTRY_PREFIX    = ''
  72.  
  73.         END
  74.          
  75.             'mipseb'    :BEGIN        ;SGI IRIX, or MIPS, or Ultix
  76.  
  77. ;;            Call_External is only supported on IRIX version 5.1.
  78. ;;        Check that this is the operating system in use.
  79.  
  80.             tmp = findfile('/usr/lib/libC.so', COUNT=CNT )
  81.  
  82.             IF((CNT eq 1)and(!VERSION.OS eq "IRIX"))THEN BEGIN
  83.           LIB_EXT = 'so'
  84.                   ENTRY_PREFIX    = ''
  85.             ENDIF ELSE BEGIN
  86.  
  87.                   MESSAGE, /CONTINUE,     $
  88.            "Operating System version does not support CALL_EXTERNAL"
  89.           RETURN
  90.  
  91.         ENDELSE 
  92.  
  93.         END
  94.  
  95.             'alpha'     :BEGIN        ;Dec OSF1
  96.  
  97.                  LIB_EXT = 'so'
  98.                  ENTRY_PREFIX    = ''
  99.             END
  100.  
  101.         'x86'    :BEGIN        ;Data General Aviion
  102.  
  103.         LIB_EXT = 'so'
  104.         ENTRY_PREFIX    = ''
  105.             END
  106.  
  107.         ELSE    :BEGIN
  108.  
  109.         MESSAGE,"CASE ERROR: User must add correct entry name", $
  110.             /CONTINUE
  111.  
  112.         RETURN
  113.  
  114.         END
  115.     ENDCASE
  116.  
  117. ;;    This procedure assumes that we are in the directory that 
  118. ;;     contains the dynamic libraries
  119.  
  120.     CD, '.', CURRENT = PWD
  121.  
  122. ;;    Declare the variables that are passed into the C function 
  123. ;;    simple
  124.  
  125.         BYTE_VAR        = 1B
  126.         SHORT_VAR       = 2
  127.         LONG_VAR        = 3L
  128.         FLOAT_VAR       = 4.0
  129.         DOUBLE_VAR      = 5D0
  130.         STRING_VAR      = "SiX"
  131.  
  132. ;;    Lets print the parameters that we are going to pass into the 
  133. ;;    C function
  134.  
  135.      PRINT,""
  136.     PRINT,"====================================================="
  137.     PRINT,"Inside IDL: Before the call to the C function simple"
  138.     PRINT,""
  139.     PRINT,"Values of variables that will be passed in:"
  140.     PRINT,""
  141.     PRINT,"         IDL BYTE Variable:          ", BYTE_VAR,     $
  142.     FORMAT="(A,I4)"
  143.     PRINT,"         IDL INT Variable:           ", SHORT_VAR,    $
  144.     FORMAT="(A,I4)"
  145.     PRINT,"         IDL LONG Variable:          ", LONG_VAR,    $
  146.     FORMAT="(A,I4)"
  147.         PRINT,"         IDL FLOAT Variable:         ", FLOAT_VAR,    $
  148.     FORMAT="(A,F4.1)"
  149.     PRINT,"         IDL DOUBLE Variable:        ", DOUBLE_VAR,    $
  150.     FORMAT="(A,F4.1)"
  151.         PRINT,"         IDL STRING Variable:        ", STRING_VAR,    $
  152.     FORMAT="(A,A4)"
  153.     PRINT,""
  154.     PRINT,"Calling the C function simple via CALL_EXTERNAL
  155.     PRINT,"====================================================="
  156.  
  157.     PRINT," "
  158.  
  159. ;;    Lets square these values. The string is RETURNED by the function.
  160.  
  161.     STRING_VAR2 = CALL_EXTERNAL(PWD+'/simple.'+LIB_EXT,     $
  162.                 ENTRY_PREFIX+'simple',             $
  163.                         BYTE_VAR, SHORT_VAR, LONG_VAR, FLOAT_VAR,       $
  164.                         DOUBLE_VAR, STRING_VAR, /S_VALUE )
  165.  
  166. ;;    Now print out the results
  167.  
  168.         PRINT,""
  169.         PRINT,"====================================================="
  170.     PRINT,"Inside IDL: Results of C function simple. "
  171.     PRINT,"            Simple variables are squared in the C function"
  172.     PRINT,""
  173.     PRINT,"Results:"
  174.         PRINT,"         Squared BYTE Variable:          ", BYTE_VAR,    $
  175.     FORMAT="(/A,I6)"
  176.         PRINT,"         Squared INT Variable:           ", SHORT_VAR,    $
  177.     FORMAT="(A,I6)"
  178.         PRINT,"         Squared LONG Variable:          ", LONG_VAR,    $
  179.     FORMAT="(A,I6)"
  180.         PRINT,"         Squared FLOAT Variable:         ", FLOAT_VAR,    $
  181.     FORMAT="(A,F6.1)"
  182.         PRINT,"         Squared DOUBLE Variable:        ", DOUBLE_VAR,    $
  183.     FORMAT="(A,F6.1)"
  184.         PRINT,"         Squared STRING Variable:        ", STRING_VAR2,    $
  185.     FORMAT="(A,A6)"
  186.         PRINT,""
  187.         PRINT,"====================================================="
  188.  
  189.     PRINT," "
  190.  
  191. ;;    Now set up the values that will be passed into the C function
  192. ;;    double_array. This function returns the maximum value in the 
  193. ;;    array. Create some data
  194.  
  195.     ARRAY_VAR = abs(randomn(seed,12))*!DPI*10d0     ;create a double array
  196.         ARRAY_SIZE = n_elements(ARRAY_VAR)
  197.  
  198.     MAX = CALL_EXTERNAL(PWD+'/double_array.'+LIB_EXT,        $
  199.               ENTRY_PREFIX+'double_array',                 $
  200.                          ARRAY_VAR, ARRAY_SIZE, /D_VALUE)
  201.  
  202.         PRINT," "
  203.         PRINT,"The max value and type returned to IDL from double array:"
  204.     HELP, MAX
  205.  
  206.         PRINT," "
  207.     PRINT,"The max value of the array using the IDL MAX function: ", $
  208.         max(ARRAY_VAR)
  209.  
  210. ;;    Now set up the values that will be passed to the C function 
  211. ;;    string_array.
  212.  
  213.     ARRAY_VAR = ['january','february','march', 'april', 'may', 'june', $
  214.              'july', 'august', 'september','october', 'november',   $
  215.              'december' ]
  216.         ARRAY_SIZE = long(n_elements(ARRAY_VAR))
  217.  
  218.       MAX = CALL_EXTERNAL(PWD+'/string_array.'+LIB_EXT,        $
  219.              ENTRY_PREFIX+'string_array',                 $
  220.                          ARRAY_VAR, ARRAY_SIZE, /S_VALUE)
  221.  
  222.         PRINT," "
  223.         PRINT,"The max value and type returned to IDL from string array:"
  224.         HELP, MAX
  225.  
  226.         PRINT," "
  227.         PRINT,"The max value of the array using the IDL MAX function: ", $
  228.                 max(ARRAY_VAR)
  229.  
  230.     RETURN
  231.  
  232.     END
  233.  
  234.