RigsofRods
Soft-body Physics Simulation
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 
30 namespace RoR {
31 
35 template<size_t L> class Str
36 {
37 public:
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 
79 private:
80  char m_buffer[L];
81  const size_t m_capacity = L;
82 };
83 
84 } // namespace RoR
RoR::Str::Str
Str(std::string::const_iterator &itor, std::string::const_iterator &endi)
Definition: Str.h:42
RoR::Str::Assign
Str & Assign(const char *src)
Definition: Str.h:55
RoR::Str::GetBuffer
char * GetBuffer()
Definition: Str.h:48
RoR::Str::Append
Str & Append(std::string::const_iterator &itor, std::string::const_iterator &endi)
Definition: Str.h:64
z
float z
Definition: (ValueTypes) quaternion.h:7
RoR::Str::Compare
int Compare(const char *str) const
Definition: Str.h:50
RoR::Str::Str
Str()
Definition: Str.h:39
RoR::Str::GetCapacity
size_t GetCapacity() const
Definition: Str.h:49
RoR::Str::Append
Str & Append(size_t z)
Definition: Str.h:62
RoR::Str::GetLength
size_t GetLength() const
Definition: Str.h:51
RoR::Str::operator<<
Str & operator<<(const char *src)
Definition: Str.h:71
RoR::Str::Append
Str & Append(int i)
Definition: Str.h:61
RoR::Str::m_buffer
char m_buffer[L]
Definition: Str.h:80
RoR::Str::m_capacity
const size_t m_capacity
Definition: Str.h:81
RoR::Str
Wrapper for classic c-string (local buffer) Refresher: strlen() excludes '\0' terminator; strncat() A...
Definition: Str.h:35
RoR::Str::ToCStr
const char * ToCStr() const
Definition: Str.h:46
RoR::Str::Assign
Str & Assign(std::string::const_iterator &itor, std::string::const_iterator &endi)
Definition: Str.h:56
RoR::Str::operator=
Str & operator=(const char *src)
Definition: Str.h:69
RoR::Str::Append
Str & Append(const char *src)
Definition: Str.h:59
RoR::Str::Str
Str(Str< L > const &src)
Definition: Str.h:40
RoR::Str::Str
Str(const char *src)
Definition: Str.h:41
RoR::Str::operator==
bool operator==(const char *other) const
Definition: Str.h:77
RoR
Definition: AppContext.h:36
RoR::Str::Clear
Str & Clear()
Definition: Str.h:54
RoR::Str::IsEmpty
bool IsEmpty() const
Definition: Str.h:47
RoR::Str::Append
Str & Append(float f)
Definition: Str.h:60
RoR::Str::Append
Str & Append(char c)
Definition: Str.h:63