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.utf8Str(), std::string("/Foo/bar/something.png"));
93 COMPARE(pb.local8BitStr(), std::string("/Foo/bar/something.png"));
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.utf8Str(), std::string("where/to/begin.txt"));
105 COMPARE(ra.local8BitStr(), std::string("where/to/begin.txt"));
106 COMPARE(ra.dir(), std::string("where/to"));
107 COMPARE(ra.file(), std::string("begin.txt"));
108 COMPARE(ra.file_base(), std::string("begin"));
109 VERIFY(!ra.isAbsolute());
110 VERIFY(ra.isRelative());
112 // dots in paths / missing extensions
113 SGPath pk("/Foo/bar.dot/thing");
114 COMPARE(pk.dir(), std::string("/Foo/bar.dot"));
115 COMPARE(pk.file(), std::string("thing"));
116 COMPARE(pk.base(), std::string("/Foo/bar.dot/thing"));
117 COMPARE(pk.file_base(), std::string("thing"));
118 COMPARE(pk.extension(), std::string());
120 // multiple file extensions
121 SGPath pj("/Foo/zot.dot/thing.tar.gz");
122 COMPARE(pj.dir(), std::string("/Foo/zot.dot"));
123 COMPARE(pj.file(), std::string("thing.tar.gz"));
124 COMPARE(pj.base(), std::string("/Foo/zot.dot/thing.tar"));
125 COMPARE(pj.file_base(), std::string("thing"));
126 COMPARE(pj.extension(), std::string("gz"));
127 COMPARE(pj.complete_lower_extension(), std::string("tar.gz"));
130 SGPath rd("where\\to\\begin.txt");
131 COMPARE(rd.utf8Str(), std::string("where/to/begin.txt"));
135 SGPath d1("/usr/local");
137 COMPARE(pc.utf8Str(), std::string("/usr/local"));
138 pc.append("include");
140 COMPARE(pc.utf8Str(), std::string("/usr/local/include"));
141 COMPARE(pc.file(), std::string("include"));
146 COMPARE(pd.utf8Str(), std::string("/Foo/bar/something.png-1"));
148 // create with relative path
149 SGPath rb(d1, "include/foo");
150 COMPARE(rb.utf8Str(), std::string("/usr/local/include/foo"));
151 VERIFY(rb.isAbsolute());
153 // lower-casing of file extensions
154 SGPath extA("FOO.ZIP");
155 COMPARE(extA.base(), "FOO");
156 COMPARE(extA.extension(), "ZIP");
157 COMPARE(extA.lower_extension(), "zip");
158 COMPARE(extA.complete_lower_extension(), "zip");
160 SGPath extB("BAH/FOO.HTML.GZ");
161 COMPARE(extB.extension(), "GZ");
162 COMPARE(extB.base(), "BAH/FOO.HTML");
163 COMPARE(extB.lower_extension(), "gz");
164 COMPARE(extB.complete_lower_extension(), "html.gz");
166 SGPath winAbs("C:\\Windows\\System32");
167 COMPARE(winAbs.local8BitStr(), std::string("C:/Windows/System32"));
171 // paths with only the file components
172 SGPath pf("something.txt.gz");
173 COMPARE(pf.base(), "something.txt");
174 COMPARE(pf.file(), "something.txt.gz");
175 COMPARE(pf.dir(), "");
176 COMPARE(pf.lower_extension(), "gz");
177 COMPARE(pf.complete_lower_extension(), "txt.gz");
179 COMPARE(pf.canRead(), true);
180 COMPARE(pf.canWrite(), true);
182 SGPath pp(&validateNone);
183 COMPARE(pp.canRead(), false);
184 COMPARE(pp.canWrite(), false);
186 pp.append("./test-dir/file.txt");
187 COMPARE(pp.create_dir(0700), -3);
189 pp.setPermissionChecker(&validateRead);
190 COMPARE(pp.canRead(), true);
191 COMPARE(pp.canWrite(), false);
192 COMPARE(pp.create_dir(0700), -3);
194 pp.setPermissionChecker(&validateWrite);
195 COMPARE(pp.canRead(), false);
196 COMPARE(pp.canWrite(), true);
200 cout << "all tests passed OK" << endl;