Beispiel #1
0
	AttributeSelector::AttributeSelector(SelectorOperator op, boost::string_ref key, boost::string_ref value) :
		m_operator(op),
		m_attributeNameString(key.to_string()),
		m_attributeNameRef(m_attributeNameString),
		m_attributeValueString(value.to_string()),
		m_attributeValueRef(m_attributeValueString)
	{
		if (m_attributeNameRef.size() == 0)
		{
			throw std::runtime_error(u8"In AttributeSelector::AttributeSelector(SelectorOperator, boost::string_ref, const bool) - Supplied attribute identifier has zero length.");
		}

		if (m_attributeValueRef.size() == 0)
		{
			throw std::runtime_error(u8"In AttributeSelector::AttributeSelector(SelectorOperator, boost::string_ref, const bool) - Supplied attribute value has zero length.");
		}

		if (m_operator == SelectorOperator::ValueContainsElementInWhitespaceSeparatedList)
		{
			if (m_attributeNameRef.find_first_of(u8"\t\r\n ") != std::string::npos)
			{
				throw std::runtime_error(u8"In AttributeSelector::AttributeSelector(SelectorOperator, boost::string_ref, const bool) - Constructed ValueContainsElementInWhitespaceSeparatedList attribute selector, but spaces exist in the search value. This is not allowed.");
			}
		}

		#ifndef NDEBUG
			#ifdef GQ_VERBOSE_DEBUG_NFO
				std::cout << "Built AttributeSelector with operator " << static_cast<size_t>(m_operator) << " with key " << m_attributeNameRef << " looking for value " << m_attributeValueRef << std::endl;
			#endif
		#endif

		switch (m_operator)
		{
			case SelectorOperator::ValueEquals:
			case SelectorOperator::ValueContainsElementInWhitespaceSeparatedList:
			{
				AddMatchTrait(m_attributeNameRef, m_attributeValueRef);
			}
			break;

			case SelectorOperator::Exists:
			case SelectorOperator::ValueContains:
			case SelectorOperator::ValueHasPrefix:
			case SelectorOperator::ValueHasSuffix:
			case SelectorOperator::ValueIsHyphenSeparatedListStartingWith:
			{
				AddMatchTrait(m_attributeNameRef, SpecialTraits::GetAnyValue());
			}
			break;
		}
	}
Beispiel #2
0
std::string get_directory_listing( boost::string_ref folder ) {
	namespace fs = boost::filesystem;
	fs::path p { folder.to_string( ) };
	std::ostringstream ss;
	try {
		if( exists( p ) ) {
			if( fs::is_regular_file( p ) ) {
				ss << p << " size is " << fs::file_size( p ) << "\r\n";
			} else if( fs::is_directory( p ) ) {
				ss << p << " is a directory containing:\n";
				std::copy( fs::directory_iterator( p ), fs::directory_iterator( ), std::ostream_iterator<fs::directory_entry>( ss, "\r\n" ) );
			} else {
				ss << p << " exists, but is neither a regular file nor a directory\n";
			}
		} else {
			ss << p << " does not exist\n";
		}
	}

	catch( const fs::filesystem_error& ex ) {
		ss << "ERROR: " << ex.what( ) << '\n';
	}

	return ss.str( );
}
Beispiel #3
0
				base::OptionalError read_file( boost::string_ref path, base::data_t & buffer, bool append_buffer ) {
					std::ifstream in_file( path.to_string( ), std::ifstream::ate | std::ifstream::binary );
					if( !in_file ) {
						auto result = base::create_optional_error( "Could not open file" );
						result->add( "where", "read_file#open" );
						return result;
					}

					auto fsize = in_file.tellg( );
					if( fsize < 0 ) {
						auto result = base::create_optional_error( "Error reading file length" );
						result->add( "where", "read_file#tellg" );
						return result;
					}
					if( !in_file.seekg( 0 ) ) {
						auto result = base::create_optional_error( "Error reseting file position to beginning" );
						result->add( "where", "read_file#seekg" );
						return result;
					}

					size_t first_pos = append_buffer ? buffer.size( ) : 0;
					buffer.resize( first_pos + static_cast<size_t>(fsize) );

					if( !in_file.read( buffer.data( ) + first_pos, fsize ) ) {
						auto result = base::create_optional_error( "Error reading file" );
						result->add( "where", "read_file#read" );
						return result;
					}
					return base::create_optional_error( );
				}
Beispiel #4
0
void
history::add (const boost::string_ref& text)
{
	this->lines[this->realpos] = text.to_string();
	this->realpos++;
	if (this->realpos == HISTORY_SIZE)
		this->realpos = 0;
	this->pos = this->realpos;
}
Beispiel #5
0
std::string CExpressionConverter::ToSuffixNotation(boost::string_ref expression)
{
	auto str = expression.to_string();
	str.erase(remove_if(str.begin(), str.end(), isspace), str.end());
	m_expression = str;
	string output;
	HandlePlusAndMinus(output);
	if (!output.empty())
	{
		output.erase(--output.end());
	}
	return output;
}
Beispiel #6
0
				base::OptionalError write_file( boost::string_ref path, base::data_t const & buffer, FileWriteMode mode, size_t bytes_to_write ) {
					// TODO: Write to a temp file first and then move
					if( 0 == bytes_to_write || bytes_to_write > buffer.size( ) ) {
						bytes_to_write = buffer.size( );	// TODO: verify this is what we want.  Covers errors but that may be OK
					}
					std::ofstream out_file;
					switch( mode ) {
					case FileWriteMode::AppendOrCreate:
						out_file.open( path.to_string( ), std::ostream::binary | std::ostream::out | std::ostream::app );
						break;
					case FileWriteMode::MustBeNew: {
						if( std::ifstream( path.to_string( ) ) ) {
							// File exists.  Error
							auto error = base::create_optional_error( "Attempt to open an existing file when MustBeNew requested" );
							error->add( "where", "write_file" );
							return error;
						}
						out_file.open( path.to_string( ), std::ostream::binary | std::ostream::out );
						break;
					}
					case FileWriteMode::OverwriteOrCreate:
						out_file.open( path.to_string( ), std::ostream::binary | std::ostream::out | std::ostream::trunc );
						break;
					default:
						throw std::runtime_error( "Unknown FileWriteMode specified" );
					}
					if( !out_file ) {
						auto error = base::create_optional_error( "Could not open file for writing" );
						error->add( "where", "write_file#open" );
						return error;
					}
					if( !out_file.write( buffer.data( ), static_cast<std::streamoff>(bytes_to_write) ) ) {
						auto error = base::create_optional_error( "Error writing data to file" );
						error->add( "where", "write_file#write" );
						return error;
					}
					return base::create_optional_error( );
				}
