1/* ----------------------------------------------------------------------------- 2 * See the LICENSE file for information on copyright, usage and redistribution 3 * of SWIG, and the README file for authors - http://www.swig.org/release.html. 4 * 5 * parms.c 6 * 7 * Parameter list class. 8 * ----------------------------------------------------------------------------- */ 9 10char cvsroot_parms_c[] = "$Id: parms.c 11080 2009-01-24 13:15:51Z bhy $"; 11 12#include "swig.h" 13 14/* ------------------------------------------------------------------------ 15 * NewParm() 16 * 17 * Create a new parameter from datatype 'type' and name 'name'. 18 * ------------------------------------------------------------------------ */ 19 20Parm *NewParm(SwigType *type, const_String_or_char_ptr name) { 21 Parm *p = NewHash(); 22 set_nodeType(p, "parm"); 23 if (type) { 24 SwigType *ntype = Copy(type); 25 Setattr(p, "type", ntype); 26 Delete(ntype); 27 } 28 Setattr(p, "name", name); 29 return p; 30} 31 32/* ------------------------------------------------------------------------ 33 * CopyParm() 34 * ------------------------------------------------------------------------ */ 35 36Parm *CopyParm(Parm *p) { 37 Parm *np = NewHash(); 38 Iterator ki; 39 for (ki = First(p); ki.key; ki = Next(ki)) { 40 if (DohIsString(ki.item)) { 41 DOH *c = Copy(ki.item); 42 Setattr(np,ki.key,c); 43 Delete(c); 44 } 45 } 46 Setfile(np, Getfile(p)); 47 Setline(np, Getline(p)); 48 return np; 49} 50 51/* ------------------------------------------------------------------ 52 * CopyParmListMax() 53 * CopyParmList() 54 * ------------------------------------------------------------------ */ 55 56ParmList *CopyParmListMax(ParmList *p, int count) { 57 Parm *np; 58 Parm *pp = 0; 59 Parm *fp = 0; 60 61 if (!p) 62 return 0; 63 64 while (p) { 65 if (count == 0) break; 66 np = CopyParm(p); 67 if (pp) { 68 set_nextSibling(pp, np); 69 Delete(np); 70 } else { 71 fp = np; 72 } 73 pp = np; 74 p = nextSibling(p); 75 count--; 76 } 77 return fp; 78} 79 80ParmList *CopyParmList(ParmList *p) { 81 return CopyParmListMax(p,-1); 82} 83 84/* ----------------------------------------------------------------------------- 85 * int ParmList_numrequired(). Return number of required arguments 86 * ----------------------------------------------------------------------------- */ 87 88int ParmList_numrequired(ParmList *p) { 89 int i = 0; 90 while (p) { 91 SwigType *t = Getattr(p, "type"); 92 String *value = Getattr(p, "value"); 93 if (value) 94 return i; 95 if (!(SwigType_type(t) == T_VOID)) 96 i++; 97 else 98 break; 99 p = nextSibling(p); 100 } 101 return i; 102} 103 104/* ----------------------------------------------------------------------------- 105 * int ParmList_len() 106 * ----------------------------------------------------------------------------- */ 107 108int ParmList_len(ParmList *p) { 109 int i = 0; 110 while (p) { 111 i++; 112 p = nextSibling(p); 113 } 114 return i; 115} 116 117/* --------------------------------------------------------------------- 118 * ParmList_str() 119 * 120 * Generates a string of parameters 121 * ---------------------------------------------------------------------- */ 122 123String *ParmList_str(ParmList *p) { 124 String *out = NewStringEmpty(); 125 while (p) { 126 String *pstr = SwigType_str(Getattr(p, "type"), Getattr(p, "name")); 127 Append(out, pstr); 128 p = nextSibling(p); 129 if (p) { 130 Append(out, ","); 131 } 132 Delete(pstr); 133 } 134 return out; 135} 136 137/* --------------------------------------------------------------------- 138 * ParmList_str_defaultargs() 139 * 140 * Generates a string of parameters including default arguments 141 * ---------------------------------------------------------------------- */ 142 143String *ParmList_str_defaultargs(ParmList *p) { 144 String *out = NewStringEmpty(); 145 while (p) { 146 String *value = Getattr(p, "value"); 147 String *pstr = SwigType_str(Getattr(p, "type"), Getattr(p, "name")); 148 Append(out, pstr); 149 if (value) { 150 Printf(out, "=%s", value); 151 } 152 p = nextSibling(p); 153 if (p) { 154 Append(out, ","); 155 } 156 Delete(pstr); 157 } 158 return out; 159} 160 161/* --------------------------------------------------------------------- 162 * ParmList_protostr() 163 * 164 * Generate a prototype string. 165 * ---------------------------------------------------------------------- */ 166 167String *ParmList_protostr(ParmList *p) { 168 String *out = NewStringEmpty(); 169 while (p) { 170 String *pstr = SwigType_str(Getattr(p, "type"), 0); 171 Append(out, pstr); 172 p = nextSibling(p); 173 if (p) { 174 Append(out, ","); 175 } 176 Delete(pstr); 177 } 178 return out; 179} 180 181/* --------------------------------------------------------------------- 182 * ParmList_has_defaultargs() 183 * 184 * Returns 1 if the parameter list passed in is has one or more default 185 * arguments. Otherwise returns 0. 186 * ---------------------------------------------------------------------- */ 187 188int ParmList_has_defaultargs(ParmList *p) { 189 while (p) { 190 if (Getattr(p, "value")) { 191 return 1; 192 } 193 p = nextSibling(p); 194 } 195 return 0; 196} 197