Skip to content

Instantly share code, notes, and snippets.

@xypnox
Created February 10, 2017 12:56
Show Gist options
  • Save xypnox/31a5549da2b97c1e10da386e93668eb8 to your computer and use it in GitHub Desktop.
Save xypnox/31a5549da2b97c1e10da386e93668eb8 to your computer and use it in GitHub Desktop.
maybe this will work!
#include <iostream.h>
#include <string.h>
#include <fstream.h>
// Classes
class Todo{
public:
char content[200];
char status;
int _index;
};
class List {
public:
Todo list[20];
char title[100];
char tags[10][20];
int _listIndex; // Variable to count the filling of list
int _tagIndex; // Variable to count the filling of tags
int _hasTags;
List(); // Asks for #title(required) and #tags(can be skipped).
List(char Title[100]); // Initialize the #list with #title
void enter(); // Enter the info given by user
void addTag(char Tag[20]); // Add the tag to Tags
void setTitle(char Title[100]); // Set the Title given
void view(); // View the list's #title #tags and #content
void indexView(); // View the contents with the index
void tagView(); // View only Title and tags
void todoView(int); // View the todo of index passed
void tagIndexView(); // View the Title and Tags with the index
void append(); // append a new ToDo in the #list
void removeTodo(int); // delete the list at index passed
void removeTag(int); // delete the tag at index passed
void changeStatus(int index, char status); // Changes Status of the Item of #index to #status
~List() {}
};
List::List(){
_listIndex = 0;
_tagIndex = 0;
_hasTags = 0;
}
List::List(char Title[100]) {
_listIndex = 0;
_tagIndex = 0;
_hasTags = 0;
strcpy(title, Title);
}
void List::enter(){
cout << "Enter title for your list" << endl << " +> " ;
cin.getline(title, sizeof(title));
char choice[10];
cout << "Do you want some tags? (yes/no)" << endl << " ?> " ;
cin.getline(choice, sizeof(choice));
if (!(strcmp(choice, "yes")*strcmp(choice, "y"))) {
char tagTemp[20];
cout << "Enter the tag one at a time" << endl << "\'d\' when done " << endl;
cout << " +> " ;
while (1) {
cin.getline(tagTemp, sizeof(tagTemp));
if (strcmp(tagTemp, "d") != 0) {
addTag(tagTemp);
cout << " +> " ;
}
else {
break;
}
}
_hasTags = 1;
}
// cout << "Has tags = " << _hasTags << endl;
}
void List::addTag(char Tag[20]) {
strcpy(tags[_tagIndex++], Tag);
}
void List::setTitle(char Title[100]) {
strcpy(title, Title);
}
void List::view() {
cout << title << endl;
cout << "============" << endl;
if (_hasTags) {
cout << "Tags : ";
for (int i = 0; i < _tagIndex; i++) {
cout << tags[i] << " ";
}
cout << endl << endl;
}
for (int i = 0; i < _listIndex; i++) {
cout << " [" << list[i].status << "] ";
cout << list[i].content << endl;
}
}
void List::indexView() {
cout << title << endl;
cout << "============" << endl;
if (_hasTags) {
cout << "Tags : ";
for (int i = 0; i < _tagIndex; i++) {
cout << tags[i] << " ";
}
cout << endl << endl;
}
for (int i = 0; i < _listIndex; i++) {
cout << " " << i << ". [" << list[i].status << "] ";
cout << list[i].content << endl;
}
}
void List::tagView() {
cout << title << endl;
if (_hasTags) {
cout << "Tags : ";
for (int i = 0; i < _tagIndex; i++) {
cout << tags[i] << " ";
}
cout << endl;
}
}
void List::todoView(int index) {
cout << index << ". [" << list[index].status << "] ";
cout << list[index].content << endl;
}
void List::tagIndexView() {
cout << title << endl;
cout << "============" << endl;
if (_hasTags) {
cout << "Tags : ";
for (int i = 0; i < _tagIndex; i++) {
cout << tags[i] << " ";
}
cout << endl << endl;
}
else {
cout << "No tags here." << endl;
}
}
void List::append(){
char Content[200];
cout << "Enter the content of ToDo" << endl << " +> ";
cin.getline(Content, sizeof(Content));
strcpy(list[_listIndex].content, Content);
list[_listIndex].status = ' ';
list[_listIndex]._index = _listIndex;
_listIndex++;
}
void List::removeTodo(int index) {
for (int i = index; i < _listIndex; i++) {
list[i] = list[i + 1];
}
_listIndex--;
}
void List::removeTag(int index) {
for (int i = index; i < _tagIndex; i++) {
strcpy(tags[i], tags[i + 1]);
}
_tagIndex--;
if (_tagIndex == 0) {
_hasTags = 0;
}
}
void List::changeStatus(int index, char status){
list[index].status = status;
}
void search(char term[], List arrayL[20]){
int len = strlen(term);
int found = 0;
// Search in titles
for (int i = 0; i < 20 ; i++) {
int z = 0;
found = 0;
for (unsigned int k = 0; k < strlen(arrayL[i].title); k++) {
if (tolower(term[z++]) == tolower(arrayL[i].title[k])) {
found = 1;
// cout << "Found title" << endl;
}
else {
found = 0;
z = 0;
}
if (z == len) {
if (found) {
break;
}
z = 0;
}
}
if (found) {
cout << "Found in title, Index => " << i << endl << arrayL[i].title << endl;
}
}
// Search in Tags
for (int i = 0; i < 20 ; i++) {
int z = 0;
found = 0;
for (int k = 0; k < arrayL[i]._tagIndex; k++) {
z = 0;
for (unsigned int c = 0; c < strlen(arrayL[i].tags[k]); c++) {
if (tolower(term[z++]) == tolower(arrayL[i].tags[k][c])) {
found = 1;
}
else {
found = 0;
z = 0;
}
if (z == len) {
if (found) {
break;
}
z = 0;
}
}
if (found) {
cout << "Found in tag, Index => " << i << endl;
arrayL[i].tagView();
break;
}
}
}
// Search in Todos
for (int i = 0; i < 20 ; i++) {
int z = 0;
found = 0;
for (int k = 0; k < arrayL[i]._listIndex; k++) {
z = 0;
for (unsigned int c = 0; c < strlen(arrayL[i].list[k].content); c++) {
if (tolower(term[z++]) == tolower(arrayL[i].list[k].content[c])) {
found = 1;
}
else {
found = 0;
z = 0;
}
if (z == len) {
if (found) {
break;
}
z = 0;
}
}
if (found) {
cout << "Found in ToDo, List Index => " << i << endl;
arrayL[i].view();
break;
}
}
}
}
void Export(List Array[20], int _arrayLindex){
char fname[40];
cout << "Enter the Export file name " << endl << " +> " ;
cin.getline(fname, sizeof(fname));
strcat(fname, ".tdexp");
ofstream file;
file.open(fname);
for (int i = 0; i < _arrayLindex; i++) {
file << Array[i].title << endl;
file << "===============" << endl;
file << "Tags : ";
for (int j = 0; j < Array[i]._tagIndex; j++) {
file << Array[i].tags[j] << " ";
}
file << endl;
for (int k = 0; k < Array[i]._listIndex; k++) {
file << " [" << Array[i].list[k].status << "] ";
file << Array[i].list[k].content << endl;
}
file << endl;
}
cout << "File exported to : " << fname << " successfully :)" << endl;
file.close();
}
List arrayL[20];
List currentL;
int _arrayLindex = 0;
int _currentLindex;
int isOpenL = 0; // For checking wether a list is open or not
char filename[20] = "data.tdx";
// Fxns Used
void initiate(); // Initiate the data before user inteface
int parse(char []); // run the command entered by the users
void stats(); // display stats of opened list
void openL(int); // open the list specified by index
void displayL(); // display all the lists
int confirm(); // ask for confirmation (y/n)
void empty(); // remove data from the program & data-file
void finish(); // save the data to the data-file
void initiate() {
// Fxn to read data from external file int the array of Lists
fstream file(filename, ios::in|ios::binary);
List tempOb; // temperary object
while (file.read((char*)&tempOb, sizeof(tempOb))) {
arrayL[_arrayLindex++] = tempOb;
}
file.close();
}
int parse(char command[80]){
// Fxn to Parse(execute) the command passed
/// Here a crazy fun expression is used in if () {} to compare strings
/// Since we wanted any of them to be zero, hence, they were multiplied
/// to give zero. Mathematically it is quite accurate and syntactically
/// beautiful.
int success = 0; // to mark wether a command completed successfully
if (!(strcmp(command, "new") * strcmp(command, "n"))) {
// Create a new List
arrayL[_arrayLindex++].enter();
success = 1;
}
else if (!(strcmp(command, "stats") * strcmp(command, "stat"))) {
// Display the stats
stats();
success = 1;
}
else if (!(strcmp(command, "open") * strcmp(command, "o"))) {
// Open existing lists
if (_arrayLindex) {
if (isOpenL) {
arrayL[_currentLindex] = currentL;
finish();
}
int choice;
cout << "Enter The No. of List to be opened" << endl;
displayL();
cout << " #> " ;
cin >> choice;
cin.ignore();
while ( !(0 <= choice && choice < _arrayLindex) ) {
cout << "Invalid choice choose again" << endl;
cout << " #> " ;
cin >> choice;
cin.ignore();
}
openL(choice);
isOpenL = 1;
}
else {
cout << "No List found, create a new one with \'n\' or \'new\'" << endl;
}
success = 1;
}
else if (!(strcmp(command, "append") * strcmp(command, "a"))) {
// Append a new Todo to the currently opened list
if (isOpenL) {
currentL.append();
}
else {
cout << "No List is opened, Open a list first" << endl;
}
success = 1;
}
else if (!(strcmp(command, "mark") * strcmp(command, "m"))) {
// Mark/(change status) a Todo with a given charater
if (isOpenL) {
int choice;
currentL.indexView();
cout << "Choose the Todo to mark : " << endl << " #> ";
cin >> choice;
while (choice >= currentL._listIndex || choice < 0) {
cout << "Invalid Choice Try again" << endl << " #> ";
cin >> choice;
}
char status;
cout << "Enter The new Status" << endl << " +> " ;
cin >> status;
cin.ignore();
currentL.changeStatus(choice, status);
}
else {
cout << "No List is opened, Open a list first" << endl;
}
success = 1;
}
else if (!(strcmp(command, "addtag") * strcmp(command, "addt"))) {
// Append a new Todo to the currently opened list
if (isOpenL) {
char newTag[40];
cout << "Enter the New Tag " << endl << " +> ";
cin.getline(newTag, sizeof(newTag));
currentL.addTag(newTag);
}
else {
cout << "No List is opened, Open a list first" << endl;
}
success = 1;
}
else if (!(strcmp(command, "view") * strcmp(command, "v"))) {
// view todo's of the current list
if (isOpenL) {
currentL.view();
}
else {
cout << "No List is opened, Open a list first" << endl;
}
success = 1;
}
else if (!(strcmp(command, "iview") * strcmp(command, "iv"))) {
// view todo's of the current list with index
if (isOpenL) {
currentL.indexView();
}
else {
cout << "No List is opened, Open a list first" << endl;
}
success = 1;
}
else if (!(strcmp(command, "search") * strcmp(command, "srch"))) {
// Search the Database
char searchTerm[40];
cout << "Enter the search term " << endl << " +> ";
cin.getline(searchTerm, sizeof(searchTerm));
if (isOpenL) {
arrayL[_currentLindex] = currentL;
}
search(searchTerm, arrayL);
success = 1;
}
else if (!(strcmp(command, "save") * strcmp(command, "s"))) {
// Save the data to the file
if (isOpenL) {
arrayL[_currentLindex] = currentL;
}
finish();
success = 1;
}
else if (!(strcmp(command, "export") * strcmp(command, "exp"))) {
// Export the data to a text file
if (isOpenL) {
arrayL[_currentLindex] = currentL;
}
Export(arrayL, _arrayLindex);
success = 1;
}
else if (!(strcmp(command, "delete") * strcmp(command, "del"))) {
// Delete Things
char choice[10];
cout << "What do you want to delete? (list/todo/tag)" << endl << " ?> ";
cin.getline(choice, sizeof(choice));
if (!(strcmp(choice, "list") * strcmp(choice, "List"))) {
int index;
cout << "Enter the Index of List to Delete " << endl;
displayL();
cout << " #> ";
cin >> index;
cin.ignore();
cout << "Are you sure, This cannot be undone, This will delete the List ->" << endl;
arrayL[index].view();
if ( confirm() ) {
for (int i = index; i < _arrayLindex - 1; i++) {
arrayL[i] = arrayL[i+1];
}
_arrayLindex--;
}
}
else if (!(strcmp(choice, "todo") * strcmp(choice, "Todo") * strcmp(choice, "ToDo"))) {
if (isOpenL) {
int index;
cout << "Enter the Index of Todo to Delete " << endl;
currentL.indexView();
cout << " #> ";
cin >> index;
cin.ignore();
cout << "Are you sure, This cannot be undone, This will delete the ToDo ->" << endl;
currentL.todoView(index);
if ( confirm() ) {
currentL.removeTodo(index);
}
arrayL[_currentLindex] = currentL; // To Help Improving Finalization
}
else {
cout << "No List is opened, Open a list first" << endl;
}
}
else if (!(strcmp(choice, "tag") * strcmp(choice, "tags") * strcmp(choice, "Tag"))) {
if (isOpenL) {
int index;
cout << "Enter the Index of Tag to Delete " << endl;
currentL.tagIndexView();
cout << " #> " ;
cin >> index;
cin.ignore();
cout << "Are you sure, This cannot be undone, This will delete the Tag ->" << endl;
cout << currentL.tags[index] << endl;
if ( confirm() ) {
currentL.removeTag(index);
}
arrayL[_currentLindex] = currentL; // To Help Improving Finalization
}
else {
cout << "No List is opened, Open a list first" << endl;
}
}
finish();
success = 1;
}
else if (!(strcmp(command, "qdelete") * strcmp(command, "qdel"))) {
// Delete Todo of current list using index
/// qdelete -> Quick Delete
if (isOpenL) {
int index;
cout << "Enter the Index of List to Delete " << endl;
currentL.indexView();
cout << " #> " ;
cin >> index;
cin.ignore();
cout << "Are you sure, This cannot be undone, This will delete ->" << endl;
currentL.todoView(index);
if ( confirm() ) {
currentL.removeTodo(index);
}
arrayL[_currentLindex] = currentL;
}
else {
cout << "No List is opened, Open a list first" << endl;
}
finish();
success = 1;
}
else if (!(strcmp(command, "clear") * strcmp(command, "clr"))) {
// Refresh the data file
// XXX WARN XXX -> Strictly, Not to be used By users, Deletes all Data
if ( confirm() ) {
ofstream file(filename, ios::trunc|ios::binary|ios::out);
file.write("", sizeof(arrayL));
file.close();
empty();
initiate();
}
success = 1;
}
else if (!(strcmp(command, "quit") * strcmp(command, "q"))) {
// exit the program after saving it to the file
if ( confirm() ) {
if (isOpenL) {
arrayL[_currentLindex] = currentL;
}
finish();
success = -1;
}
else {
success = 1;
}
}
else if (!(strcmp(command, "help") * strcmp(command, "-h"))) {
// View help
char line[80];
ifstream helpfile("help.txt");
while (!helpfile.eof()) {
helpfile.getline(line, sizeof(line));
cout << line << endl;
}
helpfile.close();
success = 1;
}
return success;
}
void stats(){
// Display wether a list is open or not
if (isOpenL) {
cout << "This List is open : " << currentL.title << endl;
cout << "Total No. of todos : " << currentL._listIndex << endl;
}
else {
cout << "No list is open" << endl;
}
cout << "Total lists = " << _arrayLindex << endl;
}
void openL(int index) {
currentL = arrayL[index];
_currentLindex = index;
cout << "Opened : " << currentL.title << endl;
currentL.view();
}
void displayL() {
for (int i = 0; i < _arrayLindex; i++) {
cout << i << ". " << arrayL[i].title << endl;
}
}
int confirm() {
// can be used as if( confirm() ) /// Improves yes/no prompts
char confm[10];
cout << "Enter \'yes\' to continue" << endl << " ?> " ;
cin.getline(confm, sizeof(confm));
if (!(strcmp(confm, "yes") * strcmp(confm, "y"))) {
return 1;
}
else{
return 0;
}
}
void empty() {
List tarrayL[20];
memcpy(arrayL, tarrayL, sizeof(arrayL));
_arrayLindex = 0;
isOpenL = 0;
}
void finish() {
// Write the changes back to file
ofstream file (filename, ios::out);
for (int i = 0; i < _arrayLindex; i++) {
file.write((char*)&arrayL[i], sizeof(arrayL[i]));
}
file.close();
}
int main() {
initiate();
cout << "=== --- --> TodX <-- --- ===" << endl;
cout << "Welcome to TodX the ultimate Todo list" << endl;
cout << "v1.03a = Borland DOS build, docs at -> http://todx.rtfd.io" << endl;
char command[80];
while (1) {
cout << "\n *> ";
cin.getline(command, sizeof(command));
if (!cin) {
cout << "Have a Great Day :)" << endl;
break;
}
int result = parse(command);
if(result > 0){
continue;
}
else if (result == 0) {
cout << "Command not found, try `help` for help" << endl;
}
else if (result == -1) {
cout << "Data Saved, Have a Great Day :)" << endl;
break;
}
else {
cout << "Something went terribly wrong, we apologize :(" << endl;
}
}
finish();
return 0;
}
@xypnox
Copy link
Author

xypnox commented Feb 10, 2017

@sgrmshrsm7 compile it ASAP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment