Skip to content

Commit

Permalink
Controller class, improved helpers and view renderer. Delegated the i…
Browse files Browse the repository at this point in the history
…nterpolation decision to the controller from view.
  • Loading branch information
cristianfrasineanu committed Nov 20, 2016
1 parent db39415 commit 5b762fb
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 53 deletions.
7 changes: 1 addition & 6 deletions app/Bootstrap.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <conio.h>

#include "Console.h"

using namespace std;
Expand All @@ -10,7 +8,6 @@ void main()
{
View::loadViewsOptions();
Console console;

do
{
try
Expand All @@ -24,9 +21,7 @@ void main()
}
catch (const invalid_argument &e)
{
cout << endl
<< e.what()
<< endl;
cout << e.what();

sleepAndClearBuffer(console.getDelay());
console.reloadView();
Expand Down
40 changes: 23 additions & 17 deletions app/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ Console::Console()

map<char, string> availableOptions = View::getViewsOptions().find(Console::initialView)->second;

this->currentView = View(Console::initialView, availableOptions, false);
this->currentView = View(Console::initialView, availableOptions);
this->delay = 2000;

this->loadActions();
this->loadViews(Console::viewsFolder);

if (find(this->loadedViews.begin(), this->loadedViews.end(), this->currentView.getViewName()) != this->loadedViews.end())
{
this->renderView(this->currentView);
this->renderView();
}
else
{
Expand All @@ -41,15 +41,15 @@ Console::Console(char *mode)
string viewName = "debug.view";
map<char, string> availableOptions = View::getViewsOptions().find(Console::initialView)->second;

this->currentView = View(viewName, availableOptions, true);
this->currentView = View(viewName, availableOptions);
this->delay = 2000;

this->loadActions();
this->loadViews(Console::viewsFolder);

if (find(this->loadedViews.begin(), this->loadedViews.end(), this->currentView.getViewName()) != this->loadedViews.end())
{
this->renderView(this->currentView);
this->renderView();
}
else
{
Expand Down Expand Up @@ -100,7 +100,7 @@ void Console::loadViews(const fs::path &viewsFolder)
while (it != endit)
{
if (fs::is_regular_file(*it)
&& it->path().extension() == ".view")
&& it->path().extension().string() == View::getViewExtension())
{
this->loadedViews.push_back(it->path().filename());
}
Expand All @@ -110,25 +110,34 @@ void Console::loadViews(const fs::path &viewsFolder)

void Console::loadActions()
{
vector<char> actions = { 'q', 'b', 'n' };
vector<char> actions = { 'q', 'b', 'n', 'c' };

this->actions = actions;
}

void Console::renderView(View &view)
void Console::renderView()
{
string content;
string path = Console::viewsFolder;
path.append("\\").append(view.getViewName());
path.append("\\").append(this->currentView.getViewName());

stringstream buffer;
ifstream viewFile(path, ios::out);

if (viewFile.is_open())
{
buffer << viewFile.rdbuf();
this->currentView.setRawFormat(buffer.str());

cout << buffer.str()
<< endl;
if (Controller::hasInput(buffer.str()))
{
this->theController = Controller(this->currentView.getViewName(), buffer.str(), View::getViewExtension());
}
else
{
cout << buffer.str()
<< endl;
}

buffer.clear();

Expand All @@ -144,13 +153,10 @@ void Console::renderNextView()
// Cache the current view
this->previousViews.push_back(this->currentView);

this->currentView = View(nextView, nextOptions, false);
this->currentView = View(nextView, nextOptions);

//TODO: detemine if the view has interpolation enabled in order to replace the template strings or to splice the view in certain places (prepareView)
clearScreen();
this->renderView(this->currentView);

//Show it directly for now...
this->renderView();
}

void Console::renderPreviousView()
Expand All @@ -159,15 +165,15 @@ void Console::renderPreviousView()
{
clearScreen();
this->currentView = this->previousViews.back();
this->renderView(this->currentView);
this->renderView();
this->previousViews.pop_back();
}
}

void Console::reloadView()
{
clearScreen();
this->renderView(this->currentView);
this->renderView();
}

bool Console::takeActionIfAny()
Expand Down
20 changes: 11 additions & 9 deletions app/Console.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include <filesystem>
#include <map>

#include "Helpers.h"
#include "View.h"
#include "Helpers.h"
#include "Controller.h"

using namespace std;
namespace fs = std::experimental::filesystem::v1;
Expand All @@ -15,12 +16,13 @@ class Console {
private:
static string initialView;
static string viewsFolder;

char *mode;
char lastInput;
unsigned delay;
bool exit;
View currentView;
Controller theController;
vector<View> previousViews;

vector<fs::path> loadedViews;
Expand All @@ -33,20 +35,20 @@ class Console {
Console(char *);

vector<char> &getActions();

void showPrompt();
char getLastInput();

void setLastInput(char);
bool takeActionIfAny();
bool shouldExit();
void breakTheLoop();

void showPrompt();
unsigned getDelay();

void renderView(View &);
void renderView();
void renderNextView();
void renderPreviousView();
void reloadView();

bool takeActionIfAny();
bool shouldExit();
void breakTheLoop();

~Console();
};
80 changes: 80 additions & 0 deletions app/Controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "Controller.h"

//---Controller---
//----------------
string Controller::viewInputFormat = "@input-";
string Controller::viewOutputFormat = "@output-";

void Controller::prepareView()
{
cout << this->viewChunk.c_str()
<< endl;
}

void Controller::prepareViewInput()
{
// Prompt the user for input
}

string &Controller::getViewInputFormat()
{
return Controller::viewInputFormat;
}

string & Controller::getViewOutputFormat()
{
return Controller::viewOutputFormat;
}

Controller::Controller()
{
this->controllerName = new char[strlen("NO_CONTROLLER") + 1];
strcpy(this->controllerName, "NO_CONTROLLER");
this->viewChunk = "";
this->controllerAttributions = {};
}

Controller::Controller(char *viewName, string &viewChunk, string &ViewExtension)
{
string controllerName = viewName;
controllerName.erase(controllerName.find(ViewExtension), ViewExtension.size()).append("Controller");

this->controllerName = new char[strlen(controllerName.c_str()) + 1];
strcpy(this->controllerName, controllerName.c_str());
this->viewChunk = viewChunk;
this->controllerAttributions = controllerAttributions;

this->prepareView();
}

vector<string>& Controller::getControllerAttributions()
{
return this->controllerAttributions;
}

char *Controller::getControllerName()
{
return this->controllerName;
}

void Controller::operator=(const Controller &controller)
{
if (controller.controllerName != NULL)
{
delete[] this->controllerName;
this->controllerName = new char[strlen(controller.controllerName) + 1];
strcpy(this->controllerName, controller.controllerName);
}
this->viewChunk = controller.viewChunk;
this->controllerAttributions = controller.controllerAttributions;
}

bool Controller::hasInput(string &raw)
{
return raw.find(Controller::getViewInputFormat()) != string::npos;
}

Controller::~Controller()
{
delete[] this->controllerName;
}
36 changes: 36 additions & 0 deletions app/Controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

#include "Helpers.h"

using namespace std;

class Controller {
private:
static string viewInputFormat;
static string viewOutputFormat;

char *controllerName;
vector<string> controllerAttributions;
string viewChunk;

void prepareView();
void prepareViewInput();
// TODO: interpolate the view strings for output with the according variables received from the model.
public:
static string &getViewInputFormat();
static string &getViewOutputFormat();
static bool hasInput(string &);

Controller();
Controller(char *, string &, string &);

vector<string> &getControllerAttributions();
char *getControllerName();

void operator=(const Controller &);

~Controller();
};
32 changes: 26 additions & 6 deletions app/Helpers.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
#include <iostream>
#include <vector>
#include <map>
#include <Windows.h>
#include <conio.h>

#include "Helpers.h"

bool isInCharStringMap(const map<char, string> &haystack, char needle)
Expand Down Expand Up @@ -42,3 +36,29 @@ void clearScreen()
{
system("cls");
}

void log(char *data, char *identifier, char *situation)
{
cout << "---///////////---" << endl;
cout << identifier << " is currently " << data << " when " << situation << endl;
cout << "---///////////---" << endl;
}

void log(string &data, char *identifier, char *situation)
{
cout << "---///////////---" << endl;
cout << identifier << " is currently " << data.c_str() << " when " << situation << endl;
cout << "---///////////---" << endl;
}

void log(vector<string> &data, char *identifier, char *situation)
{
cout << "---///////////---" << endl;
cout << identifier << " is currently ";
for (vector<string>::iterator it = data.begin(); it != data.end(); it++)
{
cout << (*it).c_str() << " ";
}
cout << " when " << situation << endl;
cout << "---///////////---" << endl;
}
7 changes: 6 additions & 1 deletion app/Helpers.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <iostream>
#include <vector>
#include <map>
#include <conio.h>
#include <Windows.h>

using namespace std;

Expand All @@ -9,4 +11,7 @@ bool isInCharVector(const vector<char> &, char);
bool isInIntVector(const vector<int> &, int);
bool isInStringVector(const vector<string> &, string);
void sleepAndClearBuffer(unsigned delay);
void clearScreen();
void clearScreen();
void log(char *, char *, char *);
void log(string &, char *, char *);
void log(vector<string> &, char *, char *);
Loading

0 comments on commit 5b762fb

Please sign in to comment.