2 #include <simgear/compiler.h>
10 #define COMPARE(a, b) \
12 cerr << "failed:" << #a << " != " << #b << endl; \
18 cerr << "failed:" << #a << endl; \
22 #include <simgear/misc/sg_path.hxx>
23 #include <simgear/misc/sg_dir.hxx>
27 simgear::Dir temp = simgear::Dir::tempDir("foo");
28 cout << "created:" << temp.path().str() << endl;
30 VERIFY(temp.exists());
31 VERIFY(temp.path().isDir());
32 VERIFY(!temp.path().isFile());
34 SGPath fileInDir = temp.file("foobaz");
35 VERIFY(!fileInDir.exists());
37 if (!temp.remove(true)) {
38 cout << "remove failed!" << endl;
41 cout << temp.path().modTime() << endl;
44 int main(int argc, char* argv[])
48 COMPARE(pa.exists(), false);
51 SGPath pb("/Foo/bar/something.png");
52 COMPARE(pb.str(), std::string("/Foo/bar/something.png"));
53 COMPARE(strcmp(pb.c_str(), "/Foo/bar/something.png"), 0);
54 COMPARE(pb.dir(), std::string("/Foo/bar"));
55 COMPARE(pb.file(), std::string("something.png"));
56 COMPARE(pb.base(), std::string("/Foo/bar/something"));
57 COMPARE(pb.file_base(), std::string("something"));
58 COMPARE(pb.extension(), std::string("png"));
59 VERIFY(pb.isAbsolute());
60 VERIFY(!pb.isRelative());
63 SGPath ra("where/to/begin.txt");
64 COMPARE(ra.str(), std::string("where/to/begin.txt"));
65 COMPARE(ra.dir(), std::string("where/to"));
66 COMPARE(ra.file(), std::string("begin.txt"));
67 COMPARE(ra.file_base(), std::string("begin"));
68 VERIFY(!ra.isAbsolute());
69 VERIFY(ra.isRelative());
71 // dots in paths / missing extensions
72 SGPath pk("/Foo/bar.dot/thing");
73 COMPARE(pk.dir(), std::string("/Foo/bar.dot"));
74 COMPARE(pk.file(), std::string("thing"));
75 COMPARE(pk.base(), std::string("/Foo/bar.dot/thing"));
76 COMPARE(pk.file_base(), std::string("thing"));
77 COMPARE(pk.extension(), std::string());
79 // multiple file extensions
80 SGPath pj("/Foo/zot.dot/thing.tar.gz");
81 COMPARE(pj.dir(), std::string("/Foo/zot.dot"));
82 COMPARE(pj.file(), std::string("thing.tar.gz"));
83 COMPARE(pj.base(), std::string("/Foo/zot.dot/thing"));
84 COMPARE(pj.file_base(), std::string("thing"));
85 COMPARE(pj.extension(), std::string("gz"));
86 COMPARE(pj.complete_lower_extension(), std::string("tar.gz"));
89 SGPath rd("where\\to\\begin.txt");
90 COMPARE(rd.str(), std::string("where/to/begin.txt"));
94 SGPath d1("/usr/local");
96 COMPARE(pc.str(), std::string("/usr/local"));
99 COMPARE(pc.str(), std::string("/usr/local/include"));
100 COMPARE(pc.file(), std::string("include"));
103 pc.add("/opt/local");
104 COMPARE(pc.str(), std::string("/usr/local/include/:/opt/local"));
109 COMPARE(pd.str(), std::string("/Foo/bar/something.png-1"));
111 // create with relative path
112 SGPath rb(d1, "include/foo");
113 COMPARE(rb.str(), std::string("/usr/local/include/foo"));
114 VERIFY(rb.isAbsolute());
116 // lower-casing of file extensions
117 SGPath extA("FOO.ZIP");
118 COMPARE(extA.base(), "FOO");
119 COMPARE(extA.extension(), "ZIP");
120 COMPARE(extA.lower_extension(), "zip");
121 COMPARE(extA.complete_lower_extension(), "zip");
123 SGPath extB("BAH/FOO.HTML.GZ");
124 COMPARE(extB.extension(), "GZ");
125 COMPARE(extB.base(), "BAH/FOO");
126 COMPARE(extB.lower_extension(), "gz");
127 COMPARE(extB.complete_lower_extension(), "html.gz");
129 COMPARE(d1.str_native(), std::string("\\usr\\local"));
131 SGPath winAbs("C:\\Windows\\System32");
132 COMPARE(winAbs.str(), std::string("C:/Windows/System32"));
134 COMPARE(d1.str_native(), std::string("/usr/local"));
139 cout << "all tests passed OK" << endl;