Memcached-libmemcached

 view release on metacpan or  search on metacpan

src/libmemcached/clients/ms_setting.c  view on Meta::CPAN

  }
  else
  {
    return CONF_NULL;
  }
} /* ms_get_conf_type */


/**
 * judge whether the line is a line with useful data. used to
 * parse the configuration file.
 *
 * @param line, string of one line
 *
 * @return if success, return EXIT_FAILURE, else return EXIT_SUCCESS
 */
static int ms_is_line_data(char *line)
{
  assert(line != NULL);

  char *begin_ptr= line;

  while (isspace(*begin_ptr))
  {
    begin_ptr++;
  }
  if ((begin_ptr[0] == '\0') || (begin_ptr[0] == '#'))
    return EXIT_SUCCESS;

  return EXIT_FAILURE;
} /* ms_is_line_data */


/**
 * function to bypass blank line and comments
 *
 * @param line, string of one line
 * @param nread, length of the line
 *
 * @return if it's EOF or not line data, return EXIT_SUCCESS, else return EXIT_FAILURE
 */
static int ms_read_is_data(char *line, ssize_t nread)
{
  if ((nread == EOF) || ! ms_is_line_data(line))
    return EXIT_SUCCESS;

  return EXIT_FAILURE;
} /* ms_read_is_data */


/**
 *  if no configuration file, use this function to create the default
 *  configuration file.
 */
static void ms_no_config_file()
{
  char userpath[PATH_MAX];
  struct passwd *usr= NULL;
  FILE *fd;

  usr= getpwuid(getuid());

  snprintf(userpath, PATH_MAX, "%s/%s", usr->pw_dir, DEFAULT_CONFIG_NAME);

  if (access (userpath, F_OK | R_OK) == 0)
    goto exit;

  fd= fopen(userpath, "w+");

  if (fd == NULL)
  {
    fprintf(stderr, "Could not create default configure file %s\n", userpath);
    perror(strerror(errno));
    exit(1);
  }
  fprintf(fd, "%s", DEFAULT_CONGIF_STR);
  fclose(fd);

exit:
  ms_setting.cfg_file= strdup(userpath);
} /* ms_no_config_file */


/**
 * parse the configuration file
 *
 * @param cfg_file, the configuration file name
 */
static void ms_parse_cfg_file(char *cfg_file)
{
  FILE *f;
  size_t start_len, end_len;
  double proportion;
  char *line= NULL;
  size_t  read_len;
  ssize_t nread;
  int cmd_type;
  ms_conf_type_t conf_type;
  int end_of_file= 0;
  ms_key_distr_t *key_distr= NULL;
  ms_value_distr_t *val_distr= NULL;

  if (cfg_file == NULL)
  {
    ms_no_config_file();
    cfg_file= ms_setting.cfg_file;
  }

  /*read key value configure file*/
  if ((f= fopen(cfg_file, "r")) == NULL)
  {
    fprintf(stderr, "Can not open file: '%s'.\n", cfg_file);
    exit(1);
  }

  while (1)
  {
    if ((((nread= getline(&line, &read_len, f)) == 1)
         || ! ms_read_is_data(line, nread)) && (nread != EOF)) /* bypass blank line */
      continue;



( run in 2.160 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )