1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-17 16:40:07 +02:00
Leland Lucius 15b9bb96cd Update nyquist to SVN r331 (r3.16+)
------------------------------------------------------------------------
   r331 | rbd | 2020-10-13 12:40:12 -0500 (Tue, 13 Oct 2020) | 2 lines

   Also forgot to install NyquistWords.txt

   ------------------------------------------------------------------------
   r330 | rbd | 2020-10-13 12:34:06 -0500 (Tue, 13 Oct 2020) | 2 lines

   Forgot to move nyquistman.pdf from docsrc/s2h to release

   ------------------------------------------------------------------------
   r329 | rbd | 2020-10-13 11:32:33 -0500 (Tue, 13 Oct 2020) | 2 lines

   Updated some version numbers for 3.16.

   ------------------------------------------------------------------------
   r328 | rbd | 2020-10-13 11:20:52 -0500 (Tue, 13 Oct 2020) | 2 lines

   Fixed NyquistIDE antialiasing for plot text, fix format of message.

   ------------------------------------------------------------------------
   r327 | rbd | 2020-10-12 21:01:53 -0500 (Mon, 12 Oct 2020) | 2 lines

   Fixed a couple of format problems in manual. This version of Nyquist has been tested wtih macOS, Linux, 32&64-bit Windows.

   ------------------------------------------------------------------------
   r326 | rbd | 2020-10-12 20:21:38 -0500 (Mon, 12 Oct 2020) | 1 line

   Modified WIN32 32-bit XLisp to use 64-bit FIXNUMs. This allows XLisp and Nyquist to handle big sounds even on 32-bit machines. Probably at some cost, but inner loops are mostly float and int32, and the Nyquist release is 64-bit anyway. Maybe we'll have to run some benchmarks on Audacity, which is still 32-bit on Windows.
   ------------------------------------------------------------------------
   r325 | rbd | 2020-10-12 13:16:57 -0500 (Mon, 12 Oct 2020) | 1 line

   Win64 passes bigfiletest.lsp now. This version should work on all 64-bit systems now. These changes untested on Linux and macOS.
   ------------------------------------------------------------------------
   r324 | rbd | 2020-10-11 21:31:53 -0500 (Sun, 11 Oct 2020) | 2 lines

   I couldn't free enough space on my linux box, so I adjusted the bigfiletest to write 8-bit ulaw. It's still >4GB and >4G samples. Works on Linux.

   ------------------------------------------------------------------------
   r323 | rbd | 2020-10-11 19:41:25 -0500 (Sun, 11 Oct 2020) | 2 lines

   Missing file from last commit.

   ------------------------------------------------------------------------
   r322 | rbd | 2020-10-11 19:36:08 -0500 (Sun, 11 Oct 2020) | 1 line

   Found another case where WIN64 needs int64_t instead of long for sample count.
   ------------------------------------------------------------------------
   r321 | rbd | 2020-10-11 19:33:25 -0500 (Sun, 11 Oct 2020) | 3 lines

   Fixed s-save to	handle optional	and keyword parameters (which should never have	been mixed in the first	place).	Documentation cleanup - should be final for this version.

   ------------------------------------------------------------------------
   r320 | rbd | 2020-10-11 14:44:37 -0500 (Sun, 11 Oct 2020) | 2 lines

   Fixes to handle IRCAM sound format and tests for big file io working on macOS.

   ------------------------------------------------------------------------
   r319 | rbd | 2020-10-10 21:31:58 -0500 (Sat, 10 Oct 2020) | 2 lines

   Changes for linux and to avoid compiler warnings on linux.

   ------------------------------------------------------------------------
   r318 | rbd | 2020-10-10 20:50:23 -0500 (Sat, 10 Oct 2020) | 1 line

   This is the test used for Win64 version.
   ------------------------------------------------------------------------
   r317 | rbd | 2020-10-10 20:34:34 -0500 (Sat, 10 Oct 2020) | 1 line

   This version works on Win64. Need to test changes on macOS and linux.
   ------------------------------------------------------------------------
   r316 | rbd | 2020-10-10 19:59:15 -0500 (Sat, 10 Oct 2020) | 2 lines

   PWL changes to avoid compiler warning.

   ------------------------------------------------------------------------
   r315 | rbd | 2020-10-10 19:34:04 -0500 (Sat, 10 Oct 2020) | 2 lines

   A few more changes for 64-bit sample counts on Win64

   ------------------------------------------------------------------------
   r314 | rbd | 2020-10-10 13:19:42 -0500 (Sat, 10 Oct 2020) | 2 lines

   Fixed int64_t declaration in gate.alg

   ------------------------------------------------------------------------
   r313 | rbd | 2020-10-10 12:07:40 -0500 (Sat, 10 Oct 2020) | 2 lines

   Fixes to gate for long sounds

   ------------------------------------------------------------------------
   r312 | rbd | 2020-10-10 11:47:29 -0500 (Sat, 10 Oct 2020) | 2 lines

   Fixed sound_save types for intgen

   ------------------------------------------------------------------------
   r311 | rbd | 2020-10-10 11:09:01 -0500 (Sat, 10 Oct 2020) | 2 lines

   Fixed a 64-bit sample count problem in siosc.alg

   ------------------------------------------------------------------------
   r310 | rbd | 2020-10-10 11:03:12 -0500 (Sat, 10 Oct 2020) | 2 lines

   Fixed sndmax to handle 64-bit sample counts.

   ------------------------------------------------------------------------
   r309 | rbd | 2020-10-10 10:57:04 -0500 (Sat, 10 Oct 2020) | 2 lines

   Forgot to re-translate all tran/*.alg files with fix for int64 cast to int32. This version compiles on macOS and ready for test on Win64.

   ------------------------------------------------------------------------
   r308 | rbd | 2020-10-10 10:16:05 -0500 (Sat, 10 Oct 2020) | 2 lines

   Everything seems to compile and run on macOS now. Moving changes to Windows for test.

   ------------------------------------------------------------------------
   r307 | rbd | 2020-10-10 09:23:45 -0500 (Sat, 10 Oct 2020) | 1 line

   Added casts to avoid compiler warnings and to review changes to support 64-bit sample counts on Windows. Still not complete, and waiting to regenerate and compile tran directory code after updates to translation code that will insert more casts.
   ------------------------------------------------------------------------
   r306 | rbd | 2020-10-09 21:55:15 -0500 (Fri, 09 Oct 2020) | 2 lines

   Rebuilt seqfnint.c from header files.

   ------------------------------------------------------------------------
   r305 | rbd | 2020-10-09 21:53:33 -0500 (Fri, 09 Oct 2020) | 1 line

   Changed some FIXNUMS to LONG to avoid compiler warnings in seqfnint.c
   ------------------------------------------------------------------------
   r304 | rbd | 2020-10-09 21:44:03 -0500 (Fri, 09 Oct 2020) | 2 lines

   I discovered forgotten regression-test.lsp and added test that requires 64-bit sample counts to pass. Fixed a few bugs revealed by running the type-checking regression tests.

   ------------------------------------------------------------------------
   r303 | rbd | 2020-10-09 12:28:58 -0500 (Fri, 09 Oct 2020) | 2 lines

   Changes for 64-bit sample counts broke mult-channel s-save. Fixed in the commit for macOS.

   ------------------------------------------------------------------------
   r302 | rbd | 2020-10-09 10:03:39 -0500 (Fri, 09 Oct 2020) | 2 lines

   Changed snd-play to return samples computed and used that to make a test for computing long sounds that would overflow 32-bit length counts.

   ------------------------------------------------------------------------
   r301 | rbd | 2020-10-09 09:11:26 -0500 (Fri, 09 Oct 2020) | 2 lines

   corrected mistake in delaycv.alg and re-translated

   ------------------------------------------------------------------------
   r300 | rbd | 2020-10-09 09:09:06 -0500 (Fri, 09 Oct 2020) | 2 lines

   Fix to delaycv.alg -- "s" changed to "input" to avoid matching "s" in "sample_type".

   ------------------------------------------------------------------------
   r299 | rbd | 2020-10-09 09:03:33 -0500 (Fri, 09 Oct 2020) | 4 lines

   To avoid compiler warnings, XLisp interfaces to C int and long are now
   specified as LONG rather than FIXNUM, and the stubs that call the C
   functions cast FIXNUMs from XLisp into longs before calling C functions.

   ------------------------------------------------------------------------
   r298 | rbd | 2020-10-08 22:20:26 -0500 (Thu, 08 Oct 2020) | 2 lines

   This commit has many more fixes to handle long (64-bit) sounds, including a lot of fixes for warnings by Visual Studio assigning int64_t to long (works on macOS, doesn't work on VS). This was compiled and tested on macOS, and even computed a 27.1-hour sound using OSC, LP, SUM and MULT (haven't tested I/O yet).

   ------------------------------------------------------------------------
   r297 | rbd | 2020-10-07 13:04:02 -0500 (Wed, 07 Oct 2020) | 2 lines

   This is a major cleanup. It started with the goal of changing long to int64_t for sample counts so that on 64-bit windows, where long is only 32-bits, the sample counts would nevertheless be 64-bit allowing long sounds, which was a limitation for long recordings in Audacity. Since I was using compiler warnings to track possible loss-of-precision conversions from 64-bit sample counts, and there were *many* warnings, I started cleaning up *all* the warnings and ended up with a very large set of changes, including "modernizing" C declarations that date back to XLisp and CMU MIDI Toolkit code and were never changed. This version runs all the examples.sal code on macOS, but will surely have problems on Windows and Linux given the number of changes.

   ------------------------------------------------------------------------
   r296 | rbd | 2020-10-06 13:34:20 -0500 (Tue, 06 Oct 2020) | 2 lines

   More changes from long to int64_t for sample counts.

   ------------------------------------------------------------------------
   r295 | rbd | 2020-10-06 11:53:49 -0500 (Tue, 06 Oct 2020) | 2 lines

   More work on using 64-bit sample counts. Changed MAX_STOP from 32-bit to 64-bit limit.

   ------------------------------------------------------------------------
   r294 | rbd | 2020-10-06 11:48:05 -0500 (Tue, 06 Oct 2020) | 2 lines

   Made some changes so that sample counts are int64_t (for windows) instead of long to support sample counts above 31 bits.

   ------------------------------------------------------------------------
   r293 | rbd | 2020-10-04 21:30:55 -0500 (Sun, 04 Oct 2020) | 2 lines

   Fixed a few minor things for Linux and tested on Linux.

   ------------------------------------------------------------------------
   r292 | rbd | 2020-10-04 21:00:28 -0500 (Sun, 04 Oct 2020) | 2 lines

   Update extensions: all are minor changes.

   ------------------------------------------------------------------------
   r291 | rbd | 2020-09-24 13:59:31 -0500 (Thu, 24 Sep 2020) | 2 lines

   New implementation of seq and seqrep, added get-real-time, documented get-real-time, fixed examples.sal and examples.lsp which are now in lib rather than extensions (so they are now back in the basic installation), other cleanup.

   ------------------------------------------------------------------------
   r290 | rbd | 2020-08-16 16:24:52 -0500 (Sun, 16 Aug 2020) | 2 lines

   Fixed bug in snd-gate, revised GATE and NOISE-GATE to handle multi-channel sound. RMS now handles multi-channel input. S-AVG added to take multichannel input (but not used, because RMS could not be written without making SND-SRATE convert multichannel sound to vector of floats. That seems to be going toward a fully vectorized model. Not going there for now.

   ------------------------------------------------------------------------
   r289 | rbd | 2020-07-09 16:27:45 -0500 (Thu, 09 Jul 2020) | 2 lines

   Added GET-REAL-TIME function to XLISP. May not work yet on Windows. Various fixes for compiler warnings. I noticed FLAC doesn't work (I guess it never did) and I cannot figure out how this even links because flac_min seems to be undefined. Something to look at later.
2021-01-27 23:45:25 -06:00

736 lines
16 KiB
C

/* xlfio.c - xlisp file i/o */
/* Copyright (c) 1985, by David Michael Betz
All Rights Reserved
Permission is granted for unrestricted non-commercial use */
/* CHANGE LOG
* --------------------------------------------------------------------
* 30Sep06 rbd added xbigendianp
* 28Apr03 dm eliminate some compiler warnings
*/
#include "switches.h"
#include <string.h>
#include "xlisp.h"
/* do some sanity checking: */
#ifndef XL_BIG_ENDIAN
#ifndef XL_LITTLE_ENDIAN
#error configuration error -- either XL_BIG_ or XL_LITTLE_ENDIAN must be defined
in xlisp.h
#endif
#endif
#ifdef XL_BIG_ENDIAN
#ifdef XL_LITTLE_ENDIAN
#error configuration error -- both XL_BIG_ and XL_LITTLE_ENDIAN are defined!
#endif
#endif
/* forward declarations */
FORWARD LOCAL LVAL getstroutput(LVAL stream);
FORWARD LOCAL LVAL printit(int pflag, int tflag);
FORWARD LOCAL LVAL flatsize(int pflag);
/* xread - read an expression */
LVAL xread(void)
{
LVAL fptr,eof,rflag,val;
/* get file pointer and eof value */
fptr = (moreargs() ? xlgetfile() : getvalue(s_stdin));
eof = (moreargs() ? xlgetarg() : NIL);
rflag = (moreargs() ? xlgetarg() : NIL);
xllastarg();
/* read an expression */
if (!xlread(fptr,&val,rflag != NIL))
val = eof;
/* return the expression */
return (val);
}
/* xprint - built-in function 'print' */
LVAL xprint(void)
{
return (printit(TRUE,TRUE));
}
/* xprin1 - built-in function 'prin1' */
LVAL xprin1(void)
{
return (printit(TRUE,FALSE));
}
/* xprinc - built-in function princ */
LVAL xprinc(void)
{
return (printit(FALSE,FALSE));
}
/* xterpri - terminate the current print line */
LVAL xterpri(void)
{
LVAL fptr;
/* get file pointer */
fptr = (moreargs() ? xlgetfile() : getvalue(s_stdout));
xllastarg();
/* terminate the print line and return nil */
xlterpri(fptr);
return (NIL);
}
/* printit - common print function */
LOCAL LVAL printit(int pflag, int tflag)
{
LVAL fptr,val;
/* get expression to print and file pointer */
val = xlgetarg();
fptr = (moreargs() ? xlgetfile() : getvalue(s_stdout));
xllastarg();
/* print the value */
xlprint(fptr,val,pflag);
/* terminate the print line if necessary */
if (tflag)
xlterpri(fptr);
/* return the result */
return (val);
}
/* xflatsize - compute the size of a printed representation using prin1 */
LVAL xflatsize(void)
{
return (flatsize(TRUE));
}
/* xflatc - compute the size of a printed representation using princ */
LVAL xflatc(void)
{
return (flatsize(FALSE));
}
/* flatsize - compute the size of a printed expression */
LOCAL LVAL flatsize(int pflag)
{
LVAL val;
/* get the expression */
val = xlgetarg();
xllastarg();
/* print the value to compute its size */
xlfsize = 0;
xlprint(NIL,val,pflag);
/* return the length of the expression */
return (cvfixnum((FIXTYPE)xlfsize));
}
/* xlopen - open a text or binary file */
LVAL xlopen(int binaryflag)
{
char *name,*mode=NULL;
FILE *fp;
LVAL dir;
/* get the file name and direction */
name = (char *)getstring(xlgetfname());
if (!xlgetkeyarg(k_direction,&dir))
dir = k_input;
/* get the mode */
if (dir == k_input)
mode = "r";
else if (dir == k_output)
mode = "w";
else
xlerror("bad direction",dir);
/* try to open the file */
if (binaryflag) {
fp = osbopen(name,mode);
} else {
fp = osaopen(name,mode);
}
return (fp ? cvfile(fp) : NIL);
}
/* xopen - open a file */
LVAL xopen(void)
{
return xlopen(FALSE);
}
/* xbopen - open a binary file */
LVAL xbopen(void)
{
return xlopen(TRUE);
}
/* xclose - close a file */
LVAL xclose(void)
{
LVAL fptr;
/* get file pointer */
fptr = xlgastream();
xllastarg();
/* make sure the file exists */
if (getfile(fptr) == NULL)
xlfail("file not open");
/* close the file */
osclose(getfile(fptr));
setfile(fptr,NULL);
/* return nil */
return (NIL);
}
/* xrdchar - read a character from a file */
LVAL xrdchar(void)
{
LVAL fptr;
int ch;
/* get file pointer */
fptr = (moreargs() ? xlgetfile() : getvalue(s_stdin));
xllastarg();
/* get character and check for eof */
return ((ch = xlgetc(fptr)) == EOF ? NIL : cvchar(ch));
}
/* xrdint - read an integer from a file */
/* positive byte count means big-endian, negative is little-endian */
LVAL xrdint(void)
{
LVAL fptr;
unsigned char b[4];
long i;
int n = 4;
int index = 0; /* where to start in array */
int incr = 1; /* how to step through array */
int rslt;
/* get file pointer */
fptr = (moreargs() ? xlgetfile() : getvalue(s_stdin));
/* get byte count */
if (moreargs()) {
LVAL count = typearg(fixp);
n = (int) getfixnum(count);
if (n < 0) {
n = -n;
index = n - 1;
incr = -1;
}
if (n > 4) {
xlerror("4-byte limit", count);
}
}
xllastarg();
for (i = 0; i < n; i++) {
int ch = xlgetc(fptr);
if (ch == EOF)
return NIL;
b[index] = ch;
index += incr;
}
/* build result, b is now big-endian */
/* extend sign bit for short integers */
rslt = ((b[0] & 0x80) ? -1 : 0);
for (i = 0; i < n; i++) {
rslt = (rslt << 8) + b[i];
}
/* return integer result */
return cvfixnum(rslt);
}
/* xrdfloat - read a float from a file */
LVAL xrdfloat(void)
{
LVAL fptr;
union {
char b[8];
float f;
double d;
} rslt;
int n = 4;
int i;
int index = 3; /* where to start in array */
int incr = -1; /* how to step through array */
/* get file pointer */
fptr = (moreargs() ? xlgetfile() : getvalue(s_stdin));
/* get byte count */
if (moreargs()) {
LVAL count = typearg(fixp);
n = (int) getfixnum(count);
if (n < 0) {
n = -n;
index = 0;
incr = 1;
}
if (n != 4 && n != 8) {
xlerror("must be 4 or 8 bytes", count);
}
}
xllastarg();
#ifdef XL_BIG_ENDIAN
/* flip the bytes */
index = n - 1 - index;
incr = -incr;
#endif
for (i = 0; i < n; i++) {
int ch = xlgetc(fptr);
if (ch == EOF) return NIL;
rslt.b[index] = ch;
index += incr;
}
/* return result */
return cvflonum(n == 4 ? rslt.f : rslt.d);
}
/* xrdbyte - read a byte from a file */
LVAL xrdbyte(void)
{
LVAL fptr;
int ch;
/* get file pointer */
fptr = (moreargs() ? xlgetfile() : getvalue(s_stdin));
xllastarg();
/* get character and check for eof */
return ((ch = xlgetc(fptr)) == EOF ? NIL : cvfixnum((FIXTYPE)ch));
}
/* xpkchar - peek at a character from a file */
LVAL xpkchar(void)
{
LVAL flag,fptr;
int ch;
/* peek flag and get file pointer */
flag = (moreargs() ? xlgetarg() : NIL);
fptr = (moreargs() ? xlgetfile() : getvalue(s_stdin));
xllastarg();
/* skip leading white space and get a character */
if (flag)
while ((ch = xlpeek(fptr)) != EOF && isspace(ch))
xlgetc(fptr);
else
ch = xlpeek(fptr);
/* return the character */
return (ch == EOF ? NIL : cvchar(ch));
}
/* xwrchar - write a character to a file */
LVAL xwrchar(void)
{
LVAL fptr,chr;
/* get the character and file pointer */
chr = xlgachar();
fptr = (moreargs() ? xlgetfile() : getvalue(s_stdout));
xllastarg();
/* put character to the file */
xlputc(fptr,getchcode(chr));
/* return the character */
return (chr);
}
/* xwrbyte - write a byte to a file */
LVAL xwrbyte(void)
{
LVAL fptr,chr;
/* get the byte and file pointer */
chr = xlgafixnum();
fptr = (moreargs() ? xlgetfile() : getvalue(s_stdout));
xllastarg();
/* put byte to the file */
xlputc(fptr,(int)getfixnum(chr));
/* return the character */
return (chr);
}
/* xwrint - write an integer to a file */
/* positive count means write big-endian */
LVAL xwrint(void)
{
LVAL val, fptr;
unsigned char b[4];
long i;
int n = 4;
int index = 3; /* where to start in array */
int incr = -1; /* how to step through array */
int v; /* xwrint only allows up to 4 bytes, so int is enough */
/* get the int and file pointer and optional byte count */
val = xlgafixnum();
v = (int) getfixnum(val);
fptr = (moreargs() ? xlgetfile() : getvalue(s_stdout));
if (moreargs()) {
LVAL count = typearg(fixp);
n = (int) getfixnum(count);
index = n - 1;
if (n < 0) {
n = -n;
index = 0;
incr = 1;
}
if (n > 4) {
xlerror("4-byte limit", count);
}
}
xllastarg();
/* build output b as little-endian */
for (i = 0; i < n; i++) {
b[i] = (unsigned char) v;
v = v >> 8;
}
/* put bytes to the file */
while (n) {
n--;
xlputc(fptr, b[index]);
index += incr;
}
/* return the integer */
return val;
}
/* xwrfloat - write a float to a file */
LVAL xwrfloat(void)
{
LVAL val, fptr;
union {
char b[8];
float f;
double d;
} v;
int n = 4;
int i;
int index = 3; /* where to start in array */
int incr = -1; /* how to step through array */
/* get the float and file pointer and optional byte count */
val = xlgaflonum();
fptr = (moreargs() ? xlgetfile() : getvalue(s_stdout));
if (moreargs()) {
LVAL count = typearg(fixp);
n = (int) getfixnum(count);
if (n < 0) {
n = -n;
index = 0;
incr = 1;
}
if (n != 4 && n != 8) {
xlerror("must be 4 or 8 bytes", count);
}
}
xllastarg();
#ifdef XL_BIG_ENDIAN
/* flip the bytes */
index = n - 1 - index;
incr = -incr;
#endif
/* build output v.b */
if (n == 4) v.f = (float) getflonum(val);
else v.d = getflonum(val);
/* put bytes to the file */
for (i = 0; i < n; i++) {
xlputc(fptr, v.b[index]);
index += incr;
}
/* return the flonum */
return val;
}
/* xreadline - read a line from a file */
LVAL xreadline(void)
{
unsigned char buf[STRMAX+1],*p,*sptr;
LVAL fptr,str,newstr;
int len,blen,ch;
/* protect some pointers */
xlsave1(str);
/* get file pointer */
fptr = (moreargs() ? xlgetfile() : getvalue(s_stdin));
xllastarg();
/* get character and check for eof */
len = blen = 0; p = buf;
while ((ch = xlgetc(fptr)) != EOF && ch != '\n') {
/* check for buffer overflow */
if (blen >= STRMAX) {
newstr = new_string(len + STRMAX + 1);
sptr = getstring(newstr); *sptr = '\0';
if (str) strcat((char *) sptr, (char *) getstring(str));
*p = '\0'; strcat((char *) sptr, (char *) buf);
p = buf; blen = 0;
len += STRMAX;
str = newstr;
}
/* store the character */
*p++ = ch; ++blen;
}
/* check for end of file */
if (len == 0 && p == buf && ch == EOF) {
xlpop();
return (NIL);
}
/* append the last substring */
if (str == NIL || blen) {
newstr = new_string(len + blen + 1);
sptr = getstring(newstr); *sptr = '\0';
if (str) strcat((char *) sptr, (char *) getstring(str));
*p = '\0'; strcat((char *) sptr, (char *) buf);
str = newstr;
}
/* restore the stack */
xlpop();
/* return the string */
return (str);
}
/* xmkstrinput - make a string input stream */
LVAL xmkstrinput(void)
{
int start,end,len,i;
unsigned char *str;
LVAL string,val;
/* protect the return value */
xlsave1(val);
/* get the string and length */
string = xlgastring();
str = getstring(string);
len = getslength(string) - 1;
/* get the starting offset */
if (moreargs()) {
val = xlgafixnum();
start = (int)getfixnum(val);
}
else start = 0;
/* get the ending offset */
if (moreargs()) {
val = xlgafixnum();
end = (int)getfixnum(val);
}
else end = len;
xllastarg();
/* check the bounds */
if (start < 0 || start > len)
xlerror("string index out of bounds",cvfixnum((FIXTYPE)start));
if (end < 0 || end > len)
xlerror("string index out of bounds",cvfixnum((FIXTYPE)end));
/* make the stream */
val = newustream();
/* copy the substring into the stream */
for (i = start; i < end; ++i)
xlputc(val,str[i]);
/* restore the stack */
xlpop();
/* return the new stream */
return (val);
}
/* xmkstroutput - make a string output stream */
LVAL xmkstroutput(void)
{
return (newustream());
}
/* xgetstroutput - get output stream string */
LVAL xgetstroutput(void)
{
LVAL stream;
stream = xlgaustream();
xllastarg();
return (getstroutput(stream));
}
/* xgetlstoutput - get output stream list */
LVAL xgetlstoutput(void)
{
LVAL stream,val;
/* get the stream */
stream = xlgaustream();
xllastarg();
/* get the output character list */
val = gethead(stream);
/* empty the character list */
sethead(stream,NIL);
settail(stream,NIL);
/* return the list */
return (val);
}
/* xformat - formatted output function */
LVAL xformat(void)
{
unsigned char *fmt;
LVAL stream,val;
int ch;
/* protect stream in case it is a new ustream */
xlsave1(stream);
/* get the stream and format string */
stream = xlgetarg();
if (stream == NIL)
val = stream = newustream();
else {
if (stream == s_true)
stream = getvalue(s_stdout);
else if (!streamp(stream) && !ustreamp(stream))
xlbadtype(stream);
val = NIL;
}
fmt = getstring(xlgastring());
/* process the format string */
while ((ch = *fmt++))
if (ch == '~') {
switch (*fmt++) {
case '\0':
xlerror("expecting a format directive",cvstring((char *) (fmt-1)));
case 'a': case 'A':
xlprint(stream,xlgetarg(),FALSE);
break;
case 's': case 'S':
xlprint(stream,xlgetarg(),TRUE);
break;
case '%':
xlterpri(stream);
break;
case '~':
xlputc(stream,'~');
break;
case '\n':
case '\r':
/* mac may read \r -- this should be ignored */
if (*fmt == '\r') fmt++;
while (*fmt && *fmt != '\n' && isspace(*fmt))
++fmt;
break;
default:
xlerror("unknown format directive",cvstring((char *) (fmt-1)));
}
}
else
xlputc(stream,ch);
/* return the value */
if (val) val = getstroutput(val);
xlpop();
return val;
}
/* getstroutput - get the output stream string (internal) */
LOCAL LVAL getstroutput(LVAL stream)
{
unsigned char *str;
LVAL next,val;
int len,ch;
/* compute the length of the stream */
for (len = 0, next = gethead(stream); next != NIL; next = cdr(next))
++len;
/* create a new string */
val = new_string(len + 1);
/* copy the characters into the new string */
str = getstring(val);
while ((ch = xlgetc(stream)) != EOF)
*str++ = ch;
*str = '\0';
/* return the string */
return (val);
}
LVAL xlistdir(void)
{
const char *path;
LVAL result = NULL;
LVAL *tail;
/* get the path, converting unsigned char * to char * */
path = (char *)getstring(xlgetfname());
/* try to start listing */
if (osdir_list_start(path)) {
const char *filename;
xlsave1(result);
tail = &result;
while ((filename = osdir_list_next())) {
*tail = cons(NIL, NIL);
rplaca(*tail, cvstring(filename));
tail = &cdr(*tail);
}
osdir_list_finish();
xlpop();
}
return result;
}
/* xbigendianp -- is this a big-endian machine? T or NIL */
LVAL xbigendianp()
{
#ifdef XL_BIG_ENDIAN
return s_true;
#else
return NIL;
#endif
}