tp_obj tp_ord(TP) { tp_obj s = TP_STR(); if (s.string.len != 1) { tp_raise(tp_None,tp_string("(tp_ord) TypeError: ord() expected a character")); } return tp_number((unsigned char)s.string.val[0]); }
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_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_load(TP) { char *fname = TP_STR(); struct stat stbuf; stat(fname, &stbuf); long l = stbuf.st_size; FILE *f; f = fopen(fname,"rb"); if (!f) { tp_raise(None,"tp_load(%s)",fname); } tp_obj r = tp_string_t(tp,l); char *s = r.string.val; fread(s,1,l,f); fclose(f); return tp_track(tp,r); }
tp_obj tp_load(TP) { FILE *f; long l; tp_obj r; char *s; char const *fname = TP_STR(); struct stat stbuf; stat(fname, &stbuf); l = stbuf.st_size; f = fopen(fname,"rb"); if (!f) { tp_raise(tp_None,"tp_load(%s)",fname); } r = tp_string_t(tp,l); s = r.string.info->s; fread(s,1,l,f); fclose(f); return tp_track(tp,r); }
tp_obj tp_load(TP) { FILE *f; long l; tp_obj r; char *s; char fname[256]; tp_cstr(tp,TP_STR(),fname,256); struct stat stbuf; stat(fname, &stbuf); l = stbuf.st_size; f = fopen(fname,"rb"); if (!f) { tp_raise(tp_None,tp_string("(tp_load) IOError: ?")); } r = tp_string_t(tp,l); s = r.string.info->s; fread(s,1,l,f); /* if (rr !=l) { printf("hmmn: %d %d\n",rr,(int)l); }*/ fclose(f); return tp_track(tp,r); }
tp_obj tp_system(TP) { char *s = TP_STR(); int r = system(s); return tp_number(r); }
tp_obj tp_mtime(TP) { char *s = TP_STR(); struct stat stbuf; if (!stat(s,&stbuf)) { return tp_number(stbuf.st_mtime); } tp_raise(None,"tp_mtime(%s)",s); }
tp_obj tp_exists(TP) { char *s = TP_STR(); struct stat stbuf; return tp_number(!stat(s,&stbuf)); }
/* Function: tp_system * * The system builtin. A grave security flaw. If your version of tinypy * enables this, you better remove it before deploying your app :P */ tp_obj tp_system(TP) { char s[TP_CSTR_LEN]; tp_cstr(tp,TP_STR(),s,TP_CSTR_LEN); int r = system(s); return tp_number(r); }
tp_obj tp_mtime(TP) { char fname[TP_CSTR_LEN]; tp_cstr(tp,TP_STR(),fname,TP_CSTR_LEN); struct stat stbuf; if (!stat(fname,&stbuf)) { return tp_number(stbuf.st_mtime); } tp_raise(tp_None,tp_string("(tp_mtime) IOError: ?")); }
tp_obj tp_exists(TP) { char fname[TP_CSTR_LEN]; tp_cstr(tp,TP_STR(),fname,TP_CSTR_LEN); struct stat stbuf; return tp_number(!stat(fname,&stbuf)); }