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;
45 std::cout << "Standard Locations:"
46 << "\n - Home: " << SGPath::standardLocation(SGPath::HOME)
47 << "\n - Desktop: " << SGPath::standardLocation(SGPath::DESKTOP)
48 << "\n - Downloads: " << SGPath::standardLocation(SGPath::DOWNLOADS)
49 << "\n - Documents: " << SGPath::standardLocation(SGPath::DOCUMENTS)
50 << "\n - Pictures: " << SGPath::standardLocation(SGPath::PICTURES)
53 VERIFY( !SGPath::standardLocation(SGPath::HOME ).isNull() );
54 VERIFY( !SGPath::standardLocation(SGPath::DESKTOP ).isNull() );
55 VERIFY( !SGPath::standardLocation(SGPath::DOWNLOADS).isNull() );
56 VERIFY( !SGPath::standardLocation(SGPath::DOCUMENTS).isNull() );
57 VERIFY( !SGPath::standardLocation(SGPath::PICTURES ).isNull() );
60 SGPath::Permissions validateNone(const SGPath&)
62 SGPath::Permissions p;
68 SGPath::Permissions validateRead(const SGPath&)
70 SGPath::Permissions p;
76 SGPath::Permissions validateWrite(const SGPath&)
78 SGPath::Permissions p;
84 int main(int argc, char* argv[])
88 COMPARE(pa.exists(), false);
91 SGPath pb("/Foo/bar/something.png");
92 COMPARE(pb.str(), std::string("/Foo/bar/something.png"));
93 COMPARE(strcmp(pb.c_str(), "/Foo/bar/something.png"), 0);
94 COMPARE(pb.dir(), std::string("/Foo/bar"));
95 COMPARE(pb.file(), std::string("something.png"));
96 COMPARE(pb.base(), std::string("/Foo/bar/something"));
97 COMPARE(pb.file_base(), std::string("something"));
98 COMPARE(pb.extension(), std::string("png"));
99 VERIFY(pb.isAbsolute());
100 VERIFY(!pb.isRelative());
103 SGPath ra("where/to/begin.txt");
104 COMPARE(ra.str(), std::string("where/to/begin.txt"));
105 COMPARE(ra.dir(), std::string("where/to"));
106 COMPARE(ra.file(), std::string("begin.txt"));
107 COMPARE(ra.file_base(), std::string("begin"));
108 VERIFY(!ra.isAbsolute());
109 VERIFY(ra.isRelative());
111 // dots in paths / missing extensions
112 SGPath pk("/Foo/bar.dot/thing");
113 COMPARE(pk.dir(), std::string("/Foo/bar.dot"));
114 COMPARE(pk.file(), std::string("thing"));
115 COMPARE(pk.base(), std::string("/Foo/bar.dot/thing"));
116 COMPARE(pk.file_base(), std::string("thing"));
117 COMPARE(pk.extension(), std::string());
119 // multiple file extensions
120 SGPath pj("/Foo/zot.dot/thing.tar.gz");
121 COMPARE(pj.dir(), std::string("/Foo/zot.dot"));
122 COMPARE(pj.file(), std::string("thing.tar.gz"));
123 COMPARE(pj.base(), std::string("/Foo/zot.dot/thing.tar"));
124 COMPARE(pj.file_base(), std::string("thing"));
125 COMPARE(pj.extension(), std::string("gz"));
126 COMPARE(pj.complete_lower_extension(), std::string("tar.gz"));
129 SGPath rd("where\\to\\begin.txt");
130 COMPARE(rd.str(), std::string("where/to/begin.txt"));
134 SGPath d1("/usr/local");
136 COMPARE(pc.str(), std::string("/usr/local"));
137 pc.append("include");
139 COMPARE(pc.str(), std::string("/usr/local/include"));
140 COMPARE(pc.file(), std::string("include"));
143 pc.add("/opt/local");
144 COMPARE(pc.str(), std::string("/usr/local/include/:/opt/local"));
149 COMPARE(pd.str(), std::string("/Foo/bar/something.png-1"));
151 // create with relative path
152 SGPath rb(d1, "include/foo");
153 COMPARE(rb.str(), std::string("/usr/local/include/foo"));
154 VERIFY(rb.isAbsolute());
156 // lower-casing of file extensions
157 SGPath extA("FOO.ZIP");
158 COMPARE(extA.base(), "FOO");
159 COMPARE(extA.extension(), "ZIP");
160 COMPARE(extA.lower_extension(), "zip");
161 COMPARE(extA.complete_lower_extension(), "zip");
163 SGPath extB("BAH/FOO.HTML.GZ");
164 COMPARE(extB.extension(), "GZ");
165 COMPARE(extB.base(), "BAH/FOO.HTML");
166 COMPARE(extB.lower_extension(), "gz");
167 COMPARE(extB.complete_lower_extension(), "html.gz");
169 COMPARE(d1.str_native(), std::string("\\usr\\local"));
171 SGPath winAbs("C:\\Windows\\System32");
172 COMPARE(winAbs.str(), std::string("C:/Windows/System32"));
174 COMPARE(d1.str_native(), std::string("/usr/local"));
177 // paths with only the file components
178 SGPath pf("something.txt.gz");
179 COMPARE(pf.base(), "something.txt");
180 COMPARE(pf.file(), "something.txt.gz");
181 COMPARE(pf.dir(), "");
182 COMPARE(pf.lower_extension(), "gz");
183 COMPARE(pf.complete_lower_extension(), "txt.gz");
185 COMPARE(pf.canRead(), true);
186 COMPARE(pf.canWrite(), true);
188 SGPath pp(&validateNone);
189 COMPARE(pp.canRead(), false);
190 COMPARE(pp.canWrite(), false);
192 pp.append("./test-dir/file.txt");
193 COMPARE(pp.create_dir(0700), -3);
195 pp.setPermissionChecker(&validateRead);
196 COMPARE(pp.canRead(), true);
197 COMPARE(pp.canWrite(), false);
198 COMPARE(pp.create_dir(0700), -3);
200 pp.setPermissionChecker(&validateWrite);
201 COMPARE(pp.canRead(), false);
202 COMPARE(pp.canWrite(), true);
206 cout << "all tests passed OK" << endl;