Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
Str.h
Go to the documentation of this file.
1/*
2 This source file is part of Rigs of Rods
3 Copyright 2005-2012 Pierre-Michel Ricordel
4 Copyright 2007-2012 Thomas Fischer
5 Copyright 2013-2020 Petr Ohlidal
6
7 For more information, see http://www.rigsofrods.org/
8
9 Rigs of Rods is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License version 3, as
11 published by the Free Software Foundation.
12
13 Rigs of Rods is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20*/
21
23
24#pragma once
25
26#include <cstdio>
27#include <cstring>
28#include <string>
29
30namespace RoR {
31
35template<size_t L> class Str
36{
37public:
38 // Constructors
39 inline Str() { this->Clear(); }
40 inline Str(Str<L> const & src) { this->Assign(src); }
41 inline Str(const char* src) { this->Assign(src); }
42 inline Str(std::string::const_iterator& itor,
43 std::string::const_iterator& endi){ this->Assign(itor, endi); }
44
45 // Reading
46 inline const char* ToCStr() const { return m_buffer; }
47 inline bool IsEmpty() const { return m_buffer[0] == '\0'; }
48 inline char* GetBuffer() { return m_buffer; }
49 inline size_t GetCapacity() const { return m_capacity; }
50 inline int Compare(const char* str) const { return std::strncmp(m_buffer, str, L); }
51 inline size_t GetLength() const { return std::strlen(m_buffer); }
52
53 // Writing
54 inline Str& Clear() { std::memset(m_buffer, 0, L); return *this; }
55 inline Str& Assign(const char* src) { this->Clear(); this->Append(src); return *this; }
56 inline Str& Assign(std::string::const_iterator& itor,
57 std::string::const_iterator& endi) { this->Clear(); this->Append(itor, endi); return *this; }
58
59 inline Str& Append(const char* src) { std::strncat(m_buffer, src, (L - (this->GetLength() + 1))); return *this; }
60 inline Str& Append(float f) { char buf[50]; std::snprintf(buf, 50, "%f", f); this->Append(buf); return *this; }
61 inline Str& Append(int i) { char buf[50]; std::snprintf(buf, 50, "%d", i); this->Append(buf); return *this; }
62 inline Str& Append(size_t z) { char buf[50]; std::snprintf(buf, 50, "%lu", static_cast<unsigned long>(z)); this->Append(buf); return *this; }
63 inline Str& Append(char c) { char buf[2] = {}; buf[0] = c; this->Append(buf); return *this; }
64 inline Str& Append(std::string::const_iterator& itor,
65 std::string::const_iterator& endi) { for(;itor!=endi;++itor) { this->Append(*itor); } return *this; }
66
67 // Operators
68 inline operator const char*() const { return this->ToCStr(); }
69 inline Str& operator= (const char* src) { return this->Assign(src); }
70 inline Str& operator= (std::string const& str) { return this->Assign(str.c_str()); }
71 inline Str& operator<< (const char* src) { return this->Append(src); }
72 inline Str& operator<< (std::string const& str) { return this->Append(str.c_str()); }
73 inline Str& operator<< (float f) { return this->Append(f); }
74 inline Str& operator<< (int i) { return this->Append(i); }
75 inline Str& operator<< (size_t z) { return this->Append(z); }
76 inline Str& operator<< (char c) { return this->Append(c); }
77 inline bool operator== (const char* other) const { return (this->Compare(other) == 0); }
78
79private:
80 char m_buffer[L];
81 const size_t m_capacity = L;
82};
83
84} // namespace RoR
Wrapper for classic c-string (local buffer) Refresher: strlen() excludes '\0' terminator; strncat() A...
Definition Str.h:36
const char * ToCStr() const
Definition Str.h:46
bool operator==(const char *other) const
Definition Str.h:77
Str & Append(char c)
Definition Str.h:63
Str()
Definition Str.h:39
Str(const char *src)
Definition Str.h:41
Str(Str< L > const &src)
Definition Str.h:40
Str & Assign(std::string::const_iterator &itor, std::string::const_iterator &endi)
Definition Str.h:56
Str & Append(int i)
Definition Str.h:61
Str & Clear()
Definition Str.h:54
Str & Append(std::string::const_iterator &itor, std::string::const_iterator &endi)
Definition Str.h:64
Str & Append(const char *src)
Definition Str.h:59
Str(std::string::const_iterator &itor, std::string::const_iterator &endi)
Definition Str.h:42
Str & operator<<(const char *src)
Definition Str.h:71
Str & operator=(const char *src)
Definition Str.h:69
char * GetBuffer()
Definition Str.h:48
bool IsEmpty() const
Definition Str.h:47
size_t GetLength() const
Definition Str.h:51
const size_t m_capacity
Definition Str.h:81
int Compare(const char *str) const
Definition Str.h:50
Str & Append(float f)
Definition Str.h:60
char m_buffer[L]
Definition Str.h:80
size_t GetCapacity() const
Definition Str.h:49
Str & Assign(const char *src)
Definition Str.h:55
Str & Append(size_t z)
Definition Str.h:62