⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 config.c

📁 linux subdivision ying gai ke yi le ba
💻 C
📖 第 1 页 / 共 2 页
字号:
static void
make_string_from_option (const char **valuep, svn_config_t *cfg,
                         cfg_section_t *section, cfg_option_t *opt,
                         apr_pool_t* x_pool)
{
  /* Expand the option value if necessary. */
  if (!opt->expanded)
    {
      apr_pool_t *tmp_pool = (x_pool ? x_pool : svn_pool_create (cfg->x_pool));

      expand_option_value (cfg, section, opt->value, &opt->x_value, tmp_pool);
      opt->expanded = TRUE;

      if (!x_pool)
        {
          /* Grab the fully expanded value from tmp_pool before its
             disappearing act. */
          if (opt->x_value)
            opt->x_value = apr_pstrmemdup (cfg->x_pool, opt->x_value,
                                           strlen (opt->x_value));
          svn_pool_destroy (tmp_pool);
        }
    }

  if (opt->x_value)
    *valuep = opt->x_value;
  else
    *valuep = opt->value;
}


/* Start of variable-replacement placeholder */
#define FMT_START     "%("
#define FMT_START_LEN (sizeof (FMT_START) - 1)

/* End of variable-replacement placeholder */
#define FMT_END       ")s"
#define FMT_END_LEN   (sizeof (FMT_END) - 1)


/* Expand OPT_VALUE in SECTION to *OPT_X_VALUE. If no variable
   replacements are done, set OPT_X_VALUE to NULL. Allocate from
   X_POOL */
static void
expand_option_value (svn_config_t *cfg, cfg_section_t *section,
                     const char *opt_value, const char **opt_x_valuep,
                     apr_pool_t *x_pool)
{
  svn_stringbuf_t *buf = NULL;
  const char *parse_from = opt_value;
  const char *copy_from = parse_from;
  const char *name_start, *name_end;

  while (parse_from != NULL
         && *parse_from != '\0'
         && (name_start = strstr (parse_from, FMT_START)) != NULL)
    {
      name_start += FMT_START_LEN;
      if (*name_start == '\0')
        /* FMT_START at end of opt_value. */
        break;

      name_end = strstr (name_start, FMT_END);
      if (name_end != NULL)
        {
          cfg_option_t *x_opt;
          apr_size_t len = name_end - name_start;
          char *name = apr_pstrmemdup (x_pool, name_start, len);

          x_opt = find_option (cfg, section->name, name, NULL);

          if (x_opt != NULL)
            {
              const char *cstring;

              /* Pass back the sub-pool originally provided by
                 make_string_from_option() as an indication of when it
                 should terminate. */
              make_string_from_option (&cstring, cfg, section, x_opt, x_pool);

              /* Append the plain text preceding the expansion. */
              len = name_start - FMT_START_LEN - copy_from;
              if (buf == NULL)
                {
                  buf = svn_stringbuf_ncreate (copy_from, len, x_pool);
                  cfg->x_values = TRUE;
                }
              else
                svn_stringbuf_appendbytes(buf, copy_from, len);

              /* Append the expansion and adjust parse pointers. */
              svn_stringbuf_appendcstr (buf, cstring);
              parse_from = name_end + FMT_END_LEN;
              copy_from = parse_from;
            }
          else
            /* Though ConfigParser considers the failure to resolve
               the requested expansion an exception condition, we
               consider it to be plain text, and look for the start of
               the next one. */
            parse_from = name_end + FMT_END_LEN;
        }
      else
        /* Though ConfigParser treats unterminated format specifiers
           as an exception condition, we consider them to be plain
           text.  The fact that there are no more format specifier
           endings means we're done parsing. */
        parse_from = NULL;
    }

  if (buf != NULL)
    {
      /* Copy the remainder of the plain text. */
      svn_stringbuf_appendcstr (buf, copy_from);
      *opt_x_valuep = buf->data;
    }
  else
    *opt_x_valuep = NULL;
}



