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_c_external.c,v 1.1 2008/06/30 17:43:53 jschimpf Exp $ 27 * 28 */ 29 30#include "eclipse.h" 31 32int 33p_isvar() 34{ 35 return ec_is_var(ec_arg(1)); 36} 37 38int 39p_comp() 40{ 41 return ec_unify_arg(1, 42 ec_compare(ec_arg(2),ec_arg(3)) < 0 ? ec_atom(ec_did("<",0)) : 43 ec_compare(ec_arg(2),ec_arg(3)) > 0 ? ec_atom(ec_did(">",0)) : 44 ec_atom(ec_did("=",0))); 45} 46 47int 48p_string_to_list() 49{ 50 pword list; 51 char *s; 52 long len; 53 int res; 54 55 res = ec_get_string_length(ec_arg(1), &s, &len); 56 if (res != PSUCCEED) return res; 57 58 list = ec_nil(); /* the list is built backwards */ 59 while (len--) 60 { 61 list = ec_list(ec_long(s[len]), list); 62 } 63 return ec_unify_arg(2, list); 64} 65 66 67int 68p_sumlist() 69{ 70 int res; 71 long x, sum = 0; 72 pword list, car, cdr; 73 74 for (list = ec_arg(1); ec_get_list(list,&car,&cdr) == PSUCCEED; list = cdr) 75 { 76 res = ec_get_long(car, &x); 77 if (res != PSUCCEED) return res; 78 sum += x; 79 } 80 res = ec_get_nil(list); 81 if (res != PSUCCEED) return res; 82 return ec_unify_arg(2, ec_long(sum)); 83} 84 85