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) 1989-2006 Cisco Systems, Inc.  All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * END LICENSE BLOCK */
22
23/*
24 * VERSION	$Id: example.c,v 1.1 2008/06/30 17:43:55 jschimpf Exp $
25 *
26 * Examples for ECLiPSe C externals, from the User Manual
27 *
28 */
29
30#include "external.h"
31
32int
33p_string_to_list(vs, ts, vl, tl)
34value		vs, vl;
35type		ts, tl;
36{
37    pword	list;
38    pword	*car;
39    pword	*cdr;
40    int		len;
41    char	*s;
42
43    Check_String(ts);
44    Check_Output_List(tl);
45    len = StringLength(vs);
46    s = StringStart(vs);
47
48    cdr = &list;
49    while (len--)
50    {
51	car = TG;
52	Push_List_Frame();
53	Make_List(cdr, car);
54	Make_Integer(car, *s++);
55	cdr = car + 1;
56    }
57    Make_Nil(cdr);
58
59    Return_Unify_Pw(vl, tl, list.val, list.tag);
60}
61
62
63int
64p_sumlist(vl, tl, v, t)
65value	vl, v;
66type	tl, t;
67{
68    long sum = 0;
69    while (IsList(tl))
70    {
71	pword *car = vl.ptr;
72	pword *cdr = car + 1;
73
74	Dereference_(car);
75	Check_Integer(car->tag);
76	sum += car->val.nint;
77
78	Dereference_(cdr);
79	tl = cdr->tag;
80	vl = cdr->val;
81    }
82    Check_List(tl);
83    Return_Unify_Integer(v, t, sum);
84}
85