VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gvribbon.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gvribbon.h
19 ///
20 
21 #ifndef GV_RIBBON__H
22 #define GV_RIBBON__H
23 
24 #include "gdef.h"
25 #include "gpath.h"
26 #include "gfiletree.h"
27 #include "gvtimezone.h"
28 #include "gdatetime.h"
29 #include <vector>
30 
31 namespace Gv
32 {
33  class Ribbon ;
34  class RibbonRange ;
35 }
36 
37 /// \class Gv::RibbonRange
38 /// Represents a day span within the file store, used by Gv::Ribbon.
39 ///
41 {
42 public:
43  RibbonRange( const G::Path & trigger_path , size_t tpos , const Gv::Timezone & tz ) ;
44  ///< Constructor for the day span that includes the given
45  ///< timestamped trigger path. The root of the file
46  ///< store is given by the first tpos characters
47  ///< of the trigger path.
48 
49  RibbonRange() ;
50  ///< Default constructor for an unusable object.
51 
52  unsigned int start() const ;
53  ///< Returns the timestamp() value for the start of the day.
54 
55  unsigned int end() const ;
56  ///< Returns the timestamp() value for the start of the next day.
57 
58  G::Path startpath() const ;
59  ///< Returns the directory path that is the start of the day
60  ///< at the minutes level (eg. "/data/2000/01/01/00/00"),
61  ///< taking the timezone into account.
62 
63  G::Path endpath() const ;
64  ///< Returns the directory path that is the start of the
65  ///< next day at the minutes level.
66 
67  RibbonRange around( const G::Path & ) const ;
68  ///< Returns a range includes the given timestamped trigger path.
69  ///< The root directory and timezone carry over.
70 
71  RibbonRange operator()( int offset ) const ;
72  ///< Returns a range that is offset from the current range.
73 
74  unsigned int timestamp( const G::Path & ) const ;
75  ///< Returns the time value for the given path, as seconds
76  ///< within some arbitrary epoch.
77 
78 private:
79  typedef G::DateTime::BrokenDownTime Tm ;
80  static g__constexpr std::time_t epoch_base = 946684800 ; // 2000
81  static unsigned int to_int( char a , char b , unsigned int error = 100U ) ;
82  static Tm bdt( const std::string & , size_t ) ;
83  static std::time_t daystamp( Tm , const Gv::Timezone & tz ) ;
84  static G::Path makePath( const G::Path & trigger_path , size_t pos , G::EpochTime t ) ;
85  static unsigned int timestamp( const G::Path & , size_t tpos ) ;
86 
87 private:
88  size_t m_tpos ;
89  Gv::Timezone m_tz ;
90  std::time_t m_start ;
91  std::time_t m_end ;
92  G::Path m_startpath ;
93  G::Path m_endpath ;
94 } ;
95 
96 /// \class Gv::Ribbon
97 /// Holds ribbon data that can be used to add a ribbon widget to a video stream display. The
98 /// ribbon is populated by searching the filesystem for files with a suitable 'timestampy'
99 /// path, ie. like '/base/yyyy/mm/dd/hh/mm/ss.ext' or '/base/yyyy/mm/dd/hh/mm/ss/xxx.ext'.
100 /// If the timestamp is in the relevant range the file is allocated into one of a number
101 /// of time buckets.
102 ///
104 {
105 public:
106  struct Item /// A time bucket within a Gv::Ribbon.
107  {
108  G::Path first ;
109  G::Path last ;
110  unsigned int first_ts ;
111  unsigned int last_ts ;
112  bool mark ;
113  Item() ;
114  bool set() const ;
115  bool includes( const G::Path & ) const ;
116  void update( const G::Path & , unsigned int ts ) ;
117  bool marked() const ;
118  } ;
119  typedef std::vector<Item> List ;
120 
121  Ribbon() ;
122  ///< Default constructor for a zero-size() ribbon.
123 
124  Ribbon( size_t size , const G::Path & base , const std::string & name , const Gv::Timezone & tz ) ;
125  ///< Constructor. The size parameter is the number of time buckets, which
126  ///< is typically the number of horizontal pixels. The base directory
127  ///< and timezone parameters are used in scan()ning.
128  ///< Use scan() to populate the ribbon.
129 
130  void scan( const G::Path & path , size_t tpos ) ;
131  ///< Does a complete scan for the day that encompases the given timestamped() file.
132  ///< Precondition: timestamped(path)
133 
134  void scan( const RibbonRange & range ) ;
135  ///< Does a complete scan for the given day range.
136 
137  bool scanStart( G::FileTree & , const RibbonRange & range ) ;
138  ///< Prepares for an iterative scan, with subsequent calls to scanSome().
139  ///< The default-constructed G::FileTree object contains the scan
140  ///< state. Returns true if there is scanSome() work to do.
141 
142  bool scanStart( G::FileTree & , const G::Path & path , size_t tpos ) ;
143  ///< Prepares for an iterative scan, with subsequent calls to scanSome().
144  ///< The default-constructed G::FileTree object contains the scan
145  ///< state. Returns true if there is scanSome() work to do.
146  ///< Precondition: timestamped(path)
147 
148  bool scanSome( G::FileTree & , G::EpochTime interval ) ;
149  ///< Does a partial scan, limited by the given time interval.
150  ///< Returns true if the scan is complete.
151 
152  G::Path find( size_t offset , bool before = false ) const ;
153  ///< Finds the first scanned path at-or-after the given bucket position, or
154  ///< returns the last() path if there are no paths at-or-after the given
155  ///< position. Alternatively, finds the first scanned path at-or-before the
156  ///< given position, or returns the first() path if there are no paths
157  ///< at-or-before the given position.
158  ///< Precondition: offset < size()
159 
160  G::Path last() const ;
161  ///< Returns the last scanned path. Returns the empty path if all the buckets
162  ///< are empty.
163 
164  G::Path first() const ;
165  ///< Returns the first scanned path. Returns the empty path if all the buckets
166  ///< are empty.
167 
168  bool apply( const G::Path & ) ;
169  ///< Called when a possibly-new timestamped file is encountered, allowing the
170  ///< ribbon to update itself for that file. Returns true if the apply()d file
171  ///< is outside the existing day range, implying that the ribbon needs
172  ///< to be rescanned.
173 
174  static int height() ;
175  ///< Returns a suggested height of the ribbon, in pixels.
176 
177  size_t size() const ;
178  ///< Returns the size of the list, as passed in to the constructor.
179 
180  static size_t timepos( const G::Path & path , const std::string & name ) ;
181  ///< Returns the timestamp position within the given path, or zero if none.
182 
183  size_t timepos( const G::Path & path ) const ;
184  ///< Returns the timestamp position within the given path, or zero if none.
185 
186  bool timestamped( const G::Path & path ) const ;
187  ///< Returns true if the given absolute path has a non-zero timepos().
188 
189  List::const_iterator begin() const ;
190  ///< Returns the item list's begin iterator.
191 
192  List::const_iterator end() const ;
193  ///< Returns the item list's end iterator.
194 
195  const RibbonRange & range() const ;
196  ///< Returns the current day range.
197 
198  RibbonRange range( int day_offset ) const ;
199  ///< Returns a day range that is offset from the current day.
200 
201  void mark( const G::Path & ) ;
202  ///< Marks the bucket that contains the given path. Marks on other buckets are
203  ///< cleared. See Gv::Ribbon::Item::marked().
204 
205 private:
206  void clear() ;
207  size_t bucket( unsigned int ts ) const ;
208  static size_t timeposImp( const G::Path & path , const std::string & match1 , const std::string & match2 ) ;
209  void unmark() ;
210 
211 private:
212  G::Path m_scan_base ;
213  Gv::Timezone m_tz ;
214  Gv::RibbonRange m_range ;
215  List m_list ;
216  static int m_height ;
217  size_t m_current ;
218  std::string m_match1 ;
219  std::string m_match2 ;
220 } ;
221 
222 #endif
A subsecond-resolution timestamp based on a time_t.
Definition: gdatetime.h:39
void scan(const G::Path &path, size_t tpos)
Does a complete scan for the day that encompases the given timestamped() file.
Definition: gvribbon.cpp:92
RibbonRange around(const G::Path &) const
Returns a range includes the given timestamped trigger path.
Definition: gvribbon.cpp:297
bool apply(const G::Path &)
Called when a possibly-new timestamped file is encountered, allowing the ribbon to update itself for ...
Definition: gvribbon.cpp:154
A class for walking files in a directory tree, with repositioning.
Definition: gfiletree.h:50
A representation of a timezone.
Definition: gvtimezone.h:35
RibbonRange()
Default constructor for an unusable object.
Definition: gvribbon.cpp:270
G::Path last() const
Returns the last scanned path.
Definition: gvribbon.cpp:206
List::const_iterator begin() const
Returns the item list's begin iterator.
Definition: gvribbon.cpp:248
RibbonRange operator()(int offset) const
Returns a range that is offset from the current range.
Definition: gvribbon.cpp:302
void mark(const G::Path &)
Marks the bucket that contains the given path.
unsigned int end() const
Returns the timestamp() value for the start of the next day.
Definition: gvribbon.cpp:327
G::Path first() const
Returns the first scanned path.
Definition: gvribbon.cpp:198
Represents a day span within the file store, used by Gv::Ribbon.
Definition: gvribbon.h:40
const RibbonRange & range() const
Returns the current day range.
Definition: gvribbon.cpp:258
static int height()
Returns a suggested height of the ribbon, in pixels.
Definition: gvribbon.cpp:238
static size_t timepos(const G::Path &path, const std::string &name)
Returns the timestamp position within the given path, or zero if none.
Definition: gvribbon.cpp:49
size_t size() const
Returns the size of the list, as passed in to the constructor.
Definition: gvribbon.cpp:233
Holds ribbon data that can be used to add a ribbon widget to a video stream display.
Definition: gvribbon.h:103
unsigned int start() const
Returns the timestamp() value for the start of the day.
Definition: gvribbon.cpp:322
bool scanSome(G::FileTree &, G::EpochTime interval)
Does a partial scan, limited by the given time interval.
Definition: gvribbon.cpp:131
G::Path startpath() const
Returns the directory path that is the start of the day at the minutes level (eg. ...
Definition: gvribbon.cpp:332
List::const_iterator end() const
Returns the item list's end iterator.
Definition: gvribbon.cpp:253
unsigned int timestamp(const G::Path &) const
Returns the time value for the given path, as seconds within some arbitrary epoch.
Definition: gvribbon.cpp:342
bool scanStart(G::FileTree &, const RibbonRange &range)
Prepares for an iterative scan, with subsequent calls to scanSome().
Definition: gvribbon.cpp:114
G::Path find(size_t offset, bool before=false) const
Finds the first scanned path at-or-after the given bucket position, or returns the last() path if the...
Definition: gvribbon.cpp:215
bool timestamped(const G::Path &path) const
Returns true if the given absolute path has a non-zero timepos().
Definition: gvribbon.cpp:243
G::Path endpath() const
Returns the directory path that is the start of the next day at the minutes level.
Definition: gvribbon.cpp:337
A Path object represents a file system path.
Definition: gpath.h:72
A time bucket within a Gv::Ribbon.
Definition: gvribbon.h:106
Ribbon()
Default constructor for a zero-size() ribbon.
Definition: gvribbon.cpp:35