strbuffer: fix indentation

This commit is contained in:
Andrzej Rybczak
2012-08-28 08:17:20 +02:00
parent 6449cc5f2c
commit e61d0a4958

View File

@@ -27,241 +27,244 @@
#include <list> #include <list>
namespace NCurses namespace NCurses {
/// Buffer template class that can store text along with its
/// format attributes. The content can be easily printed to
/// window or taken as raw string at any time.
///
template <typename C> class basic_buffer
{ {
/// Buffer template class that can store text along with its /// Struct used for storing information about
/// format attributes. The content can be easily printed to /// one color/format flag along with its position
/// window or taken as raw string at any time.
/// ///
template <typename C> class basic_buffer struct FormatPos
{ {
/// Struct used for storing information about size_t Position;
/// one color/format flag along with its position short Value;
///
struct FormatPos bool operator<(const FormatPos &f)
{ {
size_t Position; return Position < f.Position;
short Value; }
bool operator<(const FormatPos &f)
{
return Position < f.Position;
}
bool operator==(const FormatPos &f)
{
return Position == f.Position && Value == f.Value;
}
};
/// Internal buffer for storing raw text bool operator==(const FormatPos &f)
/// {
std::basic_string<C> itsString; return Position == f.Position && Value == f.Value;
}
/// List used for storing formatting informations
///
std::list<FormatPos> itsFormat;
/// Pointer to temporary string
/// @see SetTemp()
///
std::basic_string<C> *itsTempString;
public:
/// Constructs an empty buffer
///
basic_buffer() : itsTempString(0) { }
/// Constructs a buffer from the existed one
/// @param b copied buffer
///
basic_buffer(const basic_buffer &b);
/// @return raw content of the buffer without formatting informations
///
const std::basic_string<C> &Str() const;
/// Searches for given string in buffer and sets format/color at the
/// beginning and end of it using val_b and val_e flags accordingly
/// @param val_b flag set at the beginning of found occurence of string
/// @param s string that function seaches for
/// @param val_e flag set at the end of found occurence of string
/// @param case_sensitive indicates whether algorithm should care about case sensitivity
/// @param for_each indicates whether function searches through whole buffer and sets
/// the format for all occurences of given string or stops after the first one
/// @return true if at least one occurence of the string was found, false otherwise
///
bool SetFormatting(short val_b, std::basic_string<C> s, short val_e,
bool case_sensitive, bool for_each = 1);
/// Searches for given string in buffer and removes given
/// format/color from the beginning and end of its occurence
/// @param val_b flag to be removed from the beginning of the string
/// @param s string that function seaches for
/// @param val_e flag to be removed from the end of the string
/// @param case_sensitive indicates whether algorithm should care about case sensitivity
/// @param for_each indicates whether function searches through whole buffer and removes
/// given format from all occurences of given string or stops after the first one
///
void RemoveFormatting(short val_b, std::basic_string<C> pattern, short val_e,
bool case_sensitive, bool for_each = 1);
/// Removes all formating applied to string in buffer.
///
void RemoveFormatting();
/// Sets the pointer to string, that will be passed in operator<<() to window
/// object instead of the internal buffer. This is useful if you took the content
/// of the buffer, modified it somehow and want to print the modified version instead
/// of the original one, but with the original formatting informations. Note that after
/// you're done with the printing etc., this pointer has to be set to null.
/// @param tmp address of the temporary string
///
void SetTemp(std::basic_string<C> *tmp);
/// Prints to window object given part of the string, loading all needed formatting info
/// and cleaning up after. The main goal of this function is to provide interface for
/// colorful scrollers.
/// @param w window object that we want to print to
/// @param start_pos reference to start position of the string. note that this variable is
/// incremented by one after each call or set to 0 if end of string is reached
/// @param width width of the string to be printed
/// @param separator additional text to be placed between the end and the beginning of
/// the string
///
void Write(Window &w, size_t &start_pos, size_t width,
const std::basic_string<C> &separator) const;
/// Clears the content of the buffer and its formatting informations
///
void Clear();
basic_buffer<C> &operator<<(int n)
{
itsString += intTo< std::basic_string<C> >::apply(n);
return *this;
}
basic_buffer<C> &operator<<(long int n)
{
itsString += longIntTo< std::basic_string<C> >::apply(n);
return *this;
}
basic_buffer<C> &operator<<(unsigned int n)
{
itsString += unsignedIntTo< std::basic_string<C> >::apply(n);
return *this;
}
basic_buffer<C> &operator<<(unsigned long int n)
{
itsString += unsignedLongIntTo< std::basic_string<C> >::apply(n);
return *this;
}
basic_buffer<C> &operator<<(char c)
{
itsString += c;
return *this;
}
basic_buffer<C> &operator<<(wchar_t wc)
{
itsString += wc;
return *this;
}
basic_buffer<C> &operator<<(const C *s)
{
itsString += s;
return *this;
}
basic_buffer<C> &operator<<(const std::basic_string<C> &s)
{
itsString += s;
return *this;
}
/// Handles colors
/// @return reference to itself
///
basic_buffer<C> &operator<<(Color color);
/// Handles format flags
/// @return reference to itself
///
basic_buffer<C> &operator<<(Format f);
/// Handles copying one buffer to another using operator<<()
/// @param buf buffer to be copied
/// @return reference to itself
///
basic_buffer<C> &operator<<(const basic_buffer<C> &buf);
/// Friend operator that handles printing
/// the content of buffer to window object
friend Window &operator<<(Window &w, const basic_buffer<C> &buf)
{
const std::basic_string<C> &s = buf.itsTempString ? *buf.itsTempString : buf.itsString;
if (buf.itsFormat.empty())
w << s;
else
{
std::basic_string<C> tmp;
auto b = buf.itsFormat.begin(), e = buf.itsFormat.end();
for (size_t i = 0; i < s.length() || b != e; ++i)
{
while (b != e && i == b->Position)
{
if (!tmp.empty())
{
w << tmp;
tmp.clear();
}
buf.LoadAttribute(w, b->Value);
b++;
}
if (i < s.length())
tmp += s[i];
}
if (!tmp.empty())
w << tmp;
}
return w;
}
private:
/// Loads an attribute to given window object
/// @param w window object we want to load attribute to
/// @param value value of attribute to be loaded
///
void LoadAttribute(Window &w, short value) const;
}; };
/// Standard buffer that uses narrow characters /// Internal buffer for storing raw text
/// ///
typedef basic_buffer<char> Buffer; std::basic_string<C> itsString;
/// Standard buffer that uses wide characters /// List used for storing formatting informations
/// ///
typedef basic_buffer<wchar_t> WBuffer; std::list<FormatPos> itsFormat;
}
/// Pointer to temporary string
/// @see SetTemp()
///
std::basic_string<C> *itsTempString;
public:
/// Constructs an empty buffer
///
basic_buffer() : itsTempString(0) { }
/// Constructs a buffer from the existed one
/// @param b copied buffer
///
basic_buffer(const basic_buffer &b);
/// @return raw content of the buffer without formatting informations
///
const std::basic_string<C> &Str() const;
/// Searches for given string in buffer and sets format/color at the
/// beginning and end of it using val_b and val_e flags accordingly
/// @param val_b flag set at the beginning of found occurence of string
/// @param s string that function seaches for
/// @param val_e flag set at the end of found occurence of string
/// @param case_sensitive indicates whether algorithm should care about case sensitivity
/// @param for_each indicates whether function searches through whole buffer and sets
/// the format for all occurences of given string or stops after the first one
/// @return true if at least one occurence of the string was found, false otherwise
///
bool SetFormatting(short val_b, std::basic_string<C> s, short val_e,
bool case_sensitive, bool for_each = 1);
/// Searches for given string in buffer and removes given
/// format/color from the beginning and end of its occurence
/// @param val_b flag to be removed from the beginning of the string
/// @param s string that function seaches for
/// @param val_e flag to be removed from the end of the string
/// @param case_sensitive indicates whether algorithm should care about case sensitivity
/// @param for_each indicates whether function searches through whole buffer and removes
/// given format from all occurences of given string or stops after the first one
///
void RemoveFormatting(short val_b, std::basic_string<C> pattern, short val_e,
bool case_sensitive, bool for_each = 1);
/// Removes all formating applied to string in buffer.
///
void RemoveFormatting();
/// Sets the pointer to string, that will be passed in operator<<() to window
/// object instead of the internal buffer. This is useful if you took the content
/// of the buffer, modified it somehow and want to print the modified version instead
/// of the original one, but with the original formatting informations. Note that after
/// you're done with the printing etc., this pointer has to be set to null.
/// @param tmp address of the temporary string
///
void SetTemp(std::basic_string<C> *tmp);
/// Prints to window object given part of the string, loading all needed formatting info
/// and cleaning up after. The main goal of this function is to provide interface for
/// colorful scrollers.
/// @param w window object that we want to print to
/// @param start_pos reference to start position of the string. note that this variable is
/// incremented by one after each call or set to 0 if end of string is reached
/// @param width width of the string to be printed
/// @param separator additional text to be placed between the end and the beginning of
/// the string
///
void Write(Window &w, size_t &start_pos, size_t width,
const std::basic_string<C> &separator) const;
/// Clears the content of the buffer and its formatting informations
///
void Clear();
basic_buffer<C> &operator<<(int n)
{
itsString += intTo< std::basic_string<C> >::apply(n);
return *this;
}
basic_buffer<C> &operator<<(long int n)
{
itsString += longIntTo< std::basic_string<C> >::apply(n);
return *this;
}
basic_buffer<C> &operator<<(unsigned int n)
{
itsString += unsignedIntTo< std::basic_string<C> >::apply(n);
return *this;
}
basic_buffer<C> &operator<<(unsigned long int n)
{
itsString += unsignedLongIntTo< std::basic_string<C> >::apply(n);
return *this;
}
basic_buffer<C> &operator<<(char c)
{
itsString += c;
return *this;
}
basic_buffer<C> &operator<<(wchar_t wc)
{
itsString += wc;
return *this;
}
basic_buffer<C> &operator<<(const C *s)
{
itsString += s;
return *this;
}
basic_buffer<C> &operator<<(const std::basic_string<C> &s)
{
itsString += s;
return *this;
}
/// Handles colors
/// @return reference to itself
///
basic_buffer<C> &operator<<(Color color);
/// Handles format flags
/// @return reference to itself
///
basic_buffer<C> &operator<<(Format f);
/// Handles copying one buffer to another using operator<<()
/// @param buf buffer to be copied
/// @return reference to itself
///
basic_buffer<C> &operator<<(const basic_buffer<C> &buf);
/// Friend operator that handles printing
/// the content of buffer to window object
friend Window &operator<<(Window &w, const basic_buffer<C> &buf)
{
const std::basic_string<C> &s = buf.itsTempString ? *buf.itsTempString : buf.itsString;
if (buf.itsFormat.empty())
w << s;
else
{
std::basic_string<C> tmp;
auto b = buf.itsFormat.begin(), e = buf.itsFormat.end();
for (size_t i = 0; i < s.length() || b != e; ++i)
{
while (b != e && i == b->Position)
{
if (!tmp.empty())
{
w << tmp;
tmp.clear();
}
buf.LoadAttribute(w, b->Value);
b++;
}
if (i < s.length())
tmp += s[i];
}
if (!tmp.empty())
w << tmp;
}
return w;
}
private:
/// Loads an attribute to given window object
/// @param w window object we want to load attribute to
/// @param value value of attribute to be loaded
///
void LoadAttribute(Window &w, short value) const;
};
template <typename C> NCurses::basic_buffer<C>::basic_buffer(const basic_buffer &b) : itsString(b.itsString), itsFormat(b.itsFormat), itsTempString(b.itsTempString) { } /// Standard buffer that uses narrow characters
///
typedef basic_buffer<char> Buffer;
template <typename C> const std::basic_string<C> &NCurses::basic_buffer<C>::Str() const /// Standard buffer that uses wide characters
///
typedef basic_buffer<wchar_t> WBuffer;
template <typename C> basic_buffer<C>::basic_buffer(const basic_buffer &b)
: itsString(b.itsString), itsFormat(b.itsFormat), itsTempString(b.itsTempString) { }
template <typename C> const std::basic_string<C> &basic_buffer<C>::Str() const
{ {
return itsString; return itsString;
} }
template <typename C> bool NCurses::basic_buffer<C>::SetFormatting( short val_b, template <typename C> bool basic_buffer<C>::SetFormatting(
std::basic_string<C> s, short val_b,
short val_e, std::basic_string<C> s,
bool case_sensitive, short val_e,
bool for_each bool case_sensitive,
) bool for_each
)
{ {
if (s.empty()) if (s.empty())
return false; return false;
@@ -290,12 +293,13 @@ template <typename C> bool NCurses::basic_buffer<C>::SetFormatting( short val_b,
return result; return result;
} }
template <typename C> void NCurses::basic_buffer<C>::RemoveFormatting( short val_b, template <typename C> void basic_buffer<C>::RemoveFormatting(
std::basic_string<C> pattern, short val_b,
short val_e, std::basic_string<C> pattern,
bool case_sensitive, short val_e,
bool for_each bool case_sensitive,
) bool for_each
)
{ {
if (pattern.empty()) if (pattern.empty())
return; return;
@@ -320,21 +324,22 @@ template <typename C> void NCurses::basic_buffer<C>::RemoveFormatting( short val
} }
} }
template <typename C> void NCurses::basic_buffer<C>::RemoveFormatting() template <typename C> void basic_buffer<C>::RemoveFormatting()
{ {
itsFormat.clear(); itsFormat.clear();
} }
template <typename C> void NCurses::basic_buffer<C>::SetTemp(std::basic_string<C> *tmp) template <typename C> void basic_buffer<C>::SetTemp(std::basic_string<C> *tmp)
{ {
itsTempString = tmp; itsTempString = tmp;
} }
template <typename C> void NCurses::basic_buffer<C>::Write( Window &w, template <typename C> void basic_buffer<C>::Write(
size_t &start_pos, Window &w,
size_t width, size_t &start_pos,
const std::basic_string<C> &separator size_t width,
) const const std::basic_string<C> &separator
) const
{ {
std::basic_string<C> s = itsString; std::basic_string<C> s = itsString;
size_t len = Window::Length(s); size_t len = Window::Length(s);
@@ -387,21 +392,21 @@ template <typename C> void NCurses::basic_buffer<C>::Write( Window &w,
w << *this; w << *this;
} }
template <typename C> void NCurses::basic_buffer<C>::Clear() template <typename C> void basic_buffer<C>::Clear()
{ {
itsString.clear(); itsString.clear();
itsFormat.clear(); itsFormat.clear();
} }
template <typename C> void NCurses::basic_buffer<C>::LoadAttribute(Window &w, short value) const template <typename C> void basic_buffer<C>::LoadAttribute(Window &w, short value) const
{ {
if (value < NCurses::fmtNone) if (value < fmtNone)
w << NCurses::Color(value); w << Color(value);
else else
w << NCurses::Format(value); w << Format(value);
} }
template <typename C> NCurses::basic_buffer<C> &NCurses::basic_buffer<C>::operator<<(Color color) template <typename C> basic_buffer<C> &basic_buffer<C>::operator<<(Color color)
{ {
FormatPos f; FormatPos f;
f.Position = itsString.length(); f.Position = itsString.length();
@@ -410,12 +415,12 @@ template <typename C> NCurses::basic_buffer<C> &NCurses::basic_buffer<C>::operat
return *this; return *this;
} }
template <typename C> NCurses::basic_buffer<C> &NCurses::basic_buffer<C>::operator<<(Format f) template <typename C> basic_buffer<C> &basic_buffer<C>::operator<<(Format f)
{ {
return operator<<(Color(f)); return operator<<(Color(f));
} }
template <typename C> NCurses::basic_buffer<C> &NCurses::basic_buffer<C>::operator<<(const NCurses::basic_buffer<C> &buf) template <typename C> basic_buffer<C> &basic_buffer<C>::operator<<(const basic_buffer<C> &buf)
{ {
size_t length = itsString.length(); size_t length = itsString.length();
itsString += buf.itsString; itsString += buf.itsString;
@@ -427,4 +432,6 @@ template <typename C> NCurses::basic_buffer<C> &NCurses::basic_buffer<C>::operat
return *this; return *this;
} }
}
#endif #endif