Added mem-streams module

This commit is contained in:
Justin Ethier 2016-08-25 23:38:34 -04:00
parent fce6f43f7a
commit 4aec9a341f
5 changed files with 15 additions and 32 deletions

View file

@ -66,11 +66,12 @@ libcyclone.so.1: runtime.c include/cyclone/runtime.h
gcc $(CFLAGS) -c -fPIC runtime.c -o runtime.o
gcc -shared -Wl,-soname,libcyclone.so.1 -o libcyclone.so.1.0.1 runtime.o
libcyclone.a: runtime.c include/cyclone/runtime.h include/cyclone/types.h gc.c dispatch.c
libcyclone.a: runtime.c include/cyclone/runtime.h include/cyclone/types.h gc.c dispatch.c mem-streams.c
# echo $(CC_PROG)
# echo $(CC_EXEC)
# echo $(CC_LIB)
$(CC) $(CFLAGS) -c dispatch.c -o dispatch.o
$(CC) $(CFLAGS) -c mem-streams.c -o mem-streams.o
$(CC) $(CFLAGS) -std=gnu99 -c gc.c -o gc.o
$(CC) $(CFLAGS) -c \
-DCYC_INSTALL_DIR=\"$(PREFIX)\" \
@ -81,7 +82,7 @@ libcyclone.a: runtime.c include/cyclone/runtime.h include/cyclone/types.h gc.c d
-DCYC_CC_EXEC=\"$(CC_EXEC)\" \
-DCYC_CC_LIB=\"$(CC_LIB)\" \
runtime.c -o runtime.o
$(AR) rcs libcyclone.a runtime.o gc.o dispatch.o
$(AR) rcs libcyclone.a runtime.o gc.o dispatch.o mem-streams.o
# Instructions from: http://www.adp-gmbh.ch/cpp/gcc/create_lib.html
# Note compiler will have to link to this, eg:
#Linking against static library
@ -103,6 +104,7 @@ bootstrap: icyc
cp srfi/*.sld $(BOOTSTRAP_DIR)/srfi
cp srfi/*.scm $(BOOTSTRAP_DIR)/srfi
cp runtime.c $(BOOTSTRAP_DIR)
cp mem-streams.c $(BOOTSTRAP_DIR)
cp gc.c $(BOOTSTRAP_DIR)
cp dispatch.c $(BOOTSTRAP_DIR)
cp scheme/base.c $(BOOTSTRAP_DIR)/scheme
@ -163,6 +165,7 @@ tags:
indent:
indent -linux -l80 -i2 -nut gc.c
indent -linux -l80 -i2 -nut runtime.c
indent -linux -l80 -i2 -nut mem-streams.c
indent -linux -l80 -i2 -nut include/cyclone/*.h
.PHONY: clean

2
gc.c
View file

@ -425,6 +425,8 @@ char *gc_copy_obj(object dest, char *obj, gc_thread_data * thd)
type_of(hp) = port_tag;
hp->fp = ((port_type *) obj)->fp;
hp->mode = ((port_type *) obj)->mode;
hp->mem_buf = ((port_type *)obj)->mem_buf;
hp->mem_buf_len = ((port_type *)obj)->mem_buf_len;
return (char *)hp;
}
case cvar_tag:{

View file

@ -216,6 +216,8 @@ port_type Cyc_stdin(void);
port_type Cyc_stderr(void);
port_type Cyc_io_open_input_file(void *data, object str);
port_type Cyc_io_open_output_file(void *data, object str);
port_type Cyc_io_open_output_string(void *data);
void Cyc_io_get_output_string(void *data, object cont, object port);
object Cyc_io_close_port(void *data, object port);
object Cyc_io_close_input_port(void *data, object port);
object Cyc_io_close_output_port(void *data, object port);

View file

@ -410,14 +410,19 @@ typedef struct {
tag_type tag;
FILE *fp;
int mode;
char *mem_buf;
size_t mem_buf_len;
} port_type;
#define make_port(p,f,m) \
port_type p; \
p.hdr.mark = gc_color_red; \
p.hdr.grayed = 0; \
p.tag = port_tag; \
p.fp = f; \
p.mode = m;
p.mode = m; \
p.mem_buf = NULL; \
p.mem_buf_len = 0;
/* Vector type */

View file

@ -2537,35 +2537,6 @@ object Cyc_io_peek_char(void *data, object cont, object port)
return Cyc_EOF;
}
// Hacky approach to a minimal string buffer implementation. This may change in the future
// For background see: http://stackoverflow.com/questions/539537/memory-buffer-as-file
port_type Cyc_io_open_output_string(void *data)
// TODO: no, this is too hacky. fuck it, just use fmemopen or open_memstream
// for non-supported platforms we'll raise an error and not do anything
{
FILE *f = fopen("/dev/null", "w");
int i;
int written = 0;
char *buf = malloc(100000);
make_port(p, NULL, 0); // TODO: probably wrong params, need to add buf to port obj and type
//setbuffer(f, buf, 100000);
if (0 != setvbuf (f, buf, _IOFBF, 100000)){
// TODO: raise() instead?
fprintf(stderr, "Unable to setvbuf!\n");
exit(1);
}
for (i = 0; i < 1000; i++) {
written += fprintf(f, "Number %d\n", i);
}
for (i = 0; i < written; i++) {
printf("%c", buf[i]);
}
return p;
}
// END string buffers
// Functions internal to the runtime that use malloc
list malloc_make_pair(object a, object d)
{