File: test_wstp.cc

package info (click to toggle)
cadabra2 2.4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 78,732 kB
  • sloc: ansic: 133,450; cpp: 92,064; python: 1,530; javascript: 203; sh: 184; xml: 182; objc: 53; makefile: 51
file content (74 lines) | stat: -rw-r--r-- 1,859 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "wstp.h"
#include <iostream>

// https://reference.wolfram.com/legacy/language/v10.2/ref/c/WSLogStreamToFile.html
// http://edenwaith.com/development/tutorials/mathlink/ML_Tut.pdf
//
// Run as
//
//    core/test_wstp -linkname 'math -mathlink' -linkmode launch


int main(int argc, char **argv)
	{
	int errno;
	WSEnvironment stdenv;
	stdenv = WSInitialize((WSEnvironmentParameter)0);
	if(stdenv==0) {
		std::cerr << "Failed to initialise WSTP" << std::endl;
		return -1;
		}
	//	auto lp = WSLoopbackOpen(stdenv, &errno);
	auto lp = WSOpenArgcArgv(stdenv, argc, argv, &errno);
	if(lp==0 || errno!=WSEOK) {
		std::cerr << "Failed to open loopback link " << WSErrorMessage(lp) << std::endl;
		return -1;
		}

	WSActivate(lp);

	const char *fileName;
	auto res = WSLogFileNameForLink(lp, &fileName);
	if(! res) {
		std::cerr << "Failed to generate log file name" << std::endl;
		return -1;
		}
	res = WSLogStreamToFile(lp, fileName);
	std::cerr << "Logging to " << fileName << std::endl;

	WSPutFunction(lp, "EvaluatePacket", 1L);
	WSPutFunction(lp, "ToString", 1L);
	WSPutFunction(lp, "FullSimplify", 1L);
	WSPutFunction(lp, "ToExpression", 1L);
	WSPutString(lp, "Sin[x]^2 + Cos[x]^2 + Cos[x] Sin[x]");
	//	WSPutFunction(lp, "Plus", 2);
	//	WSPutInteger(lp, 3);
	//	WSPutInteger(lp, 8);
	WSEndPacket(lp);
	WSFlush(lp);

	int pkt=0;
	while( (pkt = WSNextPacket(lp), pkt) && pkt != RETURNPKT ) {
		std::cerr << "received packet " << pkt << std::endl;
		WSNewPacket(lp);
		if (WSError(lp)) {
			std::cerr << "error" << std::endl;
			}
		}
	std::cerr << "packet now " << pkt << std::endl;

	const char *out;
	if(! WSGetString(lp, &out)) {
		printf("Unable to read from link\n");
		return -1;
		}
	else {
		std::cerr << out << std::endl;
		WSReleaseString(lp, out);
		}

	WSReleaseLogFileNameForLink(lp, fileName);

	WSClose(lp);
	WSDeinitialize(stdenv);
	}