示例#1
0
void SipperMediaPortable::trim(std::string &s, const std::string &t)
{ 
   trim_right(s, t);
   trim_left(s, t); 
}  // end of trim
示例#2
0
#include <stdio.h>
#include <string.h>
#include "trim.h"
#include "describe/describe.h"

describe("trim_right", {
  it("should handle empty strings", {
    char str[] = "";
    assert_str_equal("", trim_right(str));
  });

  it("should not remove leading whitespace", {
    char str[] = "\t\n hello";
    assert_str_equal("\t\n hello", trim_right(str));
    assert_str_equal("\t\n hello", str);
  });

  it("should remove trailing whitespace", {
    char str[] = "hello \t\n";
    assert_str_equal("hello", trim_right(str));
    assert_str_equal("hello", str);
  });
});
示例#3
0
TalkActionResult_t TalkActions::onPlayerSpeak(Player* player, SpeakClasses type, const std::string& words)
{
	if (type != SPEAK_SAY)
	{
		return TALKACTION_CONTINUE;
	}

	std::string str_words_quote;
	std::string str_param_quote;
	std::string str_words_first_word;
	std::string str_param_first_word;
	// With quotation filtering
	size_t loc = words.find('"', 0);

	if (loc != std::string::npos && loc >= 0)
	{
		str_words_quote = std::string(words, 0, loc);
		str_param_quote = std::string(words, (loc + 1), words.size() - loc - 1);
	}
	else
	{
		str_words_quote = words;
		str_param_quote = std::string("");
	}

	trim_left(str_words_quote, " ");
	trim_right(str_param_quote, " ");
	// With whitespace filtering
	loc = words.find(' ', 0);

	if (loc != std::string::npos && loc >= 0)
	{
		str_words_first_word = std::string(words, 0, loc);
		str_param_first_word = std::string(words, (loc + 1), words.size() - loc - 1);
	}
	else
	{
		str_words_first_word = words;
		str_param_first_word = std::string("");
	}

	TalkActionList::iterator it;

	for (it = wordsMap.begin(); it != wordsMap.end(); ++it)
	{
		std::string cmdstring;
		std::string paramstring;

		if (it->second->getFilterType() == TALKACTION_MATCH_QUOTATION)
		{
			cmdstring = str_words_quote;
			paramstring = str_param_quote;
		}
		else if (it->second->getFilterType() == TALKACTION_MATCH_FIRST_WORD)
		{
			cmdstring = str_words_first_word;
			paramstring = str_param_first_word;
		}
		else
		{
			continue;
		}

		if (cmdstring == it->first || (!it->second->isCaseSensitive() && boost::algorithm::iequals(it->first, cmdstring)))
		{
			bool ret = true;

			if (player->getAccessLevel() < it->second->getAccessLevel())
			{
				if (player->getAccessLevel() > 0)
				{
					player->sendTextMessage(MSG_STATUS_SMALL, "You can not execute this command.");
					ret = false;
				}
			}
			else
			{
				TalkAction* talkAction = it->second;

				if (talkAction->isScripted())
				{
					ret = talkAction->executeSay(player, cmdstring, paramstring);
				}
				else
				{
					TalkActionFunction* func = talkAction->getFunction();

					if (func)
					{
						func(player, cmdstring, paramstring);
						ret = false;
					}
				}
			}

			if (ret)
			{
				return TALKACTION_CONTINUE;
			}
			else
			{
				return TALKACTION_BREAK;
			}
		}
	}

	return TALKACTION_CONTINUE;
}
示例#4
0
char *trim(char *str) {
    trim_right(str);
    trim_left(str);
    return str;
}
示例#5
0
文件: KeyFile.cpp 项目: qd-cae/qd
/** Parse a keyfile
 *
 * @param _load_mesh : whether the mesh shall loaded
 * @return success : whether loading the data was successful
 *
 * The parameter can be used to prevent the loading of the mesh,
 * even though we use parse_mesh. We need this for includes.
 */
