Skip to content
This repository has been archived by the owner on Nov 10, 2020. It is now read-only.

Commit

Permalink
Added some StringOutputStreamMethods
Browse files Browse the repository at this point in the history
Change-Id: I2eab31bcb972e78a85917dd697e6f4aa302a1d04
  • Loading branch information
Sven von Beuningen authored and Gerrit Code Review committed Jul 29, 2013
1 parent a9d85eb commit 012ddab
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 28 deletions.
127 changes: 102 additions & 25 deletions modules/capu/include/capu/util/StringOutputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,66 @@ namespace capu
class StringOutputStream: public IOutputStream
{
public:

enum FloatingPointType
{
NORMAL,
FIXED
};

StringOutputStream();

/**
* @name StringOuputStream implementation
* @see IOutputStream
* @{
*/
virtual IOutputStream& operator<<(const float_t value);
virtual IOutputStream& operator<<(const int32_t value);
virtual IOutputStream& operator<<(const uint32_t value);
virtual IOutputStream& operator<<(const int64_t value);
virtual IOutputStream& operator<<(const uint64_t value);
virtual IOutputStream& operator<<(const String& value);
virtual IOutputStream& operator<<(const bool_t value);
virtual IOutputStream& operator<<(const char_t* value);
virtual IOutputStream& operator<<(const uint16_t value);
virtual IOutputStream& operator<<(const Guid& value);
virtual IOutputStream& write(const void* data, const uint32_t size);
virtual StringOutputStream& operator<<(const float_t value);
virtual StringOutputStream& operator<<(const int32_t value);
virtual StringOutputStream& operator<<(const uint32_t value);
virtual StringOutputStream& operator<<(const int64_t value);
virtual StringOutputStream& operator<<(const uint64_t value);
virtual StringOutputStream& operator<<(const String& value);
virtual StringOutputStream& operator<<(const bool_t value);
virtual StringOutputStream& operator<<(const char_t* value);
virtual StringOutputStream& operator<<(const uint16_t value);
virtual StringOutputStream& operator<<(const Guid& value);

virtual StringOutputStream& write(const void* data, const uint32_t size);

virtual status_t flush();
/**
* @}
*/

/**
* This method accepts pointer to method which itself accepts StringOuputStream
* The method is used for endl and fixed
* @param pointer to method
* @return stream
*/
StringOutputStream& operator<< (StringOutputStream& (*pf)(StringOutputStream&));


/**
* Returns a const char_t pointer to the content of the stream
* @return the const char_t pointer to the content of the stream
*/
const char_t* c_str() const;
const char_t* c_str();

/**
* Returns the current length of the stream without terminating 0
* @return length of the stream
*/
uint32_t length() const;

/**
* Clears the stream
*/
void clear();

void setFloatingPointType(FloatingPointType type);

protected:
private:

Expand All @@ -79,6 +104,8 @@ namespace capu
*/
uint32_t mSize;

FloatingPointType mFloatingPointType;

/**
* Resizes the internal buffer to minSize
* @param minSize to resize the buffer
Expand All @@ -91,12 +118,14 @@ namespace capu
* @param size to request on the stream
*/
void requestSize(const uint32_t size);

};

inline
const char_t*
StringOutputStream::c_str() const
StringOutputStream::c_str()
{
flush();
return mBuffer.getRawData();
}

Expand All @@ -108,16 +137,25 @@ namespace capu
}

inline
IOutputStream&
StringOutputStream&
StringOutputStream::operator<<(const float_t value)
{
char_t buffer[16];
StringUtils::Sprintf(buffer, 16, "%f", value);
switch(mFloatingPointType)
{
case NORMAL:
StringUtils::Sprintf(buffer, 16, "%f", value);
break;
case FIXED:
StringUtils::Sprintf(buffer, 16, "%.4f", value);
break;
}

return operator<<(buffer);
}

inline
IOutputStream&
StringOutputStream&
StringOutputStream::operator<<(const int32_t value)
{
char_t buffer[11];
Expand All @@ -126,7 +164,7 @@ namespace capu
}

inline
IOutputStream&
StringOutputStream&
StringOutputStream::operator<<(const uint32_t value)
{
char_t buffer[11];
Expand All @@ -135,7 +173,7 @@ namespace capu
}

inline
IOutputStream&
StringOutputStream&
StringOutputStream::operator<<(const int64_t value)
{
char_t buffer[21];
Expand All @@ -144,7 +182,7 @@ namespace capu
}

inline
IOutputStream&
StringOutputStream&
StringOutputStream::operator<<(const uint64_t value)
{
char_t buffer[21];
Expand All @@ -153,14 +191,14 @@ namespace capu
}

