-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from GordonSmith/EXPAT_WASM
feat(xml): Add simple XML parser based on expat
- Loading branch information
Showing
13 changed files
with
1,260 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
// See https://go.microsoft.com/fwlink/?LinkId=733558 | ||
// for the documentation about the tasks.json format | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"type": "npm", | ||
"label": "Compile Watch", | ||
"script": "compile-es6-watch", | ||
"problemMatcher": [ | ||
"$tsc-watch" | ||
], | ||
"presentation": { | ||
"group": "group-build" | ||
} | ||
}, | ||
{ | ||
"type": "npm", | ||
"label": "Bundle Watch", | ||
"script": "bundle-watch", | ||
"problemMatcher": [], | ||
"presentation": { | ||
"group": "group-build" | ||
} | ||
}, | ||
{ | ||
"type": "npm", | ||
"label": "Web Server", | ||
"script": "dev-start", | ||
"problemMatcher": [], | ||
"presentation": { | ||
"group": "group-build" | ||
} | ||
}, | ||
{ | ||
"label": "build", | ||
"dependsOn": [ | ||
"Compile Watch", | ||
"Bundle Watch", | ||
"Web Server" | ||
], | ||
"group": "build", | ||
"problemMatcher": [] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,5 @@ ADD_LIBRARY(expat STATIC | |
${EXPAT_LIB_DIR}/xmltok_ns.c | ||
${EXPAT_LIB_DIR}/xmltok.c | ||
) | ||
|
||
ADD_SUBDIRECTORY(expatlib) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
PROJECT(expatlib) | ||
|
||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s WASM=1 -s INVOKE_RUN=0 -s ENVIRONMENT=web -s ALLOW_MEMORY_GROWTH=1") | ||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -s MODULARIZE=1 -s EXPORT_NAME='${CMAKE_PROJECT_NAME}'") | ||
|
||
# Generate Glue from IDL file --- | ||
ADD_CUSTOM_COMMAND( | ||
MAIN_DEPENDENCY ${CMAKE_CURRENT_SOURCE_DIR}/main.idl | ||
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/main_glue.js ${CMAKE_CURRENT_BINARY_DIR}/main_glue.cpp | ||
COMMAND python ${CMAKE_BINARY_DIR}/../emsdk/upstream/emscripten/tools/webidl_binder.py ${CMAKE_CURRENT_SOURCE_DIR}/main.idl ${CMAKE_CURRENT_BINARY_DIR}/main_glue | ||
) | ||
SET_PROPERTY(SOURCE main.cpp APPEND PROPERTY OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/main_glue.cpp) | ||
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --post-js ${CMAKE_CURRENT_BINARY_DIR}/main_glue.js") | ||
# --- --- --- | ||
|
||
INCLUDE_DIRECTORIES( | ||
${CMAKE_CURRENT_BINARY_DIR} | ||
${EXPAT_LIB_DIR} | ||
) | ||
|
||
ADD_EXECUTABLE(expatlib | ||
main.cpp | ||
) | ||
|
||
TARGET_LINK_LIBRARIES(expatlib | ||
expat | ||
) | ||
|
||
INSTALL(FILES | ||
${CMAKE_CURRENT_BINARY_DIR}/expatlib.wasm | ||
DESTINATION dist | ||
COMPONENT runtime | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#include "stack_parser.h" | ||
|
||
#include <string> | ||
|
||
class CExpat : public CExpatImpl<CExpat> | ||
{ | ||
private: | ||
typedef CExpatImpl<CExpat> BaseClass; | ||
|
||
protected: | ||
std::string m_tag; | ||
std::string m_attrs; | ||
std::string m_content; | ||
|
||
public: | ||
CExpat() | ||
{ | ||
} | ||
|
||
void OnPostCreate() | ||
{ | ||
EnableStartElementHandler(); | ||
EnableEndElementHandler(); | ||
EnableCharacterDataHandler(); | ||
} | ||
|
||
bool create() | ||
{ | ||
return BaseClass::Create(); | ||
} | ||
|
||
void destroy() | ||
{ | ||
BaseClass::Destroy(); | ||
} | ||
|
||
bool parse(const char *xml) | ||
{ | ||
return BaseClass::Parse(xml, (int)strlen(xml), XML_TRUE); | ||
} | ||
|
||
const char *tag() const | ||
{ | ||
return m_tag.c_str(); | ||
} | ||
|
||
const char *attrs() const | ||
{ | ||
return m_attrs.c_str(); | ||
} | ||
|
||
const char *content() const | ||
{ | ||
return m_content.c_str(); | ||
} | ||
|
||
virtual void startElement() | ||
{ | ||
} | ||
|
||
virtual void endElement() | ||
{ | ||
} | ||
|
||
virtual void characterData() | ||
{ | ||
} | ||
|
||
virtual void OnStartElement(const XML_Char *pszName, const XML_Char **papszAttrs) | ||
{ | ||
m_tag = pszName; | ||
m_attrs = ""; | ||
for (XML_Char **itr = (XML_Char **)papszAttrs; *itr != NULL; itr += 2) | ||
{ | ||
if (!m_attrs.empty()) | ||
{ | ||
m_attrs += "\1\1"; | ||
} | ||
m_attrs += *itr; | ||
m_attrs += "\1"; | ||
m_attrs += *(itr + 1); | ||
} | ||
startElement(); | ||
} | ||
|
||
virtual void OnEndElement(const XML_Char *pszName) | ||
{ | ||
m_tag = pszName; | ||
endElement(); | ||
} | ||
|
||
virtual void OnCharacterData(const XML_Char *pszData, int nLength) | ||
{ | ||
m_content.assign(pszData, nLength); | ||
characterData(); | ||
} | ||
}; | ||
|
||
// Include JS Glue --- | ||
#include "main_glue.cpp" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
interface CExpat | ||
{ | ||
void CExpat(); | ||
boolean create(); | ||
void destroy(); | ||
boolean parse([Const] DOMString xml); | ||
[Const] DOMString tag(); | ||
[Const] DOMString attrs(); | ||
[Const] DOMString content(); | ||
void startElement(); | ||
void endElement(); | ||
void characterData(); | ||
}; | ||
|
||
[JSImplementation = "CExpat"] | ||
interface CExpatJS { | ||
void CExpatJS(); | ||
void startElement(); | ||
void endElement(); | ||
void characterData(); | ||
}; |
Oops, something went wrong.