MathGroup Archive 1999

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

Search the Archive

Re: MathLink & strings

  • To: mathgroup at smc.vnet.net
  • Subject: [mg21182] Re: MathLink & strings
  • From: Dave Richardson <dhr at glue.umd.edu>
  • Date: Fri, 17 Dec 1999 01:23:32 -0500 (EST)
  • Organization: University of Maryland
  • References: <831vhq$g7c@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Roger,

What follows is the response I got from P.J. Hinton at Wolfram, when I had a
similar problem.

What turned out to be the problem for me is this:

Let me know if it answers your question.

If not, I can send you some bits of my actual code...

Good luck,
Dave!

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.



Roger Mason wrote:

> Hi,
>
> I am trying to send strings from Mathematica to C via MathLink. I have
> been trying now for two days to accomplish this seemingly simple task.
> Much of the problem stems from the absolutely hopeless documentation for
> MathLink functions. If Mathematica were free, one might sigh and accept
> the kindness of others in making code available. However, Mathematica is
> (very decidedly) not free. Is WRI going to turn into another monster
> like Micro$oft?
>
> So much for venting spleen.
>
> Does anyone have any examples of C code that implements string transfer
> between Mathematica and C, in both directions? If you do, please make
> clear what the types of the variables must be (this is an area where I
> came to grief by assuming that the documentation is accurate!).
>
> Many thanks in advance.
>
> Roger Mason

--
Dave Richardson
University of Maryland
Department of Mechanical Engineering
Center for Environmental Energy Engineering
(301) 405-8726
dhr at glue.umd.edu




  • Prev by Date: Re: Mathematica and Topology.
  • Next by Date: Re: MathLink & strings
  • Previous by thread: MathLink & strings
  • Next by thread: Re: MathLink & strings