2022年5月15日 星期日

[C/C++] 存取參數變數範例

說明:
存取參數變數範例. 例如setenv/getenv, set property/get property.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>

#define BOOL                    int
#define TRUE                    0
#define FALSE                   -1
#define MAX_LONG_LENGTH         1024
#define MAX_SHORT_LENGTH        128

typedef struct ConfigParam
{
    char   argument[MAX_SHORT_LENGTH];
    char   option[MAX_SHORT_LENGTH];
    struct ConfigParam *next;
}ConfigParamS;

BOOL is_file_exist_fun(char *filename)
{
    int bRet = FALSE;
    FILE* fp = fopen(filename, "r");
    if (fp) 
    {
        bRet = TRUE;
        fclose(fp);
    }
    return bRet;
}

BOOL load_file_fun(char *filename, char **p_content)
{
    int fd = -1;    
    int totalsize = 0;
    int readsize = 0;
    char buffer[MAX_SHORT_LENGTH];
    BOOL ret = FALSE;
    
    if ( (fd = open((char *)filename, O_RDONLY)) > 0 )
    {
        totalsize = 0;
        while( (readsize = read(fd, buffer, MAX_SHORT_LENGTH)) > 0)
            totalsize += readsize;

        if (totalsize <= 0)
        {
            printf("[ERROR] %s file size is wrong!\n", filename);
            return ret;
        }    
        
        if (lseek(fd, 0, SEEK_SET) == -1)
        {
            printf("[ERROR] %s can't seek!\n", filename);
            return ret;
        }
        
        *p_content = (char*)malloc( ( totalsize + 1) * sizeof(char));
        memset(*p_content, '\0', totalsize + 1);

        if (*p_content)
        {
            if(read(fd, *p_content, totalsize) > 0 )
            {
                ret = TRUE;
            }
            else
            {
                printf("[ERROR] %s can't read!\n", filename);
                free(*p_content);
            }

            close(fd);
        }
    }
    else
        printf("[ERROR] Open %s fail!\n", filename);

    return ret;    
}

