Esempio n. 1
0
void IO::TrimRight(std::basic_string<charType> & str, const char* chars2remove)
{
	if (!str.empty()) {  	//trim the characters in chars2remove from the right
		std::string::size_type pos = 0;
		if (chars2remove != NULL) {
			pos = str.find_last_not_of(chars2remove);

			if (pos != std::string::npos)
				str.erase(pos+1);
			else
				str.erase( str.begin() , str.end() ); // make empty
		}
		else {       		//trim space
			pos = std::string::npos;
			for (int i = str.size()-1; i >= 0; --i) {
				if (!isspace(str[i])) {
					pos = i;
					break;
				}
			}
			if (pos != std::string::npos) {
				if (pos+1 != str.size())
					str.resize(pos+1);
			}
			else {
				str.clear();
			}
		}
	}
}
Esempio n. 2
0
int UsbIo::read( std::basic_string<unsigned char> & from ,int size )
{
    if ( !isOpen() )
    {
        if ( !open() )
            return -1;
    }
    from.resize( size );
    /*
    int res = libusb_control_transfer(
                  pd->handle,
                  CONTROL_REQUEST_TYPE_IN,
                  HID_GET_REPORT,
                  0, 0,
                  const_cast<unsigned char *>( from.data() ), from.size(), pd->timeout );
    */

    int res =  usb_control_msg( pd->handle, CONTROL_REQUEST_TYPE_IN, 
                                HID_GET_REPORT,
                                0, 
                                0, 
                                (char *)( from.data() ), 
                                from.size(),
                                pd->timeout );



    if ( res < 0 )
    {
        close();
        return res;
    }
    return res;
}
Esempio n. 3
0
StorageSize read(std::istream& stream, std::basic_string<T>& value)
{
    StorageSize size = read<StorageSize>(stream);
    value.resize(size);
    stream.read(&value[0], size * sizeof(T));
    return size;
}
Esempio n. 4
0
//! Serialization for basic_string types, if binary data is supported
template<class Archive, class CharT, class Traits, class Alloc> inline
typename std::enable_if<traits::is_input_serializable<BinaryData<CharT>, Archive>(), void>::type
load(Archive & ar, std::basic_string<CharT, Traits, Alloc> & str)
{
    size_t size;
    ar( make_size_tag( size ) );
    str.resize(size);
    ar( binary_data( &(*str.begin()), size * sizeof(CharT) ) );
}
Esempio n. 5
0
 //! Serialization for basic_string types, if binary data is supported
 template<class Archive, class CharT, class Traits, class Alloc> inline
 typename std::enable_if<traits::is_input_serializable<BinaryData<CharT>, Archive>::value, void>::type
 CEREAL_LOAD_FUNCTION_NAME(Archive & ar, std::basic_string<CharT, Traits, Alloc> & str)
 {
   size_type size;
   ar( make_size_tag( size ) );
   str.resize(static_cast<std::size_t>(size));
   ar( binary_data( const_cast<CharT *>( str.data() ), static_cast<std::size_t>(size) * sizeof(CharT) ) );
 }
Esempio n. 6
0
 void load(std::basic_string<CharType> & s)
 {
     unsigned int l;
     load(l);
     s.resize(l);
     // note breaking a rule here - could be a problem on some platform
     if (l)
       load_impl(const_cast<CharType *>(s.data()),
                 get_mpi_datatype(CharType()),l);
 }
void StringTrimRightT(std::basic_string<CharType> &output)
{
	size_t length = output.length();
	const CharType *src = output.data();

	for (; length > 0; length--)
		if (NOT_SPACE(src[length-1]))
			break;

	output.resize(length);
}
 void load(std::basic_string<CharType> & s)
 {
   unsigned int l;
   load(l);
   // borland de-allocator fixup
   #if BOOST_WORKAROUND(_RWSTD_VER, BOOST_TESTED_AT(20101))
   if(NULL != s.data())
   #endif
   s.resize(l);
   // note breaking a rule here - could be a problem on some platform
   load_impl(const_cast<char *>(s.data()),l);
 }
Esempio n. 9
0
    void serialize(input_archive & ar, std::basic_string<Char, CharTraits,
        Allocator> & s, unsigned)
    {
        std::uint64_t size = 0;
        ar >> size; //-V128

        s.clear();
        if (s.size() < size)
            s.resize(size);

        load_binary(ar, &s[0], size * sizeof(Char));
    }
Esempio n. 10
0
    void serialize(input_archive & ar, std::basic_string<Char, CharTraits,
        Allocator> & s, unsigned)
    {
        typedef std::basic_string<Char, CharTraits, Allocator> string_type;
        typedef typename string_type::size_type size_type;
        size_type size = 0;
        ar >> size; //-V128

        s.clear();
        s.resize(size);

        load_binary(ar, &s[0], size * sizeof(Char));
    }
Esempio n. 11
0
 virtual typename base::int_type
     overflow(typename base::int_type __c = base::traits_type::eof())
     {
         if (__c != base::traits_type::eof())
         {
             int n = static_cast<int>(str_.size());
             str_.push_back(static_cast<CharT>(__c));
             str_.resize(str_.capacity());
             base::setp(const_cast<CharT*>(str_.data()),
                        const_cast<CharT*>(str_.data() + str_.size()));
             base::pbump(n+1);
         }
         return __c;
     }
Esempio n. 12
0
 virtual typename base::int_type
     overflow(typename base::int_type ch = base::traits_type::eof())
     {
         if (ch != base::traits_type::eof())
         {
             std::size_t n = str_.size();
             str_.push_back(static_cast<CharT>(ch));
             str_.resize(str_.capacity());
             base::setp(const_cast<CharT*>(str_.data()),
                        const_cast<CharT*>(str_.data() + str_.size()));
             base::pbump(static_cast<int>(n+1));
         }
         return ch;
     }
