Skip to content

Commit

Permalink
Add FILE wrapper around FILEIOC library (Fix #78)
Browse files Browse the repository at this point in the history
  • Loading branch information
mateoconlechuga committed Jan 29, 2018
1 parent 4285eee commit 51d2e41
Show file tree
Hide file tree
Showing 21 changed files with 203 additions and 11 deletions.
2 changes: 2 additions & 0 deletions makefile
Expand Up @@ -74,6 +74,7 @@ INSTALLBF := $(call NATIVEPATH,$(INSTALLLOC)/$(RELEASE_NAME)/include/fasmg-ez80
INSTALLINC := $(call NATIVEPATH,$(INSTALLLOC)/$(RELEASE_NAME)/include)
INSTALLLIB := $(call NATIVEPATH,$(INSTALLLOC)/$(RELEASE_NAME)/lib)
INSTALLLL := $(call NATIVEPATH,$(INSTALLLOC)/$(RELEASE_NAME)/lib/libload)
INSTALLIO := $(call NATIVEPATH,$(INSTALLLOC)/$(RELEASE_NAME)/lib/fileio)
INSTALLSH := $(call NATIVEPATH,$(INSTALLLOC)/$(RELEASE_NAME)/lib/shared)
INSTALLST := $(call NATIVEPATH,$(INSTALLLOC)/$(RELEASE_NAME)/lib/static)
INSTALLLI := $(call NATIVEPATH,$(INSTALLLOC)/$(RELEASE_NAME)/lib/linked)
Expand Down Expand Up @@ -209,6 +210,7 @@ $(DIRS):
$(WINNCHKDIR) $(call WINCHKPATH,$(INSTALLINC)) $(MKDIR) $(INSTALLINC)
$(WINNCHKDIR) $(call WINCHKPATH,$(INSTALLBF)) $(MKDIR) $(INSTALLBF)
$(WINNCHKDIR) $(call WINCHKPATH,$(INSTALLLL)) $(MKDIR) $(INSTALLLL)
$(WINNCHKDIR) $(call WINCHKPATH,$(INSTALLIO)) $(MKDIR) $(INSTALLIO)
$(WINNCHKDIR) $(call WINCHKPATH,$(INSTALLSH)) $(MKDIR) $(INSTALLSH)
$(WINNCHKDIR) $(call WINCHKPATH,$(INSTALLST)) $(MKDIR) $(INSTALLST)
$(WINNCHKDIR) $(call WINCHKPATH,$(INSTALLLI)) $(MKDIR) $(INSTALLLI)
Expand Down
1 change: 1 addition & 0 deletions src/core_makefile
Expand Up @@ -111,6 +111,7 @@ LINK_FILES += $(call TOLOWER,$(LINK_CSOURCES))
LINK_FILES += $(call TOLOWER,$(LINK_CPPSOURCES))
LINK_FILES += $(LINK_ASMSOURCES)
LINK_FILES += $(call NATIVEPATH,$(wildcard $(CEDEV)/lib/shared/*.src))
LINK_FILES += $(call NATIVEPATH,$(wildcard $(CEDEV)/lib/fileio/*.src))
LINK_LIBLOAD := $(call NATIVEPATH,$(wildcard $(CEDEV)/lib/libload/*.asm))

# check if there is an icon present that we can convert; if so, generate a recipe to build it properly
Expand Down
8 changes: 8 additions & 0 deletions src/std/fileio/fclose.c
@@ -0,0 +1,8 @@
#include <stdio.h>
#include <fileioc.h>

int fclose(FILE *stream) {
ti_var_t slot = stream->slot;
__stdio_files[slot].slot = 0;
return ti_Close(slot);
}
11 changes: 11 additions & 0 deletions src/std/fileio/feof.c
@@ -0,0 +1,11 @@
#include <stdio.h>
#include <fileioc.h>

int feof(FILE *stream) {
char a;
int sz;

sz = fread(&a, 1, 1, stream);
fseek(stream, -sz, SEEK_CUR);
return !sz;
}
6 changes: 6 additions & 0 deletions src/std/fileio/fgetc.c
@@ -0,0 +1,6 @@
#include <stdio.h>
#include <fileioc.h>

int fgetc(FILE *stream) {
return ti_GetC(stream->slot);
}
30 changes: 30 additions & 0 deletions src/std/fileio/fgets.c
@@ -0,0 +1,30 @@
#include <stdio.h>
#include <fileioc.h>

char *fgets(char *str, int num, FILE *stream) {
int c;
char *p = str;

if (num == 1) {
*p = 0;
return str;
}

for (; num > 1; num--) {
if ((c = fgetc(stream)) == EOF) {
break;
}
*p++ = c;
if (c == '\n') {
break;
}
}

if (p == str) {
return NULL;
}

*p = 0;

return str;
}
3 changes: 3 additions & 0 deletions src/std/fileio/files.c
@@ -0,0 +1,3 @@
#include <stdio.h>

FILE __stdio_files[FOPEN_MAX];
12 changes: 12 additions & 0 deletions src/std/fileio/fopen.c
@@ -0,0 +1,12 @@
#include <stdio.h>
#include <fileioc.h>

FILE *fopen(const char *filename, const char *mode) {
ti_var_t f = ti_Open(filename, mode);
if (!f) {
return NULL;
}
__stdio_files[(unsigned char)f].slot = f;

return &__stdio_files[f];
}
6 changes: 6 additions & 0 deletions src/std/fileio/fputc.c
@@ -0,0 +1,6 @@
#include <stdio.h>
#include <fileioc.h>

int fputc(int c, FILE *stream) {
return ti_PutC((char)c, stream->slot);
}
19 changes: 19 additions & 0 deletions src/std/fileio/fputs.c
@@ -0,0 +1,19 @@
#include <stdio.h>
#include <fileioc.h>

int fputs(const char *str, FILE *stream) {
unsigned char slot = stream->slot;
unsigned int i = 0;
char c;

while((c = str[i])) {
if(ti_PutC(c, slot) == EOF) {
return EOF;
}
i++;
}
if(ti_PutC('\n', slot) == EOF) {
return EOF;
}
return 1;
}
6 changes: 6 additions & 0 deletions src/std/fileio/fread.c
@@ -0,0 +1,6 @@
#include <stdio.h>
#include <fileioc.h>

size_t fread(void *ptr, size_t size, size_t count, FILE *stream) {
return ti_Read(ptr, size, count, stream->slot);
}
6 changes: 6 additions & 0 deletions src/std/fileio/fseek.c
@@ -0,0 +1,6 @@
#include <stdio.h>
#include <fileioc.h>

int fseek(FILE *stream, long int offset, int origin) {
return ti_Seek((int)offset, origin, stream->slot);
}
6 changes: 6 additions & 0 deletions src/std/fileio/ftell.c
@@ -0,0 +1,6 @@
#include <stdio.h>
#include <fileioc.h>

long int ftell(FILE *stream) {
return (long int)ti_Tell(stream->slot);
}
6 changes: 6 additions & 0 deletions src/std/fileio/fwrite.c
@@ -0,0 +1,6 @@
#include <stdio.h>
#include <fileioc.h>

size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream) {
return ti_Write(ptr, size, count, stream->slot);
}
36 changes: 36 additions & 0 deletions src/std/fileio/makefile
@@ -0,0 +1,36 @@
#----------------------------
# Makefile
#----------------------------

# common/os specific things
ifeq ($(OS),Windows_NT)
SHELL = cmd.exe
NATIVEPATH = $(subst /,\,$(1))
WINPATH = $(NATIVEPATH)
RM = del /f 2>nul
CEDEV ?= C:\CEdev
BIN ?= $(CEDEV)/bin
CC = $(call NATIVEPATH,$(BIN)/ez80cc.exe)
else
NATIVEPATH = $(subst \,/,$(1))
WINPATH = $(subst \,\\,$(shell winepath --windows $(1)))
RM = rm -f
CEDEV ?= $(HOME)/CEdev
BIN ?= $(CEDEV)/bin
CC = $(call NATIVEPATH,wine $(BIN)/ez80cc.exe)
endif

CCFLGS := -noasm -nodebug -nogenprint -nokeeplst -keepasm -promote -quiet -fplib -optsize -cpu:EZ80F91 -stdinc:"..;..\\..\\fileioc;..\\..\\ce" -define:_EZ80F91 -define:_EZ80

EZC := $(wildcard *.c)
EZSRC := $(EZC:%.c=%.src)

all: $(EZSRC)

%.src: %.c
$(CC) $(CCFLGS) $(call WINPATH,$<)

clean:
$(RM) $(EZSRC)

.PHONY: all clean
6 changes: 6 additions & 0 deletions src/std/fileio/remove.c
@@ -0,0 +1,6 @@
#include <stdio.h>
#include <fileioc.h>

int remove(const char *filename) {
return !ti_Delete(filename);
}
2 changes: 1 addition & 1 deletion src/std/linked/makefile
Expand Up @@ -20,7 +20,7 @@ BIN ?= $(CEDEV)/bin
CC = $(call NATIVEPATH,wine $(BIN)/ez80cc.exe)
endif

CCFLGS := -noasm -nodebug -nogenprint -nokeeplst -keepasm -promote -quiet -fplib -optsize -cpu:EZ80F91 -stdinc:.. -define:_EZ80F91 -define:_EZ80
CCFLGS := -noasm -nodebug -nogenprint -nokeeplst -keepasm -promote -quiet -fplib -optsize -cpu:EZ80F91 -stdinc:"..;..\\..\\fileioc;..\\..\\ce" -define:_EZ80F91 -define:_EZ80

EZC := $(wildcard *.c)
EZSRC := $(EZC:%.c=%.src)
Expand Down
7 changes: 7 additions & 0 deletions src/std/makefile
Expand Up @@ -16,6 +16,7 @@ CP = cp
PREFIX ?= $(HOME)
endif

FILEIODIR := $(call NATIVEPATH,$(CURDIR)/fileio)
STATICDIR := $(call NATIVEPATH,$(CURDIR)/static)
LINKEDDIR := $(call NATIVEPATH,$(CURDIR)/linked)
SHAREDDIR := $(call NATIVEPATH,$(CURDIR)/shared)
Expand All @@ -27,35 +28,41 @@ INSTALLLOC := $(call NATIVEPATH,$(DESTDIR)$(DEV))
LIB_LOC := $(call NATIVEPATH,$(INSTALLLOC)/lib)
LOC_H := $(call NATIVEPATH,$(INSTALLLOC)/include)

LIBFILEIO = $(call NATIVEPATH,$(wildcard $(FILEIODIR)/*.src))
LIBSTATIC = $(call NATIVEPATH,$(wildcard $(STATICDIR)/*.src))
LIBSHARED = $(call NATIVEPATH,$(wildcard $(SHAREDDIR)/*.src))
LIBLINKED = $(call NATIVEPATH,$(wildcard $(LINKEDDIR)/*.src))
LIB_H = $(call NATIVEPATH,$(wildcard *.h))

ifeq ($(OS),Windows_NT)
COPY_HEADERS = (robocopy . $(LOC_H) $(LIB_H) /njh /njs /ndl /nc /ns) ^& exit 0
COPY_LIBFILEIO = (robocopy $(FILEIODIR) $(call NATIVEPATH,$(LIB_LOC)/fileio) $(notdir $(LIBFILEIO)) /njh /njs /ndl /nc /ns) ^& exit 0
COPY_LIBSTATIC = (robocopy $(STATICDIR) $(call NATIVEPATH,$(LIB_LOC)/static) $(notdir $(LIBSTATIC)) /njh /njs /ndl /nc /ns) ^& exit 0
COPY_LIBSHARED = (robocopy $(SHAREDDIR) $(call NATIVEPATH,$(LIB_LOC)/shared) $(notdir $(LIBSHARED)) /njh /njs /ndl /nc /ns) ^& exit 0
COPY_LIBLINKED = (robocopy $(LINKEDDIR) $(call NATIVEPATH,$(LIB_LOC)/linked) $(notdir $(LIBLINKED)) /njh /njs /ndl /nc /ns) ^& exit 0
else
COPY_HEADERS = $(CP) $(LIB_H) $(LOC_H)
COPY_LIBFILEIO = $(CP) $(LIBFILEIO) $(call NATIVEPATH,$(LIB_LOC)/fileio)
COPY_LIBSTATIC = $(CP) $(LIBSTATIC) $(call NATIVEPATH,$(LIB_LOC)/static)
COPY_LIBSHARED = $(CP) $(LIBSHARED) $(call NATIVEPATH,$(LIB_LOC)/shared)
COPY_LIBLINKED = $(CP) $(LIBLINKED) $(call NATIVEPATH,$(LIB_LOC)/linked)
endif

all:
$(MAKE) -C $(FILEIODIR) BIN=$(BIN)
$(MAKE) -C $(STATICDIR) BIN=$(BIN)
$(MAKE) -C $(LINKEDDIR) BIN=$(BIN)
$(MAKE) -C $(SHAREDDIR) BIN=$(BIN)

clean:
$(MAKE) -C $(FILEIODIR) $(MAKECMDGOALS)
$(MAKE) -C $(SHAREDDIR) $(MAKECMDGOALS)
$(MAKE) -C $(LINKEDDIR) $(MAKECMDGOALS)
$(MAKE) -C $(STATICDIR) $(MAKECMDGOALS)

install:
$(COPY_HEADERS)
$(COPY_LIBFILEIO)
$(COPY_LIBSTATIC)
$(COPY_LIBSHARED)
$(COPY_LIBLINKED)
Expand Down
2 changes: 1 addition & 1 deletion src/std/shared/makefile
Expand Up @@ -20,7 +20,7 @@ BIN ?= $(CEDEV)/bin
CC = $(call NATIVEPATH,wine $(BIN)/ez80cc.exe)
endif

CCFLGS := -noasm -nodebug -nogenprint -nokeeplst -keepasm -promote -quiet -fplib -optsize -cpu:EZ80F91 -stdinc:.. -define:_EZ80F91 -define:_EZ80
CCFLGS := -noasm -nodebug -nogenprint -nokeeplst -keepasm -promote -quiet -fplib -optsize -cpu:EZ80F91 -stdinc:"..;..\\..\\fileioc;..\\..\\ce" -define:_EZ80F91 -define:_EZ80

EZC := $(wildcard *.c)
EZSRC := $(EZC:%.c=%.src)
Expand Down
2 changes: 1 addition & 1 deletion src/std/static/makefile
Expand Up @@ -20,7 +20,7 @@ BIN ?= $(CEDEV)/bin
CC = $(call NATIVEPATH,wine $(BIN)/ez80cc.exe)
endif

CCFLGS := -noasm -nodebug -nogenprint -nokeeplst -keepasm -promote -quiet -fplib -optsize -cpu:EZ80F91 -stdinc:.. -define:_EZ80F91 -define:_EZ80
CCFLGS := -noasm -nodebug -nogenprint -nokeeplst -keepasm -promote -quiet -fplib -optsize -cpu:EZ80F91 -stdinc:"..;..\\..\\fileioc;..\\..\\ce" -define:_EZ80F91 -define:_EZ80

EZC := $(wildcard *.c)
EZSRC := $(EZC:%.c=%.src)
Expand Down
37 changes: 29 additions & 8 deletions src/std/stdio.h
@@ -1,24 +1,21 @@
/*
* Copyright (C) 1999-2008 by Zilog, Inc.
* All Rights Reserved
* Modified by Matt "MateoConLechuga" Waltz for TI84+CE platform
*/
#ifndef STDIO_H
#define STDIO_H

#include <stdarg.h>
#include <format.h>
#include <fileioc.h>

#ifdef __cplusplus
extern "C" {
#endif

#define L_tmpnam 12
#define FILENAME_MAX 9
#define SYS_OPEN 5 /* max open files */
#define FOPEN_MAX 5

#define SEEK_CUR 1
#define SEEK_END 2
#define SEEK_SET 0
#define SYS_OPEN 5 /* max open files */
#define FOPEN_MAX SYS_OPEN

/* ANSI prototypes */
#ifndef SIZE_T_DEFINED
Expand All @@ -34,10 +31,34 @@ typedef unsigned int size_t;
#define EOF (-1) /* end of file */
#endif

typedef struct __stdio_file {
unsigned char slot;
} FILE;

extern FILE __stdio_files[FOPEN_MAX];

int sprintf(char *s, const char *format, ...);
int vprintf(const char *format, va_list arg);
int vsprintf(char *s, const char *format, va_list arg);

/* stdio.h functions implemented using FILEIOC */
int remove(const char *filename);
int fgetc(FILE *stream);
int fputc(int c, FILE *stream);
char *fgets(char *str, int num, FILE *stream);
int fputs(const char *str, FILE *stream);
size_t fread(void *ptr, size_t size, size_t count, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream);
int fseek(FILE *stream, long int offset, int origin);
long int ftell(FILE *stream);
int fclose(FILE *stream);
FILE *fopen(const char *filename, const char *mode);
int feof(FILE *stream);
#define ferror(stream) (0)
#define fflush(ignore) ((void)0)
#define rewind(stream) ti_Rewind(stream->slot)
#define fcloseall() ti_CloseAll()

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 51d2e41

Please sign in to comment.