在解码嵌套的元素列表时使用ei_decode_list

时间:2016-04-23 22:30:35

标签: c++ erlang

我有一个表单的嵌套列表,

{stack, "over", flow, ["is", "a"], ["knowledge", "sharing", "site"], "a", "b"}

我无法干净地解码上面的元组。我将上面的内容解码为

 vector<vector<string> >

但是,如果我将内部列表替换为元组,我可以解码那么好。我怀疑使用ERL_NIL_EXT和ERL_LIST_EXT但文档只是有点帮助。

想法是如果我在我的元组中得到列表,我正在解码它并返回一个字符串向量并且线性地行走元组。但我不确定这是错的。救命!

代码:

vector< vector<string> > decode_tuple(char *buff)
{
  vector< vector<string> > decoded_tuple;
  int index = 0;
  int size;
  int res = ei_decode_tuple_header(buff, &index, &size);

  for(int i = 0; i < size; ++i)
  {
    int entry_size;
    int type;
    int res = ei_get_type(buff, &index, &type, &entry_size);
    char *decoded_val = (char *) malloc(sizeof(char) * (entry_size + 1));
    switch (type)
    {
        case ERL_ATOM_EXT:
        {
            vector<string> value;
            ei_decode_atom(buff, &index, decoded_val);
            cout<<decoded_val<<endl;
            value.push_back(decoded_val);
            decoded_tuple.push_back(value);
            free(decoded_val);
            break;
        }
        case ERL_STRING_EXT:
        {
            vector<string> value;
            ei_decode_string(buff, &index, decoded_val);
            cout<<decoded_val<<endl;
            value.push_back(decoded_val);
            decoded_tuple.push_back(value);
            free(decoded_val);
            break;
        }
        case ERL_SMALL_TUPLE_EXT:
        case ERL_LARGE_TUPLE_EXT:
        {
            pair<int, vector<string> > value;
            value = decode_inner_tuple(buff, index);
            index = value.first;
            decoded_tuple.push_back(value.second);
            break;
        }
        case ERL_NIL_EXT:
        {
            vector<string> value;
            ei_decode_string(buff, &index, decoded_val);
            free(decoded_val);
            break;
        }
        case ERL_LIST_EXT:
        {
            pair<int, vector<string> > value;
            value = decode_inner_list(buff, index);
            index = value.first;
            decoded_tuple.push_back(value.second);
            break;
        }
        default:
            cout<<"Not a valid string"<<endl;
            break;
     }
   }
  return decoded_tuple;
 }

pair<int, vector<string> > decode_inner_list(char *buff, int index)
{
 vector<string> decoded_list;
 int size;
 int res = ei_decode_list_header(buff, &index, &size);
 for(int i = 0; i < size; ++i)
 {
    int entry_size;
    int type;
    int res = ei_get_type(buff, &index, &type, &entry_size);
    if(type == ERL_STRING_EXT)
    {
        char *decoded_value = (char *)malloc(sizeof(char) * (entry_size + 1));
        ei_decode_string(buff, &index, decoded_value);
        decoded_list.push_back(decoded_value);
        free(decoded_value);
    }
    else
    {
        cout << "Not a valid string" << endl;
        break;
    }
 }
 return make_pair(index, decoded_list);
};

1 个答案:

答案 0 :(得分:1)

问题在于你如何处理ERL_NIL_EXT。该类型表示一个空列表,这意味着它只包含一个没有元素的列表标题。要对其进行解码,只需解码列表标题:

case ERL_NIL_EXT:
    ei_decode_list_header(buff, &index, &entry_size);
    break;