avr-fw-modules/core/include/stdio.h

145 lines
2.6 KiB
C

#pragma once
#if 0
#include_next <stdio.h>
#else
#define __HWO_STDIO__
#include <stdlib.h>
#include <sys/threads.h>
struct _file;
typedef struct _file FILE;
typedef int (*fdevput)(char,FILE*);
typedef int (*fdevget)(FILE*);
typedef int (*fdevreadblock)(FILE *,void *,int);
typedef int (*fdevwriteblock)(FILE *,void *,int);
#define _FDEV_ERR -1
#define _FDEV_EOF -2
struct _file {
fdevput put;
fdevget get;
fdevreadblock
readblock;
fdevwriteblock
writeblock;
thread_t *notify;
void *udata;
char uappend[0];
};
FILE* fdev_create(int appendsize,fdevput put,fdevget get,fdevreadblock readblock,fdevwriteblock writeblock);
int fdev_notify(FILE* f,thread_t* notify);
static inline int fputc(int ch, FILE *stream) {
if ((stream == NULL) || (stream->put == NULL)) {
return -1;
};
return stream->put( ch, stream );
};
//#define fgetc(stream) (stream->get(stream))
static inline int fgetc(FILE *stream) {
int ch;
if ((stream == NULL) || (stream->get == NULL)) {
return -1;
};
ch = stream->get( stream );
return ch;
};
#define fdev_get_udata(stream) (stream->udata)
#define fdev_set_udata(stream,u) (stream->udata = u)
#define fprintf(...)
#define clearerr(stream)
int sprintf(char *dst,const char* fmt,...);
#endif
#ifndef __DONT_WRAP_STDLIB__
#include <hwo/threads.h>
#define IO_BLOCK_RD 0x01
#define IO_BLOCK_WR 0x02
#define IO_BLOCK (IO_BLOCK_RD | IO_BLOCK_WR)
FILE* dev_null(void);
FILE* pipe(int size); // Erzeuge einen FIFO der Grösse <size> Bytes
FILE* usart_open(int usart);
FILE* fdevopen_ts (int(*put)(char, FILE *),int(*get )(FILE *));
int fclose_ts(FILE* file);
#define fdevopen(__put,__get) fdevopen_ts(__put,__get)
#define fclose(file) fclose_ts(file)
//#define fprintf(file,format,...) sprintf((char*)local_buffer(),format, ## __VA_ARGS__ ); fputs( (char*)local_buffer(), file )
static inline int fputsn(char *buffer,int len,FILE *stream)
{
int n;
if (stream){
if (stream->writeblock){
return stream->writeblock(stream,buffer,len);
};
for (n=0;n<len;n++)
if (fputc( buffer[n], stream ))
break;
return n;
};
return -1;
};
static inline int fputs(char *buffer,FILE *stream)
{
while (*(buffer++))
fputc(*buffer, stream);
return 0;
};
static inline int read(FILE* stream,char *buffer,int len) {
int l = len;
while (l--)
{
uint8_t ch = fgetc( stream );
if (ch == -1)
break;
*(buffer++) = ch;
};
return len - l;
};
static inline int write(FILE* stream,char *buffer,int len) {
int l = len;
while (l--)
{
if (fputc( *(buffer++) , stream ))
break;
};
return len - l;
};
#endif