bool
KeyFile::load(bool _load_mesh)
{

  // read file
  auto my_filepath = resolve_include_filepath(get_filepath());
  std::vector<char> char_buffer = read_binary_file(my_filepath);
  has_linebreak_at_eof = char_buffer.back() == '\n';

#ifdef QD_DEBUG
  std::cout << "done." << std::endl;
#endif

  // init parallel worker if master file
  // if (parent_kf == this)
  //   _wq.init_workers(1);

  // convert buffer into blocks
  size_t iLine = 0;
  std::string last_keyword;
  std::vector<std::string> line_buffer;
  std::vector<std::string> line_buffer_tmp;
  bool found_pgp_section = false;

  std::string line;
  auto string_buffer = std::string(char_buffer.begin(), char_buffer.end());
  std::stringstream st(string_buffer);
  // for (; std::getline(st, line); ++iLine) {
  for (; std::getline(st, line); ++iLine) {

    if (line.find("-----BEGIN PGP") != std::string::npos) {
      found_pgp_section = true;
#ifdef QD_DEBUG
      std::cout << "Found PGP Section\n";
#endif
    }

    // remove windows file ending ... I hate it ...
    if (line.size() != 0 && line.back() == '\r')
      line.pop_back();

    // new keyword
    if (line[0] == '*' || found_pgp_section) {

      if (!line_buffer.empty() && !last_keyword.empty()) {

        // transfer possible header for following keyword (see function)
        transfer_comment_header(line_buffer, line_buffer_tmp);

        // get type
        auto kw_type = Keyword::determine_keyword_type(last_keyword);

#ifdef QD_DEBUG
        std::cout << last_keyword << " -> ";
        switch (kw_type) {
          case (Keyword::KeywordType::NODE):
            std::cout << "NODE\n";
            break;
          case (Keyword::KeywordType::ELEMENT):
            std::cout << "ELEMENT\n";
            break;
          case (Keyword::KeywordType::PART):
            std::cout << "PART\n";
            break;
          case (Keyword::KeywordType::GENERIC):
            std::cout << "GENERIC\n";
            break;
          case (Keyword::KeywordType::INCLUDE):
            std::cout << "INCLUDE\n";
            break;
          case (Keyword::KeywordType::INCLUDE_PATH):
            std::cout << "INCLUDE_PATH\n";
            break;
        }
#endif

        auto kw = create_keyword(line_buffer,
                                 kw_type,
                                 iLine - line_buffer.size() -
                                   line_buffer_tmp.size() + 1);
        if (kw)
          keywords[kw->get_keyword_name()].push_back(kw);

        // transfer cropped data
        line_buffer = line_buffer_tmp;
      }

      // we always trim keywords
      trim_right(line);
      last_keyword = line;

    } // IF:line[0] == '*'

    // Encrypted Sections
    //
    // Extracts encrypted section here and places it in a line in the
    // line buffer. An encrypted section is treated like a keyword.
    if (found_pgp_section) {
      found_pgp_section = false;

      // get stream position
      const auto stream_position = st.tellg();

      const auto end_position =
        string_buffer.find("-----END PGP", stream_position);

      if (end_position == std::string::npos)
        throw(
          std::runtime_error("Could not find \"-----END PGP MESSAGE-----\" for "
                             "corresponding \"-----BEGIN PGP MESSAGE-----\" "));

      // set stream position behind encrypted section
      st.seekg(end_position);

      // extract encrypted stuff
      line += '\n';
      line += std::string(char_buffer.begin() + stream_position,
                          char_buffer.begin() + end_position);

      // print_string_as_hex(line);

      if (line.back() == '\n')
        line.pop_back();
      if (line.back() == '\r')
        line.pop_back();
    }

    // we stupidly add every line to the buffer
    line_buffer.push_back(line);

  } // for:line

  // allocate last block
  if (!line_buffer.empty() && !last_keyword.empty()) {

    auto kw = create_keyword(line_buffer,
                             Keyword::determine_keyword_type(last_keyword),
                             iLine - line_buffer.size() + 1);
    if (kw)
      keywords[kw->get_keyword_name()].push_back(kw);
  }

  // only load files above *END!
  const auto end_kw_position = get_end_keyword_position();

  // includes
  if (load_includes) {

    // update include dirs
    get_include_dirs(true);

    // do the thing
    for (auto& include_kw : include_keywords) {

      if (include_kw->get_position() < end_kw_position)
        // Note: prevent loading the mesh here
        include_kw->load(false);
    }
  }

  // wait for threads to finish preloading
  // _wq.wait_for_completion();

  // Wait for completion
  // while (work_queue.size() != 0) {
  //   work_queue.front().wait();
  //   work_queue.pop();
  // }

  // load mesh if requested
  if (parse_mesh && _load_mesh) {

    // load nodes
    load_nodes();

    // load parts
    load_parts();

    // load elements
    load_elements();
  }

  return true;
}
示例#6
0
 self_type&      trim_right() {
     trim_right( self_type() ) ;
 }
