Skip to content

Commit

Permalink
chopChunkAndGetAlias, input handler for the controller, small refacto…
Browse files Browse the repository at this point in the history
…ring.
  • Loading branch information
cristianfrasineanu committed Nov 20, 2016
1 parent 52afc19 commit 4dd1bf5
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 23 deletions.
79 changes: 64 additions & 15 deletions app/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,59 @@
string Controller::viewInputFormat = "@input-";
string Controller::viewOutputFormat = "@output-";

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

// Show for now
// TODO use the input/output format to parse the chunk
}

void Controller::prepareViewInput()
void Controller::prepareView()
{
// Prompt the user for input
}
// Preserve the original one
string copyChunk = this->viewChunk;

string &Controller::getViewInputFormat()
{
return Controller::viewInputFormat;
// Determine the order of the input/output from the user.
while (this->hasInput(copyChunk) || this->hasOutput(copyChunk))
{
if (!this->hasOutput(copyChunk))
{
this->chopChunkAndGetAlias(copyChunk);
}
if (!this->hasInput(copyChunk))
{
// Gather all the required variables from the model and replace them in chunk at once (no need to wait for input), thus discarding the dummy chunk
// this->bringAllDataInChunk()
}
else
{
if (copyChunk.find(Controller::viewInputFormat) < copyChunk.find(Controller::viewOutputFormat))
{
this->chopChunkAndGetAlias(copyChunk);
}
else
{
// this->bringDataOnceInChunk()
}
}
}

// Show the rest
if (copyChunk.size())
{
cout << copyChunk
<< endl;
}
}

string & Controller::getViewOutputFormat()
void Controller::prepareViewInput(string &subChunk, string &inputAlias)
{
return Controller::viewOutputFormat;
string userInput;

cout << subChunk << " ";
cin >> userInput;
cout << endl;

this->userInputs[inputAlias] = userInput;
}

Controller::Controller()
Expand All @@ -35,6 +66,7 @@ Controller::Controller()
strcpy(this->controllerName, "NO_CONTROLLER");
this->viewChunk = "";
this->controllerAttributions = {};
this->userInputs = {};
}

Controller::Controller(char *viewName, string &viewChunk, string &ViewExtension)
Expand All @@ -46,8 +78,9 @@ Controller::Controller(char *viewName, string &viewChunk, string &ViewExtension)
strcpy(this->controllerName, controllerName.c_str());
this->viewChunk = viewChunk;
this->controllerAttributions = {};
this->userInputs = {};

this->prepareView();
(this->hasInput(viewChunk) || this->hasOutput(viewChunk)) ? this->prepareView() : this->justShow();
}

vector<string>& Controller::getControllerAttributions()
Expand All @@ -60,6 +93,17 @@ char *Controller::getControllerName()
return this->controllerName;
}

void Controller::chopChunkAndGetAlias(string &chunk)
{
// Splice the alias (take the part after the "-" in the template string).
string inputAlias = chunk.substr(chunk.find(Controller::viewInputFormat) + Controller::viewInputFormat.size(),
chunk.find("\n", chunk.find(Controller::viewInputFormat)) - (chunk.find(Controller::viewInputFormat) + Controller::viewInputFormat.size()));

this->prepareViewInput(chunk.substr(0, chunk.find(Controller::viewInputFormat) - 1), inputAlias);

chunk.erase(0, chunk.find(inputAlias) + inputAlias.size() + 2);
}

// One Controller at a time, you expected more?
void Controller::operator=(const Controller &controller)
{
Expand All @@ -75,7 +119,12 @@ void Controller::operator=(const Controller &controller)

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

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

Controller::~Controller()
Expand Down
14 changes: 9 additions & 5 deletions app/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,26 @@ class Controller {

char *controllerName;
vector<string> controllerAttributions;
map<string, string> userInputs;
string viewChunk;
// Model model;

void justShow();
void prepareView();
void prepareViewInput();
void prepareViewInput(string &, string &);
// 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 &);

bool hasInput(string &);
bool hasOutput(string &);

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

void chopChunkAndGetAlias(string &);

void operator=(const Controller &);

~Controller();
Expand Down
2 changes: 1 addition & 1 deletion app/Helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void sleepAndClearBuffer(unsigned delay)
Sleep(delay);

// Clear the keyboard buffer during the sleep cycle (if there are any keys pressed),
// so as long as the keyboard is hit during that period, the input won't be taken into consideration.
// so whatever keys are hit are discarded
while (_kbhit())
{
_getch();
Expand Down
4 changes: 2 additions & 2 deletions app/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

//---View---
//----------
// A view has attached to it a range of available actions or certain verbs that are used by the console to do some action.
// It's basically a dictionary holding the options mapping.
// A view has attached to it a range of available actions or verbs
// that are used by the console to do a certain action.
map<string, map<char, string>> View::ViewsOptions = { { "",{ { '\0', "" } } } };
string View::ViewExtenstion = ".view";

Expand Down

0 comments on commit 4dd1bf5

Please sign in to comment.