void
svn_config_get (svn_config_t *cfg, const char **valuep,
                const char *section, const char *option,
                const char *default_value)
{
  if (cfg)
    {
      cfg_section_t *sec;
      cfg_option_t *opt = find_option (cfg, section, option, &sec);
      if (opt != NULL)
        {
          make_string_from_option (valuep, cfg, sec, opt, NULL);
        }
      else
        {
          apr_pool_t *tmp_pool = svn_pool_create (cfg->x_pool);
          const char *x_default;
          expand_option_value (cfg, sec, default_value, &x_default, tmp_pool);
          if (x_default)
            {
              svn_stringbuf_set (cfg->tmp_value, x_default);
              *valuep = cfg->tmp_value->data;
            }
          else
            *valuep = default_value;
          svn_pool_destroy (tmp_pool);
        }
    }
  else
    {
      *valuep = default_value;
    }
}



void
svn_config_set (svn_config_t *cfg,
                const char *section, const char *option,
                const char *value)
{
  cfg_section_t *sec;
  cfg_option_t *opt;

  remove_expansions (cfg);

  opt = find_option (cfg, section, option, &sec);
  if (opt != NULL)
    {
      /* Replace the option's value. */
      opt->value = apr_pstrdup (cfg->pool, value);
      opt->expanded = FALSE;
      return;
    }

  /* Create a new option */
  opt = apr_palloc (cfg->pool, sizeof (*opt));
  opt->name = apr_pstrdup (cfg->pool, option);
  opt->hash_key = make_hash_key (apr_pstrdup (cfg->pool, option));

  opt->value = apr_pstrdup (cfg->pool, value);
  opt->x_value = NULL;
  opt->expanded = FALSE;

  if (sec == NULL)
    {
      /* Even the section doesn't exist. Create it. */
      sec = apr_palloc (cfg->pool, sizeof (*sec));
      sec->name = apr_pstrdup (cfg->pool, section);
      sec->hash_key = make_hash_key (apr_pstrdup (cfg->pool, section));
      sec->options = apr_hash_make (cfg->pool);
      apr_hash_set (cfg->sections, sec->hash_key, APR_HASH_KEY_STRING, sec);
    }

  apr_hash_set (sec->options, opt->hash_key, APR_HASH_KEY_STRING, opt);
}



svn_error_t *
svn_config_get_bool (svn_config_t *cfg, svn_boolean_t *valuep,
                     const char *section, const char *option,
                     svn_boolean_t default_value)
{
  const char *tmp_value;

  svn_config_get (cfg, &tmp_value, section, option, NULL);
  if (tmp_value == NULL)
    *valuep = default_value;
  else if (0 == strcasecmp (tmp_value, SVN_CONFIG_TRUE)
           || 0 == strcasecmp (tmp_value, "yes")
           || 0 == strcasecmp (tmp_value, "on")
           || 0 == strcmp (tmp_value, "1"))
    *valuep = TRUE;
  else if (0 == strcasecmp (tmp_value, SVN_CONFIG_FALSE)
           || 0 == strcasecmp (tmp_value, "no")
           || 0 == strcasecmp (tmp_value, "off")
           || 0 == strcmp (tmp_value, "0"))
    *valuep = FALSE;
  else
    return svn_error_createf (SVN_ERR_RA_DAV_INVALID_CONFIG_VALUE, NULL,
                              "Config error: invalid boolean value '%s'",
                              tmp_value);

  return SVN_NO_ERROR;
}



void
svn_config_set_bool (svn_config_t *cfg,
                     const char *section, const char *option,
                     svn_boolean_t value)
{
  svn_config_set (cfg, section, option,
                  (value ? SVN_CONFIG_TRUE : SVN_CONFIG_FALSE));
}



int
svn_config__enumerate_sections (svn_config_t *cfg,
                               svn_config__section_enumerator_t callback,
                                void *baton)
{
  return svn_config_enumerate_sections (cfg,
           (svn_config_section_enumerator_t) callback, baton);
}

