static int
pls_read_entry(char* line,pls_entry_t** _e,int* _max_entry,char** val) {
    int num,max_entry = (*_max_entry);
    pls_entry_t* e = (*_e);
    char* v;

    v = pls_entry_get_value(line);
    if(!v) {
        mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
        return 0;
    }

    num = atoi(line);
    if(num < 0) {
        num = max_entry+1;
        mp_msg(MSGT_PLAYTREE,MSGL_WARN,"No entry index in entry %s\nAssuming %d\n",line,num);
    }
    if(num > max_entry) {
        e = (pls_entry_t*)realloc(e,num*sizeof(pls_entry_t));
        memset(&e[max_entry],0,(num-max_entry)*sizeof(pls_entry_t));
        max_entry = num;
    }
    (*_e) = e;
    (*_max_entry) = max_entry;
    (*val) = v;

    return num;
}
Example #2
0
static int
pls_read_entry(char* line,pls_entry_t** _e,int* _max_entry,char** val) {
  int num,max_entry = (*_max_entry);
  pls_entry_t* e = (*_e);
  int limit = INT_MAX / sizeof(*e);
  char* v;

  v = pls_entry_get_value(line);
  if(!v) {
    mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
    return -1;
  }

  num = atoi(line);
  if(num <= 0 || num > limit) {
    if (max_entry >= limit) {
        mp_msg(MSGT_PLAYTREE, MSGL_WARN, "Too many index entries\n");
        return -1;
    }
    num = max_entry+1;
    mp_msg(MSGT_PLAYTREE,MSGL_WARN,"No or invalid entry index in entry %s\nAssuming %d\n",line,num);
  }
  if(num > max_entry) {
    e = realloc(e, num * sizeof(pls_entry_t));
    if (!e)
      return -1;
    memset(&e[max_entry],0,(num-max_entry)*sizeof(pls_entry_t));
    max_entry = num;
  }
  (*_e) = e;
  (*_max_entry) = max_entry;
  (*val) = v;

  return num;
}
/*
 Reference Ini-Format: Each entry is assumed a reference
 */
static play_tree_t*
parse_ref_ini(play_tree_parser_t* p) {
    char *line,*v;
    play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;

    mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying reference-ini playlist...\n");
    if (!(line = play_tree_parser_get_line(p)))
        return NULL;
    strstrip(line);
    if(strcasecmp(line,"[Reference]"))
        return NULL;
    mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected reference-ini playlist format\n");
    play_tree_parser_stop_keeping(p);
    line = play_tree_parser_get_line(p);
    if(!line)
        return NULL;
    while(line) {
        strstrip(line);
        if(strncasecmp(line,"Ref",3) == 0) {
            v = pls_entry_get_value(line+3);
            if(!v)
                mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
            else
            {
                mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",v);
                entry = play_tree_new();
                play_tree_add_file(entry,v);
                if(list)
                    play_tree_append_entry(last_entry,entry);
                else
                    list = entry;
                last_entry = entry;
            }
        }
        line = play_tree_parser_get_line(p);
    }

    if(!list) return NULL;
    entry = play_tree_new();
    play_tree_set_child(entry,list);
    return entry;
}
static play_tree_t*
parse_pls(play_tree_parser_t* p) {
    char *line,*v;
    pls_entry_t* entries = NULL;
    int n_entries = 0,max_entry=0,num;
    play_tree_t *list = NULL, *entry = NULL, *last_entry = NULL;

    mp_msg(MSGT_PLAYTREE,MSGL_V,"Trying Winamp playlist...\n");
    while((line = play_tree_parser_get_line(p))) {
        strstrip(line);
        if(strlen(line))
            break;
    }
    if (!line)
        return NULL;
    if(strcasecmp(line,"[playlist]"))
        return NULL;
    mp_msg(MSGT_PLAYTREE,MSGL_V,"Detected Winamp playlist format\n");
    play_tree_parser_stop_keeping(p);
    line = play_tree_parser_get_line(p);
    if(!line)
        return NULL;
    strstrip(line);
    if(strncasecmp(line,"NumberOfEntries",15) == 0) {
        v = pls_entry_get_value(line);
        n_entries = atoi(v);
        if(n_entries < 0)
            mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Invalid number of entries: very funny!!!\n");
        else
            mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Playlist claims to have %d entries. Let's see.\n",n_entries);
        line = play_tree_parser_get_line(p);
    }

    while(line) {
        strstrip(line);
        if(line[0] == '\0') {
            line = play_tree_parser_get_line(p);
            continue;
        }
        if(strncasecmp(line,"File",4) == 0) {
            num = pls_read_entry(line+4,&entries,&max_entry,&v);
            if(num < 0)
                mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
            else
                entries[num-1].file = strdup(v);
        } else if(strncasecmp(line,"Title",5) == 0) {
            num = pls_read_entry(line+5,&entries,&max_entry,&v);
            if(num < 0)
                mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
            else
                entries[num-1].title = strdup(v);
        } else if(strncasecmp(line,"Length",6) == 0) {
            num = pls_read_entry(line+6,&entries,&max_entry,&v);
            if(num < 0)
                mp_msg(MSGT_PLAYTREE,MSGL_ERR,"No value in entry %s\n",line);
            else
                entries[num-1].length = strdup(v);
        } else
            mp_msg(MSGT_PLAYTREE,MSGL_WARN,"Unknown entry type %s\n",line);
        line = play_tree_parser_get_line(p);
    }

    for(num = 0; num < max_entry ; num++) {
        if(entries[num].file == NULL)
            mp_msg(MSGT_PLAYTREE,MSGL_ERR,"Entry %d don't have a file !!!!\n",num+1);
        else {
            mp_msg(MSGT_PLAYTREE,MSGL_DBG2,"Adding entry %s\n",entries[num].file);
            entry = play_tree_new();
            play_tree_add_file(entry,entries[num].file);
            free(entries[num].file);
            if(list)
                play_tree_append_entry(last_entry,entry);
            else
                list = entry;
            last_entry = entry;
        }
        if(entries[num].title) {
            // When we have info in playtree we add this info
            free(entries[num].title);
        }
        if(entries[num].length) {
            // When we have info in playtree we add this info
            free(entries[num].length);
        }
    }

    free(entries);

    entry = play_tree_new();
    play_tree_set_child(entry,list);
    return entry;
}