MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: MathLink Data Types

  • To: mathgroup at smc.vnet.net
  • Subject: [mg21004] Re: MathLink Data Types
  • From: "P.J. Hinton" <paulh at wolfram.com>
  • Date: Thu, 2 Dec 1999 21:41:17 -0500 (EST)
  • Organization: "Wolfram Research, Inc."
  • References: <822gpm$7kf@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 1 Dec 1999, Dave Richardson wrote:

> I am working with MathLink, and I have a question.
> 
> Is String a valid type?
> 
> Here is a basic program I am trying to write to read a string (from
> Mathematica) and make a decision based upon it.  Then I want to return a
> double based upon this...
> 
> Below is: string.cpp ********************************************
> 
> #include "mathlink.h"
> #include <string.h>
> 
> int main(int argc, char *argv[]){
>  return MLMain(argc,argv);
> }
> 
> double whichx(char* sRefrigerant){
>  double rx;
>  if (strcoll(sRefrigerant, "R134a" ) == 0) rx = 12.;
>  else rx = 9;
>  return rx;
> }
> 
> end string.cpp **************************************************
> 
> Here is the Template File string.tm *******************************
> 
> :Evaluate: BeginPackage[ "StringTest`"]
> :Evaluate: WrapProps::usage = "whx("R134a"), returns 12 if the entered
> string is R-134a, 9 if not."
> 
> 
> :Evaluate: whx::usage = "a number"
> :Begin:
> :Function: whichx
> :Pattern: whx[sRefrigerant]
> :Arguments: {sRefrigerant}
> :ArgumentTypes: {String}
> :ReturnType: Real
> :End:
> 
> 
> :Evaluate: EndPackage[]
> 
> ::This is a comment line.
> 
> end string.cpp **************************************************
> 
> 
> According to the Mathematica book, String is a valid argument type in
> Mathematica (Thus the template file), and its equivalent in c (c++) is
> the char*.
> 
> For some reason, I am not getting a good link, and thus a crash and
> burn...
> 
> If I change the type to double (In the c++ code and .tm template), I get
> a compile/link/run just fine.
> 
> Can anyone point to the flaw in my logic here, and direct me around the
> problem?

There are a couple of things that I can think of here.

1) Add "const" to the declaration of sRefrigerant.

2) Change the pattern for whx[] in the template so that the argument is
real pattern.

:Pattern: whx[sRefrigerant] -> :Pattern: whx[sRefrigerant_String]

3) Just for good measure, throw in a prototype for whichx().

4) Escape out the literal quotes in the usage message in the template.

--
P.J. Hinton
Mathematica Programming Group           paulh at wolfram.com
Wolfram Research, Inc.

[begin string.cpp]
#include "mathlink.h"
#include <string.h>

double whichx(const char* sRefrigerant);

int main(int argc, char *argv[]){
 return MLMain(argc,argv);
}

double whichx(const char* sRefrigerant){
 double rx;
 if (strcoll(sRefrigerant, "R134a" ) == 0) rx = 12.;
 else rx = 9;
 return rx;
}
[end string.cpp]

[begin string.tm]
:Evaluate: BeginPackage[ "StringTest`"]
:Evaluate: WrapProps::usage = "whx(\"R134a\"), returns 12 if the entered string is R-134a, 9 if not."
:Evaluate: whx::usage = "a number"
:Begin:
:Function: whichx
:Pattern: whx[sRefrigerant_String]
:Arguments: {sRefrigerant}
:ArgumentTypes: {String}
:ReturnType: Real
:End:
[end string.tm]

This source code compiled and ran successfully with egcs-2.91.66 under
Linux.

In[1]:= Install["string"]

Out[1]= LinkObject['./string', 1, 1]

In[2]:= LinkPatterns[%]

Out[2]= {whx[sRefrigerant_String]}

In[3]:= whx["R134a"]

Out[3]= 12.

--
P.J. Hinton
Mathematica Programming Group           paulh at wolfram.com
Wolfram Research, Inc.



  • Prev by Date: RE: Packed Array Angst
  • Next by Date: Re: calculus with Grassmann variables
  • Previous by thread: MathLink Data Types
  • Next by thread: how to suppress the usage message?