Add 'String<SIZE>' buffer type to 'util/string.h'

The new 'String' buffer type is meant to replace the manually created
character buffers that are scattered throughout Genode. It plainly holds
a null-terminated string to be stored as a member variable (e.g., a
session label) or passed as RPC argument. It is not intended to become a
string API.
This commit is contained in:
Norman Feske
2013-09-19 20:35:45 +02:00
parent 339193a887
commit 5befab7f3d
4 changed files with 32 additions and 22 deletions

View File

@ -447,6 +447,36 @@ namespace Genode {
return i;
}
/**
* Buffer that contains a null-terminated string
*
* \param SIZE buffer size
*/
template <size_t SIZE>
class String
{
private:
char _buf[SIZE];
size_t _length;
public:
constexpr static size_t size() { return SIZE; }
String() : _length(0) { }
String(char const *str) : _length(min(strlen(str) + 1, SIZE))
{
strncpy(_buf, str, _length);
}
bool valid() const {
return (_length <= SIZE) && (_buf[_length - 1] == '\0'); }
char const *string() const { return valid() ? _buf : ""; }
};
}
#endif /* _INCLUDE__UTIL__STRING_H_ */