2015-09-21 2 views
0

Я написал функцию, которая должна удалить все теги из файла MPEG, за исключением тегов APIC, но я получаю смешанные и непредсказуемые результаты. Иногда все теги, кроме «года», удаляются правильно (это происходит большую часть времени), иногда один или несколько других тегов остаются дополнительно к тегу «год».Удалить все теги, кроме APIC с TagLib в C++

Я, конечно, что-то не так. Вот моя функция:

void stripTags(const char* path) { 
    MPEG::File m(path); 
    m.strip(MPEG::File::ID3v1 | MPEG::File::APE, true); //I added this because otherwise, all tags would stay the first time I executed stripTags(). The second time I would execute it then the tags would be mostly gone (except for "year" as mentioned) 
    ByteVector handle = "APIC"; 
    ID3v2::Tag *t = m.ID3v2Tag(); 
    if (t) { 
     for (ID3v2::FrameList::ConstIterator it = t->frameList().begin(); it != t->frameList().end(); it++) { 
      if ((*it)->frameID() != handle) { 
       t->removeFrames((*it)->frameID()); 
       it = t->frameList().begin(); //since the doc says that removeFrames invalidates the pointer returned by frameList, I update this pointer after each removal 
      } 
     } 
     m.save(); 
    } 
    m.strip(MPEG::File::ID3v1 | MPEG::File::APE, true); 
} 

Спасибо за помощь

ответ

1

В цикле, когда вы удалили тег, вы сбросить итератор. Однако цикл продолжается, и первое, что он делает, это it++, что означает, что ваш цикл пропустит одну запись.

Вы можете изменить свой цикл, например,

for (ID3v2::FrameList::ConstIterator it = t->frameList().begin(); it != t->frameList().end(); /* nothing here */) { 
    if ((*it)->frameID() != handle) { 
     t->removeFrames((*it)->frameID()); 
     it = t->frameList().begin(); 
    } else { 
     ++it; 
    } 
} 
+0

Это работает! Большое спасибо. – beeb

Смежные вопросы