From d6de34fec60e1eb5d23ecde69cfe92024b72e511 Mon Sep 17 00:00:00 2001
From: marco <marco@5a81b35b-ba03-0410-adc8-b2c5c5119f08>
Date: Thu, 18 Mar 2010 11:15:06 +0000
Subject: [PATCH] add FixedString class

git-svn-id: https://dng.biozentrum.unibas.ch/svn/openstructure/trunk@1838 5a81b35b-ba03-0410-adc8-b2c5c5119f08
---
 modules/base/src/CMakeLists.txt  |  1 +
 modules/base/src/fixed_string.hh | 86 ++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+)
 create mode 100644 modules/base/src/fixed_string.hh

diff --git a/modules/base/src/CMakeLists.txt b/modules/base/src/CMakeLists.txt
index c5c873955..e0ac7c7cf 100644
--- a/modules/base/src/CMakeLists.txt
+++ b/modules/base/src/CMakeLists.txt
@@ -26,6 +26,7 @@ message.hh
 platform.hh
 string_ref.hh
 pod_vector.hh
+fixed_string.hh
 )
 
 module(NAME base SOURCES ${OST_BASE_SOURCES} 
diff --git a/modules/base/src/fixed_string.hh b/modules/base/src/fixed_string.hh
new file mode 100644
index 000000000..a7858cf06
--- /dev/null
+++ b/modules/base/src/fixed_string.hh
@@ -0,0 +1,86 @@
+#ifndef OST_SMALL_STRING_HH
+#define OST_SMALL_STRING_HH
+
+#include <ost/base.hh>
+#include <cassert>
+#include <string.h>
+
+namespace ost {
+
+/// \brief string class that uses an array of static size to hold the 
+///        characters
+template <size_t LENGTH>
+class TEMPLATE_EXPORT FixedString {
+public:
+  FixedString() {
+    ::memset(bytes_, 0, LENGTH+1);
+  }
+
+  explicit FixedString(const String& str) {
+    this->assign(str);
+  }
+
+  explicit FixedString(const char* str) {
+    this->assign(str);
+  }
+
+
+  size_t length() const {
+    return strlen(bytes_);
+  }
+
+  size_t size() const {
+    return this->length();
+  }
+
+  char operator[](size_t index) const {
+    return bytes_[index];
+  }
+
+  char& operator[](size_t index) {
+    return bytes_[index];
+  }
+
+  bool operator==(const String& rhs) const {
+    return  !strcmp(bytes_, rhs.c_str());
+  }
+
+  bool operator!=(const String& rhs) const {
+    return !this->operator==(rhs);
+  }
+  bool operator==(const FixedString<LENGTH>& rhs) const {
+    return  !strcmp(bytes_, rhs.bytes_);
+  }
+
+  bool operator!=(const FixedString<LENGTH>& rhs) const {
+    return !this->operator==(rhs);
+  }
+
+  FixedString<LENGTH>& operator=(const String& rhs) {
+    this->assign(rhs);
+    return *this;
+  }
+
+  template <typename DS>
+  void Serialize(DS& ds) {
+    RawSerialize(ds, bytes_, LENGTH);
+  }
+  const char* c_str() const {
+    return bytes_;
+  }
+private:
+  void assign(const String& str) {
+    assert(str.length()<=LENGTH);
+    strcpy(bytes_, str.c_str());
+  }
+
+  void assign(const char* str) {
+    assert(strlen(str)<=LENGTH);
+    strcpy(bytes_, str);
+  }
+  char bytes_[LENGTH+1];
+};
+
+}
+
+#endif
-- 
GitLab