Exemple #1
0
void path::check() const {
	
	arx_assert_msg(pathstr.find('\\') == std::string::npos, "bad path: \"%s\"", pathstr.c_str());
	
	size_t pos = 0;
	while(is_path_up(pathstr, pos)) {
		pos += 3;
	}
	
	while(pos < pathstr.length()) {
		
		size_t next = pathstr.find(dir_sep, pos);
		if(next == std::string::npos) {
			next = pathstr.length();
		} else {
			arx_assert_msg(next + 1 != pathstr.length(), "bad path: \"%s\"", pathstr.c_str());
		}
		
		arx_assert_msg(next > pos, "bad path: \"%s\"", pathstr.c_str());
		
		arx_assert_msg(next - pos > 1 || pathstr[pos] != '.', "bad path: \"%s\"", pathstr.c_str());
		
		if(next - pos == 2) {
			arx_assert_msg(pathstr[pos] != '.' || pathstr[pos + 1] != '.', "bad path: \"%s\"", pathstr.c_str());
		}
		
		pos = next + 1;
	}
	
}
path path::resolve(const path & a, const path & b) {
	
	size_t bpos = 0;
	size_t apos = a.pathstr.length();
	while(true) {
		
		size_t dirpos = a.pathstr.find_last_of(dir_sep, apos - 1);
		
		if(dirpos == std::string::npos) {
			if(bpos + 3 >= b.pathstr.length()) {
				return create(".");
			} else {
				return b.pathstr.substr(bpos + 3);
			}
		}
		
		if(is_path_up(a.pathstr, dirpos + 1)) {
			return create(a.pathstr.substr(0, apos) + dir_sep + b.pathstr.substr(bpos));
		}
		
		if(dirpos == 0 || (dirpos == 1 && a.pathstr[0] == dir_sep)) {
			if(dirpos != apos - 1) {
				bpos += 3;
			}
			if(bpos >= b.pathstr.length()) {
				return create(a.pathstr.substr(0, dirpos) + dir_sep);
			} else {
				return create(a.pathstr.substr(0, dirpos) + dir_sep + b.pathstr.substr(bpos));
			}
		}
		
		apos = dirpos, bpos += 3;
		
		if(!is_path_up(b.pathstr, bpos)) {
			if(bpos >= b.pathstr.length()) {
				return create(a.pathstr.substr(0, apos));
			} else {
				return create(a.pathstr.substr(0, apos) + dir_sep + b.pathstr.substr(bpos));
			}
		}
	}
	
}
Exemple #3
0
path path::resolve(const path & a, const path & b) {
	
	if(a.empty()) {
		return b;
	}
	
	size_t bpos = 0;
	size_t apos = a.pathstr.length();
	while(true) {
		
		size_t dirpos = a.pathstr.find_last_of(dir_sep, apos - 1);
		
		if(dirpos == std::string::npos) {
			if(bpos + 3 >= b.pathstr.length()) {
				return path();
			} else {
				return b.pathstr.substr(bpos + 3);
			}
		}
		
		if(is_path_up(a.pathstr, dirpos + 1)) {
			return path(a.pathstr.substr(0, apos) + dir_sep + b.pathstr.substr(bpos));
		}
		
		apos = dirpos, bpos += 3;
		
		if(!is_path_up(b.pathstr, bpos)) {
			if(bpos >= b.pathstr.length()) {
				return path(a.pathstr.substr(0, apos));
			} else {
				return path(a.pathstr.substr(0, apos) + dir_sep + b.pathstr.substr(bpos));
			}
		}
		
	}
	
}