IO_METHOD(IoNumber, repeat) { /*doc Number repeat(optionalIndex, expression) Evaluates message a number of times that corresponds to the receivers integer value. This is significantly faster than a for() or while() loop. */ IoMessage_assertArgCount_receiver_(m, 1, self); { IoState *state = IOSTATE; IoSymbol *indexSlotName; IoMessage *doMessage; double i, max = CNUMBER(self); IoObject *result = IONIL(self); if(IoMessage_argCount(m) > 1) { indexSlotName = IoMessage_name(IoMessage_rawArgAt_(m, 0)); doMessage = IoMessage_rawArgAt_(m, 1); } else { indexSlotName = 0; doMessage = IoMessage_rawArgAt_(m, 0); } IoState_pushRetainPool(state); for (i = 0; i < max; i ++) { /* if (result != locals && result != self) { IoState_immediatelyFreeIfUnreferenced_(state, result); } */ IoState_clearTopPool(state); if (indexSlotName) { IoObject_setSlot_to_(locals, indexSlotName, IONUMBER(i)); } result = IoMessage_locals_performOn_(doMessage, locals, locals); if (IoState_handleStatus(IOSTATE)) { break; } } IoState_popRetainPoolExceptFor_(IOSTATE, result); return result; } }
IO_METHOD(IoDate, fromString) { /*doc Date fromString(aString, formatString) Sets the receiver to the date specified by aString as parsed according to the given formatString. See the Date asString method for formatting rules. Returns self. */ IoMessage_assertArgCount_receiver_(m, 2, self); { IoSymbol *date_input = IoMessage_locals_seqArgAt_(m, locals, 0); IoSymbol *format = IoMessage_locals_seqArgAt_(m, locals, 1); Date_fromString_format_(DATA(self), CSTRING(date_input), CSTRING(format)); } IoObject_isDirty_(self, 1); return self; }
IO_METHOD(IoDate, cpuSecondsToRun) { /*doc Date cpuSecondsToRun(expression) Evaluates message and returns a Number whose value is the cpu seconds taken to do the evaluation. */ IoMessage_assertArgCount_receiver_(m, 1, self); { double t2, t1 = clock(); IoMessage *doMessage = IoMessage_rawArgAt_(m, 0); IoMessage_locals_performOn_(doMessage, locals, locals); t2 = clock(); return IONUMBER((t2 - t1)/((double)CLOCKS_PER_SEC)); } }
IO_METHOD(IoFile, reopen) { /*doc File reopen(otherFile, mode) Reopens otherFile and redirects its stream to this file's path using mode. If mode is omitted, it is copied from otherFile. Returns self or raises a File exception on error. */ IoFile *otherFile; IoSeq *mode; DATA(self)->flags = IOFILE_FLAGS_NONE; IoMessage_assertArgCount_receiver_(m, 1, self); otherFile = IoMessage_locals_valueArgAt_(m, locals, 0); IOASSERT(ISFILE(otherFile), "arg must be a File"); mode = IoMessage_locals_valueArgAt_(m, locals, 1); if(ISSEQ(mode)) { DATA(self)->mode = IOREF(mode); } else { DATA(self)->mode = IOREF(IoSeq_newWithUArray_copy_(IOSTATE, (UArray *)DATA(DATA(otherFile)->mode), 1)); } if (!DATA(self)->stream) { FILE *fp = freopen(UTF8CSTRING(DATA(self)->path), CSTRING(DATA(self)->mode), DATA(otherFile)->stream); if (fp) { DATA(self)->stream = fp; } else { printf("%i:%s\n", errno, strerror(errno)); IoState_error_(IOSTATE, m, "unable to reopen to file '%s' with mode %s.", UTF8CSTRING(DATA(self)->path), CSTRING(DATA(self)->mode)); fclose(fp); } } return self; }