2014-01-17 06:58:31 -05:00
|
|
|
/**
|
|
|
|
* See Copyright Notice in picrin.h
|
|
|
|
*/
|
|
|
|
|
2013-10-17 09:42:47 -04:00
|
|
|
#ifndef PORT_H__
|
|
|
|
#define PORT_H__
|
|
|
|
|
2013-10-22 02:44:03 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-01-16 04:41:55 -05:00
|
|
|
#define PIC_UBUFSIZ 3
|
|
|
|
|
2014-01-16 05:56:45 -05:00
|
|
|
enum pic_file_flags {
|
|
|
|
PIC_FILE_EOF = 1,
|
|
|
|
};
|
|
|
|
|
2014-01-16 04:24:25 -05:00
|
|
|
typedef struct {
|
2014-01-16 05:56:45 -05:00
|
|
|
short flags;
|
2014-01-16 04:41:55 -05:00
|
|
|
/* buffered IO */
|
2014-01-16 04:36:47 -05:00
|
|
|
char *buf;
|
|
|
|
int mode;
|
|
|
|
int bufsiz;
|
2014-01-16 04:39:03 -05:00
|
|
|
char *s, *c, *e;
|
2014-01-16 04:41:55 -05:00
|
|
|
/* ungetc buf */
|
|
|
|
char ub[PIC_UBUFSIZ];
|
|
|
|
int us;
|
|
|
|
int ur;
|
|
|
|
/* operators */
|
2014-01-16 04:24:25 -05:00
|
|
|
struct {
|
|
|
|
void *cookie;
|
|
|
|
int (*read)(void *, char *, int);
|
|
|
|
int (*write)(void *, const char *, int);
|
2014-01-16 06:13:13 -05:00
|
|
|
long (*seek)(void *, long, int);
|
2014-01-16 04:24:25 -05:00
|
|
|
int (*close)(void *);
|
|
|
|
} vtable;
|
|
|
|
} pic_file;
|
|
|
|
|
2014-01-16 07:00:55 -05:00
|
|
|
enum pic_port_flag {
|
|
|
|
PIC_PORT_IN = 1,
|
|
|
|
PIC_PORT_OUT = 2,
|
|
|
|
PIC_PORT_TEXT = 4,
|
|
|
|
PIC_PORT_BINARY = 8,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum pic_port_status {
|
|
|
|
PIC_PORT_OPEN,
|
|
|
|
PIC_PORT_CLOSE,
|
|
|
|
};
|
|
|
|
|
2013-10-17 09:42:47 -04:00
|
|
|
struct pic_port {
|
|
|
|
PIC_OBJECT_HEADER
|
2014-01-16 04:24:25 -05:00
|
|
|
pic_file *file;
|
|
|
|
int flags;
|
|
|
|
int status;
|
2013-10-17 09:42:47 -04:00
|
|
|
};
|
|
|
|
|
2013-10-24 09:56:04 -04:00
|
|
|
#define pic_port_p(v) (pic_type(v) == PIC_TT_PORT)
|
2013-11-05 00:27:59 -05:00
|
|
|
#define pic_port_ptr(v) ((struct pic_port *)pic_ptr(v))
|
2013-10-17 09:42:47 -04:00
|
|
|
|
2014-01-12 10:49:25 -05:00
|
|
|
pic_value pic_eof_object();
|
|
|
|
|
2014-01-12 10:51:19 -05:00
|
|
|
struct pic_port *pic_stdin(pic_state *);
|
|
|
|
struct pic_port *pic_stdout(pic_state *);
|
|
|
|
struct pic_port *pic_stderr(pic_state *);
|
|
|
|
|
2014-01-16 06:27:39 -05:00
|
|
|
/* generic file constructor */
|
|
|
|
pic_file *pic_funopen(void *cookie, int (*read)(void *, char *, int), int (*write)(void *, const char *, int), long (*seek)(void *, long, int), int (*close)(void *));
|
|
|
|
|
|
|
|
/* buffering */
|
2014-01-16 04:36:47 -05:00
|
|
|
int pic_setvbuf(pic_file *, char *, int, size_t);
|
2014-01-16 04:32:05 -05:00
|
|
|
int pic_fflush(pic_file *);
|
2014-01-16 04:41:10 -05:00
|
|
|
int pic_ffill(pic_file *);
|
2014-01-16 04:32:05 -05:00
|
|
|
|
2014-01-16 23:11:52 -05:00
|
|
|
/* resource aquisition */
|
2014-01-16 04:28:37 -05:00
|
|
|
pic_file *pic_fopen(const char *, const char *);
|
2014-01-16 23:11:52 -05:00
|
|
|
pic_file *pic_mopen(const char *, size_t, const char *);
|
2014-01-16 04:28:37 -05:00
|
|
|
int pic_fclose(pic_file *);
|
|
|
|
|
2014-01-16 06:04:27 -05:00
|
|
|
/* direct IO with buffering */
|
2014-01-16 04:28:37 -05:00
|
|
|
size_t pic_fread(void *, size_t, size_t, pic_file *);
|
|
|
|
size_t pic_fwrite(const void *, size_t, size_t, pic_file *);
|
|
|
|
|
2014-01-17 06:11:06 -05:00
|
|
|
/* indicator positioning */
|
|
|
|
long pic_fseek(pic_file *, long offset, int whence);
|
|
|
|
long pic_ftell(pic_file *);
|
|
|
|
void pic_rewind(pic_file *);
|
|
|
|
|
2014-01-16 06:04:27 -05:00
|
|
|
/* character IO */
|
2014-01-16 04:30:03 -05:00
|
|
|
int pic_fgetc(pic_file *);
|
|
|
|
int pic_ungetc(int, pic_file *);
|
|
|
|
int pic_fputc(int, pic_file *);
|
|
|
|
int pic_fputs(const char *, pic_file *);
|
|
|
|
|
2013-10-17 09:42:47 -04:00
|
|
|
#endif
|