tp_obj tp_str_index(TP) { tp_obj s = TP_OBJ(); tp_obj v = TP_OBJ(); int n = _tp_str_index(s,v); if (n >= 0) { return tp_number(n); } tp_raise(tp_None,tp_string("(tp_str_index) ValueError: substring not found")); }
tp_obj tp_replace(TP) { tp_obj s = TP_OBJ(); tp_obj k = TP_OBJ(); tp_obj v = TP_OBJ(); tp_obj p = s; int i,n = 0; int c; int l; tp_obj rr; char *r; char *d; tp_obj z; while ((i = _tp_str_index(p,k)) != -1) { n += 1; p.string.val += i + k.string.len; p.string.len -= i + k.string.len; } /* fprintf(stderr,"ns: %d\n",n); */ l = s.string.len + n * (v.string.len-k.string.len); rr = tp_string_t(tp,l); r = rr.string.info->s; d = r; z = p = s; while ((i = _tp_str_index(p,k)) != -1) { p.string.val += i; p.string.len -= i; memcpy(d,z.string.val,c=(p.string.val-z.string.val)); d += c; p.string.val += k.string.len; p.string.len -= k.string.len; memcpy(d,v.string.val,v.string.len); d += v.string.len; z = p; } memcpy(d,z.string.val,(s.string.val + s.string.len) - z.string.val); return tp_track(tp,rr); }
tp_obj tp_split(TP) { tp_obj v = TP_OBJ(); tp_obj d = TP_OBJ(); tp_obj r = tp_list(tp); int i; while ((i=_tp_str_index(v,d))!=-1) { _tp_list_append(tp,r.list.val,tp_string_slice(tp,v,0,i)); v.string.val += i + d.string.len; v.string.len -= i + d.string.len; /* tp_grey(tp,r); // should stop gc or something instead*/ } _tp_list_append(tp,r.list.val,tp_string_slice(tp,v,0,v.string.len)); /* tp_grey(tp,r); // should stop gc or something instead*/ return r; }
/* Socket connect method. * * Example: * s.connect('10.10.1.1', 7000) #Connects to 10.10.1.1:7000 */ tp_obj kolibri_connect(TP) { tp_obj self = TP_TYPE(TP_DICT); tp_obj remote_addr_obj = TP_OBJ(); __u32 remote_addr; __u32 remote_port = (__u32)TP_TYPE(TP_NUMBER).number.val; __u32 local_port = tp_get(tp, self, tp_string("local_port")).number.val; __u32 socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val; int s = -1; /* Socket descriptor */ if (remote_addr_obj.type == TP_NUMBER) remote_addr = remote_addr_obj.number.val; else if (remote_addr_obj.type == TP_STRING) inet_pton(tp, (const char *)remote_addr_obj.string.val, remote_addr_obj.string.len, &remote_addr); if (socktype == SOCK_STREAM) s = __menuet__open_TCP_socket(local_port, remote_port, remote_addr, 1); else if (socktype == SOCK_DGRAM) s = __menuet__open_UDP_socket(local_port, remote_port, remote_addr); if (s >= 0) { tp_set(tp, self, tp_string("socket"), tp_number(s)); return tp_True; } else return tp_False; }
tp_obj tp_bind(TP) { tp_obj r = TP_TYPE(TP_FNC); tp_obj self = TP_OBJ(); return tp_fnc_new(tp, r.fnc.ftype|2,r.fnc.cfnc,r.fnc.info->code, self,r.fnc.info->globals); }
/* Socket listen method. * * Example: * s.listen('10.10.1.1', 5000) */ tp_obj kolibri_listen(TP) { tp_obj self = TP_TYPE(TP_DICT); tp_obj remote_addr_obj = TP_OBJ(); __u32 remote_addr; __u32 remote_port = (__u32)TP_TYPE(TP_NUMBER).number.val; __u32 local_port = tp_get(tp, self, tp_string("local_port")).number.val; __u32 socktype = (__u32)tp_get(tp, self, tp_string("type")).number.val; int s = -1; /* Socket descriptor */ if (socktype != SOCK_STREAM) tp_raise(tp_None, "IOError: attempt to listen on non-TCP socket", tp_None); if (remote_addr_obj.type == TP_NUMBER) remote_addr = remote_addr_obj.number.val; else if (remote_addr_obj.type == TP_STRING) inet_pton(tp, (const char *)remote_addr_obj.string.val, remote_addr_obj.string.len, &remote_addr); if ((s = __menuet__open_TCP_socket(local_port, remote_port, remote_addr, 0)) >= 0) { tp_set(tp, self, tp_string("socket"), tp_number(s)); return tp_True; } else return tp_False; }
tp_obj tp_max(TP) { tp_obj r = TP_OBJ(); tp_obj e; TP_LOOP(e) if (tp_cmp(tp,r,e) < 0) { r = e; } TP_END; return r; }
tp_obj tp_istype(TP) { tp_obj v = TP_OBJ(); char *t = TP_STR(); if (strcmp("string",t) == 0) { return tp_number(v.type == TP_STRING); } if (strcmp("list",t) == 0) { return tp_number(v.type == TP_LIST); } if (strcmp("dict",t) == 0) { return tp_number(v.type == TP_DICT); } if (strcmp("number",t) == 0) { return tp_number(v.type == TP_NUMBER); } tp_raise(None,"is_type(%s,%s)",STR(v),t); }
tp_obj tp_save(TP) { char *fname = TP_STR(); tp_obj v = TP_OBJ(); FILE *f; f = fopen(fname,"wb"); if (!f) { tp_raise(None,"tp_save(%s,...)",fname); } fwrite(v.string.val,v.string.len,1,f); fclose(f); return None; }
tp_obj tp_save(TP) { char fname[256]; tp_cstr(tp,TP_STR(),fname,256); tp_obj v = TP_OBJ(); FILE *f; f = fopen(fname,"wb"); if (!f) { tp_raise(tp_None,tp_string("(tp_save) IOError: ?")); } fwrite(v.string.val,v.string.len,1,f); fclose(f); return tp_None; }
tp_obj tp_copy(TP) { tp_obj r = TP_OBJ(); int type = r.type; if (type == TP_LIST) { return _tp_list_copy(tp,r); } else if (type == TP_DICT) { return _tp_dict_copy(tp,r); } tp_raise(None,"tp_copy(%s)",STR(r)); }
tp_obj tp_copy(TP) { tp_obj r = TP_OBJ(); int type = r.type; if (type == TP_LIST) { return _tp_list_copy(tp,r); } else if (type == TP_DICT) { return _tp_dict_copy(tp,r); } tp_raise(tp_None,tp_string("(tp_copy) TypeError: ?")); }
tp_obj tp_istype(TP) { tp_obj v = TP_OBJ(); tp_obj t = TP_STR(); if (tp_cmp(tp,t,tp_string("string")) == 0) { return tp_number(v.type == TP_STRING); } if (tp_cmp(tp,t,tp_string("list")) == 0) { return tp_number(v.type == TP_LIST); } if (tp_cmp(tp,t,tp_string("dict")) == 0) { return tp_number(v.type == TP_DICT); } if (tp_cmp(tp,t,tp_string("number")) == 0) { return tp_number(v.type == TP_NUMBER); } if (tp_cmp(tp,t,tp_string("fnc")) == 0) { return tp_number(v.type == TP_FNC && (v.fnc.ftype&2) == 0); } if (tp_cmp(tp,t,tp_string("method")) == 0) { return tp_number(v.type == TP_FNC && (v.fnc.ftype&2) != 0); } tp_raise(tp_None,tp_string("(is_type) TypeError: ?")); }
tp_obj tp_float(TP) { tp_obj v = TP_OBJ(); int ord = TP_DEFAULT(tp_number(0)).number.val; int type = v.type; if (type == TP_NUMBER) { return v; } if (type == TP_STRING) { if (strchr(STR(v),'.')) { return tp_number(atof(STR(v))); } return(tp_number(strtol(STR(v),0,ord))); } tp_raise(None,"tp_float(%s)",STR(v)); }
tp_obj tp_float(TP) { tp_obj v = TP_OBJ(); int ord = TP_DEFAULT(tp_number(0)).number.val; int type = v.type; if (type == TP_NUMBER) { return v; } if (type == TP_STRING && v.string.len < 32) { char s[32]; memset(s,0,v.string.len+1); memcpy(s,v.string.val,v.string.len); if (strchr(s,'.')) { return tp_number(atof(s)); } return(tp_number(strtol(s,0,ord))); } tp_raise(tp_None,tp_string("(tp_float) TypeError: ?")); }
tp_obj tp_join(TP) { tp_obj delim = TP_OBJ(); tp_obj val = TP_OBJ(); int l=0,i; tp_obj r; char *s; for (i=0; i<val.list.val->len; i++) { if (i!=0) { l += delim.string.len; } l += tp_str(tp,val.list.val->items[i]).string.len; } r = tp_string_t(tp,l); s = r.string.info->s; l = 0; for (i=0; i<val.list.val->len; i++) { tp_obj e; if (i!=0) { memcpy(s+l,delim.string.val,delim.string.len); l += delim.string.len; } e = tp_str(tp,val.list.val->items[i]); memcpy(s+l,e.string.val,e.string.len); l += e.string.len; } return tp_track(tp,r); }
/* Socket bind method. * * In KolibriOS it just sets local address and port. * * Example: * s.bind('10.10.1.2', 6000) #Connects to 10.10.1.2:6000 */ tp_obj kolibri_bind(TP) { tp_obj self = TP_TYPE(TP_DICT); tp_obj local_addr_obj = TP_OBJ(); __u32 local_port = (__u32)TP_TYPE(TP_NUMBER).number.val; __u32 local_addr; if (local_addr_obj.type == TP_NUMBER) local_addr = local_addr_obj.number.val; else if (local_addr_obj.type == TP_STRING) inet_pton(tp, (const char *)local_addr_obj.string.val, local_addr_obj.string.len, &local_addr); tp_set(tp, self, tp_string("local_addr"), tp_number(local_addr)); tp_set(tp, self, tp_string("local_port"), tp_number(local_port)); return tp_None; }
/* Function: tp_builtins_bool * Coerces any value to a boolean. */ tp_obj tp_builtins_bool(TP) { tp_obj v = TP_OBJ(); return (tp_number(tp_bool(tp, v))); }
tp_obj tp_len_(TP) { tp_obj e = TP_OBJ(); return tp_len(tp,e); }
tp_obj tp_find(TP) { tp_obj s = TP_OBJ(); tp_obj v = TP_OBJ(); return tp_number(_tp_str_index(s,v)); }
tp_obj tp_str2(TP) { tp_obj v = TP_OBJ(); return tp_str(tp,v); }
tp_obj tp_bind(TP) { tp_obj r = TP_OBJ(); tp_obj self = TP_OBJ(); return tp_fnc_new(tp,r.fnc.ftype|2,r.fnc.fval,self,r.fnc.val->globals); }