Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 22 additions & 34 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,40 +1,28 @@
CXXFLAGS=-std=c++11 -Wall -pedantic
ifeq "$(shell uname)" "Darwin"
LDFLAGS= -lglut -framework OpenGL
CXX=g++
LDFLAGS := -lglut -framework OpenGL
CXX := g++
else
LDFLAGS= -lglut -lGL -lGLU -lX11
CXX=clang++-3.9
LDFLAGS := -lglut -lGL -lGLU -lX11 -lc++ -lc++abi
CXX := clang++
CXXFLAGS += -stdlib=libc++
endif

all: http.o Node.o Parser.o LayoutEngine.o main.o render.o box.o text.o loadimage.o
$(CXX) $(LDFLAGS) render.o box.o text.o loadimage.o http.o Node.o Parser.o LayoutEngine.o main.o -o main

render.o:
$(CXX) -c -std=c++11 render/render.cpp

box.o:
$(CXX) -c -std=c++11 render/box.cpp

text.o:
$(CXX) -c -std=c++11 render/text.cpp

loadimage.o:
$(CXX) -c -std=c++11 images/loadimage.cpp

main.o:
$(CXX) -c -std=c++11 main.cpp

LayoutEngine.o:
$(CXX) -c -std=c++11 layout/LayoutEngine.cpp

Parser.o:
$(CXX) -c -std=c++11 dom/Parser.cpp

Node.o:
$(CXX) -c -std=c++11 dom/Node.cpp

http.o:
$(CXX) -c -std=c++11 http/http.cpp
OBJS := \
main.o \
http/http.o \
images/loadimage.o \
dom/Node.o \
dom/Parser.o \
layout/LayoutEngine.o \
render/render.o \
render/box.o \
render/text.o

all: main

main: $(OBJS)
$(CXX) $(LDFLAGS) $^ -o $@

