3
MediaTomb - http://www.mediatomb.cc/
5
playlist_parser_script.cc - this file is part of MediaTomb.
7
Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
8
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
10
Copyright (C) 2006-2007 Gena Batyan <bgeradz@mediatomb.cc>,
11
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
12
Leonhard Wimmer <leo@mediatomb.cc>
14
MediaTomb is free software; you can redistribute it and/or modify
15
it under the terms of the GNU General Public License version 2
16
as published by the Free Software Foundation.
18
MediaTomb is distributed in the hope that it will be useful,
19
but WITHOUT ANY WARRANTY; without even the implied warranty of
20
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
GNU General Public License for more details.
23
You should have received a copy of the GNU General Public License
24
version 2 along with MediaTomb; if not, write to the Free Software
25
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
27
$Id: playlist_parser_script.cc 1388 2007-07-11 17:08:40Z jin_eld $
30
/// \file playlist_parser_script.cc
33
#include "autoconfig.h"
38
#include "playlist_parser_script.h"
39
#include "config_manager.h"
40
#include "js_functions.h"
42
#define ONE_TEXTLINE_BYTES 1024
49
js_readln(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
51
PlaylistParserScript *self = (PlaylistParserScript *)JS_GetPrivate(cx, obj);
57
line = self->readln();
59
catch (ServerShutdownException se)
61
log_warning("Aborting script execution due to server shutdown.\n");
70
JSString *jsline = JS_NewStringCopyZ(cx, line.c_str());
72
*rval = STRING_TO_JSVAL(jsline);
79
PlaylistParserScript::PlaylistParserScript(Ref<Runtime> runtime) : Script(runtime)
82
currentObjectID = INVALID_OBJECT_ID;
85
defineFunction(_("readln"), js_readln, 0);
87
String scriptPath = ConfigManager::getInstance()->getOption(CFG_IMPORT_SCRIPTING_PLAYLIST_SCRIPT);
91
String PlaylistParserScript::readln()
94
throw _Exception(_("Readline not yet setup for use"));
96
if ((currentTask == nil) || (!currentTask->isValid()))
99
if (fgets(currentLine, ONE_TEXTLINE_BYTES, currentHandle) == NULL)
102
return trim_string(String(currentLine));
105
void PlaylistParserScript::processPlaylistObject(zmm::Ref<CdsObject> obj, Ref<CMTask> task)
108
if ((currentObjectID != INVALID_OBJECT_ID) || (currentHandle != NULL) ||
109
(currentLine != NULL))
110
throw _Exception(_("recursion not allowed!"));
112
if (!IS_CDS_PURE_ITEM(obj->getObjectType()))
114
throw _Exception(_("only allowed for pure items"));
118
currentObjectID = obj->getID();
119
currentLine = (char *)MALLOC(ONE_TEXTLINE_BYTES);
122
currentObjectID = INVALID_OBJECT_ID;
124
throw _Exception(_("failed to allocate memory for playlist parsing!"));
126
currentLine[0] = '\0';
128
currentHandle = fopen(obj->getLocation().c_str(), "r");
131
currentObjectID = INVALID_OBJECT_ID;
134
throw _Exception(_("failed to open file: ") + obj->getLocation());
137
JSObject *playlist = JS_NewObject(cx, NULL, NULL, glob);
141
setObjectProperty(glob, _("playlist"), playlist);
142
cdsObject2jsObject(obj, playlist);
149
fclose(currentHandle);
150
currentHandle = NULL;
155
currentObjectID = INVALID_OBJECT_ID;
161
fclose(currentHandle);
162
currentHandle = NULL;
167
currentObjectID = INVALID_OBJECT_ID;