File: seek.cpp

package info (click to toggle)
stlport4.6 4.6.2-7
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 7,056 kB
  • ctags: 16,390
  • sloc: ansic: 46,190; cpp: 18,805; sh: 266; asm: 93; perl: 58; makefile: 10
file content (138 lines) | stat: -rw-r--r-- 3,659 bytes parent folder | download | duplicates (5)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138


// Regression test for STL seekg

#include <cstdio>       // I really like formatted output.
#include <cstdarg>
#include <fstream>
#include <vector>

#ifndef cstd
#ifdef _MSC_VER
#define cstd
#else
#define cstd std
#endif
#endif

#ifndef USING_STLPORT
#define vsnprintf _vsnprintf
#endif

class Error : public std::exception
{
 public:
    Error(const char *pszfmt, ...);
    const char *what() const;
 private:
    char mMessage[256];
};

#define countof(x) (sizeof(x)/sizeof((x)[0]))

Error::Error(const char *pszfmt, ...)
{
    cstd::va_list ap;
    va_start(ap, pszfmt);
    cstd::vsnprintf(mMessage, countof(mMessage), pszfmt, ap);
    va_end(ap);
}

const char *Error::what() const
{
    return mMessage;
}

// usage: testseek filename
int main(int argc, char **argv)
{
    try
    {
        if (argc != 2) {
            throw Error("Wrong number of arguments (Usage: testseek filename)");
        }
        std::ifstream ifs(argv[1]);
        if (!ifs) {
            throw Error("Failed to open file \"%s\"", argv[1]);
        }

        ifs.exceptions(std::ios::badbit);
        // First pass: read contents of file into character vector
        std::vector<char> charvec;
        cstd::printf("First pass:\n");
        for(;;)
        {
            char ch;
            if (!ifs.get(ch)) {
                break;
            }
            cstd::printf("%c", ch);
            charvec.push_back(ch);
        }
        printf("First pass done\n");

        // Second pass: get positions into pos vector, checking chars
        std::vector<std::ios::pos_type> posvec;
        ifs.clear();    // necessary?
        ifs.seekg(0);
        std::vector<char>::const_iterator char_it;
        printf("Second pass:\n");
        for(char_it = charvec.begin(); char_it != charvec.end(); ++char_it)
        {
            std::ios::pos_type pos = ifs.tellg();
            posvec.push_back(pos);
            char ch;
            if (!ifs.get(ch)) {
                break;
            }
            cstd::printf("%c", ch);
            if (ch != *char_it) {
                throw Error("Character mismatch: got '%c', expected '%c'",
                            ch, *char_it);
            }
        }
        if (char_it != charvec.end()) {
            throw Error("Unexpected end of file");
        }

        printf("Second pass done\n");

        // Third pass: seek to each saved position and read next char
        ifs.clear();    // necessary, but I don't like it
        std::vector<std::ios::pos_type>::const_iterator pos_it;

        for(pos_it = posvec.begin();
            pos_it != posvec.end(); ++pos_it)
        {
            printf ("%d ", (int)*pos_it);
        }
        printf ("\n");

        printf("Third pass:\n");
        for(char_it = charvec.begin(), pos_it = posvec.begin();
            char_it != charvec.end(); ++char_it, ++pos_it)
        {
            ifs.seekg(*pos_it);
            char ch;
            if (!ifs.get(ch)) {
                throw Error("End of file when expecting '%c'", *char_it);
            }
            cstd::printf("%c", ch);
            if (ch != *char_it) {
                throw Error("Character mismatch: got '%c', expected '%c'",
                            ch, *char_it);
            }
        }
        printf("Third pass done\n");
        printf("Test passed\n");
        
            
    }
    catch (std::exception &e)
    {
        cstd::printf("\nCaught exception: %s\n", e.what());
        cstd::printf("Test failed!\n");
        return 1;
    }
    return 0;
}