inline
IOutputStream&
StringOutputStream&
StringOutputStream::operator<<(const String& value)
{
return write(value.c_str(), static_cast<uint32_t>(value.getLength()));
}

inline
IOutputStream&
StringOutputStream&
StringOutputStream::operator<<(const bool_t value)
{
char_t buffer[2];
Expand All @@ -169,15 +207,15 @@ namespace capu
}

inline
IOutputStream&
StringOutputStream&
StringOutputStream::operator<<(const char_t* value)
{
const uint_t length = ConstString(value).length();
return write(value, static_cast<uint32_t>(length));
}

inline
IOutputStream&
StringOutputStream&
StringOutputStream::operator<<(const uint16_t value)
{
char_t buffer[6];
Expand All @@ -187,14 +225,14 @@ namespace capu
}

inline
IOutputStream&
StringOutputStream&
StringOutputStream::operator<<(const Guid& value)
{
return operator<<(value.toString());
}

inline
IOutputStream&
StringOutputStream&
StringOutputStream::write(const void* data, const uint32_t size)
{
requestSize(size + 1); // Terminating 0
Expand All @@ -203,13 +241,52 @@ namespace capu
return *this;
}

inline
void
StringOutputStream::clear()
{
mSize = 0;
mBuffer[0] = '\0';
}

inline
status_t
StringOutputStream::flush()
{
mBuffer[mSize] = '\0';
return CAPU_OK;
}

inline
void
StringOutputStream::setFloatingPointType(FloatingPointType type)
{
mFloatingPointType = type;
}

inline
StringOutputStream& StringOutputStream::operator<< (StringOutputStream& (*pf)(StringOutputStream&))
{
return pf(*this);
}

inline
StringOutputStream&
endl(StringOutputStream& outputStream)
{
const char_t c = '\n';
outputStream.write(&c, sizeof(char_t));
outputStream.flush();
return outputStream;
}

inline
StringOutputStream&
fixed(StringOutputStream& outputStream)
{
outputStream.setFloatingPointType(StringOutputStream::FIXED);
return outputStream;
}
}


Expand Down
5 changes: 4 additions & 1 deletion modules/capu/src/util/StringOutputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@

namespace capu
{



StringOutputStream::StringOutputStream()
: mBuffer(16)
, mSize(0)
, mFloatingPointType(NORMAL)
{

}
Expand All @@ -49,5 +53,4 @@ namespace capu
resize(mSize + size);
}
}

}
5 changes: 3 additions & 2 deletions modules/capu/test/os/EnvironmentVariablesTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ TEST(EnvironmentVariables, getTest)
capu::EnvironmentVariables myEnv;
capu::String tmp;
capu::HashTable<capu::String, capu::String>::Iterator it = myEnv.getAll().begin();
while (it != myEnv.getAll().end())
const capu::HashTable<capu::String, capu::String>::Iterator end = myEnv.getAll().end();

for(; it != end; ++it)
{
EXPECT_TRUE(myEnv.get(it->key, tmp));
EXPECT_EQ(tmp, it->value);
it++;
}
}
43 changes: 43 additions & 0 deletions modules/capu/test/util/StringOutputStreamTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,49 @@ namespace capu
EXPECT_EQ(36U, outputStream.length());
}


TEST_F(StringOutputStreamTest, WriteEndl)
{
outputStream << "test" << endl;
outputStream.flush();
EXPECT_STREQ("test\n", outputStream.c_str());
EXPECT_EQ(5u, outputStream.length());
}

TEST_F(StringOutputStreamTest, WriteFixed)
{
outputStream << 47.11f;
outputStream.flush();
EXPECT_STREQ("47.110001", outputStream.c_str());
EXPECT_EQ(9u, outputStream.length());
outputStream.clear();
outputStream << fixed << 47.11f;
outputStream.flush();
EXPECT_STREQ("47.1100", outputStream.c_str());
EXPECT_EQ(7u, outputStream.length());
}

TEST_F(StringOutputStreamTest, Clear)
{
outputStream << "Some data";
outputStream.clear();
EXPECT_STREQ("", outputStream.c_str());
EXPECT_EQ(0u, outputStream.length());

outputStream << "Some data";
outputStream.flush();
outputStream.clear();
EXPECT_STREQ("", outputStream.c_str());
EXPECT_EQ(0u, outputStream.length());
}

TEST_F(StringOutputStreamTest, AutoFlush)
{
outputStream << "Some data";
EXPECT_STREQ("Some data", outputStream.c_str());
EXPECT_EQ(9u, outputStream.length());
}

TEST_F(StringOutputStreamTest, WriteData)
{
outputStream.write("Some data to write", 18);
Expand Down

0 comments on commit 012ddab

Please sign in to comment.