配置文件的读取,纯C代码

来源:互联网 发布:js设置input只读 编辑:程序博客网 时间:2024/05/02 07:43

/*****************************strlist.h***************************/
#ifndef DS_STRING_LIST_H_
#define DS_STRING_LIST_H_
struct strlist_node {
  char key_[64]; //Store the data as key
  char value_[64]; //the value
  struct strlist_node* next;
  //struct strlist* prev;
};
struct strlist {
  int size_;
  struct strlist_node* head_;
};
struct strlist* strlist_create ();
void strlist_destroy (struct strlist* list);
int strlist_add (struct strlist* list, char const* d);
int strlist_add2 (struct strlist* list, char const* key, char const* value);
int strlist_remove (struct strlist* list, const char* key);
int strlist_size (struct strlist* list);
void strlist_clear (struct strlist* list);
///Get value of special item, if it is not existed return NULL
char const* strlist_value (struct strlist* list, char const* key);
#ifdef _DEBUG
void strlist_print ();
#endif
#endif
</p>
<p>
/****************************strlist.c******************************/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "strlist.h"
#include "common.h" ///define TRACE macro
struct strlist* strlist_create ()
{
  struct strlist* list = (struct strlist*) malloc (sizeof (struct strlist));
  list->size_ = 0;
  list->head_ = NULL;
  return list;
}
void strlist_destroy (struct strlist* list)
{
  strlist_clear (list);
  free (list);
}
int strlist_add (struct strlist* list, char const* d)
{
  char* key = NULL;
  char* ret = NULL;
  if (NULL == list)
    return 0;
  
  ret = strchr (d, ':');
  if (ret == d || ret == NULL || *(++ret) == 0)  ///Invalid datum format ignore it
    return 0;
  key = (char*) malloc (ret - d);
  strncpy (key, d, ret - d - 1);
  strlist_remove (list, key);
  struct strlist_node* node = (struct strlist_node*) malloc (sizeof (struct strlist_node));
  memset (node->key_, 0, 64);
  memset (node->value_, 0, 64);
  strncpy (node->key_, key, ret - d - 1);
  strcpy (node->value_, ret);
  TRACEN ("nAdd key  : %snand value: %sn", node->key_, node->value_);
  node->next = NULL;
  free (key);
  
  if (NULL == list->head_)
  {
    list->head_ = node;
    list->size_ = 1;
    return list->size_;
  }
  node->next = list->head_;
  list->head_ = node;
  ++(list->size_);
  return list->size_;
}
int strlist_add2 (struct strlist* list, char const* key, char const* value)
{
  if (NULL == list)
    return 0;
  strlist_remove (list, key);
  
  struct strlist_node* node = (struct strlist_node*) malloc (sizeof (struct strlist_node));
  memset (node->key_, 0, 64);
  memset (node->value_, 0, 64);
  strcpy (node->key_, key);
  strcpy (node->value_, value);
  node->next = NULL;
  
  if (NULL == list->head_)
  {
    list->head_ = node;
    list->size_ = 1;
    return list->size_;
  }
  node->next = list->head_;
  list->head_ = node;
  ++(list->size_);
  return list->size_;
}
int strlist_remove (struct strlist* list, char const* key)
{
  if (NULL == list || NULL == list->head_)
    return -1;
  struct strlist_node* work = list->head_;
  struct strlist_node* prev = work;
  while (work != NULL)
  {
    if (strcmp (work->key_, key) == 0)
    {
      break;
    }
    prev = work;
    work = work->next;
  }
  
  if (NULL == work)
    return -1;
  prev->next = work->next;
  if (work == list->head_)
    list->head_ = work->next;
  
  free (work);
  
  return --(list->size_);
}
int strlist_size (struct strlist* list)
{
  return list->size_;
}
void strlist_clear (struct strlist* list)
{
  struct strlist_node* work = list->head_;
  while (work != NULL)
  {
    TRACE;
    struct strlist_node* p = work;
    work = work->next;
    free (p);
  }
  list->head_ = NULL;
  list->size_ = 0;
}
char const* strlist_value (struct strlist* list, char const* key)
{
  if (NULL == list || NULL == list->head_)
    return NULL;
  struct strlist_node* work = list->head_;
  while (work != NULL)
  {
    if (strcmp (work->key_, key) == 0)
    {
      return work->value_;
    }
    work = work->next;
  }
  
  return NULL;
}
#ifdef _DEBUG
void strlist_print (struct strlist* list)
{
  TRACE;
  if (NULL == list || NULL == list->head_)
    return;
  fprintf (stderr, "list's size is %dn", list->size_);
  struct strlist_node* work = list->head_;
  while (work != NULL)
  {
    fprintf (stderr, "%s = %sn", work->key_, work->value_);
    work = work->next;
  }
}
#endif
</p>
下面是读取,解析文件部分
<p>
/*******************read_config.h********************/
#ifndef DS_CONFIG_HANDLE_H_
#define DS_CONFIG_HANDLE_H_
#include <stdlib.h>
int config_load (char const* filename);
void config_clean ();
char const* config_string (char const* section, char const* key);
int config_integer (char const* section, char const* key);
float config_float (char const* section, char const* key);
//int config_save (char const* filename);
#endif //DS_CONFIG_HANDLE_H_
</p>
<p>
/***********************read_config.c************************/
#include "config_reader.h"
#include "strlist.h"
#include <stdio.h>
#include <string.h>
#include "common.h"
struct strlist* config_list = NULL;
char section[32];
int process_line (char key[], char value[], char const* line);
int config_load (char const* filename)
{
  char* buf_ = NULL;
  int read_ = 0;
  int size_ = 256;
  char* config_pos;
  char* config_offset;
  char line[100];
  char key[32];
  char value[64];
  int count = 0;
  
  if (NULL == filename)
    return -1;
  
  FILE* file = fopen (filename, "r");
  if (NULL == file)
    return -1;
  ///Read file into memory
  do {
    if (NULL != buf_)
      free (buf_);
    size_ *= 2;
    buf_ = (char*) malloc (size_);
    rewind (file);
    read_ = fread (buf_, 1, size_, file);
  } while (read_ >= size_);
  TRACE1 ("Read the file finished");
  fclose (file);
  
  config_pos = buf_;
  config_offset = buf_;
  ///parse the config file now
  config_list = strlist_create ();
  
  while (*config_offset != 0)
  {
    if (*config_pos == 'n' || *config_pos == 0)
    {
      memset (line, 0, sizeof (line));
      strncpy (line, config_offset, config_pos - config_offset);
 
      TRACE1 (line);
      if (process_line (key, value, line))
      {
  TRACE;
  ++count;
  char tk[64];
  sprintf (tk, "%s-%s", section, key);
        strlist_add2 (config_list, tk, value);
      }
      
      config_offset = config_pos + 1;
    }
    ++config_pos;
  }
  free (buf_);
  
#ifdef _DEBUG
  strlist_print (config_list);
#endif
  return count;
}
int process_line (char key[], char value[], char const* line)
{
  char buf[100];
  char* r;
  char* w;
  memset (buf, 0, sizeof (buf));
  strcpy (buf, line);
  memset (key, 0, 32);
  memset (value, 0, 64);
  
  r = buf;
  while (*r != 0)
  {
    if (*r == ';')
      break;
    ++r;
  }
  while (*r != 0)
    *r++ = 0;
  r = buf;
  w = key;
  while (*r != 0)
  {
    if (*r == '=')
      break;
    //if (*r = ';' && w == key) ///If the first non space character is ';', the line is comment
      //return 0;
    if (*r != ' ')
      *w++ = *r;
    ++r;
  }
  TRACE1 (key);
  --w;
  
  if (*key == '[' &&  *(key + strlen (key) - 1) == ']')
  {
    memset (section, 0, 64);
    w = key;
    strncpy (section, key + 1, strlen (key) - 2);
    return 0;
  }
  ++r;
  while (*r != 0)
  {
    if (*r == ' ')
      ++r;
    else
      break;
  }
  strcpy (value, r);
  if (strlen (key) > 0 && strlen (value) > 0)
    return 1;
  return 0;
}
void config_clean ()
{
  strlist_destroy (config_list);
}
char const* config_string (char const* section, char const* key)
{
  char tk[64];
  sprintf (tk, "%s-%s", section, key);
  return strlist_value (config_list, tk);
}
int config_integer (char const* section, char const* key)
{
  char tk[64];
  sprintf (tk, "%s-%s", section, key);
  return atoi (strlist_value (config_list, tk));
}
float config_float (char const* section, char const* key)
{
  char tk[64];
  sprintf (tk, "%s-%s", section, key);
  return atof (strlist_value (config_list, tk));
}
//int config_save (char const* filename)
//{  

原创粉丝点击