]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/util/buffer.h
Implement buffer operations
[xonotic/netradiant.git] / libs / util / buffer.h
index 9ce31d62a472b0242cff24137c1b7c5bf409cca1..a621f4920db076594ba8fdac241f573760156908 100644 (file)
@@ -1,6 +1,10 @@
 #ifndef INCLUDED_BUFFER_H
 #define INCLUDED_BUFFER_H
 
+#include <cstdarg>
+#include <cstdio>
+#include <cstring>
+
 namespace u {
 
     using byte = char;
@@ -59,9 +63,44 @@ namespace u {
             return this->data();
         }
 
-        operator byte *() {
+        operator cstring() const {
+            return c_str();
+        }
+
+        std::size_t strlen() const {
+            return ::strlen(c_str());
+        }
+
+        byte *mut() {
             return this->data();
         }
+
+//        operator byte *() {
+//            return mut();
+//        }
+
+        void terminate(long offset = -1) {
+            if (offset < 0) {
+                offset += this->size();
+            }
+            mut()[offset] = '\0';
+        }
+
+        void copy(cstring str, unsigned int offset = 0, unsigned int limit = 0) {
+            if (!limit) {
+                limit = this->size() - offset;
+            }
+            strncpy(mut() + offset, str, limit);
+        }
+
+        __attribute__((format(printf, 2, 3)))
+        void sprintf(const char *format, ...) {
+            // todo: check for overflow
+            va_list argptr;
+            va_start(argptr, format);
+            vsprintf(this->data(), format, argptr);
+            va_end(argptr);
+        }
     };
 
     template<int sz>