(file-position stream) and (file-position stream position) would be how it’s done in Common Lisp, so that sounds reasonable. I’m not sure how to get access to the SDgfile from a passed stream, and I’d rather not repeatedly flush the buffer to get to the desired position.
I guess since SDgfile is a global variable, one could just run SDgfile.seek(position) directly. Perhaps after checking that the stream is the correct type, and that the position given is less than the size of the file that is currently open. Something like the following:
object *fn_file_position(object *args, object *env) {
(void) env;
#if defined(sdcardsupport)
if (isstream(first(args))>>8 != SDSTREAM) error2("invalid SD file stream");
if (second(args) != NULL) {
unsigned long pos = (unsigned long)(checkinteger(second(args)));
if (pos <= SDgfile.size()) {
SDgfile.seek(pos); //TODO: check for write mode, use SDpfile instead
return number(pos);
}
} else {
return number(SDgfile.position()); //TODO: check for write mode?
}
#endif
return nil;
}
const char string_file_position[] PROGMEM = "file-position";
//...
const char doc_file_position[] PROGMEM = "(file-position stream)\n (file-position stream position)\n Sets with-sd-card stream's file position, or returns it current value";
//...
{ string_file_position, fn_file_position, 0212, doc_file_position },
Unfortunately the above code fails to return a file position when no second argument is given…and doesn’t always work when you give a position. I think I’m messing up the passing of position to and from lisp objects, as it’s expecting unsigned longs.
If you have any suggestions, I’d love to hear them. Otherwise I’ll try and work on this more when I have time.