Re-work Subtitle class; remove STL text writer.
[libsub.git] / src / stl_binary_writer.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <list>
21 #include <cmath>
22 #include <fstream>
23 #include "stl_binary_writer.h"
24 #include "compose.hpp"
25
26 using std::list;
27 using std::ofstream;
28 using std::string;
29 using namespace sub;
30
31 static void
32 put_string (char* p, string s)
33 {
34         memcpy (p, s.c_str (), s.length ());
35 }
36
37 static void
38 put_string (char* p, int n, string s)
39 {
40         memcpy (p, s.c_str (), s.length ());
41         memset (p + s.length(), ' ', s.length () - n);
42 }
43
44 /** @param language ISO 3-character country code for the language of the subtitles */
45 void
46 sub::write_stl_binary (
47         list<Subtitle> subtitles,
48         float frames_per_second,
49         string language,
50         string original_programme_title,
51         string original_episode_title,
52         string translated_programme_title,
53         string translated_episode_title,
54         string translator_name,
55         string translator_contact_details,
56         string creation_date,
57         string revision_date,
58         int revision_number,
59         string country_of_origin,
60         string publisher,
61         string editor_name,
62         string editor_contact_details,
63         boost::filesystem::path file_name
64         )
65 {
66         assert (language.size() == 3);
67         assert (original_programme_title.size() <= 32);
68         assert (original_episode_title.size() <= 32);
69         assert (translated_programme_title.size() <= 32);
70         assert (translated_episode_title.size() <= 32);
71         assert (translator_name.size() <= 32);
72         assert (translator_contact_details.size() <= 32);
73         assert (creation_date.size() == 6);
74         assert (revision_date.size() == 6);
75         assert (revision_number <= 99);
76         assert (country_of_origin.size() == 3);
77         assert (publisher.size() <= 32);
78         assert (editor_name.size() <= 32);
79         assert (editor_contact_details.size() <= 32);
80         
81         char* buffer = new char[1024];
82         ofstream output (file_name.string().c_str ());
83         
84         /* Code page: 850 */
85         put_string (buffer + 0, "850");
86         /* Disk format code */
87         put_string (buffer + 3, String::compose ("STL%1.01", rint (frames_per_second)));
88         /* Display standard code: open subtitling */
89         put_string (buffer + 11, "0");
90         /* Character code table: Latin (ISO 6937) */
91         put_string (buffer + 12, "00");
92         put_string (buffer + 14, language);
93         put_string (buffer + 16, 32, original_programme_title);
94         put_string (buffer + 48, 32, original_episode_title);
95         put_string (buffer + 80, 32, translated_programme_title);
96         put_string (buffer + 112, 32, translated_episode_title);
97         put_string (buffer + 144, 32, translator_name);
98         put_string (buffer + 176, 32, translator_contact_details);
99         /* Subtitle list reference code */
100         put_string (buffer + 208, "0000000000000000");
101         put_string (buffer + 224, creation_date);
102         put_string (buffer + 230, revision_date);
103         put_string (buffer + 236, String::compose ("%02d", revision_number));
104         /* TTI blocks */
105         put_string (buffer + 238, String::compose ("%05d", subtitles.size ()));
106         /* Total number of subtitles */
107         put_string (buffer + 243, String::compose ("%05d", subtitles.size ()));
108         /* Total number of subtitle groups */
109         put_string (buffer + 248, "000");
110         /* Maximum number of displayable characters in any text row */
111         /* XXX */
112         put_string (buffer + 251, "99");
113         /* Maximum number of displayable rows */
114         /* XXX */
115         put_string (buffer + 253, "99");
116         /* Time code status */
117         put_string (buffer + 255, "1");
118         /* Start-of-programme time code */
119         put_string (buffer + 256, "00000000");
120         /* First-in-cue time code */
121         put_string (buffer + 264, "00000000");
122         /* Total number of disks */
123         put_string (buffer + 272, "1");
124         /* Disk sequence number */
125         put_string (buffer + 273, "1");
126         put_string (buffer + 274, country_of_origin);
127         put_string (buffer + 277, publisher);
128         put_string (buffer + 309, editor_name);
129         put_string (buffer + 341, editor_contact_details);
130
131         output.write (buffer, 1024);
132
133         delete[] buffer;
134 }