cleanup and minor changes

This commit is contained in:
vrd
2023-03-07 21:00:56 +02:00
parent 83d47cbefa
commit 7b05b7c5c1
6 changed files with 67 additions and 279 deletions
+32 -21
View File
@@ -45,20 +45,31 @@ namespace /* internal */
/* result */ /* result */
Tree* cur_tree; Tree* cur_tree;
void err_three_parents(const char* extra_name) void err_three_parents(const string& extra_name)
{ {
endwin(); endwin();
cerr << "Line " << line << " two parents are already defined, " cerr << "Line " << line << " two parents are already defined, "
<< extra_name << " is third" << endl; << extra_name << " is third" << endl;
exit(1);
} }
/* void err_name_index(const string& name, U32 i) */
/* { */
/* endwin(); */
/* cerr << "Line " << line << " the number given after the declaration of a person can" */
/* " only be his place among the same named. " << i << " doesn't satisfy this for '" */
/* << name << '\'' << endl; */
/* exit(1); */
/* } */
bool isNum(char c) bool isNum(char c)
{ {
return c >= '0' && c <= '9'; return c >= '0' && c <= '9';
} }
void proceedToNameEnd(char*& it_data) string proceedToNameEnd(char*& it_data)
{ {
char* name_start = it_data;
while(it_data < end_data && while(it_data < end_data &&
*it_data != ',' && *it_data != ',' &&
*it_data != '\n' && *it_data != '\n' &&
@@ -68,7 +79,7 @@ namespace /* internal */
ERR_UNEXPECTED_CHAR(*it_data); ERR_UNEXPECTED_CHAR(*it_data);
++it_data; ++it_data;
} }
return string(name_start, it_data); /* it_data points past end */
} }
U32 sameNamedIndex(char*& it_data) U32 sameNamedIndex(char*& it_data)
@@ -162,11 +173,11 @@ namespace /* internal */
{ {
case ',': case ',':
if(res.first == Nobody) if(res.first == Nobody)
res.first = cur_tree->findId(name.data())[same_name_index]; /* todo validate index */ res.first = cur_tree->findId(name)[same_name_index]; /* todo validate index */
else if(res.second == Nobody) else if(res.second == Nobody)
res.second = cur_tree->findId(name.data())[same_name_index]; res.second = cur_tree->findId(name)[same_name_index];
else else
err_three_parents(name.data()); err_three_parents(name);
same_name_index = 0; same_name_index = 0;
name.clear(); name.clear();
@@ -179,9 +190,9 @@ namespace /* internal */
case '\n': case '\n':
++line; ++line;
if(res.first == Nobody) if(res.first == Nobody)
res.first = cur_tree->findId(name.data())[same_name_index]; res.first = cur_tree->findId(name)[same_name_index];
else else
res.second = cur_tree->findId(name.data())[same_name_index]; res.second = cur_tree->findId(name)[same_name_index];
return prev_parents = res; return prev_parents = res;
case '_': case '_':
name.push_back(' '); name.push_back(' ');
@@ -214,7 +225,7 @@ namespace /* internal */
case ',': case ',':
if(!name.empty()) if(!name.empty())
{ {
res.push_back( cur_tree->findId(name.data())[same_name_index] ); res.push_back( cur_tree->findId(name)[same_name_index] );
name.clear(); name.clear();
same_name_index = 0; same_name_index = 0;
} }
@@ -224,7 +235,7 @@ namespace /* internal */
case '\n': case '\n':
++line; ++line;
if(!name.empty()) if(!name.empty())
res.push_back( cur_tree->findId(name.data())[same_name_index] ); res.push_back( cur_tree->findId(name)[same_name_index] );
return res; return res;
case '_': case '_':
name.push_back(' '); name.push_back(' ');
@@ -237,7 +248,7 @@ namespace /* internal */
} }
} }
if(!name.empty()) if(!name.empty())
res.push_back( cur_tree->findId(name.data())[same_name_index] ); res.push_back( cur_tree->findId(name)[same_name_index] );
return res; return res;
} }
@@ -264,24 +275,22 @@ namespace /* internal */
void state_person(char*& it_data) void state_person(char*& it_data)
{ {
char* name_start = it_data;
pair<person_id, person_id> parents(Nobody, Nobody); pair<person_id, person_id> parents(Nobody, Nobody);
vector<person_id> spouses; vector<person_id> spouses;
EventTime birth{}; EventTime birth{};
EventTime death{}; EventTime death{};
proceedToNameEnd(it_data); /* todo merge funcs */ string name = proceedToNameEnd(it_data);
string name(name_start, it_data);
U32 same_name_index = sameNamedIndex(it_data);
/* string_view name(name_start, it_data); */
for(char& c: name) for(char& c: name)
{ {
if(c == '_') if(c == '_')
c = ' '; c = ' ';
} }
U32 same_name_index = sameNamedIndex(it_data);
/* todo */
/* if(cur_tree->findId(name).size() != same_name_index) */
/* err_name_index(name, same_name_index); */
Sex sex = findSex(it_data); Sex sex = findSex(it_data);
@@ -320,7 +329,7 @@ namespace /* internal */
} }
else else
{ {
cur_tree->addPerson(name.c_str(), sex, parents.first, parents.second, birth, death); cur_tree->addPerson(name.c_str(), sex, parents.first, parents.second, birth, death); /* todo prevent copy */
for(person_id spouse: spouses) for(person_id spouse: spouses)
cur_tree->addRelation(spouse, Spouse, cur_tree->last()); cur_tree->addRelation(spouse, Spouse, cur_tree->last());
--it_data; /* rewind 1 */ --it_data; /* rewind 1 */
@@ -369,9 +378,11 @@ namespace /* internal */
} }
Tree parseTftFile(fd_t file) Tree parseTftFile(const char* path)
{ {
Tree result("t"); fd_t file = openOrDie(path);
Tree result(path);
cur_tree = &result; cur_tree = &result;
MappedFile mapedfile = mapfile(file); MappedFile mapedfile = mapfile(file);
+1 -1
View File
@@ -1,3 +1,3 @@
#include "tree.hpp" #include "tree.hpp"
Tree parseTftFile(fd_t); Tree parseTftFile(const char* path);
+10 -230
View File
@@ -2,247 +2,27 @@
#include "utils.hpp" #include "utils.hpp"
#include "parser.hpp" #include "parser.hpp"
#include <ncursesw/ncurses.h> #include <ncursesw/ncurses.h>
#include <string_view> /* #include <string_view> */
#include <iostream> #include <iostream>
using std::string_view; /* using std::string_view; */
using std::cerr; using std::cout;
using namespace std::literals; /* using namespace std::literals; */
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
init();
if(argc < 2) if(argc < 2)
{ {
cerr << "Usage: tft FILES...\nTry 'tft --help' for more information"; cout << "Usage: tft FILES...\n";
return 1; return 0;
} }
init();
for(int i = 1; i < argc; ++i) for(int i = 1; i < argc; ++i)
{ {
if(string_view(argv[i]) == "--help"sv) /* sv worth it? */ parseTftFile(argv[i]).display();
{
/* todo */
return 0; /* todo exit curses */
}
fd_t file = openOrDie(argv[i]);
parseTftFile(file).display();
} }
/* ====================================================== */ return 0;
/* std::vector<Tree> trees; */
/* bool quit = F; */
/* char input[128]; */
/* short i; //Number of the entered command */
/* const short numCom = 24; */
/* const char* const commandList[] = */
/* { */
/* "quit", //0 */
/* "rename", //1 */
/* "load", //2 */
/* "save", //3 */
/* "addPerson", //4 */
/* "addPersonWP", //5 Adds a person and connects him to his parents, by their number */
/* "addRelation", //6 Adds a relation between two people input by name */
/* "addRelationID", //7 -||- by number */
/* "findID", //8 */
/* "equate", //9 Enter the number of two trees, the second becomes equal to the first */
/* "combine", //10 */
/* "subtract", //11 */
/* "removePerson", //12 */
/* "removePersonID", //13 */
/* "removeRelation", //14 */
/* "removeRelationID", //15 */
/* "addTree", //16 */
/* "prtRelatives", //17 */
/* "prtMember", //18 */
/* "prtAncestors", //19 */
/* "chooseTree", //20 */
/* "prtCommonAncestor", //21 */
/* "prtOldestAncestors",//22 */
/* "prtTree" //23 */
/* }; */
/* unsigned curTree = 0, //Currently selected tree ID */
/* id, secondId; // Person IDs */
/* Tree* newTree; */
/* char name[128], secondName[128]; */
/* U8 day, month; //TODO add h m */
/* U16 year; */
/* bool sex; */
/* unsigned father, mother; // IDs */
/* do */
/* { */
/* clear(); */
/* printw("Enter a command\n"); */
/* refresh(); */
/* // getmaxyx(stdscr,row,col); /\* get the number of rows and columns *\/ */
/* std::cin >> input; */
/* for (i = 0; i < numCom; ++i) */
/* { */
/* if (strComp(input, commandList[i])) */
/* break; */
/* } */
/* switch (i) */
/* { */
/* default: */
/* std::cout << "Unknown command, try again\n\n"; */
/* break; */
/* case 0: //quit */
/* quit = 1; */
/* break; */
/* case 1: //rename */
/* std::cout << "Enter new name\n\n"; */
/* std::cin >> name; */
/* trees[curTree].rename(name); */
/* break; */
/* case 2: //load */
/* cin >> input; */
/* trees.push(Tree(input)); */
/* if (!trees[trees.gNum() - 1].loadFromFile()) */
/* std::cout << "File doesnt exist or is empty.\n\n"; */
/* break; */
/* case 3: //save */
/* trees[curTree].saveToFile(); */
/* break; */
/* case 4: //addPerson */
/* std::cin >> name >> day >> month >> year >> sex; */
/* trees[curTree].addPerson(name, sex, Nobody, Nobody, EventTime(year, month, day)); */
/* break; */
/* case 5: //addPersonWP */
/* std::cin >> name >> day >> month >> year >> sex >> father >> mother; */
/* trees[curTree].addPerson(name, sex, father, mother, EventTime(year, month, day)); */
/* break; */
/* case 6: //addRelation */
/* std::cin >> name >> input >> secondName; */
/* while (!strToRel(input)) */
/* { */
/* std::cout << "Invalid Relation, try again!\n"; */
/* std::cin >> input; */
/* } */
/* if(!trees[curTree].addRelation(name, strToRel(input), secondName)) */
/* std::cout << "Failure! One or both of the relatives don't exist.\n\n"; */
/* break; */
/* case 7: //addRelationID */
/* std::cin >> id >> input >> secondId; */
/* while (!strToRel(input)) */
/* { */
/* std::cout << "Invalid Relation, try again!\n"; */
/* std::cin >> input; */
/* } */
/* if(trees[curTree].addRelation(id, strToRel(input), secondId)) */
/* std::cout << "Failure! One or both of the relatives don't exist.\n\n"; */
/* break; */
/* case 8: //findID */
/* std::cin >> name; */
/* id = trees[curTree].findId(name); */
/* if (id == Nobody) */
/* std::cout << "No such person in tree " << curTree <<"\n\n"; */
/* else */
/* std::cout << id << "\n\n"; */
/* break; */
/* case 9: //equate */
/* std::cin >> id >> secondId; */
/* trees[id] = trees[secondId]; */
/* break; */
/* case 10: //combine */
/* std::cin >> id >> secondId; */
/* trees[id] += trees[secondId]; */
/* break; */
/* case 11: //subtract */
/* std::cin >> id >> secondId; */
/* trees[id] -= trees[secondId]; */
/* break; */
/* case 12: //removePerson */
/* std::cin >> name; */
/* if (trees[curTree].findId(name) != Nobody) */
/* trees[curTree].removePerson(name); */
/* else */
/* std::cout << "No such member!\n\n"; */
/* break; */
/* case 13: //removePersonID */
/* std::cin >> id; */
/* if (trees[curTree].gNumP() > id) */
/* trees[curTree].removePerson(id); */
/* else */
/* std::cout << "No such member!\n\n"; */
/* break; */
/* case 14: //removeRelation */
/* std::cin >> name >> secondName; */
/* if (trees[curTree].gNumP() > trees[curTree].findId(name) && trees[curTree].gNumP() > trees[curTree].findId(secondName)) */
/* trees[curTree].removeRelation(name, secondName); */
/* else */
/* std::cout << "No such member!\n\n"; */
/* break; */
/* case 15: //removeRelationID */
/* std::cin >> id >> secondId; */
/* if (trees[curTree].gNumP() > id && trees[curTree].gNumP() > secondId) */
/* trees[curTree].removeRelation(id, secondId); */
/* else */
/* std::cout << "No such member\n\n"; */
/* break; */
/* case 16: //addTree */
/* std::cin >> name; */
/* newTree = new Tree(name); */
/* trees.push(*newTree); */
/* delete newTree; */
/* break; */
/* case 17: //printRelatives */
/* std::cin >> id; */
/* if (id < trees[curTree].gNumP()) */
/* trees[curTree].printRel(id); */
/* else */
/* std::cout << "No such member\n\n"; */
/* break; */
/* case 18: //printMember */
/* std::cin >> id; */
/* if (id < trees[curTree].gNumP()) */
/* trees[curTree].printMember(id); */
/* else */
/* std::cout << "No such member\n\n"; */
/* break; */
/* case 19: //printAncestors */
/* std::cin >> id; */
/* /\*if (id < trees[curTree].gNumP()) */
/* trees[curTree].printAncestors(id); */
/* else */
/* std::cout << "No such member\n\n";*\/ */
/* break; */
/* case 20: //chooseTree */
/* std::cout << "Enter tree ID\n\n"; */
/* std::cin >> id; */
/* if (id < trees.gNum()) */
/* curTree = id; */
/* else */
/* std::cout << "No such tree\n\n"; */
/* break; */
/* case 21: //prtCommonAncestor */
/* std::cin >> id >> secondId; */
/* if (id < trees[curTree].gNumP() || secondId < trees[curTree].gNumP()) */
/* trees[curTree].printMember(trees[curTree].findCommonAncestor(id, secondId)); */
/* else */
/* std::cout << "No such member\n\n"; */
/* break; */
/* case 22: //prtOldestAncestors */
/* std::cin >> id; */
/* if (id < trees[curTree].gNumP()) */
/* trees[curTree].printOldestAncestors(id); */
/* else */
/* std::cout << "No such member\n\n"; */
/* break; */
/* case 23: */
/* trees[curTree].print(); */
/* break; */
/* } */
/* } while (!quit); */
/* TFT_EXIT(0); */
} }
+19 -19
View File
@@ -100,7 +100,7 @@ person_id Tree::last()
return people.size() - 1; return people.size() - 1;
} }
vector<person_id> Tree::findId(const char* name) const vector<person_id> Tree::findId(const string& name) const
{ {
vector<person_id> res; vector<person_id> res;
for (person_id i = 0; i < people.size(); ++i) for (person_id i = 0; i < people.size(); ++i)
@@ -111,16 +111,16 @@ vector<person_id> Tree::findId(const char* name) const
return res; return res;
} }
person_id Tree::findId(const char* name, Sex s) const /* person_id Tree::findId(const string& name, Sex s) const */
{ /* { */
Person sought(name, {}, s); /* Person sought(name, {}, s); */
for (person_id i = 0; i < people.size(); ++i) /* for (person_id i = 0; i < people.size(); ++i) */
{ /* { */
if (sought == people[i]) /* if (sought == people[i]) */
return i; /* return i; */
} /* } */
return Nobody; /* return Nobody; */
} /* } */
person_id Tree::findRootAncestor(person_id id) const person_id Tree::findRootAncestor(person_id id) const
{ {
@@ -245,11 +245,11 @@ void Tree::addPerson(const char* name, Sex sex, unsigned father, unsigned mother
updateRels(new_person_id); updateRels(new_person_id);
} }
bool Tree::addRelation(const char* firstName, const RelType type, const char* secondName) /* bool Tree::addRelation(const char* firstName, const RelType type, const char* secondName) */
{ /* { */
return addRelation(findId(firstName)[0], type, findId(secondName)[0]); /* return addRelation(findId(firstName)[0], type, findId(secondName)[0]); */
} /* } */
bool Tree::addRelation(const unsigned first, const RelType type, const unsigned second, bool update) bool Tree::addRelation(const unsigned first, const RelType type, const unsigned second, bool update)
{ {
@@ -301,10 +301,10 @@ void Tree::addRelOneSide(person_id first, RelType type, person_id second)
} }
} }
void Tree::removeRelation(const char* firstName, const char* secondName) /* void Tree::removeRelation(const char* firstName, const char* secondName) */
{ /* { */
removeRelation(findId(firstName)[0], findId(secondName)[0]); /* removeRelation(findId(firstName)[0], findId(secondName)[0]); */
} /* } */
void Tree::removePerson(const person_id id) void Tree::removePerson(const person_id id)
{ {
+2 -2
View File
@@ -45,8 +45,8 @@ public:
void rename(const char* newName); void rename(const char* newName);
person_id last(); person_id last();
std::vector<person_id> findId(const char* name) const; std::vector<person_id> findId(const std::string& name) const;
person_id findId(const char* name, Sex) const; person_id findId(const std::string& name, Sex) const;
person_id findRootAncestor(person_id) const; person_id findRootAncestor(person_id) const;
person_id findOldestAncestor(person_id, Sex) const;// person_id findOldestAncestor(person_id, Sex) const;//
+3 -6
View File
@@ -1,8 +1,6 @@
open tree
Parser: Parser:
- clean wh - clean wh
- tokens?
--help --help
help - lowercase help - lowercase
@@ -19,12 +17,13 @@ Combine trees on the command line
Zooming Zooming
Visualization: Visualization:
- draw parents of focused
- horizontal navigation between cousins - horizontal navigation between cousins
- color on different consoles - color on different consoles
- screen resize - screen resize
- empty tree - empty tree
- half children and main tree - half children and main tree
- distincs tree regions
- info bar
Compilation Compilation
- static and dynamic ncurses - static and dynamic ncurses
@@ -39,8 +38,6 @@ Backend
- create tree backup - create tree backup
Refactoring Refactoring
- gender enum
- person struct
- comment style /* - comment style /*
readelf -a /usr/lib/x86_64-linux-gnu/libncursesw.a | grep lto readelf -a /usr/lib/x86_64-linux-gnu/libncursesw.a | grep lto