clean:
rm *.o main
rm -f $(OBJS) main
5 changes: 5 additions & 0 deletions dom/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ Node * Parser::findNode(Node *n, std::string name) {
return n;
} else {
for (uint i = 0; i < n->children.size(); i++) {
// FIXME
// why is this a loop? this will just call
// findNode(n->children[0], name) and return that
return findNode(n->children[i], name);
}
}
Expand All @@ -155,6 +158,8 @@ std::string Parser::findCSS(Node *r) {
return r->textData;
} else if (r->children.size() > 1) {
for (uint i = 1; i < r->children.size(); i++) {
// FIXME again, I'm not seeing why this is in a loop since it
// returns right away
return findCSS(r->children[i-1]) + findCSS(r->children[i]);
}
} else if (r->children.size() == 1) {
Expand Down
6 changes: 3 additions & 3 deletions dom/Parser.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef PARSER_H
#define PARSER_H

#include "Node.h"
#include <map>
#include <string>

#ifndef PARSER_H
#define PARSER_H

class Parser {
public:
Node *root;
Expand Down
23 changes: 8 additions & 15 deletions http/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,35 @@ extern "C" {
}
#include <iostream>
#include <string>
#include <vector>
#include <strings.h>
#include <string.h>
#include <unistd.h>

#define BUFFER_SIZE 10000

void Http::connect(std::string url) {
extern int h_errno;
const char *nameC = url.c_str();
struct hostent host = *gethostbyname(nameC);
if (h_errno != 0) std::cout<<"gethostbyname failure\n";
if (h_errno != 0) std::cerr << "gethostbyname failure\n";

struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
bcopy((char*)host.h_addr, (char*)&addr.sin_addr.s_addr, host.h_length);

Http::sck = socket(AF_INET, SOCK_STREAM, 0);
if (Http::sck == -1) std::cout<<"socket creation failure\n";
if (Http::sck == -1) std::cerr << "socket creation failure\n";
if (::connect(Http::sck, (const struct sockaddr*)&addr, sizeof(addr)) != 0) {
std::cout<<"connection failure\n";
} else {
//std::cout<<"successful connection\n";
std::cerr << "connection failure\n";
}
}

std::string Http::makeRequest(const char *req) {
constexpr auto BUFFER_SIZE = 10000;
send(Http::sck, req, std::string(req).length(), 0);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::string(req).length() should be std::strlen(req) mah b.

char* buffer = (char *)malloc(BUFFER_SIZE);
memset(buffer, 0, BUFFER_SIZE);
std::string response;
sleep(1);
read(Http::sck, buffer, BUFFER_SIZE-1);
response = std::string(buffer);
//std::cout<<response;
return response;
std::vector<char> buffer(BUFFER_SIZE);
read(Http::sck, buffer.data(), buffer.size());
return {buffer.data()};
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you really were to malloc, the most C++-ish thing here would've been
auto buffer = static_cast<char*>(std::malloc(BUFFER_SIZE));

}

std::string Http::dropHeaders(std::string doc) {
Expand Down
6 changes: 4 additions & 2 deletions layout/LayoutData.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Frame Tree data encapsulation class
*/

#include <array>

class LayoutData {
public:

Expand All @@ -16,8 +18,8 @@ class LayoutData {
int width = 0;
int height = 0;

int topColor[3];
int bottomColor[3];
std::array<int, 3> topColor;
std::array<int, 3> bottomColor;

const char* text = "wut";
int textsize = 2;
Expand Down
33 changes: 17 additions & 16 deletions layout/LayoutEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ std::vector<Node*> LayoutEngine::DFS() {
Node* n = stack.top();
stack.pop();
result.push_back(n);
for (int i = n->children.size() - 1; i >=0; i--)
stack.push(n->children[i]);
for (auto it = n->children.rbegin(), end = n->children.rend();
it != end;
++it) {
stack.push(*it);
}
}
return result;
}
Expand All @@ -38,24 +41,23 @@ std::vector<LayoutData> LayoutEngine::toLayoutData() {
{"pink", 6}
};

int colorVals[7][3] = {
{255,255,0},
{0,0,255},
{255,0,0},
{0,0,0},
{0,255,255},
{0,128,0},
{255,192,203}
};
std::array<std::array<int, 3>, 7> colorVals = {{
{{255,255,0}},
{{0,0,255}},
{{255,0,0}},
{{0,0,0}},
{{0,255,255}},
{{0,128,0}},
{{255,192,203}}
}};

std::vector<Node*> nodes = DFS();
std::vector<LayoutData> result;
for (Node* n : nodes) {
std::string bgcolor = style[n->attributes["class"]]["background"];
if (bgcolor.empty()) {
bgcolor = style[n->attributes["class"]]["background-color"];
bcopy(colorVals[colorNames[bgcolor]], n->ld.topColor, 3*sizeof(int));
bcopy(colorVals[colorNames[bgcolor]], n->ld.bottomColor, 3*sizeof(int));
n->ld.bottomColor = n->ld.topColor = colorVals[colorNames[bgcolor]];
} else {
debug(1);
uint first = bgcolor.find("("); //first color
Expand All @@ -66,8 +68,8 @@ std::vector<LayoutData> LayoutEngine::toLayoutData() {
debug(bgcolor);
debug(color1);
debug(color2);
bcopy(colorVals[colorNames[color1]], n->ld.topColor, 3*sizeof(int));
bcopy(colorVals[colorNames[color2]], n->ld.bottomColor, 3*sizeof(int));
n->ld.topColor = colorVals[colorNames[color1]];
n->ld.bottomColor = colorVals[colorNames[color2]];
}
n->ld.text = n->textData.c_str();
result.push_back(n->ld);
Expand Down Expand Up @@ -189,7 +191,6 @@ void LayoutEngine::layout(Node* n, int voff_, int hoff_) {
}

LayoutData LayoutEngine::layoutText(std::string in) {
in.length();
LayoutData d;
// d.width = 100;
d.width = glutBitmapLength(GLUT_BITMAP_HELVETICA_12, (unsigned char *)d.text);
Expand Down
3 changes: 1 addition & 2 deletions layout/LayoutEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
* Layout Engine
*/

typedef std::map<std::string, std::map<std::string, std::string> > css;

class LayoutEngine {
using css = std::map<std::string, std::map<std::string, std::string>>;
public:
const int BROWSER_WIDTH = 960;
Node* root;
Expand Down
3 changes: 2 additions & 1 deletion render/box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ using std::vector;

Box::Box(const char *text, bool image, int voff, int hoff,
int width, int height,
int size, int *topcolori, int *botcolori)
int size, const std::array<int, 3>& topcolori,
const std::array<int, 3>& botcolori)
{
// debug(111);
this->text = text;
Expand Down
6 changes: 4 additions & 2 deletions render/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "render.h"
#include "../images/loadimage.h"
#include <vector>
#include <array>

struct Point
{
Expand All @@ -23,8 +24,9 @@ class Box
bool image;
GLuint texture;
public:
Box(const char *text, bool image, int voff, int hoff, int width, int height,
int size, int *boxtop, int *boxbottom);
Box(const char *text, bool image, int voff, int hoff, int width,
int height, int size, const std::array<int, 3>& boxtop,
const std::array<int, 3>& boxbottom);
~Box();
void draw(void);
};
Expand Down