示例#1
0
CL_String CL_PathHelp::get_fullname(
	const CL_String &fullpath,
	const CL_String &filename,
	const CL_String &extension,
	PathType path_type)
{
	if (!extension.empty() && extension[0] == '.')
		return add_trailing_slash(fullpath, path_type) + filename + extension;
	else if (!extension.empty())
		return add_trailing_slash(fullpath, path_type) + filename + "." + extension;
	else
		return add_trailing_slash(fullpath, path_type) + filename;
}
示例#2
0
CL_String CL_PathHelp::get_fullname(
	const CL_String &location,
	const CL_String &basepath,
	const CL_String &filename,
	const CL_String &extension,
	PathType path_type)
{
	CL_String fullname = location;
	fullname += add_trailing_slash(basepath, path_type);
	fullname += filename;
	if (!extension.empty() && extension[0] == '.')
		fullname += extension;
	else if (!extension.empty())
		fullname += "." + extension;
	return fullname;
}
示例#3
0
CL_String HTMLPage::download_url(const CL_String &page_url, const CL_String &refererer_url)
{
	HTMLUrl url(page_url, refererer_url);
	CL_Console::write_line("Downloading URL: %1", url.to_string());
	CL_String8 request;
	request = cl_format("GET %1 HTTP/1.1\r\n", url.path+url.query);
	if (refererer_url.empty())
		request += cl_format("Host: %1\r\nConnection: close\r\nAccept: text/plain, text/html\r\nUser-Agent: CSSTokenize/0.1\r\n\r\n", url.host);
	else
		request += cl_format("Host: %1\r\nConnection: close\r\nReferer: %2\r\nAccept: text/plain, text/html\r\nUser-Agent: CSSTokenize/0.1\r\n\r\n", url.host, refererer_url);
	//MessageBoxW(0, CL_StringHelp::utf8_to_ucs2(cl_format("GET %1 HTTP/1.1\r\n", url.path+url.query)).c_str(), L"Download URL", MB_OK);;

	CL_TCPConnection connection(CL_SocketName(url.host, url.port));
	connection.set_nodelay(true);
	connection.send(request.data(), request.length(), true);

	CL_String response;
	while (connection.get_read_event().wait(15000))
	{
		char buffer[16*1024];
		int received = connection.read(buffer, 16*1024, false);
		if (received == 0)
			break;
		response.append(buffer, received);
	}
	connection.disconnect_graceful();

	CL_String response_header = response.substr(0, response.find("\r\n\r\n"));
	CL_String content = response.substr(response_header.length() + 4);

	if (response_header.find("Transfer-Encoding: chunked") != CL_String::npos)
	{
		CL_String::size_type start = 0;
		while (true)
		{
			CL_String::size_type end = content.find("\r\n", start);
			if (end == CL_String::npos)
				end = content.length();

			CL_String str_length = content.substr(start, end-start);
			int length = CL_StringHelp::text_to_int(str_length, 16);
			content = content.substr(0, start) + content.substr(end+2);
			start += length;


			end = content.find("\r\n", start);
			if (end == CL_String::npos)
				end = content.length();
			content = content.substr(0, start) + content.substr(end+2);

			if (length == 0)
				break;
		}
	}

	return content;
}
示例#4
0
CL_String CL_PathHelp::remove_trailing_slash(const CL_String &path)
{
	if (path.empty())
		return path;

	if (path[path.length()-1] == '/' || path[path.length()-1] == '\\')
		return path.substr(0, path.length()-1);

	return path;
}
示例#5
0
void RankingFindQueryImpl::submit()
{
	G_ASSERT(!m_playerId.empty() && "player id not set");

	m_parent->start();

	Game &game = Game::getInstance();
	Client &client = game.getNetworkConnection();
	RankingClient &rankingClient = client.getRankingClient();

	m_recvSlot =
			rankingClient.sig_entriesReceived.connect(
					this, &RankingFindQueryImpl::onEntriesReceived);

	m_token = rankingClient.findEntry(m_playerId);
}
void CL_ImageProviderFactory::save(
	CL_PixelBuffer buffer,
	const CL_String &filename,
	CL_VirtualDirectory &directory,
	const CL_String &type_
	)
{
	CL_String type = type_;

	if (type.empty())
		type = CL_PathHelp::get_extension(filename, CL_PathHelp::path_type_virtual);
	
	if (types.find(type) == types.end()) throw CL_Exception("Unknown image provider type " + type);
	
	CL_ImageProviderType *factory = types[type];
	factory->save(buffer, filename, directory);
}
示例#7
0
bool CL_Directory::create(const CL_String &dir_name)
{
	if (dir_name.empty())
		return false;

	// this will be a full path
	CL_String full_path; // calculate the full path

	#ifdef WIN32
		DWORD buff_len = ::GetFullPathName(CL_StringHelp::utf8_to_ucs2(dir_name).c_str(), 0, 0, 0);

		if (buff_len == 0)
			// can't calculate, return bad status
			return false;
		else
		{
			std::vector<TCHAR> buffer_vector;
			buffer_vector.resize(buff_len + 1);
			TCHAR *buffer = &(buffer_vector[0]);
			TCHAR *buffer_ptr_to_filename = 0;
			// Obtaining full path
			buff_len = ::GetFullPathName(CL_StringHelp::utf8_to_ucs2(dir_name).c_str(), buff_len, buffer, &buffer_ptr_to_filename);
			if (buff_len == 0)
				// can't obtain full path, return bad status
				return false;
			else
				// ok, save it
				full_path = buffer;
		}
	#else
		// TODO: add here Linux version of GetFullPathName
		full_path = dir_name;
	#endif

#ifdef WIN32
		return ::CreateDirectory(CL_StringHelp::utf8_to_ucs2(full_path).c_str(), NULL) != 0;
#else
		return ::mkdir(full_path.c_str(), 0755) == 0;
#endif
}
示例#8
0
CL_String CL_PathHelp::add_trailing_slash(const CL_String &path, PathType path_type)
{
	if (path.empty())
		return path;
	if (path_type == path_type_file)
	{
#ifdef WIN32
		if (path[path.length()-1] != '/' && path[path.length()-1] != '\\')
			return path + "\\";
#else
		if (path[path.length()-1] != '/' && path[path.length()-1] != '\\')
			return path + "/";
#endif
		return path;
	}
	else
	{
		if (path[path.length()-1] != '/')
			return path + "/";
		return path;
	}
}
示例#9
0
bool CL_Directory::remove(const CL_String &dir_name, bool delete_files, bool delete_sub_directories)
{
	if (dir_name.empty())
		return false;

	// this will be a full path
	CL_String full_path;

	// calculate the full path
	#ifdef WIN32
		DWORD buff_len = ::GetFullPathName(CL_StringHelp::utf8_to_ucs2(dir_name).c_str(), 0, 0, 0);

		if (buff_len == 0)
			// can't calculate, return bad status
			return false;
		else
		{
			std::vector<TCHAR> buffer_vector;
			buffer_vector.resize(buff_len + 1);
			TCHAR *buffer = &(buffer_vector[0]);
			TCHAR *buffer_ptr_to_filename = 0;
			// Obtaining full path
			buff_len = ::GetFullPathName(CL_StringHelp::utf8_to_ucs2(dir_name).c_str(), buff_len, buffer, &buffer_ptr_to_filename);
			if (buff_len == 0)
				// can't obtaing full path, return bad status
				return false;
			else
				// ok, save it
				full_path = buffer;
		}
	#else
		// TODO: add here Linux version of GetFullPathName
		full_path = dir_name;
	#endif

	// This scope needed for deleting directiory at end of function,
	// because scanner lock current dir :(
	{
		CL_DirectoryScanner scanner;

		if (!scanner.scan(full_path))
			// can't even start scaning
			return false;

		// FIXME: probably bug in directory_scanner, it return ""
		// for first file :(
		if (scanner.next())
		while(true)
		{
			// If found sub_directory, try remove it,
			// also checking for "." and "..", because they are unremovable
			if (scanner.is_directory() && delete_sub_directories &&
				scanner.get_name() != "." && scanner.get_name() != "..")
			{
				// FIXME: directory_scanner lock directory, so it can't be
				// removed, this is workaround
				CL_String sub_dir_path = scanner.get_pathname();
				bool scann_successfull = scanner.next();

				// delete files in sub_directory
				if (!CL_Directory::remove(sub_dir_path.c_str(),
							  delete_files,
							  delete_sub_directories))
					return false;

				if (!scann_successfull)
					break;
				else
					continue;
			}
			else
			{
				// Check for deleting file (or whatever is not directory),
				// if this is allowed
				if (delete_files && !scanner.is_directory())
				{
					// delete a file
					#ifdef WIN32
						if (::DeleteFile(CL_StringHelp::utf8_to_ucs2(scanner.get_pathname()).c_str()) == 0)
							return false;
					#else
						if (::remove(scanner.get_pathname().c_str()) != 0)
							return false;
					#endif
					if (!scanner.next())
						break;
				}
				// This is for "." and ".."
				else
				{
					if (!scanner.next())
						break;
				}
			}
		}
	}
	// Finaly remove the directory (or sub_directory if in recursion)
	#ifdef WIN32
		return ::RemoveDirectory(CL_StringHelp::utf8_to_ucs2(full_path).c_str()) != 0;
	#else
		return ::rmdir(full_path.c_str()) == 0;
	#endif
}
CL_Resource CL_ResourceManager::create_resource(const CL_String &resource_id, const CL_String &type)
{
	if (resource_exists(resource_id))
		throw CL_Exception(cl_format("Resource %1 already exists", resource_id));

	std::vector<CL_String> path_elements = CL_PathHelp::split_basepath(resource_id);
	CL_String name = CL_PathHelp::get_filename(resource_id);

	// Walk tree as deep as we can get:
	CL_DomNode parent = impl->document.get_document_element();
	CL_DomNode cur = parent.get_first_child();
	std::vector<CL_String>::iterator path_it = path_elements.begin();
	while (!cur.is_null() && path_it != path_elements.end())
	{
		if (cur.is_element() &&
			cur.get_namespace_uri() == impl->ns_resources &&
			cur.get_local_name() == "section")
		{
			CL_DomElement element = cur.to_element();
			CL_String name = element.get_attribute_ns(impl->ns_resources, "name");
			if (name == *path_it)
			{
				++path_it;
				parent = cur;
				cur = cur.get_first_child();
				continue;
			}
		}
		cur = cur.get_next_sibling();
	}

	// Create any missing parent nodes:
	CL_String prefix = parent.get_prefix();
	while (path_it != path_elements.end())
	{
		CL_DomElement section;
		if (prefix.empty())
		{
			section = impl->document.create_element_ns( impl->ns_resources, (*path_it) );
		}
		else
		{
			section = impl->document.create_element_ns( impl->ns_resources,(prefix + ":" + *path_it));
		}
		parent.append_child(section);
		parent = section;
		++path_it;
	}

	// Create node:
	CL_DomElement resource_node;
	if (prefix.empty())
	{
		resource_node = impl->document.create_element_ns( impl->ns_resources, (type));
	}
	else
	{
		resource_node = impl->document.create_element_ns( impl->ns_resources,(prefix + ":" + type));
	}

	resource_node.set_attribute_ns(
		impl->ns_resources,
		prefix.empty() ? "name" : (prefix + ":name"),
		name);
	parent.append_child(resource_node);

	// Create resource:
	impl->resources[resource_id] = CL_Resource(resource_node, *this);
	return impl->resources[resource_id];
}
示例#11
0
CL_String CL_PathHelp::normalize(
	const CL_String &input_path,
	PathType path_type)
{
	if (input_path.empty())
		return CL_String();

	CL_String location = get_location(input_path, path_type);
	CL_String path = input_path.substr(location.length());

	bool ends_in_slash = false;

	if (input_path[input_path.size()-1] == '/' ||
		input_path[input_path.size()-1] == '\\')
	{
		ends_in_slash = true;
		if (input_path.size() == 1)
		{
#ifdef WIN32
			if (path_type == path_type_file)
			{
				return "\\";
			}
#endif
			return "/";
		}
	}

	std::vector<CL_String> elements;
	bool absolute = false;
	int level = 0;
	CL_String::size_type pos = 0, last_pos = 0;

	while (true)
	{
		pos = path.find_first_of("/\\", last_pos);

		if (pos == CL_String::npos)
			pos = path.length();

		if (pos == 0)
		{
			absolute = true;
		}
		else
		{
			CL_String element = path.substr(last_pos, pos-last_pos);
			if (element.empty() && pos != path.length())
				throw CL_Exception(cl_format("Unable to normalize invalid path %1", input_path));

			if (element.empty())
			{
			}
			else if (element == ".")
			{
			}
			else if (element == "..")
			{
				level--;
				if (!elements.empty())
				{
					if (elements[elements.size()-1] != "..")
						elements.erase(elements.begin() + elements.size() - 1);
					else
						elements.push_back("..");
				}
				else
				{
					elements.push_back("..");
				}

				if (absolute && level < 0)
					throw CL_Exception(cl_format("Unable to normalize invalid path %1", input_path));
			}
			else
			{
				elements.push_back(element);
				level++;
			}
		}

		if (pos == path.length())
			break;

		last_pos = pos+1;
	}

	CL_String normalized_path;

	if (absolute)
	{
#ifdef WIN32
		if (path_type == path_type_file)
			normalized_path += "\\";
		else
			normalized_path += "/";
#else
		normalized_path += "/";
#endif
	}

	for (CL_String::size_type i = 0; i < elements.size(); i++)
	{
#ifdef WIN32
		if (path_type == path_type_file)
			normalized_path += elements[i] + "\\";
		else
			normalized_path += elements[i] + "/";
#else
		normalized_path += elements[i] + "/";
#endif
	}
	if (!elements.empty() && !ends_in_slash)
		normalized_path = normalized_path.substr(0, normalized_path.length()-1);

	return location + normalized_path;
}