diff options
author | alex <alex@ASUS> | 2018-09-19 14:00:54 +0200 |
---|---|---|
committer | alex <alex@ASUS> | 2018-09-19 14:00:54 +0200 |
commit | 545b9dabd4e72c291884367621d0cdfe2da469d2 (patch) | |
tree | 4471839a2f2486c27c25931f1bbf5c20a6d1fc35 | |
parent | c9c4bacd594ed5f56c8f64e7fc899f1eeadb7e58 (diff) |
Print files to a string which can be used in many ways
-rw-r--r-- | libalx/inc/alx_file.h | 2 | ||||
-rw-r--r-- | libalx/src/alx_file.c | 56 | ||||
-rw-r--r-- | modules/about/src/about.c | 49 |
3 files changed, 95 insertions, 12 deletions
diff --git a/libalx/inc/alx_file.h b/libalx/inc/alx_file.h index 951d98c..352d422 100644 --- a/libalx/inc/alx_file.h +++ b/libalx/inc/alx_file.h @@ -13,7 +13,7 @@ /******************************************************************************
******* functions ************************************************************
******************************************************************************/
-void alx_prn_file (const char *filepath);
+void alx_snprint_file (char *dest, int destsize, const char *filepath);
/******************************************************************************
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);
}
diff --git a/modules/about/src/about.c b/modules/about/src/about.c index 0f5ed88..6df46fc 100644 --- a/modules/about/src/about.c +++ b/modules/about/src/about.c @@ -24,6 +24,20 @@ /******************************************************************************
+ ******* 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
+
+
+/******************************************************************************
******* variables ************************************************************
******************************************************************************/
char share_path [FILENAME_MAX];
@@ -43,56 +57,81 @@ void about_init (void) void print_cpright (void)
{
char file_name [FILENAME_MAX];
+ char str [BUFF_SIZE_TEXT];
strcpy(file_name, share_path);
strcat(file_name, "/");
strcat(file_name, "COPYRIGHT.txt");
- alx_prn_file(file_name);
+ alx_snprint_file(str, BUFF_SIZE_TEXT, file_name);
+
+ printf(BEGINNING);
+ printf("%s", str);
+ printf(ENDING);
}
void print_disclaim (void)
{
char file_name [FILENAME_MAX];
+ char str [BUFF_SIZE_TEXT];
strcpy(file_name, share_path);
strcat(file_name, "/");
strcat(file_name, "DISCLAIMER.txt");
- alx_prn_file(file_name);
+ alx_snprint_file(str, BUFF_SIZE_TEXT, file_name);
+
+ printf(BEGINNING);
+ printf("%s", str);
+ printf(ENDING);
}
void print_help (void)
{
char file_name [FILENAME_MAX];
+ char str [BUFF_SIZE_TEXT];
strcpy(file_name, share_path);
strcat(file_name, "/");
strcat(file_name, "HELP.txt");
- alx_prn_file(file_name);
+ alx_snprint_file(str, BUFF_SIZE_TEXT, file_name);
+
+ printf(BEGINNING);
+ printf("%s", str);
+ printf(ENDING);
}
void print_license (void)
{
char file_name [FILENAME_MAX];
+ char str [BUFF_SIZE_TEXT];
strcpy(file_name, share_path);
strcat(file_name, "/");
strcat(file_name, "LICENSE.txt");
- alx_prn_file(file_name);
+ alx_snprint_file(str, BUFF_SIZE_TEXT, file_name);
+
+ printf(BEGINNING);
+ printf("%s", str);
+ printf(ENDING);
}
void print_usage (void)
{
char file_name [FILENAME_MAX];
+ char str [BUFF_SIZE_TEXT];
strcpy(file_name, share_path);
strcat(file_name, "/");
strcat(file_name, "USAGE.txt");
- alx_prn_file(file_name);
+ alx_snprint_file(str, BUFF_SIZE_TEXT, file_name);
+
+ printf(BEGINNING);
+ printf("%s", str);
+ printf(ENDING);
}
void print_version (void)
|