~ubuntu-branches/ubuntu/oneiric/strigi/oneiric

« back to all changes in this revision

Viewing changes to src/streams/tests/ArchiveReaderTest.cpp

  • Committer: Package Import Robot
  • Author(s): Felix Geyer
  • Date: 2011-09-24 17:12:15 UTC
  • mfrom: (1.2.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: package-import@ubuntu.com-20110924171215-zmbi1f77jntvz65h
Tags: upstream-0.7.6
ImportĀ upstreamĀ versionĀ 0.7.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* This file is part of Strigi Desktop Search
2
 
 *
3
 
 * Copyright (C) 2006 Jos van den Oever <jos@vandenoever.info>
4
 
 *
5
 
 * This library is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU Library General Public
7
 
 * License as published by the Free Software Foundation; either
8
 
 * version 2 of the License, or (at your option) any later version.
9
 
 *
10
 
 * This library is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
 * Library General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU Library General Public License
16
 
 * along with this library; see the file COPYING.LIB.  If not, write to
17
 
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
 
 * Boston, MA 02110-1301, USA.
19
 
 */
20
 
 
21
 
#include "../archivereader.h"
22
 
#include <sys/types.h>
23
 
#include <sys/stat.h>
24
 
#include <dirent.h>
25
 
#include <unistd.h>
26
 
 
27
 
using namespace std;
28
 
using namespace Strigi;
29
 
 
30
 
int errors;
31
 
 
32
 
void
33
 
test1(const char* path) {
34
 
    FileStreamOpener opener;
35
 
    ArchiveReader r;
36
 
    r.addStreamOpener(&opener);
37
 
    StreamBase<char>* s = r.openStream(path);
38
 
    if (s == 0) {
39
 
        fprintf(stderr, "cannot open stream to %s\n", path);
40
 
        errors++;
41
 
    }
42
 
    r.closeStream(s);
43
 
}
44
 
void
45
 
test2(const char* path) {
46
 
    FileStreamOpener opener;
47
 
    ArchiveReader r;
48
 
    r.addStreamOpener(&opener);
49
 
    ArchiveReader::DirLister dl = r.dirEntries(path);
50
 
    EntryInfo e;
51
 
    while (dl.nextEntry(e)) {
52
 
        string filepath(path);
53
 
        filepath += "/";
54
 
        filepath += e.filename;
55
 
        if (e.type == EntryInfo::File) {
56
 
            test1(filepath.c_str());
57
 
        }
58
 
        test2(filepath.c_str());
59
 
    }
60
 
}
61
 
 
62
 
void
63
 
walkdirectories(const char* path, void (*callback)(const char*)) {
64
 
    DIR* dir = opendir(path);
65
 
    if (dir == 0) {
66
 
        callback(path);
67
 
        return;
68
 
    }
69
 
    string p(path);
70
 
    if (p.size() > 0 && p[p.size()-1] != '/') {
71
 
        p.append("/");
72
 
    }
73
 
    struct dirent* subdir = readdir(dir);
74
 
    struct stat dirstat;
75
 
    while (subdir) {
76
 
        if (subdir->d_name[0] == '.') {
77
 
            subdir = readdir(dir);
78
 
            continue;
79
 
        }
80
 
        string name = subdir->d_name;
81
 
        string filepath = p + name;
82
 
        if (lstat(filepath.c_str(), &dirstat) == 0) {
83
 
            if (S_ISREG(dirstat.st_mode)) {
84
 
                callback(filepath.c_str());
85
 
            } else if (S_ISDIR(dirstat.st_mode)) {
86
 
                filepath += "/";
87
 
                walkdirectories(filepath.c_str(), callback);
88
 
            }
89
 
        }
90
 
        subdir = readdir(dir);
91
 
    }
92
 
    closedir(dir);
93
 
}
94
 
 
95
 
/**
96
 
 * Test the class ArchiveReader by analyzing all files in the given
97
 
 * directory.
98
 
 **/
99
 
int
100
 
ArchiveReaderTest(int argc, char** argv) {
101
 
    if (argc < 2) return 1;
102
 
    errors = 0;
103
 
    walkdirectories(argv[1], test1);
104
 
    walkdirectories(argv[1], test2);
105
 
    if (errors) {
106
 
        fprintf(stderr, "%i errors\n", errors);
107
 
    }
108
 
    return errors;
109
 
}