PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/tcl/tclprimtypes.swg

#
Unknown | 239 lines | 219 code | 20 blank | 0 comment | 0 complexity | d47de7ab6c8f06f3645312f69aeba330 MD5 | raw file
  1/* ------------------------------------------------------------
  2 * Primitive Types
  3 * ------------------------------------------------------------ */
  4
  5/* boolean */
  6
  7%fragment(SWIG_From_frag(bool),"header") {
  8  %define_as(SWIG_From_dec(bool),           Tcl_NewBooleanObj)
  9}
 10
 11%fragment(SWIG_AsVal_frag(bool),"header") {
 12SWIGINTERN int
 13SWIG_AsVal_dec(bool)(Tcl_Obj *obj, bool *val)
 14{
 15  int v;
 16  if (Tcl_GetBooleanFromObj(0, obj, &v) == TCL_OK) {
 17    if (val) *val = v ? true : false;
 18    return SWIG_OK;
 19  }
 20  return SWIG_TypeError;
 21}
 22}
 23
 24/* long */
 25
 26%fragment(SWIG_From_frag(long),"header",
 27	  fragment="<limits.h>") {
 28SWIGINTERNINLINE Tcl_Obj* 
 29SWIG_From_dec(long)(long value)
 30{
 31  if (((long) INT_MIN <= value) && (value <= (long) INT_MAX)) {
 32    return Tcl_NewIntObj(%numeric_cast(value,int));
 33  } else {
 34    return Tcl_NewLongObj(value);
 35  }
 36}
 37}
 38
 39%fragment(SWIG_AsVal_frag(long),"header") {
 40SWIGINTERN int
 41SWIG_AsVal_dec(long)(Tcl_Obj *obj, long* val)
 42{
 43  long v;
 44  if (Tcl_GetLongFromObj(0,obj, &v) == TCL_OK) {
 45    if (val) *val = (long) v;
 46    return SWIG_OK;
 47  }
 48  return SWIG_TypeError;
 49}
 50}
 51
 52/* unsigned long */
 53
 54%fragment(SWIG_From_frag(unsigned long),"header",
 55	  fragment=SWIG_From_frag(long),
 56	  fragment="<stdio.h>") {
 57SWIGINTERNINLINE Tcl_Obj* 
 58SWIG_From_dec(unsigned long)(unsigned long value)
 59{
 60  if (value < (unsigned long) LONG_MAX) {
 61    return SWIG_From(long)(%numeric_cast(value, long));
 62  } else {
 63    char temp[256]; 
 64    sprintf(temp, "%lu", value);
 65    return Tcl_NewStringObj(temp,-1);
 66  }
 67}
 68}
 69
 70%fragment(SWIG_AsVal_frag(unsigned long),"header",
 71	  fragment="<limits.h>") {
 72SWIGINTERN int
 73SWIG_AsVal_dec(unsigned long)(Tcl_Obj *obj, unsigned long *val) {
 74  long v;
 75  if (Tcl_GetLongFromObj(0,obj, &v) == TCL_OK) {
 76    if (v >= 0) {
 77      if (val) *val = (unsigned long) v;
 78      return SWIG_OK;
 79    }
 80    /* If v is negative, then this could be a negative number, or an
 81       unsigned value which doesn't fit in a signed long, so try to
 82       get it as a string so we can distinguish these cases. */
 83  }
 84  {
 85    int len = 0;
 86    const char *nptr = Tcl_GetStringFromObj(obj, &len);
 87    if (nptr && len > 0) {
 88      char *endptr;
 89      unsigned long v;
 90      if (*nptr == '-') return SWIG_OverflowError;
 91      errno = 0;
 92      v = strtoul(nptr, &endptr,0);
 93      if (nptr[0] == '\0' || *endptr != '\0')
 94	return SWIG_TypeError;
 95      if (v == ULONG_MAX && errno == ERANGE) {
 96	errno = 0;
 97	return SWIG_OverflowError;
 98      } else {
 99	if (*endptr == '\0') {
100	  if (val) *val = v;
101	  return SWIG_OK;
102	}
103      }
104    }
105  }
106  
107  return SWIG_TypeError;
108}
109}
110
111/* long long */
112
113%fragment(SWIG_From_frag(long long),"header",
114	  fragment=SWIG_From_frag(long),
115	  fragment="<limits.h>",
116	  fragment="<stdio.h>") {
117SWIGINTERNINLINE Tcl_Obj* 
118SWIG_From_dec(long long)(long long value)
119{
120  if (((long long) LONG_MIN <= value) && (value <= (long long) LONG_MAX)) {
121    return SWIG_From(long)(%numeric_cast(value,long));
122  } else {    
123    char temp[256]; 
124    sprintf(temp, "%lld", value);
125    return Tcl_NewStringObj(temp,-1);
126  }
127}
128}
129
130%fragment(SWIG_AsVal_frag(long long),"header",
131	  fragment="<limits.h>",
132	  fragment="<stdlib.h>") {
133SWIGINTERN int
134SWIG_AsVal_dec(long long)(Tcl_Obj *obj, long long *val)
135{
136  long v;
137  if (Tcl_GetLongFromObj(0,obj, &v) == TCL_OK) {
138    if (val) *val = v;
139    return SWIG_OK;
140  } else {
141    int len = 0;
142    const char *nptr = Tcl_GetStringFromObj(obj, &len);
143    if (nptr && len > 0) {
144      char *endptr;
145      long long v;
146      errno = 0;
147      v = strtoll(nptr, &endptr,0);
148      if (nptr[0] == '\0' || *endptr != '\0')
149	return SWIG_TypeError;
150      if ((v == LLONG_MAX || v == LLONG_MIN) && errno == ERANGE) {
151	errno = 0;
152	return SWIG_OverflowError;
153      } else {
154	if (*endptr == '\0') {
155	  if (val) *val = v;
156	  return SWIG_OK;
157	}
158      }
159    }
160  }
161  return SWIG_TypeError;
162}
163}
164
165/* unsigned long long */
166
167%fragment(SWIG_From_frag(unsigned long long),"header",
168	  fragment=SWIG_From_frag(long long),
169	  fragment="<limits.h>",
170	  fragment="<stdio.h>") {
171SWIGINTERNINLINE Tcl_Obj* 
172SWIG_From_dec(unsigned long long)(unsigned long long value)
173{
174  if (value < (unsigned long long) LONG_MAX) {
175    return SWIG_From(long long)(%numeric_cast(value, long long));
176  } else {
177    char temp[256]; 
178    sprintf(temp, "%llu", value);
179    return Tcl_NewStringObj(temp,-1);
180  }
181}
182}
183
184%fragment(SWIG_AsVal_frag(unsigned long long),"header",
185	  fragment=SWIG_AsVal_frag(unsigned long),
186	  fragment="<limits.h>",
187	  fragment="<stdlib.h>") {
188SWIGINTERN int
189SWIG_AsVal_dec(unsigned long long)(Tcl_Obj *obj, unsigned long long *val)
190{
191  long v;
192  if (Tcl_GetLongFromObj(0,obj, &v) == TCL_OK) {
193    if (val) *val = (unsigned long) v;
194    return SWIG_OK;
195  } else {
196    int len = 0;
197    const char *nptr = Tcl_GetStringFromObj(obj, &len);
198    if (nptr && len > 0) {
199      char *endptr;
200      unsigned long long v;
201      if (*nptr == '-') return SWIG_OverflowError;
202      errno = 0;
203      v = strtoull(nptr, &endptr,0);
204      if (nptr[0] == '\0' || *endptr != '\0')
205	return SWIG_TypeError;
206      if (v == ULLONG_MAX && errno == ERANGE) {
207	errno = 0;
208	return SWIG_OverflowError;
209      } else {
210	if (*endptr == '\0') {
211	  if (val) *val = v;
212	  return SWIG_OK;
213	}
214      }
215    }
216  }
217  return SWIG_TypeError;
218}
219}
220
221/* double */
222
223%fragment(SWIG_From_frag(double),"header") {
224  %define_as(SWIG_From(double),         Tcl_NewDoubleObj)
225}
226
227%fragment(SWIG_AsVal_frag(double),"header") {
228SWIGINTERN int
229SWIG_AsVal_dec(double)(Tcl_Obj *obj, double *val)
230{
231  double v;
232  if (Tcl_GetDoubleFromObj(0, obj, &v) == TCL_OK) {
233    if (val) *val = v;
234    return SWIG_OK;
235  }
236  return SWIG_TypeError;
237}
238}
239