Note that I do not give the code in every change but I add a lot of comments in the code so people can see exactly when I added a relevant code.
Files to add to the project are:
1)main.cpp(include main function that return 0)
2)protos.h(include nothing) except a line the compiler enter to h files automatically
3)command.cpp(main function is going to call it and first function that I plan to use is readCommands that I will define later)
4)defines.h that has basic definitions(not variables but constants or new name that I like to use for type of variables instead of long existing names in the language)
5)globals.h that is file of global variables
6)extglobals.h that is a file of extra global variables that are basically same variables as 5(reason that I need it twice is because these variables are not external variable to main but external variables to other files that use them.
7)main,cpp is the only file that include globals.h because I want the global variables to be non external to main.cpp
8)every other cpp file include extglobals.h if I want to allow it to use global variables.
I want to allow command.cpp to use global variables so I include extglobals.h in it.
9)globals.h include defines.h because I may use basic definition to describe global variables(for example if I define C to be 589 I can have A[C] instead of A[589] in the global variables)
10)extglobals.h include defines.h for the same reason
11)I want main to include "protos.h" otherwise it does not know the structure of readCommands function and cannot call it.
Note that it make sense to add other files to help debugging but this is only in the next rewrite.
12)define the prototype of the function readCommands in protos.h
13)define function readCommands in command.cpp with empty code.
14)call readCommands in main.cpp
15)define variable nextc in readCommands(nextc means next command)
16)include iostream in command.cpp that I need for the loop at step 17
17)add a loop that simply wait for the user to enter something when only after the user click enter the program quit
I have now a function readCommands that simply wait and finish when the user enter something.
basically I have 2 cases about the value of nextc
a)nextc is the character of a new line so I need to do something with the input
b)nextc is not a new line so I need to memoprize the input
18)adding a code if else for the 2 cases
19)first I want to deal with the else part and memorize the string that the user define
I decide about a constant 256 that is the maximal size of the input
20)I add an array with MAX_CMD_BUFF to globals.h and extglobals.h
21)I add a variable for the size of the input to globals.h and extglobals.h
22)I add inside the else that I entered in command.cpp a code that put the user command in the buffer and warn the user if the buffer if full
23)I add BOOLTYPE to be an integer in defines.h(I want a new function to enter true or false so I do not like to use int
content of defines.h
24)I add the function doCommand in command.cpp that get the user input and return false only when the user asked to quit the program
not sure if I need CMD_BUFF_COUNT = '\0'; when I exit the program but it is in the code they give in the site
http://web.archive.org/web/201201120650 ... 20commands
25)using doCommand inside readCommand and deleting return that I added at step 17 that I now do not need because doCommand decide if to exit the program.
26)adding version number in defines.h
27)printing the version in the beginning of the program.
28)printing wt before the user enter a new command when I finished to take care of the old command
29)printing command not implemented if I did nothing
30)printing some information if the user ask for help or h or ?
31)added the types U64 and BitMap in defines.h in order to work with 64 bit integers without the ugly C code
code now after all of these things is:
1)main.cpp
Code: Select all
#include "globals.h" //added this line in step 7
#include "protos.h"//added this line in step 11
int main()
{
readCommands();//added this line in step 14
return 0; //added this file this function and this line in step 1
}
Code: Select all
#pragma once //added this file in step 4
#define MAX_CMD_BUFF 256 // Console command input buffer added this line in step 19
typedef int BOOLTYPE;//added this line in step 23
#define chess_prog_version "analyzer 0.0, Copyright (C) 2018, Uri Blass Stef Luijten"//added this line in step 26
typedef unsigned long long U64;// added this line in step 31
typedef unsigned long long BitMap;//added this line in step 31
Code: Select all
#pragma once //added this file in step 5
#include "defines.h" //added this line in step 9
char CMD_BUFF[MAX_CMD_BUFF];//added this line in step 20 and same for extglobals.h
int CMD_BUFF_COUNT = 0;//added this line in step 21 and same for extglobals.h
Code: Select all
#pragma once //added this file in step 6
#include "defines.h"//added this line in step 10
extern char CMD_BUFF[];//added this line in step 20
extern int CMD_BUFF_COUNT; //added this line in step 21
Code: Select all
#pragma once //added this file in step 2
void readCommands();//added this line in step 12
Code: Select all
//added this file in step 3
#include <iostream>//added this line in step 16
#include "extglobals.h"//added this line in step 8
BOOLTYPE doCommand(const char *buf)//added this function in step 24
{
if ((!strcmp(buf, "exit")) || (!strcmp(buf, "quit"))) //added in step 24
{
CMD_BUFF_COUNT = '\0'; //added in step 24
return false; //added in step 24
}
// ================================================================= //added in step 30
// help, h, or ?: show this help //added in step 30
// ================================================================= //added in step 30
if ((!strcmp(buf, "help")) || (!strcmp(buf, "h")) || (!strcmp(buf, "?")))//added in step 30
{//added in step 30
printf(" help\n ");//added in step 30
printf("exit : exit program\n ");//added in step 30
printf("help,h or ? : show this help\n ");//added in step 30
printf("quit : exit program\n ");//added in step 30
CMD_BUFF_COUNT = '\0';//added in step 30
return true;//added in step 30
}
printf("command not implemented: %s\n", buf);//added this line in step 29
CMD_BUFF_COUNT = '\0';//added in step 24
return true;//added in step 24
}
void readCommands() //added this line in step 13
{//added this line in step 13
int nextc;//added this line in step 15
printf(chess_prog_version"\n");//added this line in step 27
printf("wt> ");//added this line in step 28
while ((nextc = getc(stdin)) != EOF)//added this line in step 17(wait for the user to click something with enter
{//added this line in step 17
if (nextc == '\n') //added this line in step 18
{//added this line in step 18
CMD_BUFF[CMD_BUFF_COUNT] = '\0';//added this line in step 25
while (CMD_BUFF_COUNT)//added this loop in step 25
{
if (!doCommand(CMD_BUFF)) return;//added this line in step 25
}
printf("wt> ");//added this line in step 28
}//added this line in step 18
else//added this line in step 18
{//added this line in step 18
//added next 6 lines in step 22
if (CMD_BUFF_COUNT >= MAX_CMD_BUFF - 1)
{
printf("Warning command buffer is full !!\n");
CMD_BUFF_COUNT = 0;
}
CMD_BUFF[CMD_BUFF_COUNT++] = nextc;
}//added this line in step 18
//added return here in step 17 otherwise it is an infinite loop
//deleted return here in step 25 because after step 25 doCommand decide if to exit the program and there is no infinite loop
}//added this line in step 17
}//added this line in step 13