feat: move file2char to library
This commit is contained in:
parent
cc9d6f3526
commit
4bda62f7d0
|
@ -0,0 +1,87 @@
|
||||||
|
/*to use exit*/
|
||||||
|
#include <stdlib.h>
|
||||||
|
/*to use *print**/
|
||||||
|
#include <stdio.h>
|
||||||
|
/*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;
|
||||||
|
}
|
|
@ -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 */
|
107
file2char/main.c
107
file2char/main.c
|
@ -2,105 +2,11 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
/*to use *print**/
|
/*to use *print**/
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
/*to user file2char*/
|
||||||
/*
|
#include "file2char.h"
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv){
|
int main(int argc, char** argv){
|
||||||
/*declaration*/
|
/*declaration*/
|
||||||
int fileSize;
|
|
||||||
char *buf;
|
char *buf;
|
||||||
|
|
||||||
fprintf(stdout,"main() Started...\n");
|
fprintf(stdout,"main() Started...\n");
|
||||||
|
@ -109,11 +15,14 @@ int main(int argc, char** argv){
|
||||||
fprintf(stderr, "Usage: %s <file path>\n", argv[0]);
|
fprintf(stderr, "Usage: %s <file path>\n", argv[0]);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
fprintf(stdout,"main() argv[1]: %s.\n",argv[1]);
|
||||||
|
|
||||||
|
if(!convert_file2char(argv[1], &buf)){
|
||||||
fileSize = load_file(argv[1], &buf);
|
fprintf(stderr,"main() Error calling convert_file2char().\n");
|
||||||
|
}else{
|
||||||
|
fprintf(stdout,"main() convert_file2char() done.\n");
|
||||||
|
}
|
||||||
free(buf);
|
free(buf);
|
||||||
fprintf(stdout,"main() fileSize: %d.\n",fileSize);
|
|
||||||
fprintf(stdout,"main() Done.\n");
|
fprintf(stdout,"main() Done.\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
# command
|
# command
|
||||||
#
|
#
|
||||||
RM = /bin/rm -f
|
RM = /bin/rm -f
|
||||||
OBJ = main.o
|
OBJ = main.o file2char.o
|
||||||
EXE = main
|
EXE = main
|
||||||
CC = /usr/bin/gcc
|
CC = /usr/bin/gcc
|
||||||
CFLAGS = -Wall
|
CFLAGS = -Wall
|
||||||
|
@ -12,7 +12,10 @@ all: $(EXE)
|
||||||
$(EXE): $(OBJ)
|
$(EXE): $(OBJ)
|
||||||
$(CC) $(CFLAGS) $(OBJ) -o $(EXE)
|
$(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
|
$(CC) -c main.c -o main.o
|
||||||
#
|
#
|
||||||
clean:
|
clean:
|
||||||
|
|
Loading…
Reference in New Issue