Esempio n. 13
0
    inline void serializeString(SF::Archive & ar, std::basic_string<C,T,A> & s)
    {
        if (ar.isRead())
        {
            boost::uint32_t count = 0;
            ar & count;

            SF::IStream &is = *ar.getIstream();

            s.resize(0);

            std::size_t minSerializedLength = sizeof(C);
            if (ar.verifyAgainstArchiveSize(count*minSerializedLength))
            {
                if (count > s.capacity())
                {
                    s.reserve(count);
                }
            }

            boost::uint32_t charsRemaining = count;
            const boost::uint32_t BufferSize = 512;
            C buffer[BufferSize];
            while (charsRemaining)
            {
                boost::uint32_t charsToRead = RCF_MIN(BufferSize, charsRemaining);
                boost::uint32_t bytesToRead = charsToRead*sizeof(C);

                RCF_VERIFY(
                    is.read( (char *) buffer, bytesToRead) == bytesToRead,
                    RCF::Exception(RCF::_SfError_ReadFailure()))
                    (bytesToRead)(BufferSize)(count);

                s.append(buffer, charsToRead);
                charsRemaining -= charsToRead;
            }
        }
        else if (ar.isWrite())
        {
            boost::uint32_t count = static_cast<boost::uint32_t >(s.length());
            ar & count;
            ar.getOstream()->writeRaw(
                (char *) s.c_str(),
                count*sizeof(C));
        }

    }
Esempio n. 14
0
    void IO::TrimLeft(std::basic_string<charType> & str, const char* chars2remove)
    {
        if (!str.empty())   //trim the characters in chars2remove from the left
        {
            std::string::size_type pos = 0;
            if (chars2remove != NULL)
            {
                pos = str.find_first_not_of(chars2remove);

                if (pos != std::string::npos)
                    str.erase(0,pos);
                else
                    str.erase( str.begin() , str.end() ); // make empty
            }
            else        //trim space
            {
                pos = std::string::npos;        //pos = -1
                for (size_t i = 0; i < str.size(); ++i)
                {
                    if (!isspace(str[i]))
                    {
                        pos = i;
                        break;
                    }
                }
                if (pos != std::string::npos)
                {
                    if (pos > 0)
                    {
                        size_t length = str.size() - pos;
                        for (size_t i = 0; i < length; ++i) str[i] = str[i+pos];
                        str.resize(length);
                    }
                }
                else
                {
                    str.clear();
                }
            }
        }
    }
void StringTrimT(std::basic_string<CharType> &output)
{
	if (output.empty())
		return;
	size_t bound1 = 0;
	size_t bound2 = output.length();
	const CharType *src = output.data();

	for (; bound2 > 0; bound2--)
		if (NOT_SPACE(src[bound2-1]))
			break;

	for (; bound1 < bound2; bound1++)
		if (NOT_SPACE(src[bound1]))
			break;

	if (bound1 < bound2) {
		memmove((void *)src,
				src + bound1,
				sizeof(CharType) * (bound2 - bound1));
	}

	output.resize(bound2 - bound1);
}
size_t StringReplaceAllT(const std::basic_string<CharType> &find,
						 const std::basic_string<CharType> &replace,
						 std::basic_string<CharType> &output)
{
	size_t find_length = find.size();
	size_t replace_length = replace.size();
	size_t offset = 0, endpos;
	size_t target = 0, found_pos;
	size_t replaced = 0;
	CharType *data_ptr;

	if (find.empty() || output.empty())
		return 0;

	/*
	 * to avoid extra memory reallocating,
	 * we use two passes to finish the task in the case that replace.size() is greater find.size()
	 */

	if (find_length < replace_length)
	{
		/* the first pass, count all available 'find' to be replaced  */
		for (;;)
		{
			offset = output.find(find, offset);
			if (offset == std::basic_string<CharType>::npos)
				break;
			replaced++;
			offset += find_length;
		}

		if (replaced == 0)
			return 0;

		size_t newsize = output.size() + replaced * (replace_length - find_length);

		/* we apply for more memory to hold the content to be replaced */
		endpos = newsize;
		offset = newsize - output.size();
		output.resize(newsize);
		data_ptr = &output[0];

		memmove((void*)(data_ptr + offset),
				(void*)data_ptr,
				(output.size() - offset) * sizeof(CharType));
	}
	else
	{
		endpos = output.size();
		offset = 0;
		data_ptr = const_cast<CharType *>(&output[0]);
	}

	/* the second pass,  the replacement */
	while (offset < endpos)
	{
		found_pos = output.find(find, offset);
		if (found_pos != std::basic_string<CharType>::npos)
		{
			/* move the content between two targets */
			if (target != found_pos)
				memmove((void*)(data_ptr + target),
						(void*)(data_ptr + offset),
						(found_pos - offset) * sizeof(CharType));

			target += found_pos - offset;

			/* replace */
			memcpy(data_ptr + target,
				   replace.data(),
				   replace_length * sizeof(CharType));

			target += replace_length;
			offset = find_length + found_pos;
			replaced++;
		}
		else
		{
			/* ending work  */
			if (target != offset)
				memcpy((void*)(data_ptr + target),
					   (void*)(data_ptr + offset),
					   (endpos - offset) * sizeof(CharType));
			break;
		}
	}

	if (replace_length < find_length)
		output.resize(output.size() - replaced * (find_length - replace_length));

	return replaced;
}