Custom file struct

This commit is contained in:
2024-02-11 18:02:22 -06:00
parent f680fe177d
commit 72c117b388
5 changed files with 99 additions and 3 deletions

75
src/ncdgfile.c Normal file
View File

@@ -0,0 +1,75 @@
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ncdgfile.h>
static int fileputc(struct ncdgfile *file, int c);
static int fileputs(struct ncdgfile *file, char *s);
static void filefree(struct ncdgfile *file);
static int stringputc(struct ncdgfile *file, int c);
static int stringputs(struct ncdgfile *file, char *s);
static void stringfree(struct ncdgfile *file);
struct ncdgfile *file2ncdg(FILE *file) {
struct ncdgfile *ret;
if ((ret = malloc(sizeof *ret)) == NULL) {
return NULL;
}
ret->putc = fileputc;
ret->puts = fileputs;
ret->free = filefree;
ret->handle = (void *) file;
return ret;
}
struct ncdgfile *stringfile(void) {
struct ncdgfile *ret;
if ((ret = malloc(sizeof *ret)) == NULL) {
goto error1;
}
if ((ret->handle = (void *) newstring()) == NULL) {
goto error2;
}
ret->putc = stringputc;
ret->puts = stringputs;
ret->free = stringfree;
return ret;
error2:
free(ret);
error1:
return NULL;
}
static int fileputc(struct ncdgfile *file, int c) {
return fputc(c, (FILE *) file->handle) == EOF ? -1 : 0;
}
static int fileputs(struct ncdgfile *file, char *s) {
return fputs(s, (FILE *) file->handle) == EOF ? -1 : 0;
}
static void filefree(struct ncdgfile *file) {
fclose((FILE *) file->handle);
free(file);
}
static int stringputc(struct ncdgfile *file, int c) {
return appendchar((struct string *) file->handle, c) ? -1 : 0;
}
static int stringputs(struct ncdgfile *file, char *s) {
long i;
for (i = 0; s[i] != '\0'; ++i) {
if (appendchar((struct string *) file->handle, s[i])) {
return -1;
}
}
return 0;
}
static void stringfree(struct ncdgfile *file) {
freestring((struct string *) file->handle);
free(file);
}