]> git.decadent.org.uk Git - dak.git/blob - tools/dsync-0.0/libdsync/contrib/slidingwindow.h
Merge upstream
[dak.git] / tools / dsync-0.0 / libdsync / contrib / slidingwindow.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description                                                          /*{{{*/
3 // $Id: slidingwindow.h,v 1.2 1999/11/15 07:59:49 jgg Exp $
4 /* ######################################################################
5    
6    Sliding Window - Implements a sliding buffer over a file. 
7    
8    The buffer can be of arbitary size and where possible mmap is used
9    to optimize IO.
10    
11    To use, init the class and then call Extend with a 0 input pointer
12    to receive the first block and then call extend with Start <= End 
13    to get the next block. If Start != End then Start will be returned
14    with a new value, but pointing at the same byte, that is the new
15    region will contain the subregion Start -> End(o) but with a new
16    length End-Start, End != End(o).
17    
18    After the file has been exhausted Start == End will be returned, but
19    the old region Start -> End(o) will remain valid.
20    
21    ##################################################################### */
22                                                                         /*}}}*/
23 #ifndef SLIDING_WINDOW_H
24 #define SLIDING_WINDOW_H
25
26 #ifdef __GNUG__
27 #pragma interface "dsync/slidingwindow.h"
28 #endif 
29
30 #include <sys/types.h>
31 #include <dsync/fileutl.h>
32
33 class SlidingWindow
34 {
35    unsigned char *Buffer;
36    unsigned long Size;
37    unsigned long MinSize;
38    FileFd &Fd;
39    unsigned long PageSize;
40    off_t Offset;
41    off_t Left;
42    
43    inline unsigned long Align(off_t V) const {return ((V % PageSize) == 0)?V:V + PageSize - (V % PageSize);};
44    inline unsigned long Align(unsigned long V) const {return ((V % PageSize) == 0)?V:V + PageSize - (V % PageSize);};
45    inline unsigned long AlignDn(off_t V) const {return ((V % PageSize) == 0)?V:V - (V % PageSize);};
46    inline unsigned long AlignDn(unsigned long V) const {return ((V % PageSize) == 0)?V:V - (V % PageSize);};
47    
48    public:
49
50    // Make the distance Start - End longer if possible
51    bool Extend(unsigned char *&Start,unsigned char *&End);
52    
53    SlidingWindow(FileFd &Fd,unsigned long MinSize = 0);
54    ~SlidingWindow();
55 };
56
57 #endif