root/library/tests/test_util.cpp @ 425

Revision 425, 1.5 kB (checked in by vbarta, 15 years ago)

moved logger test to testsuite, updated logger docs (a bit)

Line 
1#include "test_util.h"
2#include <fstream>
3#include <stdexcept>
4#include <string>
5#include <dirent.h>
6#include <errno.h>
7#include <fcntl.h>
8#include <string.h>
9#include <sys/stat.h>
10#include <sys/types.h>
11#include <unistd.h>
12
13namespace bdm {
14
15std::string load_test_file(const char *fname)
16{
17    char buffer[8192];
18    memset(buffer, 0, sizeof(buffer));
19    std::ifstream src(fname, std::ios_base::binary);
20    src.read(buffer, sizeof(buffer) - 1);
21    return std::string(buffer);
22}
23
24bool remove_all(const char *path) {
25    DIR *dir;
26    dirent *de;
27
28    bool rv = true;
29    if ((dir = opendir(path)) != 0) {
30        try {
31            std::string top(path);
32            top += "/";
33
34            while ((de = readdir(dir)) != 0) {
35                if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {
36                    std::string subpath(top);
37                    subpath += de->d_name;
38                    remove_all(subpath.c_str());
39                }
40            }
41        } catch (...) {
42            closedir(dir);
43            throw;
44        }
45
46        closedir(dir);
47
48        if (rmdir(path)) {
49            std::string msg = "can't remove dir ";
50            msg += path;
51            throw std::runtime_error(msg);
52        }
53    } else { 
54        if (errno == ENOTDIR) {
55            if (unlink(path)) {
56                std::string msg = "can't remove file ";
57                msg += path;
58                throw std::runtime_error(msg);
59            }
60        } else {
61            if (errno != ENOENT) {
62                std::string msg = "can't remove ";
63                msg += path;
64                throw std::runtime_error(msg);
65            } else {
66                // it wasn't there in the first place
67                rv = false;
68            }
69        }
70    }
71
72    return rv;
73}
74
75}
Note: See TracBrowser for help on using the browser.