C++11: Cleaned up commands code with range-based for

This commit is contained in:
Paul Beckingham 2015-05-11 17:45:15 -04:00
parent bd3d58484a
commit 5a57dfd70d
42 changed files with 911 additions and 1065 deletions

View file

@ -62,14 +62,13 @@ int CmdHistoryMonthly::execute (std::string& output)
std::vector <Task> filtered;
filter.subset (filtered);
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task)
for (auto& task : filtered)
{
Date entry (task->get_date ("entry"));
Date entry (task.get_date ("entry"));
Date end;
if (task->has ("end"))
end = Date (task->get_date ("end"));
if (task.has ("end"))
end = Date (task.get_date ("end"));
time_t epoch = entry.startOfMonth ().toEpoch ();
groups[epoch] = 0;
@ -78,7 +77,7 @@ int CmdHistoryMonthly::execute (std::string& output)
++addedGroup[epoch];
// All deleted tasks have an end date.
if (task->getStatus () == Task::deleted)
if (task.getStatus () == Task::deleted)
{
epoch = end.startOfMonth ().toEpoch ();
groups[epoch] = 0;
@ -86,7 +85,7 @@ int CmdHistoryMonthly::execute (std::string& output)
}
// All completed tasks have an end date.
else if (task->getStatus () == Task::completed)
else if (task.getStatus () == Task::completed)
{
epoch = end.startOfMonth ().toEpoch ();
groups[epoch] = 0;
@ -113,16 +112,15 @@ int CmdHistoryMonthly::execute (std::string& output)
int priorYear = 0;
int row = 0;
std::map <time_t, int>::iterator i;
for (i = groups.begin (); i != groups.end (); ++i)
for (auto& i : groups)
{
row = view.addRow ();
totalAdded += addedGroup [i->first];
totalCompleted += completedGroup [i->first];
totalDeleted += deletedGroup [i->first];
totalAdded += addedGroup [i.first];
totalCompleted += completedGroup [i.first];
totalDeleted += deletedGroup [i.first];
Date dt (i->first);
Date dt (i.first);
int m, d, y;
dt.toMDY (m, d, y);
@ -135,22 +133,22 @@ int CmdHistoryMonthly::execute (std::string& output)
int net = 0;
if (addedGroup.find (i->first) != addedGroup.end ())
if (addedGroup.find (i.first) != addedGroup.end ())
{
view.set (row, 2, addedGroup[i->first]);
net +=addedGroup[i->first];
view.set (row, 2, addedGroup[i.first]);
net +=addedGroup[i.first];
}
if (completedGroup.find (i->first) != completedGroup.end ())
if (completedGroup.find (i.first) != completedGroup.end ())
{
view.set (row, 3, completedGroup[i->first]);
net -= completedGroup[i->first];
view.set (row, 3, completedGroup[i.first]);
net -= completedGroup[i.first];
}
if (deletedGroup.find (i->first) != deletedGroup.end ())
if (deletedGroup.find (i.first) != deletedGroup.end ())
{
view.set (row, 4, deletedGroup[i->first]);
net -= deletedGroup[i->first];
view.set (row, 4, deletedGroup[i.first]);
net -= deletedGroup[i.first];
}
Color net_color;
@ -219,14 +217,13 @@ int CmdHistoryAnnual::execute (std::string& output)
std::vector <Task> filtered;
filter.subset (filtered);
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task)
for (auto& task : filtered)
{
Date entry (task->get_date ("entry"));
Date entry (task.get_date ("entry"));
Date end;
if (task->has ("end"))
end = Date (task->get_date ("end"));
if (task.has ("end"))
end = Date (task.get_date ("end"));
time_t epoch = entry.startOfYear ().toEpoch ();
groups[epoch] = 0;
@ -235,7 +232,7 @@ int CmdHistoryAnnual::execute (std::string& output)
++addedGroup[epoch];
// All deleted tasks have an end date.
if (task->getStatus () == Task::deleted)
if (task.getStatus () == Task::deleted)
{
epoch = end.startOfYear ().toEpoch ();
groups[epoch] = 0;
@ -243,7 +240,7 @@ int CmdHistoryAnnual::execute (std::string& output)
}
// All completed tasks have an end date.
else if (task->getStatus () == Task::completed)
else if (task.getStatus () == Task::completed)
{
epoch = end.startOfYear ().toEpoch ();
groups[epoch] = 0;
@ -269,16 +266,15 @@ int CmdHistoryAnnual::execute (std::string& output)
int priorYear = 0;
int row = 0;
std::map <time_t, int>::iterator i;
for (i = groups.begin (); i != groups.end (); ++i)
for (auto& i : groups)
{
row = view.addRow ();
totalAdded += addedGroup [i->first];
totalCompleted += completedGroup [i->first];
totalDeleted += deletedGroup [i->first];
totalAdded += addedGroup [i.first];
totalCompleted += completedGroup [i.first];
totalDeleted += deletedGroup [i.first];
Date dt (i->first);
Date dt (i.first);
int m, d, y;
dt.toMDY (m, d, y);
@ -290,22 +286,22 @@ int CmdHistoryAnnual::execute (std::string& output)
int net = 0;
if (addedGroup.find (i->first) != addedGroup.end ())
if (addedGroup.find (i.first) != addedGroup.end ())
{
view.set (row, 1, addedGroup[i->first]);
net +=addedGroup[i->first];
view.set (row, 1, addedGroup[i.first]);
net +=addedGroup[i.first];
}
if (completedGroup.find (i->first) != completedGroup.end ())
if (completedGroup.find (i.first) != completedGroup.end ())
{
view.set (row, 2, completedGroup[i->first]);
net -= completedGroup[i->first];
view.set (row, 2, completedGroup[i.first]);
net -= completedGroup[i.first];
}
if (deletedGroup.find (i->first) != deletedGroup.end ())
if (deletedGroup.find (i.first) != deletedGroup.end ())
{
view.set (row, 3, deletedGroup[i->first]);
net -= deletedGroup[i->first];
view.set (row, 3, deletedGroup[i.first]);
net -= deletedGroup[i.first];
}
Color net_color;
@ -373,14 +369,13 @@ int CmdGHistoryMonthly::execute (std::string& output)
std::vector <Task> filtered;
filter.subset (filtered);
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task)
for (auto& task : filtered)
{
Date entry (task->get_date ("entry"));
Date entry (task.get_date ("entry"));
Date end;
if (task->has ("end"))
end = Date (task->get_date ("end"));
if (task.has ("end"))
end = Date (task.get_date ("end"));
time_t epoch = entry.startOfMonth ().toEpoch ();
groups[epoch] = 0;
@ -389,7 +384,7 @@ int CmdGHistoryMonthly::execute (std::string& output)
++addedGroup[epoch];
// All deleted tasks have an end date.
if (task->getStatus () == Task::deleted)
if (task.getStatus () == Task::deleted)
{
epoch = end.startOfMonth ().toEpoch ();
groups[epoch] = 0;
@ -397,7 +392,7 @@ int CmdGHistoryMonthly::execute (std::string& output)
}
// All completed tasks have an end date.
else if (task->getStatus () == Task::completed)
else if (task.getStatus () == Task::completed)
{
epoch = end.startOfMonth ().toEpoch ();
groups[epoch] = 0;
@ -424,14 +419,13 @@ int CmdGHistoryMonthly::execute (std::string& output)
// Determine the longest line, and the longest "added" line.
int maxAddedLine = 0;
int maxRemovedLine = 0;
std::map <time_t, int>::iterator i;
for (i = groups.begin (); i != groups.end (); ++i)
for (auto& i : groups)
{
if (completedGroup[i->first] + deletedGroup[i->first] > maxRemovedLine)
maxRemovedLine = completedGroup[i->first] + deletedGroup[i->first];
if (completedGroup[i.first] + deletedGroup[i.first] > maxRemovedLine)
maxRemovedLine = completedGroup[i.first] + deletedGroup[i.first];
if (addedGroup[i->first] > maxAddedLine)
maxAddedLine = addedGroup[i->first];
if (addedGroup[i.first] > maxAddedLine)
maxAddedLine = addedGroup[i.first];
}
int maxLine = maxAddedLine + maxRemovedLine;
@ -445,16 +439,15 @@ int CmdGHistoryMonthly::execute (std::string& output)
int priorYear = 0;
int row = 0;
std::map <time_t, int>::iterator i;
for (i = groups.begin (); i != groups.end (); ++i)
for (auto& i : groups)
{
row = view.addRow ();
totalAdded += addedGroup[i->first];
totalCompleted += completedGroup[i->first];
totalDeleted += deletedGroup[i->first];
totalAdded += addedGroup[i.first];
totalCompleted += completedGroup[i.first];
totalDeleted += deletedGroup[i.first];
Date dt (i->first);
Date dt (i.first);
int m, d, y;
dt.toMDY (m, d, y);
@ -465,33 +458,33 @@ int CmdGHistoryMonthly::execute (std::string& output)
}
view.set (row, 1, Date::monthName(m));
unsigned int addedBar = (widthOfBar * addedGroup[i->first]) / maxLine;
unsigned int completedBar = (widthOfBar * completedGroup[i->first]) / maxLine;
unsigned int deletedBar = (widthOfBar * deletedGroup[i->first]) / maxLine;
unsigned int addedBar = (widthOfBar * addedGroup[i.first]) / maxLine;
unsigned int completedBar = (widthOfBar * completedGroup[i.first]) / maxLine;
unsigned int deletedBar = (widthOfBar * deletedGroup[i.first]) / maxLine;
std::string bar = "";
if (context.color ())
{
std::string aBar = "";
if (addedGroup[i->first])
if (addedGroup[i.first])
{
aBar = format (addedGroup[i->first]);
aBar = format (addedGroup[i.first]);
while (aBar.length () < addedBar)
aBar = " " + aBar;
}
std::string cBar = "";
if (completedGroup[i->first])
if (completedGroup[i.first])
{
cBar = format (completedGroup[i->first]);
cBar = format (completedGroup[i.first]);
while (cBar.length () < completedBar)
cBar = " " + cBar;
}
std::string dBar = "";
if (deletedGroup[i->first])
if (deletedGroup[i.first])
{
dBar = format (deletedGroup[i->first]);
dBar = format (deletedGroup[i.first]);
while (dBar.length () < deletedBar)
dBar = " " + dBar;
}
@ -569,14 +562,13 @@ int CmdGHistoryAnnual::execute (std::string& output)
std::vector <Task> filtered;
filter.subset (filtered);
std::vector <Task>::iterator task;
for (task = filtered.begin (); task != filtered.end (); ++task)
for (auto& task : filtered)
{
Date entry (task->get_date ("entry"));
Date entry (task.get_date ("entry"));
Date end;
if (task->has ("end"))
end = Date (task->get_date ("end"));
if (task.has ("end"))
end = Date (task.get_date ("end"));
time_t epoch = entry.startOfYear ().toEpoch ();
groups[epoch] = 0;
@ -585,7 +577,7 @@ int CmdGHistoryAnnual::execute (std::string& output)
++addedGroup[epoch];
// All deleted tasks have an end date.
if (task->getStatus () == Task::deleted)
if (task.getStatus () == Task::deleted)
{
epoch = end.startOfYear ().toEpoch ();
groups[epoch] = 0;
@ -593,7 +585,7 @@ int CmdGHistoryAnnual::execute (std::string& output)
}
// All completed tasks have an end date.
else if (task->getStatus () == Task::completed)
else if (task.getStatus () == Task::completed)
{
epoch = end.startOfYear ().toEpoch ();
groups[epoch] = 0;
@ -619,14 +611,13 @@ int CmdGHistoryAnnual::execute (std::string& output)
// Determine the longest line, and the longest "added" line.
int maxAddedLine = 0;
int maxRemovedLine = 0;
std::map <time_t, int>::iterator i;
for (i = groups.begin (); i != groups.end (); ++i)
for (auto& i : groups)
{
if (completedGroup[i->first] + deletedGroup[i->first] > maxRemovedLine)
maxRemovedLine = completedGroup[i->first] + deletedGroup[i->first];
if (completedGroup[i.first] + deletedGroup[i.first] > maxRemovedLine)
maxRemovedLine = completedGroup[i.first] + deletedGroup[i.first];
if (addedGroup[i->first] > maxAddedLine)
maxAddedLine = addedGroup[i->first];
if (addedGroup[i.first] > maxAddedLine)
maxAddedLine = addedGroup[i.first];
}
int maxLine = maxAddedLine + maxRemovedLine;
@ -640,16 +631,15 @@ int CmdGHistoryAnnual::execute (std::string& output)
int priorYear = 0;
int row = 0;
std::map <time_t, int>::iterator i;
for (i = groups.begin (); i != groups.end (); ++i)
for (auto& i : groups)
{
row = view.addRow ();
totalAdded += addedGroup[i->first];
totalCompleted += completedGroup[i->first];
totalDeleted += deletedGroup[i->first];
totalAdded += addedGroup[i.first];
totalCompleted += completedGroup[i.first];
totalDeleted += deletedGroup[i.first];
Date dt (i->first);
Date dt (i.first);
int m, d, y;
dt.toMDY (m, d, y);
@ -659,33 +649,33 @@ int CmdGHistoryAnnual::execute (std::string& output)
priorYear = y;
}
unsigned int addedBar = (widthOfBar * addedGroup[i->first]) / maxLine;
unsigned int completedBar = (widthOfBar * completedGroup[i->first]) / maxLine;
unsigned int deletedBar = (widthOfBar * deletedGroup[i->first]) / maxLine;
unsigned int addedBar = (widthOfBar * addedGroup[i.first]) / maxLine;
unsigned int completedBar = (widthOfBar * completedGroup[i.first]) / maxLine;
unsigned int deletedBar = (widthOfBar * deletedGroup[i.first]) / maxLine;
std::string bar = "";
if (context.color ())
{
std::string aBar = "";
if (addedGroup[i->first])
if (addedGroup[i.first])
{
aBar = format (addedGroup[i->first]);
aBar = format (addedGroup[i.first]);
while (aBar.length () < addedBar)
aBar = " " + aBar;
}
std::string cBar = "";
if (completedGroup[i->first])
if (completedGroup[i.first])
{
cBar = format (completedGroup[i->first]);
cBar = format (completedGroup[i.first]);
while (cBar.length () < completedBar)
cBar = " " + cBar;
}
std::string dBar = "";
if (deletedGroup[i->first])
if (deletedGroup[i.first])
{
dBar = format (deletedGroup[i->first]);
dBar = format (deletedGroup[i.first]);
while (dBar.length () < deletedBar)
dBar = " " + dBar;
}