Skip to content

Commit

Permalink
lib: add Document::getId and fix in parseBuffer test
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlivio committed Apr 30, 2021
1 parent 730acb5 commit 912ed18
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 78 deletions.
29 changes: 27 additions & 2 deletions lib/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ Document::Document ()
_settings = obj;
}

/**
* @brief Creates a new document with id
*
* @return New #Document.
*/
Document::Document (const string &id) : Document ()
{
_id = id;
}

/**
* @brief Destroys document.
*
Expand All @@ -58,6 +68,16 @@ Document::~Document ()
delete _root;
}

/**
* @brief Gets document id.
* @return Id string.
*/
const string
Document::getId ()
{
return _id;
}

/**
* @brief Gets document objects.
* @return The set of objects in document.
Expand Down Expand Up @@ -132,8 +152,9 @@ Document::addObject (Object *obj)
Media *media = cast (Media *, obj);
g_assert_nonnull (media);
_medias.insert (media);
if (Player::mayUsePlayerRemote(media)){
_mediasRemote.insert (media);
if (Player::mayUsePlayerRemote (media))
{
_mediasRemote.insert (media);
}
}
else if (instanceof (Context *, obj))
Expand Down Expand Up @@ -545,6 +566,10 @@ Document::getData (const string &key, void **value)
bool
Document::setData (const string &key, void *value, UserDataCleanFunc fn)
{
if (key == "id"){
string *str = (string *) value;
_id = string(*str);
}
return _udata.setData (key, value, fn);
}

Expand Down
3 changes: 3 additions & 0 deletions lib/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ class Document
{
public:
Document ();
Document (const string&);
virtual ~Document ();

const set<Object *> *getObjects ();
Object *getObjectById (const string &);
Object *getObjectByIdOrAlias (const string &);
bool addObject (Object *);

const string getId ();
Context *getRoot ();
MediaSettings *getSettings ();
const set<Media *> *getMedias ();
Expand All @@ -58,6 +60,7 @@ class Document
bool setData (const string &, void *, UserDataCleanFunc fn = nullptr);

private:
string _id;
list<Action> evalActionInContext (Action, Context *);
set<Object *> _objects; ///< Objects.
map<string, Object *> _objectsById; ///< Objects indexed by id.
Expand Down
11 changes: 10 additions & 1 deletion lib/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Object::toString ()
list<string> pres;
list<string> attr;
list<string> sel;
list<string> prep;
for (auto evt : _events)
switch (evt->getType ())
{
Expand All @@ -136,12 +137,20 @@ Object::toString ()
+ Event::getEventStateAsString (evt->getState ())
+ ')');
break;
case Event::PREPARATION:
prep.push_back (evt->getId () + " ("
+ Event::getEventStateAsString (evt->getState ())
+ ')');
break;
default:
g_assert_not_reached ();
}

list<pair<string, list<string> *> > evts = {
{ "evts pres.", &pres }, { "evts attr.", &attr }, { "evts sel.", &sel },
{ "evts pres.", &pres },
{ "evts attr.", &attr },
{ "evts sel.", &sel },
{ "evts prep.", &prep },
};

for (auto it_evts : evts)
Expand Down
89 changes: 45 additions & 44 deletions lib/Parser.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2006-2018 PUC-Rio/Laboratorio TeleMidia
/* Copyright (C) 2006-2018 PUC-Rio/Laboratorio TeleMidia
This file is part of Ginga (Ginga-NCL).
Expand All @@ -15,6 +15,8 @@ License for more details.
You should have received a copy of the GNU General Public License
along with Ginga. If not, see <https://www.gnu.org/licenses/>. */

#include <memory>

#include "aux-ginga.h"
#include "Parser.h"

Expand Down Expand Up @@ -353,10 +355,10 @@ class ParserState
// NCL syntax.

/// Type for element push function.
typedef bool(ParserSyntaxEltPush) (ParserState *, ParserElt *);
typedef bool (ParserSyntaxEltPush) (ParserState *, ParserElt *);

/// Type for element pop function.
typedef bool(ParserSyntaxEltPop) (ParserState *, ParserElt *);
typedef bool (ParserSyntaxEltPop) (ParserState *, ParserElt *);