Container trim(const T& x, const Container& xs)
{
    return trim_right(x, trim_left(x, xs));
}
示例#8
0
文件: http-parser.c 项目: azalpy/sdk
// H4.2 Message Headers
// message-header = field-name ":" [ field-value ]
// field-name = token
// field-value = *( field-content | LWS )
// field-content = <the OCTETs making up the field-value
//					and consisting of either *TEXT or combinations
//					of token, separators, and quoted-string>
static int http_parse_header_line(struct http_context *ctx)
{
	enum { 
		SM_HEADER_START = SM_HEADER, 
		SM_HEADER_NAME,
		SM_HEADER_NAME_SP,
		SM_HEADER_SEPARATOR,
		SM_HEADER_VALUE
	};

	int r;
	for(; ctx->offset < ctx->raw_size; ctx->offset++)
	{
		switch(ctx->stateM)
		{
		case SM_HEADER_START:
			switch(ctx->raw[ctx->offset])
			{
			case '\r':
				assert(0 == ctx->header.npos);
				assert(0 == ctx->header.vpos);
				if(ctx->offset + 2 > ctx->raw_size)
					return 0; // wait more date

				++ctx->offset;
				assert('\n' == ctx->raw[ctx->offset]);

			case '\n':
				assert(0 == ctx->header.npos);
				assert(0 == ctx->header.vpos);
				++ctx->offset;
				ctx->stateM = SM_BODY;
				return 0;

			case ' ':
			case '\t':
				assert(0); // multi-line header ?
				break;

			default:
				assert(0 == ctx->header.npos);
				assert(0 == ctx->header.nlen);
				ctx->header.npos = ctx->offset;
				ctx->stateM = SM_HEADER_NAME;
			}
			break;

		case SM_HEADER_NAME:
			switch(ctx->raw[ctx->offset])
			{
			case '\r':
			case '\n':
				assert(0);
				return -1; // invalid

			case ' ':
				ctx->header.nlen = ctx->offset - ctx->header.npos;
				assert(ctx->header.nlen > 0 && is_valid_token(ctx->raw+ctx->header.npos, ctx->header.nlen));
				ctx->stateM = SM_HEADER_NAME_SP;
				break;

			case ':':
				ctx->header.nlen = ctx->offset - ctx->header.npos;
				assert(ctx->header.nlen > 0 && is_valid_token(ctx->raw+ctx->header.npos, ctx->header.nlen));
				ctx->stateM = SM_HEADER_SEPARATOR;
				break;
			}
			break;

		case SM_HEADER_NAME_SP:
			switch(ctx->raw[ctx->offset])
			{
			case ' ':
				break; // skip SP

			case ':':
				ctx->stateM = SM_HEADER_SEPARATOR;
				break;

			default:
				assert(0);
				return -1;
			}
			break;

		case SM_HEADER_SEPARATOR:
			switch(ctx->raw[ctx->offset])
			{
			case '\r':
				// empty value
				// e.g. x-wap-profile: \r\nx-forwarded-for: 10.25.110.244, 115.168.35.85\r\n
				++ctx->offset;
				assert('\n' == ctx->raw[ctx->offset]);

			case '\n':
				ctx->header.vpos = ctx->offset;
				ctx->header.vlen = 0;
				r = http_header_add(ctx, &ctx->header);
				if(0 != r)
					return r;
				memset(&ctx->header, 0, sizeof(struct http_header)); // reuse header
				ctx->stateM = SM_HEADER;
				break;

			case ' ':
				break; // skip SP

			default:
				ctx->stateM = SM_HEADER_VALUE;
				ctx->header.vpos = ctx->offset;
				break;
			}
			break;

		case SM_HEADER_VALUE:
			switch(ctx->raw[ctx->offset])
			{
			case '\n':
				assert('\r' == ctx->raw[ctx->offset-1]);
				ctx->header.vlen = ctx->offset - 1 - ctx->header.vpos;
				trim_right(ctx->raw, &ctx->header.vpos, &ctx->header.vlen);
				ctx->stateM = SM_HEADER;

				// add new header
				r = http_header_add(ctx, &ctx->header);
				if(0 != r)
					return r;
				memset(&ctx->header, 0, sizeof(struct http_header)); // reuse header
				break;

			default:
				break;
			}
			break;

		default:
			assert(0);
			return -1;
		}
	}

	return 0;
}
示例#9
0
文件: http-parser.c 项目: azalpy/sdk
// H6.1 Status-Line
// Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
static int http_parse_status_line(struct http_context *ctx)
{
	int i;
	enum { 
		SM_STATUS_VERSION = SM_FIRSTLINE, 
		SM_STATUS_CODE, 
		SM_STATUS_CODE_SP, 
		SM_STATUS_REASON 
	};

	for(; ctx->offset < ctx->raw_size; ctx->offset++)
	{
		switch(ctx->stateM)
		{
		case SM_STATUS_VERSION:
			assert('\r' != ctx->raw[ctx->offset]);
			assert('\n' != ctx->raw[ctx->offset]);
			if(ctx->offset + 8 > ctx->raw_size)
				return 0; // wait for more data

			assert(0 == ctx->offset);
			if(2 != sscanf(ctx->raw+ctx->offset, "HTTP/%1d.%1d",&ctx->vermajor, &ctx->verminor))
				return -1;

			assert(1 == ctx->vermajor);
			assert(1 == ctx->verminor || 0 == ctx->verminor);
			ctx->offset += 7; // skip
			ctx->stateM = SM_STATUS_CODE;
			break;

		case SM_STATUS_CODE:
			assert('\r' != ctx->raw[ctx->offset]);
			assert('\n' != ctx->raw[ctx->offset]);
			if(' ' == ctx->raw[ctx->offset])
				break; // skip SP

			if('0' > ctx->raw[ctx->offset] || ctx->raw[ctx->offset] > '9')
				return -1; // invalid

			if(ctx->offset + 3 > ctx->raw_size)
				return 0; // wait for more data

			assert(0 == ctx->reply.code);
			for(i = 0; i < 3; i++)
				ctx->reply.code = ctx->reply.code * 10 + (ctx->raw[ctx->offset+i] - '0');

			ctx->offset += 2; // skip
			ctx->stateM = SM_STATUS_CODE_SP;
			break;

		case SM_STATUS_CODE_SP:
			assert('\r' != ctx->raw[ctx->offset]);
			assert('\n' != ctx->raw[ctx->offset]);
			if(ISSPACE(ctx->raw[ctx->offset]))
				break; // skip SP

			assert(0 == ctx->reply.reason_pos);
			assert(0 == ctx->reply.reason_len);
			ctx->reply.reason_pos = ctx->offset;
			ctx->stateM = SM_STATUS_REASON;
			break;

		case SM_STATUS_REASON:
			switch(ctx->raw[ctx->offset])
			{
			//case '\r':
			//	break;
			case '\n':
				assert('\r' == ctx->raw[ctx->offset-1]);
				ctx->reply.reason_len = ctx->offset - 1 - ctx->reply.reason_pos;
				trim_right(ctx->raw, &ctx->reply.reason_pos, &ctx->reply.reason_len);
				ctx->raw[ctx->reply.reason_pos + ctx->reply.reason_len] = '\0';
				ctx->stateM = SM_HEADER;
				++ctx->offset; // skip \n
				return 0;

			default:
				break;
			}
			break;

		default:
			assert(0);
			return -1;
		}
	}

	return 0;
}