コード例 #1
0
ファイル: Directories.cpp プロジェクト: dbuksbaum/FreeOrion
void InitBinDir(const std::string& argv0) {
    bool problem = false;
    try {
        // get this executable's path by following link
        const size_t BUF_SIZE = 2048;
        char buf[BUF_SIZE] = {0};

#ifdef __FreeBSD__
        int mib[4];
        mib[0] = CTL_KERN;
        mib[1] = KERN_PROC;
        mib[2] = KERN_PROC_PATHNAME;
        mib[3] = -1;
        size_t buf_size = sizeof(buf);
        sysctl(mib, 4, buf, &buf_size, 0, 0);
#else
        size_t exe_path_size = readlink("/proc/self/exe", buf, BUF_SIZE);
        if (exe_path_size == static_cast<size_t>(-1)) {
            problem = true;

        } else {
            if (exe_path_size >= BUF_SIZE || exe_path_size < 0)
                exe_path_size = BUF_SIZE - 1;   // ensure buffer isn't accessed out of range
            buf[exe_path_size] = 0;             // null terminate c-string
        }
#endif

        if (!problem) {
            buf[BUF_SIZE - 1] = 0;              // to be safe, else initializing an std::string with a non-null-terminated string could read invalid data outside the buffer range
            std::string path_text(buf);

            fs::path binary_file = fs::system_complete(fs::path(path_text));
            bin_dir = binary_file.branch_path();

            // check that a "freeorion" file (hopefully the freeorion binary) exists in the found directory
            fs::path p(bin_dir);
            p /= "freeorion";
            if (!exists(p))
                problem = true;
        }

    } catch (fs::filesystem_error err) {
        problem = true;
    }

    if (problem) {
        // failed trying to parse the call path, so try hard-coded standard location...
        char* dir_name = br_find_bin_dir("/usr/local/bin");
        fs::path p(dir_name);
        std::free(dir_name);

        // if the path does not exist, fall back to the working directory
        if (!exists(p)) {
            bin_dir = fs::initial_path();
        } else {
            bin_dir = p;
        }
    }
}
コード例 #2
0
ファイル: XMLParserTest.cpp プロジェクト: rayfill/cpplib
	void XMLPathTraversalTest()
	{
		typedef std::map<std::basic_string<wchar_t>,
			std::basic_string<wchar_t> > AttributesType;

		TagElement<wchar_t> rootTag(L"root");
		TagElement<wchar_t> childTag1(L"child1");
		TagElement<wchar_t> childTag1_(L"child1");
		TagElement<wchar_t> childTag2(L"child2");
		StringElement<wchar_t> stringTag(L"string");
		CommentElement<wchar_t> commentTag(L"comment");

		rootTag.addChild(&childTag1);
		rootTag.addChild(&childTag2);
		rootTag.addChild(&childTag1_);
		childTag1.addChild(&stringTag);
		childTag1_.addChild(&commentTag);

		CPPUNIT_ASSERT(rootTag.children.size() == 3);
		CPPUNIT_ASSERT(dynamic_cast<TagElement<wchar_t>*>
					   (rootTag.getChildElement(L"child1"))->getTagName() ==
					   L"child1");
		CPPUNIT_ASSERT(dynamic_cast<TagElement<wchar_t>*>
					   (rootTag.getChildElement(L"child2"))->getTagName() ==
					   L"child2");

		std::vector<Element<wchar_t>*> result;

		XMLPath<wchar_t> path_root(L"/root");
		result = path_root.evaluate(&rootTag);
		CPPUNIT_ASSERT(result.size() == 1);
		CPPUNIT_ASSERT(result[0] == &rootTag);

		XMLPath<wchar_t> path_roots(L"/root[]");
		result = path_roots.evaluate(&rootTag);
		CPPUNIT_ASSERT(result.size() == 1);
		CPPUNIT_ASSERT(result[0] == &rootTag);

		XMLPath<wchar_t> path_child1(L"/root/child1");
		result = path_child1.evaluate(&rootTag);
		CPPUNIT_ASSERT(result.size() == 1);
		CPPUNIT_ASSERT(result[0] == &childTag1);

		XMLPath<wchar_t> path_child1s(L"/root/child1[]");
		result = path_child1s.evaluate(&rootTag);
		CPPUNIT_ASSERT(result.size() == 2);
		CPPUNIT_ASSERT(result[0] == &childTag1);
		CPPUNIT_ASSERT(result[1] == &childTag1_);

		XMLPath<wchar_t> path_text(L"/root/child1[]/#text");
		result = path_text.evaluate(&rootTag);
		CPPUNIT_ASSERT(result.size() == 1);
		CPPUNIT_ASSERT(result[0] == &stringTag);

		XMLPath<wchar_t> path_comment(L"/root/child1[]/#comment");
		result = path_comment.evaluate(&rootTag);
		CPPUNIT_ASSERT(result.size() == 1);
		CPPUNIT_ASSERT(result[0] == &commentTag);

		rootTag.removeChild(&childTag1);
		rootTag.removeChild(&childTag2);
		rootTag.removeChild(&childTag1_);
		childTag1.removeChild(&stringTag);
		childTag1_.removeChild(&commentTag);
	}