/**
* @brief NCL attribute syntax.
Expand All @@ -382,7 +384,8 @@ typedef struct ParserSyntaxElt
/**
* @brief NCL attribute processing flags.
*/
typedef enum {
typedef enum
{
PARSER_SYNTAX_ATTR_NONEMPTY = 1 << 1, ///< Cannot be not empty.
PARSER_SYNTAX_ATTR_REQUIRED = 1 << 2, ///< Is required.
PARSER_SYNTAX_ATTR_TYPE_ID = 1 << 3, ///< Is an id.
Expand Down Expand Up @@ -410,7 +413,8 @@ typedef enum {
/**
* @brief NCL element processing flags.
*/
typedef enum {
typedef enum
{
PARSER_SYNTAX_ELT_CACHE = 1 << 1, ///< Save element in #Parser cache.
PARSER_SYNTAX_ELT_GEN_ID = 1 << 2, ///< Generate id if not present.
} ParserSyntaxEltFlag;
Expand Down Expand Up @@ -444,7 +448,8 @@ static map<string, ParserSyntaxElt> parser_syntax_table = {
{ "xmlns", 0 } } },
},
{
"head", { nullptr, nullptr, 0, { "ncl" }, {} },
"head",
{ nullptr, nullptr, 0, { "ncl" }, {} },
},
{
"regionBase",
Expand Down Expand Up @@ -695,33 +700,27 @@ static map<string, ParserSyntaxElt> parser_syntax_table = {
{ ParserState::pushImportBase,
nullptr,
ELT_CACHE,
{"connectorBase", "descriptorBase", "regionBase", "ruleBase",
"transitionBase", "fontBase"},
{ "connectorBase", "descriptorBase", "regionBase", "ruleBase",
"transitionBase", "fontBase" },
{ { "alias", ATTR_REQUIRED_NONEMPTY_NAME },
{ "documentURI", ATTR_REQUIRED },
{ "region", 0 }, // unused
{ "baseId", 0 } } }, // unused
},
{
"fontBase",
{ "fontBase",
{
nullptr,
nullptr,
ELT_CACHE,
{"head"},
{} // no attributes
}
},
nullptr, nullptr, ELT_CACHE, { "head" }, {} // no attributes
} },
{
"font",
{ParserState::pushFont,
nullptr,
ELT_CACHE,
{"fontBase"},
{ {"fontFamily", ATTR_REQUIRED},
{"src", ATTR_REQUIRED},
{"fontStyle", 0},
{"fontWeight", 0} } },
"font",
{ ParserState::pushFont,
nullptr,
ELT_CACHE,
{ "fontBase" },
{ { "fontFamily", ATTR_REQUIRED },
{ "src", ATTR_REQUIRED },
{ "fontStyle", 0 },
{ "fontWeight", 0 } } },
},
{
"body",
Expand Down Expand Up @@ -2316,13 +2315,16 @@ bool
ParserState::pushNcl (ParserState *st, ParserElt *elt)
{
Context *root;
string id;
string *id = new string();

root = st->_doc->getRoot ();
g_assert_nonnull (root);

if (elt->getAttribute ("id", &id))
root->addAlias (id);
if (elt->getAttribute ("id", id))
{
root->addAlias (*id);
st->_doc->setData ("id", id, xstrdelete);
}

st->objStackPush (root);
return true;
Expand Down Expand Up @@ -2778,9 +2780,9 @@ borderColor='%s'}",
{
string eventId = evt->getId ();

if(eventId == "@lambda")
if (eventId == "@lambda")
{
obj->addPreparationEvent(eventId);
obj->addPreparationEvent (eventId);
}

act.event = obj->getPreparationEvent (eventId);
Expand Down Expand Up @@ -3554,9 +3556,8 @@ ParserState::pushImportBase (ParserState *st, ParserElt *elt)
return status;

fail_no_such_base:
return st->errEltImport (elt->getNode (),
"no <" + parent_elt->getTag ()
+ "> in imported document");
return st->errEltImport (elt->getNode (), "no <" + parent_elt->getTag ()
+ "> in imported document");
}

/**
Expand Down Expand Up @@ -3974,7 +3975,7 @@ ParserState::pushMedia (ParserState *st, ParserElt *elt)
delete tmpMedia;
}

gchar *scheme = g_uri_parse_scheme (src.c_str ());
gchar *scheme = g_uri_parse_scheme (src.c_str ());
if (g_strcmp0 (scheme, "streambuf") == 0)
{
string filename = src.substr (12);
Expand Down Expand Up @@ -4240,29 +4241,29 @@ ParserState::pushFont (ParserState *st, ParserElt *elt)
// fixme: We should also handle remote URIs; g_file_move could help us.
if (st->getURI () != "")
{
xmlChar *s = xmlBuildURI (toXmlChar (src), toXmlChar (st->getURI()));
xmlChar *s = xmlBuildURI (toXmlChar (src), toXmlChar (st->getURI ()));
src = toCPPString (s);
xmlFree (s);
src = xpathfromuri (src.c_str ());
}
else
src = xpathmakeabs (src);

const FcChar8 *fcfilename = (const FcChar8 *) src.c_str();
const FcChar8 *fcfilename = (const FcChar8 *) src.c_str ();
FcBool fontAddStatus = FcConfigAppFontAddFile (NULL, fcfilename);

TRACE ("Adding font family='%s' src='%s' success: %d.",
family.c_str(), src.c_str(), fontAddStatus);
TRACE ("Adding font family='%s' src='%s' success: %d.", family.c_str (),
src.c_str (), fontAddStatus);

if (fontAddStatus == FcTrue)
{
// Replaces font metadata with the specifications in the <font> elt.
FcFontSet *fontSet = FcConfigGetFonts (FcConfigGetCurrent(),
FcSetApplication);
FcPattern *font = fontSet->fonts[fontSet->nfont-1];
FcFontSet *fontSet
= FcConfigGetFonts (FcConfigGetCurrent (), FcSetApplication);
FcPattern *font = fontSet->fonts[fontSet->nfont - 1];

// family
const FcChar8 *fcfamily = (const FcChar8 *) family.c_str();
const FcChar8 *fcfamily = (const FcChar8 *) family.c_str ();
FcPatternRemove (font, FC_FAMILY, 0);
FcPatternAddString (font, FC_FAMILY, fcfamily);

Expand All @@ -4282,7 +4283,7 @@ ParserState::pushFont (ParserState *st, ParserElt *elt)

FcPatternRemove (font, FC_POSTSCRIPT_NAME, 0);

// FcPatternPrint (new_font);
// FcPatternPrint (new_font);
}

return (fontAddStatus == FcTrue);
Expand Down
7 changes: 7 additions & 0 deletions lib/aux-ginga.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,13 @@ xstrsplit (const string &s, char sep)
return result;
}

void
xstrdelete (void *str)
{
string * tmp = (string*) str;
delete (string*) str;
}

// Paths -------------------------------------------------------------------

/**
Expand Down
1 change: 1 addition & 0 deletions lib/aux-ginga.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ int G_GNUC_PRINTF (2,3) xstrassign (string &, const char *, ...);
string G_GNUC_PRINTF (1,2) xstrbuild (const char *, ...);
string xstrstrip (string);
list<string> xstrsplit (const string &, char);
void xstrdelete(void *);

// Path functions.
string xpathbasename (string);
Expand Down
12 changes: 10 additions & 2 deletions tests/test-Document-new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ main (void)

tests_create_document (&doc, &root, &settings);

g_assert_cmpint (doc->getObjects ()->size (), ==, 2); // root and settings
g_assert_cmpint (doc->getMedias ()->size (), ==, 1); // settings
g_assert_cmpint (doc->getObjects ()->size (), ==, 2); // root and settings
g_assert_cmpint (doc->getMedias ()->size (), ==, 1); // settings
g_assert_cmpint (doc->getContexts ()->size (), ==, 1); // root
g_assert_cmpint (doc->getSwitches ()->size (), ==, 0); // none

string *id = new string ("testId");
doc->setData ("id", (void *) id, xstrdelete);
g_assert (doc->getId () == "testId");

delete doc;

doc = new Document ("testId");
g_assert (doc->getId () == "testId");
delete doc;

exit (EXIT_SUCCESS);
Expand Down
Loading

0 comments on commit 912ed18

Please sign in to comment.