Beispiel #7
0
	AttributeSelector::AttributeSelector(boost::string_ref key) :
		m_operator(SelectorOperator::Exists),
		m_attributeNameString(key.to_string()),
		m_attributeNameRef(m_attributeNameString)
	{
		if (m_attributeNameRef.size() == 0)
		{
			throw std::runtime_error(u8"In AttributeSelector::AttributeSelector(SelectorOperator, boost::string_ref, const bool) - Supplied attribute identifier has zero length.");
		}

		#ifndef NDEBUG
			#ifdef GQ_VERBOSE_DEBUG_NFO
				std::cout << "Built Exists AttributeSelector with key " << m_attributeNameRef << std::endl;
			#endif
		#endif

		// Add attribute key as a match trait for EXISTS, specifying any ("*") as the value. 
		AddMatchTrait(m_attributeNameRef, SpecialTraits::GetAnyValue());		
	}
Beispiel #8
0
/* Handle message tags.
 *
 * See http://ircv3.atheme.org/specification/message-tags-3.2 
 */
static void
handle_message_tags (const server &serv, const boost::string_ref & tags_str,
							message_tags_data &tags_data)
{
	/* FIXME We might want to avoid the allocation overhead here since 
	 * this might be called for every message from the server.
	 */
	std::istringstream inbuf{ tags_str.to_string() };
	for (std::string tag; std::getline(inbuf, tag, ';');)
	{
		auto value_loc = tag.find_first_of('=');
		if (value_loc == std::string::npos)
			continue;

		auto key = tag.substr(0, value_loc - 1);
		auto value = tag.substr(value_loc + 1);

		if (serv.have_server_time && key == "time")
			handle_message_tag_time (value, tags_data);
	}
}
Beispiel #9
0
		/**
		 * コンストラクタ
		 * @param name ファイル名
		 * @param msg  エラーメッセージ
		 */
		explicit FileException(boost::string_ref name, boost::string_ref msg)
			: Exception(name.to_string() + '\n' + msg.to_string(), nullptr) {}
Beispiel #10
0
				std::streampos file_size( boost::string_ref path ) {
					std::ifstream in_file( path.to_string( ), std::ifstream::ate | std::ifstream::binary );
					return file_size( in_file );
				}
Beispiel #11
0
/* irc_inline() - 1 single line received from serv */
void
server::p_inline (const boost::string_ref& text)
{
	session *sess;
	char *type;
	char *word[PDIWORDS+1];
	char *word_eol[PDIWORDS+1];
	message_tags_data tags_data = message_tags_data();

	std::string pdibuf(text.size(), '\0');

	sess = this->front_session;

	/* Python relies on this */
	word[PDIWORDS] = NULL;
	word_eol[PDIWORDS] = NULL;
	std::string buf;
	if (text.starts_with('@'))
	{
		auto sep = text.find_first_of(' ');
		if (sep == boost::string_ref::npos)
			return;
		
		/* skip the '@' */
		auto tags = text.substr(1, sep - 1);
		
		buf = text.substr(sep + 1).to_string();

		handle_message_tags(*this, tags, tags_data);
	}
	else
	{
		buf = text.to_string();
	}

	url_check_line(buf.data(), buf.size());

	/* split line into words and words_to_end_of_line */
	process_data_init (&pdibuf[0], &buf[0], word, word_eol, false, false);

	if (buf[0] == ':')
	{
		/* find a context for this message */
		if (this->is_channel_name (word[3]))
		{
			auto tmp = find_channel (word[3]);
			if (tmp)
				sess = &(*tmp);
		}

		/* for server messages, the 2nd word is the "message type" */
		type = word[2];

		word[0] = type;
		word_eol[1] = &buf[0];	/* keep the ":" for plugins */

		if (plugin_emit_server(sess, type, word, word_eol,
			tags_data.timestamp))
		{
			return;
		}

		word[1]++;
		word_eol[1] = &buf[1];	/* but not for HexChat internally */

	} else
	{
		word[0] = type = word[1];

		if (plugin_emit_server(sess, type, word, word_eol,
			tags_data.timestamp))
		{
			return;
		}
	}

	if (buf[0] != ':')
	{
		process_named_servermsg (sess, &buf[0], word[0], word_eol, &tags_data);
		return;
	}

	/* see if the second word is a numeric */
	std::locale locale;
	if (std::isdigit (word[2][0], locale))
	{
		char* t = word_eol[4];
		if (*t == ':')
			t++;

		process_numeric (sess, atoi (word[2]), word, word_eol, t, &tags_data);
	} else
	{
		process_named_msg (sess, type, word, word_eol, &tags_data);
	}
}
Beispiel #12
0
std::string CHttpUrl::ParseDocument(boost::string_ref & url)
{
	return url.to_string();
}