int
svn_config_enumerate_sections (svn_config_t *cfg,
                               svn_config_section_enumerator_t callback,
                               void *baton)
{
  apr_hash_index_t *sec_ndx;
  int count = 0;

  for (sec_ndx = apr_hash_first (cfg->x_pool, cfg->sections);
       sec_ndx != NULL;
       sec_ndx = apr_hash_next (sec_ndx))
    {
      void *sec_ptr;
      cfg_section_t *sec;

      apr_hash_this (sec_ndx, NULL, NULL, &sec_ptr);
      sec = sec_ptr;
      ++count;
      if (!callback (sec->name, baton))
        break;
    }

  return count;
}



int
svn_config_enumerate (svn_config_t *cfg, const char *section,
                      svn_config_enumerator_t callback, void *baton)
{
  cfg_section_t *sec;
  apr_hash_index_t *opt_ndx;
  int count;

  find_option (cfg, section, NULL, &sec);
  if (sec == NULL)
    return 0;

  count = 0;
  for (opt_ndx = apr_hash_first (cfg->x_pool, sec->options);
       opt_ndx != NULL;
       opt_ndx = apr_hash_next (opt_ndx))
    {
      void *opt_ptr;
      cfg_option_t *opt;
      const char *temp_value;

      apr_hash_this (opt_ndx, NULL, NULL, &opt_ptr);
      opt = opt_ptr;

      ++count;
      make_string_from_option (&temp_value, cfg, sec, opt, NULL);
      if (!callback (opt->name, temp_value, baton))
        break;
    }

  return count;
}



/* Baton for search_groups() */
struct search_groups_baton
{
  const char *key;          /* Provided by caller of svn_config_find_group */
  const char *match;        /* Filled in by search_groups */
  apr_pool_t *pool;
};


/* This is an `svn_config_enumerator_t' function, and BATON is a
 * `struct search_groups_baton *'.
 */
static svn_boolean_t search_groups (const char *name,
                                    const char *value,
                                    void *baton)
{
  struct search_groups_baton *b = baton;
  apr_array_header_t *list;

  list = svn_cstring_split (value, ",", TRUE, b->pool);
  if (svn_cstring_match_glob_list (b->key, list))
    {
      /* Fill in the match and return false, to stop enumerating. */
      b->match = apr_pstrdup (b->pool, name);
      return FALSE;
    }
  else
    return TRUE;
}


const char *svn_config_find_group (svn_config_t *cfg, const char *key,
                                   const char *master_section,
                                   apr_pool_t *pool)
{
  struct search_groups_baton gb;

  gb.key = key;
  gb.match = NULL;
  gb.pool = pool;
  svn_config_enumerate (cfg, master_section, search_groups, &gb);
  return gb.match;
}


const char*
svn_config_get_server_setting (svn_config_t *cfg,
                               const char* server_group,
                               const char* option_name,
                               const char* default_value)
{
  const char *retval;
  svn_config_get (cfg, &retval, SVN_CONFIG_SECTION_GLOBAL,
                  option_name, default_value);
  if (server_group)
    {
      svn_config_get (cfg, &retval, server_group, option_name, retval);
    }
  return retval;
}

svn_error_t*
svn_config_get_server_setting_int (svn_config_t *cfg,
                                   const char *server_group,
                                   const char *option_name,
                                   apr_int64_t default_value,
                                   apr_int64_t *result_value,
                                   apr_pool_t *pool)
{
  const char* tmp_value;
  char *end_pos;

  tmp_value = svn_config_get_server_setting (cfg, server_group,
                                             option_name, NULL);
  if (tmp_value == NULL)
    *result_value = default_value;
  else
    {
      /* read tmp_value as an int now */
      *result_value = apr_strtoi64 (tmp_value, &end_pos, 0);

      if (*end_pos != 0)
        {
          return svn_error_createf (SVN_ERR_RA_DAV_INVALID_CONFIG_VALUE, NULL,
                                    "Config error: invalid integer value '%s'",
                                    tmp_value);
        }
    }

  return SVN_NO_ERROR;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -