Example #1
0
File: keys.c Project: acklinr/tig
enum status_code
get_key_value(const char **name_ptr, struct key *key)
{
	const char *name = *name_ptr;
	const char *end = NULL;

	memset(key, 0, sizeof(*key));

	if (*name == '<') {
		end = strchr(name + 1, '>');
		if (!end)
			return error("Missing '>' from key mapping: %s", name);

		if (!prefixcmp(name, "<Ctrl-")) {
			key->modifiers.control = 1;
			return parse_key_value(key, name_ptr, 6, NULL, end);

		} else if (!prefixcmp(name, "<C-")) {
			key->modifiers.control = 1;
			return parse_key_value(key, name_ptr, 3, NULL, end);

		} else {
			const struct key_mapping *mapping;
			const char *start = name + 1;
			int len = end - start;

			mapping = get_key_mapping(start, len);
			if (!mapping)
				return error("Unknown key mapping: %.*s", len, start);

			if (strchr(" #<'\"", mapping->value)) {
				const char replacement[] = { mapping->value, 0 };

				return parse_key_value(key, name_ptr, 0, replacement, end);
			}

			*name_ptr = end + 1;
			key->data.value = mapping->value;
			return SUCCESS;
		}
	}

	if (name[0] == '^' && name[1] == '[') {
		return error("Escape key combo must now use '<Esc>%s' "
			     "instead of '%s'", name + 2, name);
	} else if (name[0] == '^' && name[1] != '\0') {
		return error("Control key mapping must now use '<Ctrl-%s>' "
			     "instead of '%s'", name + 1, name);
	}

	return parse_key_value(key, name_ptr, 0, NULL, end);
}
/**
 * anjuta_plugin_description_new_from_string:
 * @data: The data to parse. The format of the data is .ini style.
 *
 * Parses the given plugin description data (usally read from the plugin
 * description file and creates an instance of #AnjutaPluginDescription.
 * The format of the content string is similar to .ini format.
 *
 * Return value: a new #AnjutaPluginDescription object
 */
AnjutaPluginDescription *
anjuta_plugin_description_new_from_string (char *data, GError **error)
{
  AnjutaPluginDescriptionParser parser;

  parser.df = g_new0 (AnjutaPluginDescription, 1);
  parser.current_section = -1;

  parser.n_allocated_lines = 0;
  parser.n_allocated_sections = 0;
  parser.line_nr = 1;

  parser.line = data;

	/* Put any initial comments in a NULL segment */
	open_section (&parser, NULL);
	while (parser.line != NULL && strlen(parser.line))
	{ 
		if (*parser.line == '[') {
			if (!parse_section_start (&parser, error))
				return NULL;
		} else if (is_blank_line (&parser) ||
		           *parser.line == '#')
			parse_comment_or_blank (&parser);
		else
		{
			if (!parse_key_value (&parser, error))
				return NULL;
		}
	}
 
  return parser.df;
}
/**
 * gnome_theme_file_new_from_string:
 * @data:  the string used to create a #GnomeThemeFile.
 * @error: location to store the error occuring, or NULL to ignore errors 
 *
 * Creates a #GnomeThemeFile from the data string passed.
 * 
 * Returns: a #GnomeThemeFile.
 * 
 * Since: 2.2
 **/
GnomeThemeFile *
gnome_theme_file_new_from_string (char                       *data,
				  GError                    **error)
{
  GnomeThemeFileParser parser;

  parser.df = g_new0 (GnomeThemeFile, 1);
  parser.current_section = -1;

  parser.n_allocated_lines = 0;
  parser.n_allocated_sections = 0;
  parser.line_nr = 1;

  parser.line = data;

  /* Put any initial comments in a NULL segment */
  open_section (&parser, NULL);
  
  while (parser.line && *parser.line)
    {
      if (*parser.line == '[') {
	if (!parse_section_start (&parser, error))
	  return NULL;
      } else if (is_blank_line (&parser) ||
		 *parser.line == '#')
	parse_comment_or_blank (&parser);
      else
	{
	  if (!parse_key_value (&parser, error))
	    return NULL;
	}
    }

  return parser.df;
}
Example #4
0
struct key_value_st *read_local_conf(const char *filename)
{

        char    str[1024] = {0};
        int     ch;
        struct key_value_st stKeyValue;//存储
        struct key_value_st *p = &stKeyValue;

        if (NULL == filename || '\0' == *filename)
                return NULL;


        //打开配置文件
        FILE* fs = fopen(filename, "r");
        if (NULL == fs) 
        {
                perror("fopen");
                return NULL;
        }
        //逐行读取配置文件
        while (1) 
        {
                fgets(str, 1024, fs);
                //将末尾的'\n'换成'\0'
                str[strlen(str) - 1] = '\0';
                /* 去年str中所有的空格 */
                trim_str_all(str);

                if ( *str != '\0') 
                {
                        p->next = (struct key_value_st *)malloc(sizeof(struct key_value_st));
                        memset(p->next, 0, sizeof(stKeyValue));
                        if (parse_key_value(p->next, str) == 0) 
                        {
                                p = p->next;
                                p->next = NULL;
                                //printf("%s:%s\n", p->key, p->szValue);
                        } 
                        else 
                        {
                                free(p->next);
                                p->next = NULL;
                        }
                }
                //////////////////////
                memset(str, 0, sizeof(str));

                ch = fgetc(fs);
                if (EOF == ch)
                        break;

                ungetc(ch, fs);
        }

