summaryrefslogtreecommitdiff
path: root/src/stl_binary_writer.cc
blob: 34e4d73c1ecef2922ab1d43b5692b35fa7cde8fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
    Copyright (C) 2014 Carl Hetherington <cth@carlh.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <list>
#include <cmath>
#include <fstream>
#include "stl_binary_writer.h"
#include "compose.hpp"

using std::list;
using std::ofstream;
using std::string;
using namespace sub;

static void
put_string (char* p, string s)
{
	memcpy (p, s.c_str (), s.length ());
}

static void
put_string (char* p, int n, string s)
{
	memcpy (p, s.c_str (), s.length ());
	memset (p + s.length(), ' ', s.length () - n);
}

/** @param language ISO 3-character country code for the language of the subtitles */
void
sub::write_stl_binary (
	list<Subtitle> subtitles,
	float frames_per_second,
	string language,
	string original_programme_title,
	string original_episode_title,
	string translated_programme_title,
	string translated_episode_title,
	string translator_name,
	string translator_contact_details,
	string creation_date,
	string revision_date,
	int revision_number,
	string country_of_origin,
	string publisher,
	string editor_name,
	string editor_contact_details,
	boost::filesystem::path file_name
	)
{
	assert (language.size() == 3);
	assert (original_programme_title.size() <= 32);
	assert (original_episode_title.size() <= 32);
	assert (translated_programme_title.size() <= 32);
	assert (translated_episode_title.size() <= 32);
	assert (translator_name.size() <= 32);
	assert (translator_contact_details.size() <= 32);
	assert (creation_date.size() == 6);
	assert (revision_date.size() == 6);
	assert (revision_number <= 99);
	assert (country_of_origin.size() == 3);
	assert (publisher.size() <= 32);
	assert (editor_name.size() <= 32);
	assert (editor_contact_details.size() <= 32);
	
	char* buffer = new char[1024];
	ofstream output (file_name.string().c_str ());
	
	/* Code page: 850 */
	put_string (buffer + 0, "850");
	/* Disk format code */
	put_string (buffer + 3, String::compose ("STL%1.01", rint (frames_per_second)));
	/* Display standard code: open subtitling */
	put_string (buffer + 11, "0");
	/* Character code table: Latin (ISO 6937) */
	put_string (buffer + 12, "00");
	put_string (buffer + 14, language);
	put_string (buffer + 16, 32, original_programme_title);
	put_string (buffer + 48, 32, original_episode_title);
	put_string (buffer + 80, 32, translated_programme_title);
	put_string (buffer + 112, 32, translated_episode_title);
	put_string (buffer + 144, 32, translator_name);
	put_string (buffer + 176, 32, translator_contact_details);
	/* Subtitle list reference code */
	put_string (buffer + 208, "0000000000000000");
	put_string (buffer + 224, creation_date);
	put_string (buffer + 230, revision_date);
	put_string (buffer + 236, String::compose ("%02d", revision_number));
	/* TTI blocks */
	put_string (buffer + 238, String::compose ("%05d", subtitles.size ()));
	/* Total number of subtitles */
	put_string (buffer + 243, String::compose ("%05d", subtitles.size ()));
	/* Total number of subtitle groups */
	put_string (buffer + 248, "000");
	/* Maximum number of displayable characters in any text row */
	/* XXX */
	put_string (buffer + 251, "99");
	/* Maximum number of displayable rows */
	/* XXX */
	put_string (buffer + 253, "99");
	/* Time code status */
	put_string (buffer + 255, "1");
	/* Start-of-programme time code */
	put_string (buffer + 256, "00000000");
	/* First-in-cue time code */
	put_string (buffer + 264, "00000000");
	/* Total number of disks */
	put_string (buffer + 272, "1");
	/* Disk sequence number */
	put_string (buffer + 273, "1");
	put_string (buffer + 274, country_of_origin);
	put_string (buffer + 277, publisher);
	put_string (buffer + 309, editor_name);
	put_string (buffer + 341, editor_contact_details);

	output.write (buffer, 1024);

	delete[] buffer;
}