In this step I add 2 files
1)extglobals.h that include the global variables that I use and I want cpp files that are not the main function to recognize that they are external variables(they are not external only in main.cpp that include globals.h directly and including globals.h in another file cause an error because I cannot include the same file twice in the project).
2)commands.cpp that is the function that read the commands
main file is now very short and here is the code
Code: Select all
#include <stdio.h>
#include "defines.h"//added this file in step 7
#include "globals.h"//added this file in step 8
#include "protos.h"//added this file in step 9
//deleted functions and saved them in command.cpp in step 10
int main()
{
std::cout << "my program" << std::endl;// do both printing my program and printing a new line in one line of code
readCommands();//calling a function that is defined in the file(I will later put the function in a different file)
return 0;
}
Code: Select all
#pragma once
#include "defines.h"
extern char CMD_BUFF[];
extern int CMD_BUFF_COUNT;
Code: Select all
#include <iostream>
#include "defines.h"
#include "extglobals.h"
#include "protos.h"
BOOLTYPE doCommand(const char *buf)
{
//this function now only look at the string of the input and return false only if it is identical to the word exit or quit
CMD_BUFF_COUNT = '\0';
if ((!strcmp(buf, "exit")) || (!strcmp(buf, "quit")))
{
CMD_BUFF_COUNT = '\0';
return false;
}
return true;
}
void readCommands()
{
int nextc;//next command
while ((nextc = getc(stdin)) != EOF)//waiting for the user to print something with enter to going into the loop at this point
{
//the program finished to calculate getc because the user entered some chars and clicked enter and got an input
//nextc is the first char that the user entered
if (nextc == '\n') //we are at the end of the input that the user entered
{
CMD_BUFF[CMD_BUFF_COUNT] = '\0';
while (CMD_BUFF_COUNT)
if (!doCommand(CMD_BUFF)) return;
}
else
{
//we are not at the end of the user input so we put the user input in the buffer in case that we have space
if (CMD_BUFF_COUNT >= MAX_CMD_BUFF - 1)
{
std::cout << "Warning: command buffer full !! " << std::endl;
CMD_BUFF_COUNT = 0;//we do not have space so we practically delete the user input and start from scratch.
}
CMD_BUFF[CMD_BUFF_COUNT++] = nextc;
}
}
}