        fclose(fs);
        return (&stKeyValue)->next;
}
Example #5
0
static void parse_object(int indent, json_obj_t * object){
  if(object->type != JSON_OBJECT){
    printf("%d: unexpected type %d\n", __LINE__, object->type);
    return;
  }

  json_obj_t * object_members = object->children;

  while(object_members != NULL){
    parse_key_value(indent, object_members);
    object_members = object_members->next_sibling;
  }
}
Example #6
0
void ff_http_auth_handle_header(HTTPAuthState *state, const char *key,
                                const char *value)
{
    if (!strcmp(key, "WWW-Authenticate")) {
        const char *p;
        if (av_stristart(value, "Basic ", &p) &&
            state->auth_type <= HTTP_AUTH_BASIC) {
            state->auth_type = HTTP_AUTH_BASIC;
            state->realm[0] = 0;
            parse_key_value(p, handle_basic_params, state);
        } else if (av_stristart(value, "Digest ", &p) &&
                   state->auth_type <= HTTP_AUTH_DIGEST) {
            state->auth_type = HTTP_AUTH_DIGEST;
            memset(&state->digest_params, 0, sizeof(DigestParams));
            state->realm[0] = 0;
            parse_key_value(p, handle_digest_params, state);
            choose_qop(state->digest_params.qop,
                       sizeof(state->digest_params.qop));
        }
    } else if (!strcmp(key, "Authentication-Info")) {
        parse_key_value(value, handle_digest_update, state);
    }
}
Example #7
0
static struct htable *htable_from_str(const char *str)
{
    struct htable *ht;
    char *cstr = NULL, *saveptr = NULL, *tok;
    int ret = ENOMEM;

    ht = htable_alloc(8, ht_hash_string, ht_compare_string);
    if (!ht) {
        goto done;
    }
    if (!str) {
        ret = 0;
        goto done;
    }
    cstr = strdup(str);
    if (!cstr) {
        goto done;
    }

    for (tok = strtok_r(cstr, ";", &saveptr); tok;
             tok = strtok_r(NULL, ";", &saveptr)) {
        char *key = NULL, *val = NULL;
        ret = parse_key_value(tok, &key, &val);
        if (ret) {
            goto done;
        }
        ret = htable_put(ht, key, val);
        if (ret) {
            goto done;
        }
    }
    ret = 0;
done:
    if (ret) {
        htable_free(ht);
        ht = NULL;
    }
    free(cstr);
    return ht;
}
BusDesktopFile*
bus_desktop_file_load (DBusString *filename,
		       DBusError  *error)
{
  DBusString str;
  BusDesktopFileParser parser;
  DBusStat sb;

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  
  /* Clearly there's a race here, but it's just to make it unlikely
   * that we do something silly, we still handle doing it below.
   */
  if (!_dbus_stat (filename, &sb, error))
    return NULL;

  if (sb.size > _DBUS_ONE_KILOBYTE * 128)
    {
      dbus_set_error (error, DBUS_ERROR_FAILED,
                      "Desktop file size (%ld bytes) is too large", (long) sb.size);
      return NULL;
    }
  
  if (!_dbus_string_init (&str))
    return NULL;
  
  if (!_dbus_file_get_contents (&str, filename, error))
    {
      _dbus_string_free (&str);
      return NULL;
    }

  if (!_dbus_string_validate_utf8 (&str, 0, _dbus_string_get_length (&str)))
    {
      _dbus_string_free (&str);
      dbus_set_error (error, DBUS_ERROR_FAILED,
                      "invalid UTF-8");   
      return NULL;
    }
  
  parser.desktop_file = dbus_new0 (BusDesktopFile, 1);
  if (parser.desktop_file == NULL)
    {
      _dbus_string_free (&str);
      BUS_SET_OOM (error);
      return NULL;
    }
  
  parser.data = str;
  parser.line_num = 1;
  parser.pos = 0;
  parser.len = _dbus_string_get_length (&parser.data);
  parser.current_section = -1;

  while (parser.pos < parser.len)
    {
      if (_dbus_string_get_byte (&parser.data, parser.pos) == '[')
	{
	  if (!parse_section_start (&parser, error))
            {
              return NULL;
            }
	}
      else if (is_blank_line (&parser) ||
	       _dbus_string_get_byte (&parser.data, parser.pos) == '#')
	parse_comment_or_blank (&parser);
      else
	{
	  if (!parse_key_value (&parser, error))
            {
              return NULL;
            }
	}
    }

  _dbus_string_free (&parser.data);

  return parser.desktop_file;
}