mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-07-07 20:06:39 +02:00
Datafile: Refactored data I/O
This commit is contained in:
parent
cee6ff0c2e
commit
91f6600909
2 changed files with 69 additions and 124 deletions
162
src/Datafile.cpp
162
src/Datafile.cpp
|
@ -27,6 +27,7 @@
|
||||||
#include <cmake.h>
|
#include <cmake.h>
|
||||||
#include <Datafile.h>
|
#include <Datafile.h>
|
||||||
#include <format.h>
|
#include <format.h>
|
||||||
|
#include <algorithm>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
@ -52,56 +53,86 @@ std::string Datafile::name () const
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
Interval Datafile::getLatestInterval ()
|
std::string Datafile::lastLine ()
|
||||||
{
|
{
|
||||||
// Load data
|
if (! _lines_loaded)
|
||||||
if (! _intervals_loaded)
|
load_lines ();
|
||||||
load_intervals ();
|
|
||||||
|
|
||||||
// Return the last element in _lines.
|
std::vector <std::string>::reverse_iterator ri;
|
||||||
if (_intervals.size ())
|
for (ri = _lines.rbegin (); ri != _lines.rend (); ri++)
|
||||||
return _intervals.back ();
|
if (ri->operator[] (0) == 'i')
|
||||||
|
return *ri;
|
||||||
|
|
||||||
return Interval ();
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
std::vector <Interval> Datafile::getAllIntervals ()
|
std::vector <std::string> Datafile::allLines ()
|
||||||
{
|
{
|
||||||
if (! _intervals_loaded)
|
if (! _lines_loaded)
|
||||||
load_intervals ();
|
load_lines ();
|
||||||
|
|
||||||
return _intervals;
|
return _lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void Datafile::setExclusions (const std::vector <std::string>& exclusions)
|
void Datafile::setExclusions (const std::vector <std::string>& exclusions)
|
||||||
{
|
{
|
||||||
|
// TODO Overwrite local copy
|
||||||
|
// TODO load current exclusion set
|
||||||
|
// TODO if local copy != exclusion set
|
||||||
|
// TODO remove old exclusion set from _lines
|
||||||
|
// TODO add local copy
|
||||||
|
// TODO _dirty = ture;
|
||||||
|
|
||||||
_exclusions = exclusions;
|
_exclusions = exclusions;
|
||||||
_dirty = true;
|
_dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void Datafile::addInterval (const Interval& interval)
|
bool Datafile::addInterval (const Interval& interval)
|
||||||
{
|
{
|
||||||
_lines_added.push_back (interval.serialize ());
|
// Return false if the interval does not belong in this file.
|
||||||
|
if (interval.start () > _dayN ||
|
||||||
|
interval.end () < _day1)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (! _lines_loaded)
|
||||||
|
load_lines ();
|
||||||
|
|
||||||
|
// TODO if _lines contains no exclusions
|
||||||
|
// TODO add _exclusions
|
||||||
|
|
||||||
|
// TODO if interval is not a duplicate
|
||||||
|
// TODO insert interval.serialize into _lines
|
||||||
|
// TODO _dirty = true;
|
||||||
|
|
||||||
|
_lines.push_back (interval.serialize ());
|
||||||
_dirty = true;
|
_dirty = true;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// Only checks if the start time matches.
|
bool Datafile::deleteInterval (const Interval& interval)
|
||||||
void Datafile::modifyInterval (const Interval& interval)
|
|
||||||
{
|
{
|
||||||
for (auto& i : _intervals)
|
// Return false if the interval does not belong in this file.
|
||||||
|
if (interval.start () > _dayN ||
|
||||||
|
interval.end () < _day1)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (! _lines_loaded)
|
||||||
|
load_lines ();
|
||||||
|
|
||||||
|
auto serialized = interval.serialize ();
|
||||||
|
auto i = std::find (_lines.begin (), _lines.end (), serialized);
|
||||||
|
if (i != _lines.end ())
|
||||||
{
|
{
|
||||||
if (i.start () == interval.start ())
|
_lines.erase (i);
|
||||||
{
|
|
||||||
i = interval;
|
|
||||||
_dirty = true;
|
_dirty = true;
|
||||||
_lines_modified = true;
|
return true;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -109,55 +140,22 @@ void Datafile::commit ()
|
||||||
{
|
{
|
||||||
// The _dirty flag indicates that the file needs to be written.
|
// The _dirty flag indicates that the file needs to be written.
|
||||||
if (_dirty)
|
if (_dirty)
|
||||||
{
|
|
||||||
// Special case: added but no modified means just append to the file.
|
|
||||||
if (_lines_added.size () &&
|
|
||||||
! _lines_modified)
|
|
||||||
{
|
{
|
||||||
if (_file.open ())
|
if (_file.open ())
|
||||||
{
|
{
|
||||||
_file.lock ();
|
_file.lock ();
|
||||||
|
|
||||||
// Write out all the added intervals.
|
|
||||||
_file.append (std::string("")); // Seek to end of file
|
|
||||||
|
|
||||||
// Write out all the added lines.
|
|
||||||
for (auto& line : _lines_added)
|
|
||||||
_file.write_raw (line + "\n");
|
|
||||||
|
|
||||||
_lines_added.clear ();
|
|
||||||
_file.close ();
|
|
||||||
|
|
||||||
_dirty = false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
throw format ("Could not write to data file {1}", _file._data);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (_file.open ())
|
|
||||||
{
|
|
||||||
_file.lock ();
|
|
||||||
|
|
||||||
// Truncate the file and rewrite.
|
|
||||||
_file.truncate ();
|
_file.truncate ();
|
||||||
|
_file.append (std::string("")); // Seek to EOF.
|
||||||
|
|
||||||
// Rewrite all the intervals, because at least one was modified.
|
// Write out all the lines.
|
||||||
_file.append (std::string("")); // Seek to end of file
|
for (auto& line : _lines)
|
||||||
for (auto& interval : _intervals)
|
|
||||||
_file.write_raw (interval.serialize () + "\n");
|
|
||||||
|
|
||||||
// Write out all the added lines.
|
|
||||||
for (auto& line : _lines_added)
|
|
||||||
_file.write_raw (line + "\n");
|
_file.write_raw (line + "\n");
|
||||||
|
|
||||||
_lines_added.clear ();
|
|
||||||
_file.close ();
|
_file.close ();
|
||||||
_dirty = false;
|
_dirty = false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
throw format ("Could not write to data file {1}", _file._data);
|
throw format ("Could not write to data file {1}", _file._data);
|
||||||
}
|
|
||||||
|
|
||||||
_dirty = false;
|
_dirty = false;
|
||||||
}
|
}
|
||||||
|
@ -172,10 +170,6 @@ std::string Datafile::dump () const
|
||||||
<< " dirty: " << (_dirty ? "true" : "false") << "\n"
|
<< " dirty: " << (_dirty ? "true" : "false") << "\n"
|
||||||
<< " lines: " << _lines.size () << "\n"
|
<< " lines: " << _lines.size () << "\n"
|
||||||
<< " loaded " << (_lines_loaded ? "true" : "false") << "\n"
|
<< " loaded " << (_lines_loaded ? "true" : "false") << "\n"
|
||||||
<< " added: " << _lines_added.size () << "\n"
|
|
||||||
<< " modified: " << (_lines_modified ? "true" : "fasle") << "\n"
|
|
||||||
<< " intervals: " << _intervals.size () << "\n"
|
|
||||||
<< " loaded " << (_intervals_loaded ? "true" : "false") << "\n"
|
|
||||||
<< " exclusions: " << _exclusions.size () << "\n"
|
<< " exclusions: " << _exclusions.size () << "\n"
|
||||||
<< " day1: " << _day1.toISO () << "\n"
|
<< " day1: " << _day1.toISO () << "\n"
|
||||||
<< " dayN: " << _dayN.toISO () << "\n";
|
<< " dayN: " << _dayN.toISO () << "\n";
|
||||||
|
@ -183,48 +177,6 @@ std::string Datafile::dump () const
|
||||||
return out.str ();
|
return out.str ();
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
|
||||||
void Datafile::load_intervals ()
|
|
||||||
{
|
|
||||||
if (! _lines_loaded)
|
|
||||||
{
|
|
||||||
load_lines ();
|
|
||||||
|
|
||||||
// Apply previously added lines.
|
|
||||||
for (auto& line : _lines_added)
|
|
||||||
_lines.push_back (line);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& line : _lines)
|
|
||||||
{
|
|
||||||
// "inc ..."
|
|
||||||
if (line[0] == 'i')
|
|
||||||
{
|
|
||||||
Interval i;
|
|
||||||
i.initialize (line);
|
|
||||||
_intervals.push_back (i);
|
|
||||||
}
|
|
||||||
|
|
||||||
// "exc ..."
|
|
||||||
else if (line[0] == 'e')
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
Exclusion e;
|
|
||||||
e.initialize (line);
|
|
||||||
_intervals.push_back (i);
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
// ?
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// TODO Ignore blank lines?
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_intervals_loaded = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
void Datafile::load_lines ()
|
void Datafile::load_lines ()
|
||||||
{
|
{
|
||||||
|
|
|
@ -39,12 +39,12 @@ public:
|
||||||
void initialize (const std::string&);
|
void initialize (const std::string&);
|
||||||
std::string name () const;
|
std::string name () const;
|
||||||
|
|
||||||
Interval getLatestInterval ();
|
std::string lastLine ();
|
||||||
std::vector <Interval> getAllIntervals ();
|
std::vector <std::string> allLines ();
|
||||||
|
|
||||||
void setExclusions (const std::vector <std::string>&);
|
void setExclusions (const std::vector <std::string>&);
|
||||||
void addInterval (const Interval&);
|
bool addInterval (const Interval&);
|
||||||
void modifyInterval (const Interval&);
|
bool deleteInterval (const Interval&);
|
||||||
|
|
||||||
void commit ();
|
void commit ();
|
||||||
|
|
||||||
|
@ -52,7 +52,6 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void load_lines ();
|
void load_lines ();
|
||||||
void load_intervals ();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// File representing data.
|
// File representing data.
|
||||||
|
@ -61,14 +60,8 @@ private:
|
||||||
|
|
||||||
// Lines read from file, not parsed.
|
// Lines read from file, not parsed.
|
||||||
std::vector <std::string> _lines {};
|
std::vector <std::string> _lines {};
|
||||||
std::vector <std::string> _lines_added {};
|
|
||||||
bool _lines_modified {false};
|
|
||||||
bool _lines_loaded {false};
|
bool _lines_loaded {false};
|
||||||
|
|
||||||
// Intervals parsed from lines.
|
|
||||||
std::vector <Interval> _intervals {};
|
|
||||||
bool _intervals_loaded {false};
|
|
||||||
|
|
||||||
// Exclusions fed from Database.
|
// Exclusions fed from Database.
|
||||||
std::vector <std::string> _exclusions {};
|
std::vector <std::string> _exclusions {};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue