Debian Codegraph @

components/extension/youtube/converter.cpp

raw symbols SCIP
1 /* fre:ac - free audio converter
2 * Copyright (C) 2001-2016 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #include "converter.h"
14
15 #ifdef __WIN32__
16 # include <windows.h>
17 #else
18 # include <sys/wait.h>
19 #endif
20
21 Array<BoCA::Converter *> BoCA::Converter::converters;
22
23 Array<BoCA::Converter *> &BoCA::Converter::Get()
24 {
25 if (converters.Length() == 0)
26 {
27 Directory dir(Utilities::GetBoCADirectory().Append("freac.extension.youtube"));
28
29 if (!dir.Exists()) dir = Utilities::GetBoCADirectory().Append("../freac/freac.extension.youtube");
30
31 const Array<File> &files = dir.GetFilesByPattern("converter_*.xml");
32
33 foreach (const File &file, files)
34 {
35 Converter *converter = new Converter(file);
36
37 if (converter->IsSane())
38 {
39 converters.Add(converter);
40 }
41 else
42 {
43 delete converter;
44 }
45 }
46 }
47
48 return converters;
49 }
50
51 Bool BoCA::Converter::Free()
52 {
53 foreach (Converter *converter, converters) delete converter;
54
55 converters.RemoveAll();
56
57 return True;
58 }
59
60 BoCA::Converter::Converter(const String &fileName)
61 {
62 ParseXML(fileName);
63 }
64
65 BoCA::Converter::~Converter()
66 {
67 }
68
69 Bool BoCA::Converter::IsSane()
70 {
71 if (!File(external_command).Exists()) return False;
72 else return True;
73 }
74
75 Int BoCA::Converter::ParseXML(const String &fileName)
76 {
77 XML::Document *document = new XML::Document();
78
79 document->LoadFile(fileName);
80
81 XML::Node *root = document->GetRootNode();
82
83 for (Int i = 0; i < root->GetNOfNodes(); i++)
84 {
85 XML::Node *node = root->GetNthNode(i);
86
87 if (node->GetName() == "name") name = node->GetContent();
88 else if (node->GetName() == "version") version = node->GetContent();
89 else if (node->GetName() == "decoder") decoder = node->GetContent();
90 else if (node->GetName() == "format")
91 {
92 for (Int j = 0; j < node->GetNOfNodes(); j++)
93 {
94 XML::Node *node2 = node->GetNthNode(j);
95
96 if (node2->GetName() == "name") format.SetName(node2->GetContent());
97 else if (node2->GetName() == "extension") format.AddExtension(node2->GetContent());
98 else if (node2->GetName() == "tag")
99 {
100 AS::TagFormat tagFormat;
101
102 tagFormat.SetName(node2->GetContent());
103
104 if (node2->GetAttributeByName("id") != NIL) tagFormat.SetTagger(node2->GetAttributeByName("id")->GetContent());
105
106 if (node2->GetAttributeByName("mode") != NIL)
107 {
108 if (node2->GetAttributeByName("mode")->GetContent() == "prepend") tagFormat.SetMode(TAG_MODE_PREPEND);
109 else if (node2->GetAttributeByName("mode")->GetContent() == "append") tagFormat.SetMode(TAG_MODE_APPEND);
110 else if (node2->GetAttributeByName("mode")->GetContent() == "other") tagFormat.SetMode(TAG_MODE_OTHER);
111 }
112
113 format.AddTagFormat(tagFormat);
114 }
115 }
116 }
117 else if (node->GetName() == "external")
118 {
119 for (Int j = 0; j < node->GetNOfNodes(); j++)
120 {
121 XML::Node *node2 = node->GetNthNode(j);
122
123 if (node2->GetName() == "command")
124 {
125 external_command = node2->GetContent();
126
127 if (node2->GetAttributeByName("ignoreExitCode") != NIL)
128 {
129 external_ignoreExitCode = (node2->GetAttributeByName("ignoreExitCode")->GetContent() == "true");
130 }
131 }
132 else if (node2->GetName() == "arguments")
133 {
134 external_arguments = node2->GetContent();
135 }
136 }
137 }
138 }
139
140 delete document;
141
142 /* Check if external command actually exists.
143 */
144 if (external_command[0] != '/' && external_command[1] != ':')
145 {
146 #if defined __WIN32__
147 static const char *places[] = { "%APPDIR\\codecs\\cmdline\\%COMMAND", "%APPDIR\\codecs\\cmdline\\%COMMAND.exe", NIL };
148 #elif defined __APPLE__
149 static const char *places[] = { "%APPDIR\\codecs\\cmdline\\%COMMAND", "/usr/bin/%COMMAND", "/usr/local/bin/%COMMAND", "/opt/local/bin/%COMMAND", "/sw/bin/%COMMAND", NIL };
150 #elif defined __HAIKU__
151 static const char *places[] = { "/boot/common/bin/%COMMAND", NIL };
152 #elif defined __NetBSD__
153 static const char *places[] = { "/usr/bin/%COMMAND", "/usr/local/bin/%COMMAND", "/usr/pkg/bin/%COMMAND", NIL };
154 #else
155 static const char *places[] = { "/usr/bin/%COMMAND", "/usr/local/bin/%COMMAND", NIL };
156 #endif
157
158 for (Int i = 0; places[i] != NIL; i++)
159 {
160 String delimiter = Directory::GetDirectoryDelimiter();
161 String file = String(places[i]).Replace("%APPDIR", GUI::Application::GetApplicationDirectory()).Replace("%COMMAND", external_command).Replace(String(delimiter).Append(delimiter), delimiter);
162
163 if (File(file).Exists()) { external_command = file; break; }
164 }
165
166 if (external_command[0] != '/' && external_command[1] != ':') return False;
167 }
168
169 if (!File(external_command).Exists()) return Error();
170
171 return Success();
172 }
173
174 Int BoCA::Converter::Run(const String &inFile, const String &outFile)
175 {
176 /* Start 3rd party command line encoder
177 */
178 #if defined __WIN32__
179 SHELLEXECUTEINFOA execInfo;
180
181 ZeroMemory(&execInfo, sizeof(execInfo));
182
183 execInfo.cbSize = sizeof(execInfo);
184 execInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
185 execInfo.lpVerb = "open";
186 execInfo.lpFile = String(external_command).Replace("/", Directory::GetDirectoryDelimiter());
187 execInfo.lpParameters = String(external_arguments).Replace("%INFILE", String("\"").Append(inFile).Append("\""))
188 .Replace("%OUTFILE", String("\"").Append(outFile).Append("\""));
189
190 execInfo.lpDirectory = GUI::Application::GetApplicationDirectory();
191 execInfo.nShow = SW_HIDE;
192
193 ShellExecuteExA(&execInfo);
194
195 /* Wait until the encoder exits
196 */
197 unsigned long exitCode = 0;
198
199 while (True)
200 {
201 GetExitCodeProcess(execInfo.hProcess, &exitCode);
202
203 if (exitCode != STILL_ACTIVE) break;
204
205 S::System::System::Sleep(10);
206 }
207 #else
208 String command = String(external_command).Replace("/", Directory::GetDirectoryDelimiter());
209 String arguments = String(external_arguments).Replace("%INFILE", inFile.Replace("\\", "\\\\").Replace(" ", "\\ ")
210 .Replace("\"", "\\\"").Replace("\'", "\\\'").Replace("`", "\\`")
211 .Replace("(", "\\(").Replace(")", "\\)").Replace("<", "\\<").Replace(">", "\\>")
212 .Replace("&", "\\&").Replace(";", "\\;").Replace("$", "\\$").Replace("|", "\\|"))
213 .Replace("%OUTFILE", outFile.Replace("\\", "\\\\").Replace(" ", "\\ ")
214 .Replace("\"", "\\\"").Replace("\'", "\\\'").Replace("`", "\\`")
215 .Replace("(", "\\(").Replace(")", "\\)").Replace("<", "\\<").Replace(">", "\\>")
216 .Replace("&", "\\&").Replace(";", "\\;").Replace("$", "\\$").Replace("|", "\\|"));
217
218 FILE *pipe = popen(String(command).Append(" ").Append(arguments), "r");
219
220 /* Wait until the encoder exits
221 */
222 unsigned long exitStatus = pclose(pipe);
223 unsigned long exitCode = WEXITSTATUS(exitStatus);
224
225 #endif
226
227 /* Check if anything went wrong
228 */
229 if (!external_ignoreExitCode && exitCode != 0)
230 {
231 /* Remove output file
232 */
233 File(outFile).Delete();
234
235 return Error();
236 }
237
238 return Success();
239 }