BOOL save_file_fun(char *filename, char *buffer)
{
    int     fd = 0;
    int     length = 0;
    BOOL    ret = FALSE;

    length = strlen(buffer);
    
    if ( (fd = open( filename,
            O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) > 0 )
    {
        if ( write(fd, buffer, length) == length )
            ret = TRUE;
        else
            printf("[ERROR] write %s failed!\n", filename);

        if (fd) 
            close(fd);
        
        sync();
    }
    else
        printf("[ERROR] cannot creat %s!\n", filename);

    return ret;
}

void make_default_param_fun(ConfigParamS **p_argu)
{
    ConfigParamS *p_new;
    p_new = malloc(sizeof(ConfigParamS));
    memset(p_new->argument, '\0', sizeof(p_new->argument));
    memset(p_new->option, '\0', sizeof(p_new->option));
    p_new->next = NULL;
    *p_argu = p_new;
}

void get_param_option_fun(char *p_inargu, ConfigParamS **p_outargu)
{
    int nindex, nstart, nend;
    nindex = nstart = nend = 0;
    ConfigParamS *p_first, *p_current, *p_new;
    p_first = p_current = p_new = NULL;
    void *p_buffer = NULL; 
    p_buffer = malloc((strlen(p_inargu) + 1) * sizeof(char));
    char* p_bufferf = NULL;
    p_bufferf = (char *)p_buffer;
    strcpy(p_bufferf, p_inargu);

    while (nindex < strlen(p_inargu))
    {
        if (*(p_inargu + nindex) == '=')
        {
            make_default_param_fun(&p_new);
            if (p_first == NULL)
            {
                p_first = p_current = p_new;
            } else {
                p_current->next = p_new;
                p_current       = p_new;
            }
            
            nend = nindex;
            memcpy(p_current->argument, p_bufferf + nstart, nend - nstart);
            nstart = nindex + 1;
       }
       else if ( *(p_inargu + nindex) == '\n' || *(p_inargu + nindex + 1 ) == '\0')
       {
           nend = nindex;
           memcpy(p_current->option, p_bufferf + nstart, nend - nstart);
           nstart = nend + 1;
       }
       nindex ++;
    }
    *p_outargu = p_first;
}

void free_param_option_fun(ConfigParamS *p_inargu)
{
    ConfigParamS *p_current, *p_next;
    p_current = p_inargu;

    while(p_current)
    {
        p_next = p_current->next;
        free(p_current);
        p_current = p_next;
    }
}

BOOL set_config_value_fun(char *file, char *p_argument, char *p_option)
{
    char write_buffer[MAX_LONG_LENGTH];
    int ret = FALSE;
    char *p_buffer = NULL;
    ConfigParamS *p_outargu = NULL;
    ConfigParamS *p_tmp1 = NULL;
    ConfigParamS *p_tmp2 = NULL;
    
    memset(write_buffer, '\0', MAX_LONG_LENGTH);
    
    if (is_file_exist_fun(file) == TRUE)
        load_file_fun(file, &p_buffer);
    
    if (p_buffer)
    {
        get_param_option_fun(p_buffer, &p_outargu);
        if (p_outargu)
        {
            p_tmp1 = p_outargu;
            
            while(p_tmp1)
            {
                if (strcmp(p_tmp1->argument, p_argument) ==0)
                {
                    ret = TRUE;
                    strcpy(p_tmp1->option, p_option);
                    break;
                }
                p_tmp2 = p_tmp1;
                p_tmp1 = p_tmp1->next;
            }
            
            if (ret == FALSE)
            {
                ConfigParamS *p_new = malloc(sizeof(ConfigParamS));
                memset(p_new->argument, '\0', sizeof(p_new->argument));
                memset(p_new->option, '\0', sizeof(p_new->option));
                strcpy(p_new->argument, p_argument);
                strcpy(p_new->option, p_option);
                p_new->next = NULL;
                p_tmp2->next = p_new;
            }
            
            p_tmp1 = p_outargu;
            
            while(p_tmp1)
            {
                strcat(write_buffer, p_tmp1->argument);
                strcat(write_buffer, "=");
                strcat(write_buffer, p_tmp1->option);
                strcat(write_buffer, "\n");
                p_tmp1 = p_tmp1->next;
            }
            
            save_file_fun(file, write_buffer);
            
            free_param_option_fun(p_outargu);            
        }
        free(p_buffer);
    }  
    else
    {
        sprintf(write_buffer, "%s=%s\n", p_argument, p_option);
        save_file_fun(file, write_buffer);
    }
    
    return TRUE;
    
}

int get_config_value_fun(char *file, char *p_argument, char *p_option)
{
    int ret = FALSE;
    char *p_buffer = NULL;
    ConfigParamS *p_outargu = NULL;
    ConfigParamS *p_tmp1 = NULL;
    
    load_file_fun(file, &p_buffer);
    
    if (p_buffer)
    {
        get_param_option_fun(p_buffer, &p_outargu);
        if (p_outargu)
        {
            p_tmp1 = p_outargu;
            
            while(p_tmp1)
            {
                if (strcmp(p_tmp1->argument, p_argument) ==0)
                {
                    ret = TRUE;
                    strcpy((char *)p_option, p_tmp1->option);
                    break;
                }
                p_tmp1 = p_tmp1->next;
            }
            free_param_option_fun(p_outargu);            
        }
        
        free(p_buffer);
    }       
    
    return ret;
}

int main(int argc, char *argv[])
{
    char value[MAX_SHORT_LENGTH];
    
    set_config_value_fun("./test.conf", "AA", "123");
    set_config_value_fun("./test.conf", "BB", "456");
    set_config_value_fun("./test.conf", "CC", "456");
    
    if (get_config_value_fun("./test.conf", "BB", (char *)value) == TRUE)
        printf("BB : %s\n", value);
    else
        printf("[ERROR] get failed!\n");
    
    return 0;
}

Result:
BB : 456


沒有留言:

張貼留言