1/* BEGIN LICENSE BLOCK
2 * Version: CMPL 1.1
3 *
4 * The contents of this file are subject to the Cisco-style Mozilla Public
5 * License Version 1.1 (the "License"); you may not use this file except
6 * in compliance with the License.  You may obtain a copy of the License
7 * at www.eclipse-clp.org/license.
8 *
9 * Software distributed under the License is distributed on an "AS IS"
10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11 * the License for the specific language governing rights and limitations
12 * under the License.
13 *
14 * The Original Code is  The ECLiPSe Constraint Logic Programming System.
15 * The Initial Developer of the Original Code is  Cisco Systems, Inc.
16 * Portions created by the Initial Developer are
17 * Copyright (C) 1997-2006 Cisco Systems, Inc.  All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * END LICENSE BLOCK */
22
23/*
24 * Examples for ECLiPSe C++ externals, from the User Manual
25 *
26 * $Id: eg_cc_external.cc,v 1.1 2008/06/30 17:43:54 jschimpf Exp $
27 *
28 */
29
30#include "eclipseclass.h"
31
32extern "C" int
33p_isvar()
34{
35    return EC_arg(1).is_var();
36}
37
38extern "C" int
39p_comp()
40{
41    return unify(EC_arg(1),
42	/*
43    	compare(EC_arg(2),EC_arg(3)) < 0 ? EC_atom("<") :
44    	compare(EC_arg(2),EC_arg(3)) > 0 ? EC_atom(">") :
45	EC_atom("="));
46	*/
47    	EC_arg(2) == EC_arg(3) ? EC_atom("==") :
48	EC_atom("\\="));
49}
50
51extern "C" int
52p_eq()
53{
54    return EC_arg(1) == EC_arg(2) ? EC_succeed : EC_fail;
55}
56
57
58extern "C" int
59p_string_to_list()
60{
61    EC_word  the_string(EC_arg(1));
62    EC_word  the_list(nil());
63    char *s;
64    long len;
65    int res;
66
67    res = the_string.is_string( &s );
68    if (res != EC_succeed) return res;
69    len = strlen(s);
70
71    /* the list is built backwards */
72    while (len--)
73    {
74	the_list = list(EC_word(s[len]), the_list);
75    }
76    return unify(EC_word(EC_arg(2)), the_list);
77}
78
79
80extern "C" int
81p_sumlist()
82{
83    int res;
84    long x, sum = 0;
85    EC_word list(EC_arg(1));
86    EC_word car,cdr;
87
88    for ( ; list.is_list(car,cdr) == EC_succeed; list = cdr)
89    {
90	res = car.is_long( &x);
91	if (res != EC_succeed) return res;
92	sum += x;
93    }
94    res = list.is_nil();
95    if (res != EC_succeed) return res;
96    return unify(EC_arg(2), EC_word(sum));
97}
98
99