From 4bda62f7d09c76a43944fac74f9e6ea0890a5883 Mon Sep 17 00:00:00 2001 From: "Begerad, Stefan" Date: Fri, 20 Jan 2023 14:08:59 +0100 Subject: [PATCH] feat: move file2char to library --- file2char/file2char.c | 87 ++++++++++++++++++++++++++++++++++ file2char/file2char.h | 16 +++++++ file2char/main.c | 107 ++++-------------------------------------- file2char/makefile | 7 ++- 4 files changed, 116 insertions(+), 101 deletions(-) create mode 100644 file2char/file2char.c create mode 100644 file2char/file2char.h diff --git a/file2char/file2char.c b/file2char/file2char.c new file mode 100644 index 0000000..483a366 --- /dev/null +++ b/file2char/file2char.c @@ -0,0 +1,87 @@ +/*to use exit*/ +#include +/*to use *print**/ +#include +/*to user file2char*/ +#include "file2char.h" + +int convert_file2char(char const* path, char **buf){ + /*declarations*/ + FILE *fp; + long lSize; + + /*TODO Check path for validity!*/ + /*fopen() + *filename + *mode + *return: set errno value in error case + */ + fp=fopen(path,"r"); + + if(fp){ + fprintf(stdout,"file opened.\n"); + + /*set offset to file end*/ + /*fseek() + *stream + *offset + *origin + */ + fseek(fp,0L,SEEK_END); + if (ferror(fp)){ + printf ("Error seeking file\n"); + } + + /*get file size*/ + lSize = ftell( fp ); + fprintf(stdout,"lSize: %ld.\n",lSize); + + /*set offset to file start*/ + fseek(fp,0,SEEK_SET); + if (ferror(fp)){ + printf ("Error seeking file\n"); + } + + /*allocate memory for entire content + null termination*/ + *buf=(char *)malloc((lSize+1)*sizeof(char)); + if(!*buf){ + fclose(fp); + fputs("Error allocating memory",stderr); + exit(EXIT_FAILURE); + } + fprintf(stdout,"mem allocated.\n"); + + /*copy file into buffer*/ + /*fread() + *ptr + *size + *count + *stream + */ + if( 1!=fread(*buf,lSize,1,fp)){ + fclose(fp); + free(*buf); + fputs("Error reading entire file",stderr); + exit(EXIT_FAILURE); + } + fprintf(stdout,"file read.\n"); + + /*close file*/ + if(EOF==fclose(fp)){ + free(*buf); + exit(EXIT_FAILURE); + } + fprintf(stdout,"file closed.\n"); + + (*buf)[lSize] = '\0'; + fprintf(stdout,"mem null terminated.\n"); + + printf("buffer = %s\n", *buf); + + }else{ + perror(path); + exit(EXIT_FAILURE); + } + /*done*/ + return 0; +} diff --git a/file2char/file2char.h b/file2char/file2char.h new file mode 100644 index 0000000..0e244a0 --- /dev/null +++ b/file2char/file2char.h @@ -0,0 +1,16 @@ +/*We can use CPP tricks to avoid parsing the same header file more than once*/ +#ifndef FILE2CHAR_H +#define FILE2CHAR_H + +/* + * convert_file2char reads the file identified by 'path' into a character buffer + * pointed at by 'buf'. + * On success, 0 is returned. + * On failure, exit(EXIT_FAILURE) is returned. + * + * WARNING: convert_file2char malloc()s memory to '*buf' which must be freed by + * the caller. + */ +int convert_file2char(char const* path, char **buf); + +#endif /* FILE2CHAR_H */ diff --git a/file2char/main.c b/file2char/main.c index 8f7bb31..61234df 100644 --- a/file2char/main.c +++ b/file2char/main.c @@ -2,105 +2,11 @@ #include /*to use *print**/ #include - -/* - * load_file reads the file identified by 'path' into a character buffer - * pointed at by 'buf'. - * On success, the size of the file is returned. - * On failure, exit(EXIT_FAILURE) is returned and. - * - * WARNING: load_file malloc()s memory to '*buf' which must be freed by - * the caller. - */ -int load_file(char const* path, char **buf){ - /*declarations*/ - FILE *fp; - long lSize; - - /*fopen() - *filename - *mode - *return: set errno value in error case - */ - fp=fopen(path,"r"); - - if(fp){ - fprintf(stdout,"file opened.\n"); - - /*set offset to file end*/ - /*fseek() - *stream - *offset - *origin - */ - fseek(fp,0L,SEEK_END); - if (ferror(fp)){ - printf ("Error seeking file\n"); - } - - /*get file size*/ - lSize = ftell( fp ); - fprintf(stdout,"lSize: %ld.\n",lSize); - - /*set offset to file start*/ - fseek(fp,0,SEEK_SET); - if (ferror(fp)){ - printf ("Error seeking file\n"); - } - - /*allocate memory for entire content + null termination*/ - *buf=(char *)malloc((lSize+1)*sizeof(char)); - if(!*buf){ - fclose(fp); - fputs("Error allocating memory",stderr); - exit(EXIT_FAILURE); - } - fprintf(stdout,"mem allocated.\n"); - - /*copy file into buffer*/ - /*fread() - *ptr - *size - *count - *stream - */ - if( 1!=fread(*buf,lSize,1,fp)){ - fclose(fp); - free(*buf); - fputs("Error reading entire file",stderr); - exit(EXIT_FAILURE); - } - fprintf(stdout,"file read.\n"); - - /*close file*/ - if(EOF==fclose(fp)){ - free(*buf); - exit(EXIT_FAILURE); - } - fprintf(stdout,"file closed.\n"); - - (*buf)[lSize] = '\0'; - fprintf(stdout,"mem null terminated.\n"); - - /*TODO Cleanup debugging!*/ - /* - for (int i = 0; i < lSize; i++) { - printf("buffer[%d] == %c\n", i, buffer[i]); - } - */ - printf("buffer = %s\n", *buf); - - }else{ - perror(path); - exit(EXIT_FAILURE); - } - /*done*/ - return lSize; -} +/*to user file2char*/ +#include "file2char.h" int main(int argc, char** argv){ /*declaration*/ - int fileSize; char *buf; fprintf(stdout,"main() Started...\n"); @@ -109,11 +15,14 @@ int main(int argc, char** argv){ fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXIT_FAILURE); } + fprintf(stdout,"main() argv[1]: %s.\n",argv[1]); - - fileSize = load_file(argv[1], &buf); + if(!convert_file2char(argv[1], &buf)){ + fprintf(stderr,"main() Error calling convert_file2char().\n"); + }else{ + fprintf(stdout,"main() convert_file2char() done.\n"); + } free(buf); - fprintf(stdout,"main() fileSize: %d.\n",fileSize); fprintf(stdout,"main() Done.\n"); return 0; } diff --git a/file2char/makefile b/file2char/makefile index bcd60d7..b1d5360 100644 --- a/file2char/makefile +++ b/file2char/makefile @@ -2,7 +2,7 @@ # command # RM = /bin/rm -f -OBJ = main.o +OBJ = main.o file2char.o EXE = main CC = /usr/bin/gcc CFLAGS = -Wall @@ -12,7 +12,10 @@ all: $(EXE) $(EXE): $(OBJ) $(CC) $(CFLAGS) $(OBJ) -o $(EXE) # -main.o: main.c +file2char.o: file2char.c file2char.h + $(CC) -c file2char.c -o file2char.o +# +main.o: main.c file2char.h $(CC) -c main.c -o main.o # clean: