Add "U8" character code table for binary STL.
[libsub.git] / src / collect.h
index 62e872ff4ee757c78a3f826a0108fe2b7c4482a4..5fcf6948c7aacec47ccf371754343725bbe30ab9 100644 (file)
 
 */
 
+#ifndef LIBSUB_COLLECT_H
+#define LIBSUB_COLLECT_H
+
 #include "subtitle.h"
 #include "raw_subtitle.h"
 
 namespace sub {
-       
-std::list<Subtitle> collect (std::list<RawSubtitle>);
+
+/** Collect sub::RawSubtitle objects into sub::Subtitles.
+ *  This method is templated so that any container type can be used for the result.
+ */
+template <class T>
+T
+collect (std::vector<RawSubtitle> raw)
+{
+       std::stable_sort (raw.begin(), raw.end());
+
+       T out;
+
+       boost::optional<Subtitle> current;
+       for (auto const& i: raw) {
+               if (current && current->same_metadata(i)) {
+                       /* This RawSubtitle can be added to current... */
+                       if (!current->lines.empty() && current->lines.back().same_metadata(i)) {
+                               /* ... and indeed to its last line */
+                               current->lines.back().blocks.push_back(Block(i));
+                       } else {
+                               /* ... as a new line */
+                               current->lines.push_back(Line(i));
+                       }
+               } else {
+                       /* We must start a new Subtitle */
+                       if (current) {
+                               out.push_back (current.get ());
+                       }
+                       current = Subtitle (i);
+               }
+       }
+
+       if (current) {
+               out.push_back (current.get ());
+       }
+
+       return out;
+}
 
 }
+
+#endif