2 #include <simgear/compiler.h>
12 #define COMPARE(a, b) \
14 cerr << "failed:" << #a << " != " << #b << endl; \
20 cerr << "failed:" << #a << endl; \
24 #include <simgear/misc/sg_path.hxx>
25 #include <simgear/misc/sg_dir.hxx>
29 simgear::Dir temp = simgear::Dir::tempDir("foo");
30 cout << "created:" << temp.path() << endl;
32 VERIFY(temp.exists());
33 VERIFY(temp.path().isDir());
34 VERIFY(!temp.path().isFile());
36 SGPath fileInDir = temp.file("foobaz");
37 VERIFY(!fileInDir.exists());
39 if (!temp.remove(true)) {
40 cout << "remove failed!" << endl;
43 cout << temp.path().modTime() << endl;
46 int main(int argc, char* argv[])
50 COMPARE(pa.exists(), false);
53 SGPath pb("/Foo/bar/something.png");
54 COMPARE(pb.str(), std::string("/Foo/bar/something.png"));
55 COMPARE(strcmp(pb.c_str(), "/Foo/bar/something.png"), 0);
56 COMPARE(pb.dir(), std::string("/Foo/bar"));
57 COMPARE(pb.file(), std::string("something.png"));
58 COMPARE(pb.base(), std::string("/Foo/bar/something"));
59 COMPARE(pb.file_base(), std::string("something"));
60 COMPARE(pb.extension(), std::string("png"));
61 VERIFY(pb.isAbsolute());
62 VERIFY(!pb.isRelative());
65 SGPath ra("where/to/begin.txt");
66 COMPARE(ra.str(), std::string("where/to/begin.txt"));
67 COMPARE(ra.dir(), std::string("where/to"));
68 COMPARE(ra.file(), std::string("begin.txt"));
69 COMPARE(ra.file_base(), std::string("begin"));
70 VERIFY(!ra.isAbsolute());
71 VERIFY(ra.isRelative());
73 // dots in paths / missing extensions
74 SGPath pk("/Foo/bar.dot/thing");
75 COMPARE(pk.dir(), std::string("/Foo/bar.dot"));
76 COMPARE(pk.file(), std::string("thing"));
77 COMPARE(pk.base(), std::string("/Foo/bar.dot/thing"));
78 COMPARE(pk.file_base(), std::string("thing"));
79 COMPARE(pk.extension(), std::string());
81 // multiple file extensions
82 SGPath pj("/Foo/zot.dot/thing.tar.gz");
83 COMPARE(pj.dir(), std::string("/Foo/zot.dot"));
84 COMPARE(pj.file(), std::string("thing.tar.gz"));
85 COMPARE(pj.base(), std::string("/Foo/zot.dot/thing.tar"));
86 COMPARE(pj.file_base(), std::string("thing"));
87 COMPARE(pj.extension(), std::string("gz"));
88 COMPARE(pj.complete_lower_extension(), std::string("tar.gz"));
91 SGPath rd("where\\to\\begin.txt");
92 COMPARE(rd.str(), std::string("where/to/begin.txt"));
96 SGPath d1("/usr/local");
98 COMPARE(pc.str(), std::string("/usr/local"));
101 COMPARE(pc.str(), std::string("/usr/local/include"));
102 COMPARE(pc.file(), std::string("include"));
105 pc.add("/opt/local");
106 COMPARE(pc.str(), std::string("/usr/local/include/:/opt/local"));
111 COMPARE(pd.str(), std::string("/Foo/bar/something.png-1"));
113 // create with relative path
114 SGPath rb(d1, "include/foo");
115 COMPARE(rb.str(), std::string("/usr/local/include/foo"));
116 VERIFY(rb.isAbsolute());
118 // lower-casing of file extensions
119 SGPath extA("FOO.ZIP");
120 COMPARE(extA.base(), "FOO");
121 COMPARE(extA.extension(), "ZIP");
122 COMPARE(extA.lower_extension(), "zip");
123 COMPARE(extA.complete_lower_extension(), "zip");
125 SGPath extB("BAH/FOO.HTML.GZ");
126 COMPARE(extB.extension(), "GZ");
127 COMPARE(extB.base(), "BAH/FOO.HTML");
128 COMPARE(extB.lower_extension(), "gz");
129 COMPARE(extB.complete_lower_extension(), "html.gz");
131 COMPARE(d1.str_native(), std::string("\\usr\\local"));
133 SGPath winAbs("C:\\Windows\\System32");
134 COMPARE(winAbs.str(), std::string("C:/Windows/System32"));
136 COMPARE(d1.str_native(), std::string("/usr/local"));
139 // paths with only the file components
140 SGPath pf("something.txt.gz");
141 COMPARE(pf.base(), "something.txt");
142 COMPARE(pf.file(), "something.txt.gz");
143 COMPARE(pf.dir(), "");
144 COMPARE(pf.lower_extension(), "gz");
145 COMPARE(pf.complete_lower_extension(), "txt.gz");
149 cout << "all tests passed OK" << endl;