int string::compare(size_type pos1, size_type n1, const std::u32string& str) const
{
    if ( n1 == 0 && !str.empty() )
        return -1;
    
    size_type sz = (n1 == npos ? size() - pos1 : n1);
    size_type len = str.size();
    size_type n = std::min(sz, len);
    
    auto pos = cbegin()+pos1;
    auto spos = str.cbegin();
    for ( ; n; pos++, spos++ )
    {
        if ( traits_type::lt(*pos, *spos) )
            return -1;
        if ( traits_type::lt(*spos, *pos) )
            return 1;
    }
    
    if ( sz < len )
        return -1;
    if ( sz > len )
        return 1;
    return 0;
}
예제 #2
0
Text::Text(Mat4f const & transformation, std::string const & fontname, std::u32string const & text) :
   dynamicHint(text.empty()), modelMat(transformation),
   fontname(fontname), string(text),
   vao(nullptr), vbo(nullptr), ibo(nullptr)
{
   init();
}
int wmain(int argc, wchar_t *argv[])
{
	try
	{
		if (argc < 2)
		{
			std::cout << "No url" << std::endl;
			return 1;
		}

		//Получение страницы посредством get-запроса
		const Web::Url l_url(Encoding::utf16to8(reinterpret_cast<char16_t *>(argv[1])));
		const std::string l_content(get(l_url));

		//Извлечение статьи
		std::string l_text;
		if (!recognize(l_content, l_text))
		{
			std::cout << "Couldn't recognize" << std::endl;
			return 1;
		}

		//Формирование путей
		const std::u32string l_utf32path(Encoding::utf8to32(l_url.path()));
		std::string l_fixedPath;
		for (auto j(l_utf32path.begin()); j != l_utf32path.end();)
		{
			const auto i(*j == '/' ? l_fixedPath += '/', j + 1 : j);
			j = std::find(i, l_utf32path.end(), '/');
			const size_t l_size = std::min(30, std::distance(i, j));
			const std::u32string l_part(i, i + l_size);
			l_fixedPath += Encoding::utf32to8(l_part);
		}
		const std::u32string l_u32fn(Encoding::utf8to32(l_url.file()));
		const std::u32string l_base(l_u32fn.substr(0, l_u32fn.find('.')).substr(0, 30));
		const std::string l_fixedName(l_base.empty() ? std::string("a.txt") : Encoding::utf32to8(l_base) + ".txt");
		//http://boost.2283326.n4.nabble.com/boost-filesystem-path-as-utf-8-tp4320098p4322460.html
		boost::filesystem::detail::utf8_codecvt_facet l_utf8;
		const boost::filesystem::path l_dirPath(l_url.host() + l_fixedPath, l_utf8);
		const boost::filesystem::path l_filePath(l_url.host() + l_fixedPath + '/' + l_fixedName, l_utf8);

		//Создание каталогов и сохранения статьи в текстовый файл
		boost::filesystem::create_directories(l_dirPath);
		boost::filesystem::ofstream l_of(l_filePath, std::ios::binary | std::ios::out);
		if (!l_of.is_open())
		{
			std::cout << "Output file open error" << std::endl;
			return -1;
		}
		l_of.write("\xef\xbb\xbf", 3);	//BOM
		l_of.write(l_text.data(), l_text.size());
		l_of.close();

		if (argc > 2)
		{
			const boost::filesystem::path l_xfp(l_url.host() + l_fixedPath + "/1.html", l_utf8);
			boost::filesystem::ofstream l_xof(l_xfp, std::ios::binary | std::ios::out);
			if (l_xof.is_open())
			{
				l_xof.write("\xef\xbb\xbf", 3);
				l_xof.write(l_content.data(), l_content.size());
				l_xof.close();
			}
		}

		return 0;
	}
	catch (const std::bad_alloc &)
	{
		::printf("Mem alloc error");
	}
	catch (const std::exception &a)
	{
		std::cout << "Error" << std::endl << a.what() << std::endl;
	}
	return -1;
}