Skip to content

Commit

Permalink
Fixed to work with Arduino, added a class to subscribe for paths
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszbielawski committed Jan 5, 2017
1 parent 85ed7fd commit fbd6f7d
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 46 deletions.
49 changes: 27 additions & 22 deletions AJSP.cpp
Expand Up @@ -9,13 +9,18 @@

#include <ctype.h>

#include <iostream>
#include <sstream>
#include <unistd.h>
#include <Arduino.h>

using namespace AJSP;
using namespace std;

static std::string localToString(uint32_t v)
{
char buffer[12];
snprintf(buffer, 12, "%d", v);
return std::string(buffer);
}

AJSP::Parser::Parser(): listener(nullptr), state(State::NONE), offset(0), lastKey("root")
{
stack.emplace(Entity::VALUE, State::NONE);
Expand Down Expand Up @@ -222,7 +227,7 @@ bool AJSP::Parser::parseArray(char c)
uint32_t newLastIndex = (uint32_t)currentElement.state;
newLastIndex++;
currentElement.state = (State) newLastIndex;
lastKey = to_string(newLastIndex);
lastKey = localToString(newLastIndex);
stack.emplace(Entity::VALUE, State::NONE);
return true;
}
Expand Down Expand Up @@ -311,24 +316,24 @@ bool AJSP::Parser::parseRaw(char c)

std::string getSEString(const AJSP::Parser::StackElement& se)
{
stringstream ss;
ss << "E: ";

switch (se.entity)
{
case AJSP::Parser::Entity::ARRAY: ss << "Array"; break;
case AJSP::Parser::Entity::KEY: ss << "Key"; break;
case AJSP::Parser::Entity::OBJECT:ss << "Object"; break;
case AJSP::Parser::Entity::STRING:ss << "String"; break;
case AJSP::Parser::Entity::RAW: ss << "Raw"; break;
case AJSP::Parser::Entity::VALUE: ss << "Value"; break;
}

ss << " ";

ss << int(se.state);

return ss.str();
// stringstream ss;
// ss << "E: ";
//
// switch (se.entity)
// {
// case AJSP::Parser::Entity::ARRAY: ss << "Array"; break;
// case AJSP::Parser::Entity::KEY: ss << "Key"; break;
// case AJSP::Parser::Entity::OBJECT:ss << "Object"; break;
// case AJSP::Parser::Entity::STRING:ss << "String"; break;
// case AJSP::Parser::Entity::RAW: ss << "Raw"; break;
// case AJSP::Parser::Entity::VALUE: ss << "Value"; break;
// }
//
// ss << " ";
//
// ss << int(se.state);
//
// return ss.str();
}

void AJSP::Parser::printStack()
Expand Down
57 changes: 57 additions & 0 deletions PathConstructor.cpp
@@ -0,0 +1,57 @@
/*
* PathConstructor.cpp
*
* Created on: 04.01.2017
* Author: caladan
*/

#include "PathConstructor.h"
#include <string.h>

PathConstructor::PathConstructor(uint32_t reserve, char separator): size(0), separator(separator)
{
buffer.reserve(reserve);
sizes.reserve(reserve / 8);
}

PathConstructor::~PathConstructor()
{
}

void PathConstructor::push(const std::string& s)
{
sizes.push_back(s.size()+1); //this part + separator
size += s.size() + 1;

buffer += separator;
buffer.append(s);

}

void PathConstructor::push(const char* s)
{
int len = strlen(s) + 1;
sizes.push_back(len);
size += len;

buffer += separator;
buffer.append(s);
}

void PathConstructor::pop()
{
if (sizes.size() == 0)
return; //nothing to pop

int sizeToRemove = sizes.back();
sizes.pop_back();
int startPos = buffer.size() - sizeToRemove;
buffer.erase(startPos, sizeToRemove);
size -= sizeToRemove;
}

void PathConstructor::clear()
{
sizes.clear();
buffer.clear();
}
34 changes: 34 additions & 0 deletions PathConstructor.h
@@ -0,0 +1,34 @@
/*
* PathConstructor.h
*
* Created on: 04.01.2017
* Author: caladan
*/

#ifndef PATHCONSTRUCTOR_H_
#define PATHCONSTRUCTOR_H_

#include <string>
#include <vector>

class PathConstructor {
public:
PathConstructor(uint32_t reserve = 64, char separator = '/');
virtual ~PathConstructor();

void push(const std::string& s);
void push(const char* s);

void pop();

void clear();

const std::string& getPath() {return buffer;}
private:
std::string buffer;
size_t size;
std::vector<uint16_t> sizes;
char separator;
};

#endif /* PATHCONSTRUCTOR_H_ */
27 changes: 8 additions & 19 deletions PathPrinter.cpp
Expand Up @@ -19,25 +19,25 @@ using namespace std;
void PathPrinter::arrayStart()
{
//cout << "AS" << stack.size() << endl;
stack.emplace_back(parser->getLastKey());
pathConstructor.push(parser->getLastKey());
}

void PathPrinter::arrayEnd()
{
//cout << "AE" << stack.size() << endl;
stack.pop_back();
pathConstructor.pop();
}

void PathPrinter::objectStart()
{
//cout << "OS" << stack.size() << endl;
stack.emplace_back(parser->getLastKey());
pathConstructor.push(parser->getLastKey());
}

void PathPrinter::objectEnd()
{
//cout << "OE" << stack.size() << endl;
stack.pop_back();
pathConstructor.pop();
}

void PathPrinter::key(const std::string& key)
Expand All @@ -47,26 +47,15 @@ void PathPrinter::key(const std::string& key)

void PathPrinter::value(const std::string& value)
{
//cout << "V" << stack.size() << endl;
//append the last key here, don't push it on the stack and pop it...
cout << getCurrentPath() << "/" << parser->getLastKey() << ": " << value << endl;
pathConstructor.push(parser->getLastKey());
cout << pathConstructor.getPath() << "/" << parser->getLastKey() << ": " << value << endl;
pathConstructor.pop();
}


const std::string& PathPrinter::getCurrentPath()
{
bufferedCurrentPath.clear();

for (auto& e: stack)
{
bufferedCurrentPath.append(e);
bufferedCurrentPath.append("/");
}

if (bufferedCurrentPath.size())
bufferedCurrentPath.pop_back();

return bufferedCurrentPath;
return pathConstructor.getPath();
}


Expand Down
7 changes: 2 additions & 5 deletions PathPrinter.hpp
Expand Up @@ -9,6 +9,7 @@
#define PATHPRINTER_HPP_

#include "AJSP.hpp"
#include "PathConstructor.h"

class PathPrinter: public AJSP::Listener
{
Expand All @@ -26,14 +27,10 @@ class PathPrinter: public AJSP::Listener
virtual void value(const std::string& value);

virtual void done();


private:
const std::string& getCurrentPath();

std::string bufferedCurrentPath;

std::vector<std::string> stack;
PathConstructor pathConstructor;
};

#endif /* PATHPRINTER_HPP_ */

0 comments on commit fbd6f7d

Please sign in to comment.