summaryrefslogtreecommitdiffstats
path: root/libalx/src/alx_file.c
diff options
context:
space:
mode:
Diffstat (limited to 'libalx/src/alx_file.c')
-rw-r--r--libalx/src/alx_file.c56
1 files changed, 50 insertions, 6 deletions
diff --git a/libalx/src/alx_file.c b/libalx/src/alx_file.c
index fa0d6e3..26ab468 100644
--- a/libalx/src/alx_file.c
+++ b/libalx/src/alx_file.c
@@ -10,6 +10,7 @@
* * * Standard * * * * * *
* * * * * * * * * */
#include <errno.h>
+ #include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
@@ -22,32 +23,75 @@
/******************************************************************************
******* macros ***************************************************************
******************************************************************************/
+ # define BUFF_SIZE_TEXT (1048576)
+
+#if defined OS_LINUX
# define BEGINNING "\n┌──────────────────────────────────────────────────────────────────────────────┐\n"
# define ENDING "└──────────────────────────────────────────────────────────────────────────────┘\n\n"
+#elif defined OS_WIN
+ # define BEGINNING "\n________________________________________________________________________________\n"
+ # define ENDING "________________________________________________________________________________\n\n"
+#endif
# define ERR_FPTR_MSG "¡ FILE error !"
+ # define ERR_FSIZE_MSG "¡ FILE is too big !"
/******************************************************************************
******* main *****************************************************************
******************************************************************************/
-void alx_prn_file (const char *filepath)
+void alx_snprint_file (char *dest, int destsize, const char *filepath)
{
- int64_t c;
+ char buff [BUFF_SIZE_TEXT];
FILE *fp;
+ /* Open file */
fp = fopen(filepath, "r");
- printf(BEGINNING);
+
+#if 0
+ /* Implemented using getc instead of fread */
+ char *str;
+ int64_t c;
+
if (fp) {
- while ((c = getc(fp)) != EOF){
- putchar(c);
+ str = buff;
+ while (((c = getc(fp)) != EOF) && str < BUFF_SIZE_TEXT) {
+ sprintf(p, "%c", (char)c);
+ str++;
}
fclose(fp);
} else {
puts(ERR_FPTR_MSG);
printf(" errno = %i;\n", errno);
}
- printf(ENDING);
+#else
+ long len;
+ if (fp) {
+ /* File lenght */
+ fseek(fp, 0, SEEK_END);
+ len = ftell(fp);
+ fseek(fp, 0, SEEK_SET);
+
+ /* Read file into buff and append '\0' */
+ if (len < BUFF_SIZE_TEXT) {
+ fread(buff, sizeof(char), len, fp);
+ buff[len] = '\0';
+ } else {
+ /* Prevent buffer overflow */
+ puts(ERR_FSIZE_MSG);
+ }
+
+ /* Close file */
+ fclose(fp);
+ } else {
+ /* File error */
+ puts(ERR_FPTR_MSG);
+ printf(" errno = %i;\n", errno);
+ }
+#endif
+
+ /* Print into dest */
+ snprintf(dest, destsize, "%s", buff);
}