ritwik12 / Virtual-Assistant

A linux based Virtual assistant on Artificial Intelligence in C
GNU General Public License v3.0
128 stars 95 forks source link

Reorgainize preprocessor macros to outside of main function calls #78

Closed aurochs-angels closed 4 years ago

aurochs-angels commented 4 years ago

I suggest moving the #include directives outside of function calls, given that they are text replacement by the preprocessor and not handled at runtime. In the process init_config.c can be reorganized with appropriate sanity checks and functions, as generally using an include directive in place of functions is unusual code style.

aurochs-angels commented 4 years ago

Current init_config.h

/**
 *  Define configuration functions
 * */

#include "stdio.h"
#include "string.h"
/***"Init funtions"***/
FILE *get_Config_File();
char *set_HomeDir_Var(FILE *conf);
char *set_MediaPlayer_Var(FILE *conf);
char *set_WebBrowser_Var(FILE *conf);
int config_Cleanup_Status(FILE *conf);
/***Configuration variables**/
char HOMEDIR[1000];
char WebBrowser[1000];
char MediaPlayer[1000];
/***Interface to other modules***/
char* getVar(char *varName);

Corresponding changes to init_config.c

/* 
 * File:   init_config.c
 *
 * Created on April 10, 2018, 9:18 AM
 */
#include "init_config.h"
#define DEBUG
char str[1000], *start, pv, location[1000], youtube[1000], songs[1000], cal[100], search[100];
int c, d, len = 0;
FILE *get_Config_File()
{
    FILE *fp;
    fp = fopen("config", "rw");
    //TODO:Search path for virtual_assistant.conf or similar.
    if (fp == NULL)
    {
        fprintf(stderr, "Unable to open config file\n");
        return NULL;
    }
    else
    {
        return fp;
    }
}
char *set_HomeDir_Var(FILE *conf)
{
    //Getting home directory out of configuration file
    int i = 0;
    char cfg_line[1000];
    cfg_line[i] = fgetc(conf);
    char *pos;
    i++;
    while (cfg_line[i - 1] != '\n' && cfg_line[i - 1] != EOF)
    {
        cfg_line[i] = fgetc(conf);
        i++;
    }
    cfg_line[i - 1] = '\0';
    pos = strchr(cfg_line, '=');

    pos = pos + 2;
    strcpy(HOMEDIR, pos);
#ifdef DEBUG
    printf("%s is home dir\r\n", HOMEDIR);
#endif
    return HOMEDIR;
}
char *set_MediaPlayer_Var(FILE *conf)
{
    //Get preferred media player from config file
    int i = 0;
    MediaPlayer[i] = fgetc(conf);
    i++;
    while (MediaPlayer[i - 1] != '\n' && MediaPlayer[i - 1] != EOF)
    {
        MediaPlayer[i] = fgetc(conf);
        i++;
    }

    MediaPlayer[i - 1] = '\0';
    char *M_P = strchr(MediaPlayer, '=');
    M_P = M_P + 2;
    strcpy(MediaPlayer, M_P);
#ifdef DEBUG
    printf("%s is MediaPlayer\r\n", MediaPlayer);
#endif
    return MediaPlayer;
}
char *set_WebBrowser_Var(FILE *conf)
{
    //Get preferred Webbrowser out of config file
    int i = 0;
    char *pos;
    WebBrowser[i] = fgetc(conf);
    i++;
    while (WebBrowser[i - 1] != '\n' && WebBrowser[i - 1] != EOF)
    {
        WebBrowser[i] = fgetc(conf);
        i++;
    }

    WebBrowser[i - 1] = '\0';
    pos = strchr(WebBrowser, '=');
    pos = pos + 2;
    strcpy(WebBrowser, pos);
#ifdef DEBUG
    printf("%s is webbrowser\r\n", WebBrowser);
#endif
    return WebBrowser;
}
//Clean up file pointer
//TODO: Proper error status codes & signals
int config_Cleanup_Status(FILE *conf)
{
    int status = 0;
    if (conf != NULL)
    {
        status = fclose(conf);
    }
    else
    {
        return 1;
    }
    return status;
}
char *getVar(char *varName)
{
    if (!strcmp(WebBrowser, varName))
    {
        return WebBrowser;
    }
    else if (!strcmp(MediaPlayer, varName))
    {
        return MediaPlayer;
    }
    else if (!strcmp(HOMEDIR, varName))
    {
        return HOMEDIR;
    }
    else
    {
        return NULL;
    }
}

Necessary alterations to main.c

 conffile = get_Config_File();
        HomeDir = set_HomeDir_Var(conffile);
        Mediaplay = set_MediaPlayer_Var(conffile);
        browser = set_WebBrowser_Var(conffile);
        retval = config_Cleanup_Status(conffile);
        /* Make sure initial configuration succeeded*/
        if (!retval && HomeDir && Mediaplay && browser)
        {
                //Inform user about preferred media player, as to config file
                char preferred_media_player[1000];
                sprintf(preferred_media_player, "say Your preferred media player is %s", Mediaplay);
                system(preferred_media_player); //TODO replace unsafe system call with environment controlled fork/popen style call 

                //Inform user about preferred web browser, as to config file
                char preferred_webbrowser[1000];
                sprintf(preferred_webbrowser, "say Your preferred webbrowser is %s", WebBrowser);
                system(preferred_webbrowser); //TODO replace unsafe system call with environment controlled fork/popen style call 
        }

Intended to contribute to an overall safer, more modular, and more platform agnostic implementation.

ritwik12 commented 4 years ago

@aurochs-angels I am not exactly getting the changes, I can't see the code for your Current init_config.h that you have mentioned at the beginning

ritwik12 commented 4 years ago

@aurochs-angels Please create a PR for this, it will be better understood that way

ritwik12 commented 4 years ago

Solved in #79, Thanks @aurochs-angels