API Documentation
Loading...
Searching...
No Matches
SubtitleFile.h
Go to the documentation of this file.
1#pragma once
2#include <NDEVR/TimeSpan.h>
3#include <NDEVR/BufferedScanner.h>
4namespace NDEVR
5{
7 {
8 public:
10 : m_file(file)
12 , m_text_index(1U)
13 {
14 m_file.create(true);
16 }
18 {
19 lib_assert(m_current_time <= span, "Unexpected out of order subtitle time");
20 if(m_current_time < span)
21 m_current_time = span;
22 }
23 void addSubtitle(const String& text)
24 {
25 if (m_last_text == text)
26 return;
27 if (m_last_text.size() > 0)
28 writeText();
30 m_last_text = text;
31 }
32 void writeText()
33 {
34 if (m_last_text.size() > 0)
35 {
37 {
38 m_file << String(m_text_index) + "\n";
40 m_file << " --> ";
41 m_file << m_current_time.getTimeString("HH:mm:ss,fff");
42 m_file << "\n";
43 m_file << m_last_text + "\n\n";
46 }
47 }
48 }
49 void finish(File file)
50 {
51 if (m_last_text.size() > 0)
52 writeText();
53 m_file.close();
54 if (m_file != file)
55 {
56 m_file.moveTo(file, true);
57 }
58 }
59 protected:
65 };
66
68 {
69 public:
71 : m_scanner(file)
72 {
73 readText();
74 }
75 void readText()
76 {
77 while (m_scanner.nextLine())
78 {
81 String time_string = m_scanner.getNext<String>();
82 Buffer<String> times = time_string.splitString('>');
84 if (times.size() >= 2)
85 {
86 spans[0] = convertFromString(times[0]);
87 spans[1] = convertFromString(times[1]);
88 }
90 m_subtitles.add({ spans, m_scanner.getNext<String>() });
91 }
92 }
93 String currentText(const TimeSpan& current) const
94 {
95 for (auto iter : m_subtitles)
96 {
97 if (iter.first[MAX] >= current)
98 {
99 if (iter.first[MIN] <= current)
100 return iter.second;
101 else
102 return String();
103 }
104 }
105 return String();
106 }
107 protected:
109 {
110 Buffer<String> parts = string.splitString(':');
111 sint08 nano_seconds = 0;
112 nano_seconds += parts[0].getAs<sint08>() * cast<sint08>(Time::HOUR);
113 nano_seconds += parts[1].getAs<sint08>() * cast<sint08>(Time::MINUTE);
114 nano_seconds += parts[2].getAs<sint08>() * cast<sint08>(Time::SECOND);
115 return TimeSpan(nano_seconds);
116 }
117 protected:
120 };
121
122}
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:70
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:64
constexpr t_index_type size() const
Definition Buffer.hpp:1461
Buffer< t_other_type, t_other_index_type, t_other_memory_allocator, t_other_memory_manager > getAs() const
Definition Buffer.hpp:177
void clear()
Definition Buffer.hpp:572
Definition BufferedScanner.h:38
bool nextLine(String &string, bool clear_string=true) final override
Definition BufferedScanner.cpp:42
Definition File.h:47
NDEVR_BASE_API FILE * open(OpenMode mode, bool append=false)
Definition File.cpp:1419
NDEVR_BASE_API void close()
Definition File.cpp:1610
NDEVR_BASE_API void create(bool override_file) const
Definition File.cpp:1744
@ e_ascii_write
Definition File.h:54
NDEVR_BASE_API bool moveTo(File &file, bool override_file, ProgressInfo *log=nullptr, bool is_source_temp=false)
Definition File.cpp:645
t_type getNext()
Definition String.h:40
NDEVR_BASE_API Buffer< String, uint04, ObjectAllocator< false > > splitString(char delimiter, bool preserve_empty=true) const
Definition String.cpp:57
Definition SubtitleFile.h:68
String currentText(const TimeSpan &current) const
Definition SubtitleFile.h:93
SubtitleFileReader(const File &file)
Definition SubtitleFile.h:70
Buffer< std::pair< Vector< 2, TimeSpan >, String > > m_subtitles
Definition SubtitleFile.h:118
TimeSpan convertFromString(const String &string)
Definition SubtitleFile.h:108
BufferedScanner m_scanner
Definition SubtitleFile.h:119
void readText()
Definition SubtitleFile.h:75
Definition SubtitleFile.h:7
TimeSpan m_current_time
Definition SubtitleFile.h:63
String m_last_text
Definition SubtitleFile.h:61
TimeSpan m_last_subtitle_start
Definition SubtitleFile.h:62
void setCurrentTime(TimeSpan span)
Definition SubtitleFile.h:17
void addSubtitle(const String &text)
Definition SubtitleFile.h:23
SubtitleFileWriter(const File &file)
Definition SubtitleFile.h:9
uint04 m_text_index
Definition SubtitleFile.h:64
void finish(File file)
Definition SubtitleFile.h:49
File m_file
Definition SubtitleFile.h:60
void writeText()
Definition SubtitleFile.h:32
static constexpr uint08 SECOND
Definition Time.h:84
static constexpr uint08 MINUTE
Definition Time.h:85
static constexpr uint08 HOUR
Definition Time.h:86
Definition TimeSpan.h:40
NDEVR_BASE_API String getTimeString(const String &format_string) const
Definition TimeSpan.cpp:18
An element of a vector space. An element of the real coordinate space Rn Basis vector,...
Definition Vector.hpp:62
Definition ACIColor.h:37
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
Definition BaseValues.hpp:86
@ MIN
Definition BaseValues.hpp:226
@ MAX
Definition BaseValues.hpp:227
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514