You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
4.8 KiB
136 lines
4.8 KiB
#include "CommandLineParams.h" |
|
#include "graphics/opengl/Window.h" |
|
#include "html/HTMLParser.h" |
|
#include "Log.h" |
|
#include "URL.h" |
|
#include "WebResource.h" |
|
#include "tlsf.h" |
|
#include <ctime> |
|
#include <iostream> |
|
#include <sys/stat.h> |
|
#ifdef _WIN32 |
|
extern "C"{ |
|
void init_heap(); |
|
} |
|
#endif |
|
#if defined(_WIN32) && !defined(_WIN64) |
|
#define PLATFORM "i686-pc-winnt" |
|
#endif |
|
#ifdef _WIN64 |
|
#define PLATFORM "amd64-pc-winnt" |
|
#endif |
|
|
|
const std::unique_ptr<Window> window = std::make_unique<Window>(); |
|
//URL currentURL; |
|
|
|
bool setWindowContent(URL const& url) { |
|
logDebug() << "main::setWindowContent - " << url << std::endl; |
|
|
|
// download URL |
|
WebResource res = getWebResource(url); |
|
if (res.resourceType == ResourceType::INVALID) { |
|
logError() << "Invalid resource type: " << res.raw << std::endl; |
|
return false; |
|
} |
|
|
|
// parse HTML |
|
if (res.resourceType == ResourceType::HTML) { |
|
HTMLParser parser; |
|
const std::clock_t begin = clock(); |
|
std::shared_ptr<Node> rootNode = parser.parse(res.raw); |
|
const std::clock_t end = clock(); |
|
logDebug() << "main::setWindowContent - Parsed document in: " << std::fixed << ((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) << std::scientific << " seconds" << std::endl; |
|
|
|
// send NodeTree to window |
|
window->setDOM(rootNode); |
|
} else if (res.resourceType == ResourceType::TXT) { |
|
std::cout << "Rendering text document" << std::endl; |
|
std::shared_ptr<Node> rootNode = std::make_shared<Node>(NodeType::ROOT); |
|
std::shared_ptr<TagNode> tagNode = std::make_shared<TagNode>(); |
|
std::shared_ptr<TextNode> textNode = std::make_shared<TextNode>(); |
|
textNode->text = res.raw; |
|
tagNode->tag="p"; |
|
|
|
// bind text to tag |
|
textNode->parent = tagNode; |
|
tagNode->children.push_back(textNode); |
|
|
|
// bind tag to root |
|
tagNode->parent = rootNode; |
|
rootNode->children.push_back(tagNode); |
|
// send NodeTree to window |
|
window->setDOM(rootNode); |
|
} else { |
|
std::cout << "setWindowContent() - I don't know how to render non-html files" << std::endl; |
|
} |
|
return true; |
|
} |
|
|
|
bool isAbsolutePath(const std::string s); |
|
bool isAbsolutePath(const std::string s) { |
|
return (s.length() > 0 && s[0] == '/'); |
|
} |
|
|
|
bool fileExists(const std::string s); |
|
bool fileExists(const std::string s) { |
|
struct stat buf; |
|
return stat(s.c_str(), &buf) != -1; |
|
} |
|
|
|
int main(int argc, char *argv[]) { |
|
// show help msg when "--help" appears |
|
if (argv[1] && (strcmp(argv[1], "--help")==0)) { |
|
std::cout << "./netrunner [http://host.tld/|/path/to/file.html] [-log <error|warning|notice|info|debug>]" << std::endl; |
|
return 1; |
|
} |
|
# ifdef _WIN32 |
|
init_heap(); // the NT port requires it. We do it at startup now, to allow 2LSF to run at any time |
|
# endif |
|
std::cout << "/g/ntr - NetRunner build " << __DATE__ << ": rev-" << VERSION << " for " << PLATFORM << std::endl; |
|
|
|
// we need to set up OGL before we can setDOM (because component can't be constructed (currently) without OGL) |
|
// but should be after CommandLineParams incase we need to change some type of window config |
|
window->windowWidth = 1024; |
|
window->windowHeight = 640; |
|
window->init(); |
|
if (!window->window) { |
|
return 1; |
|
} |
|
//std::cout << "argc " << argc << std::endl; |
|
if (argc > 1) { |
|
initCLParams(argc, argv); |
|
// this isn't going to work |
|
std::string rawUrl = getCLParamByIndex(1); |
|
// if we do this here, shouldn't we do this in parseUri too? |
|
if (rawUrl.find("://") == rawUrl.npos) { |
|
// Path should always be absolute for file:// |
|
if (isAbsolutePath(rawUrl)) { |
|
rawUrl = "file://" + rawUrl; |
|
} else { |
|
auto absolutePath = std::string(getenv("PWD")) + '/' + rawUrl; |
|
if (fileExists(absolutePath)) { |
|
rawUrl = "file://" + absolutePath; |
|
} else { |
|
// Default to http if the file wasn't found |
|
rawUrl = "http://" + rawUrl; |
|
} |
|
} |
|
} |
|
//logDebug() << "pre URL parse [" << url << "]" << std::endl; |
|
window->currentURL = URL(rawUrl); |
|
logDebug() << "loading [" << window->currentURL << "]" << std::endl; |
|
if (!setWindowContent(window->currentURL)) { |
|
return 1; |
|
} |
|
} |
|
|
|
|
|
while (!glfwWindowShouldClose(window->window)) { |
|
//const std::clock_t begin = clock(); |
|
window->render(); |
|
glfwWaitEvents(); // block until something changes |
|
//const std::clock_t end = clock(); |
|
//std::cout << '\r' << std::fixed << (((static_cast<double>(end - begin)) / CLOCKS_PER_SEC) * 1000) << std::scientific << " ms/f " << std::flush; |
|
} |
|
return 0; |
|
}
|
|
|