File: autodir.h

package info (click to toggle)
rsyncrypto 1.12-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 1,312 kB
  • ctags: 576
  • sloc: sh: 3,958; cpp: 3,253; makefile: 70
file content (78 lines) | stat: -rw-r--r-- 1,722 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Win32 implementation

#if !defined(_WIN32)
#error Win32 only header included from non-Win32 build environment
#endif

#if !defined(AUTODIR_H)
#define AUTODIR_H

struct dirent {
    char d_name[MAX_PATH];
};

class autodir {
    HANDLE h_dirscan;
    WIN32_FIND_DATA finddata;
    dirent posixdir;
    bool eof;

    // Disable default copy constructor
    autodir( const autodir & );
    autodir &operator=( const autodir & );
public:
    explicit autodir( const char *dirname ) : eof(false)
    {
        h_dirscan=FindFirstFile((std::string(dirname)+"\\*").c_str(), &finddata );
#if defined(EXCEPT_CLASS)
        if( h_dirscan==INVALID_HANDLE_VALUE )
            throw rscerror("opendir failed", Error2errno(GetLastError()), dirname);
#endif
    }
    ~autodir()
    {
        clear();
    }

    void clear()
    {
        if( h_dirscan!=INVALID_HANDLE_VALUE ) {
            FindClose( h_dirscan );
            h_dirscan=INVALID_HANDLE_VALUE;
            eof=false;
        }
    }

    struct dirent *read()
    {
        if( !eof ) {
            strcpy_s(posixdir.d_name, finddata.cFileName);
            if( !FindNextFile(h_dirscan, &finddata) ) {
                eof=true;
                DWORD error=GetLastError();
                if( error!=ERROR_NO_MORE_FILES ) {
                    throw rscerror("Error getting directory listing", Error2errno(error));
                }
            }
            return &posixdir;
        } else {
            return NULL;
        }
    }
    /*
    void rewind()
    {
        ::rewinddir(dir);
    }
    void seek( off_t offset )
    {
        seekdir( dir, offset );
    }
    off_t telldir()
    {
        return ::telldir( dir );
    }
    */
};

#endif // AUTODIR_H