summaryrefslogtreecommitdiff
path: root/src/misc
diff options
context:
space:
mode:
authorCharles Pigott <charlespigott@googlemail.com>2021-04-02 21:08:16 +0100
committerCharles Pigott <charlespigott@googlemail.com>2021-04-04 08:01:54 +0100
commitf481c9fc2c68ff7b83eef78d3e37982492dae981 (patch)
tree922b22de6b2e48716c763b19dcc3177e1c8f1b47 /src/misc
parent33c5f984f533bc101f4a92130a2b56432d5dcc08 (diff)
downloadopenttd-f481c9fc2c68ff7b83eef78d3e37982492dae981.tar.xz
Codechange: Replace CStrA with std::string
Diffstat (limited to 'src/misc')
-rw-r--r--src/misc/CMakeLists.txt1
-rw-r--r--src/misc/array.hpp12
-rw-r--r--src/misc/dbg_helpers.cpp73
-rw-r--r--src/misc/dbg_helpers.h50
-rw-r--r--src/misc/str.hpp149
5 files changed, 66 insertions, 219 deletions
diff --git a/src/misc/CMakeLists.txt b/src/misc/CMakeLists.txt
index 9ed598004..76780679f 100644
--- a/src/misc/CMakeLists.txt
+++ b/src/misc/CMakeLists.txt
@@ -11,5 +11,4 @@ add_files(
getoptdata.h
hashtable.hpp
lrucache.hpp
- str.hpp
)
diff --git a/src/misc/array.hpp b/src/misc/array.hpp
index d23fb7a0d..aea58aba4 100644
--- a/src/misc/array.hpp
+++ b/src/misc/array.hpp
@@ -11,7 +11,7 @@
#define ARRAY_HPP
#include "fixedsizearray.hpp"
-#include "str.hpp"
+#include "../string_func.h"
/**
* Flexible array with size limit. Implemented as fixed size
@@ -103,14 +103,14 @@ public:
*/
template <typename D> void Dump(D &dmp) const
{
- dmp.WriteLine("capacity = %d", Tcapacity);
+ dmp.WriteValue("capacity", Tcapacity);
uint num_items = Length();
- dmp.WriteLine("num_items = %d", num_items);
- CStrA name;
+ dmp.WriteValue("num_items", num_items);
for (uint i = 0; i < num_items; i++) {
const T &item = (*this)[i];
- name.Format("item[%d]", i);
- dmp.WriteStructT(name.Data(), &item);
+ char name[32];
+ seprintf(name, lastof(name), "item[%d]", i);
+ dmp.WriteStructT(name, &item);
}
}
};
diff --git a/src/misc/dbg_helpers.cpp b/src/misc/dbg_helpers.cpp
index 7d38c7e65..85d6ea1b9 100644
--- a/src/misc/dbg_helpers.cpp
+++ b/src/misc/dbg_helpers.cpp
@@ -10,6 +10,10 @@
#include "../stdafx.h"
#include "../rail_map.h"
#include "dbg_helpers.h"
+#include "blob.hpp"
+
+#include <sstream>
+#include <iomanip>
#include "../safeguards.h"
@@ -20,19 +24,15 @@ static const char * const trackdir_names[] = {
};
/** Return name of given Trackdir. */
-CStrA ValueStr(Trackdir td)
+std::string ValueStr(Trackdir td)
{
- CStrA out;
- out.Format("%d (%s)", td, ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV"));
- return out.Transfer();
+ return std::to_string(td) + " (" + ItemAtT(td, trackdir_names, "UNK", INVALID_TRACKDIR, "INV") + ")";
}
/** Return composed name of given TrackdirBits. */
-CStrA ValueStr(TrackdirBits td_bits)
+std::string ValueStr(TrackdirBits td_bits)
{
- CStrA out;
- out.Format("%d (%s)", td_bits, ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV").Data());
- return out.Transfer();
+ return std::to_string(td_bits) + " (" + ComposeNameT(td_bits, trackdir_names, "UNK", INVALID_TRACKDIR_BIT, "INV") + ")";
}
@@ -42,11 +42,9 @@ static const char * const diagdir_names[] = {
};
/** Return name of given DiagDirection. */
-CStrA ValueStr(DiagDirection dd)
+std::string ValueStr(DiagDirection dd)
{
- CStrA out;
- out.Format("%d (%s)", dd, ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV"));
- return out.Transfer();
+ return std::to_string(dd) + " (" + ItemAtT(dd, diagdir_names, "UNK", INVALID_DIAGDIR, "INV") + ")";
}
@@ -56,20 +54,19 @@ static const char * const signal_type_names[] = {
};
/** Return name of given SignalType. */
-CStrA ValueStr(SignalType t)
+std::string ValueStr(SignalType t)
{
- CStrA out;
- out.Format("%d (%s)", t, ItemAtT(t, signal_type_names, "UNK"));
- return out.Transfer();
+ return std::to_string(t) + " (" + ItemAtT(t, signal_type_names, "UNK") + ")";
}
/** Translate TileIndex into string. */
-CStrA TileStr(TileIndex tile)
+std::string TileStr(TileIndex tile)
{
- CStrA out;
- out.Format("0x%04X (%d, %d)", tile, TileX(tile), TileY(tile));
- return out.Transfer();
+ std::stringstream ss;
+ ss << "0x" << std::setfill('0') << std::setw(4) << std::hex << tile; // 0x%04X
+ ss << " (" << TileX(tile) << ", " << TileY(tile) << ")";
+ return ss.str();
}
/**
@@ -81,21 +78,21 @@ CStrA TileStr(TileIndex tile)
}
/** Return structured name of the current class/structure. */
-CStrA DumpTarget::GetCurrentStructName()
+std::string DumpTarget::GetCurrentStructName()
{
- CStrA out;
+ std::string out;
if (!m_cur_struct.empty()) {
/* we are inside some named struct, return its name */
out = m_cur_struct.top();
}
- return out.Transfer();
+ return out;
}
/**
* Find the given instance in our anti-recursion repository.
* Return true and set name when object was found.
*/
-bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, CStrA &name)
+bool DumpTarget::FindKnownName(size_t type_id, const void *ptr, std::string &name)
{
KNOWN_NAMES::const_iterator it = m_known_names.find(KnownStructKey(type_id, ptr));
if (it != m_known_names.end()) {
@@ -111,33 +108,29 @@ void DumpTarget::WriteIndent()
{
int num_spaces = 2 * m_indent;
if (num_spaces > 0) {
- memset(m_out.GrowSizeNC(num_spaces), ' ', num_spaces);
+ m_out += std::string(num_spaces, ' ');
}
}
-/** Write a line with indent at the beginning and \<LF\> at the end. */
-void DumpTarget::WriteLine(const char *format, ...)
+/** Write 'name = value' with indent and new-line. */
+void DumpTarget::WriteValue(const char *name, int value)
{
WriteIndent();
- va_list args;
- va_start(args, format);
- m_out.AddFormatL(format, args);
- va_end(args);
- m_out.AppendStr("\n");
+ m_out += std::string(name) + " = " + std::to_string(value) + "\n";
}
/** Write 'name = value' with indent and new-line. */
void DumpTarget::WriteValue(const char *name, const char *value_str)
{
WriteIndent();
- m_out.AddFormat("%s = %s\n", name, value_str);
+ m_out += std::string(name) + " = " + value_str + "\n";
}
/** Write name & TileIndex to the output. */
void DumpTarget::WriteTile(const char *name, TileIndex tile)
{
WriteIndent();
- m_out.AddFormat("%s = %s\n", name, TileStr(tile).Data());
+ m_out += std::string(name) + " = " + TileStr(tile) + "\n";
}
/**
@@ -146,12 +139,12 @@ void DumpTarget::WriteTile(const char *name, TileIndex tile)
void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr)
{
/* make composite name */
- CStrA cur_name = GetCurrentStructName().Transfer();
- if (cur_name.Size() > 0) {
+ std::string cur_name = GetCurrentStructName();
+ if (cur_name.size() > 0) {
/* add name delimiter (we use structured names) */
- cur_name.AppendStr(".");
+ cur_name += ".";
}
- cur_name.AppendStr(name);
+ cur_name += name;
/* put the name onto stack (as current struct name) */
m_cur_struct.push(cur_name);
@@ -160,7 +153,7 @@ void DumpTarget::BeginStruct(size_t type_id, const char *name, const void *ptr)
m_known_names.insert(KNOWN_NAMES::value_type(KnownStructKey(type_id, ptr), cur_name));
WriteIndent();
- m_out.AddFormat("%s = {\n", name);
+ m_out += std::string(name) + " = {\n";
m_indent++;
}
@@ -171,7 +164,7 @@ void DumpTarget::EndStruct()
{
m_indent--;
WriteIndent();
- m_out.AddFormat("}\n");
+ m_out += "}\n";
/* remove current struct name from the stack */
m_cur_struct.pop();
diff --git a/src/misc/dbg_helpers.h b/src/misc/dbg_helpers.h
index 7c9b4c876..f9d251b8a 100644
--- a/src/misc/dbg_helpers.h
+++ b/src/misc/dbg_helpers.h
@@ -12,8 +12,7 @@
#include <map>
#include <stack>
-
-#include "str.hpp"
+#include <string>
#include "../direction_type.h"
#include "../signal_type.h"
@@ -67,9 +66,9 @@ inline typename ArrayT<T>::item_t ItemAtT(E idx, const T &t, typename ArrayT<T>:
* or t_unk when index is out of bounds.
*/
template <typename E, typename T>
-inline CStrA ComposeNameT(E value, T &t, const char *t_unk, E val_inv, const char *name_inv)
+inline std::string ComposeNameT(E value, T &t, const char *t_unk, E val_inv, const char *name_inv)
{
- CStrA out;
+ std::string out;
if (value == val_inv) {
out = name_inv;
} else if (value == 0) {
@@ -77,18 +76,22 @@ inline CStrA ComposeNameT(E value, T &t, const char *t_unk, E val_inv, const cha
} else {
for (size_t i = 0; i < ArrayT<T>::length; i++) {
if ((value & (1 << i)) == 0) continue;
- out.AddFormat("%s%s", (out.Size() > 0 ? "+" : ""), (const char*)t[i]);
+ out += (!out.empty() ? "+" : "");
+ out += t[i];
value &= ~(E)(1 << i);
}
- if (value != 0) out.AddFormat("%s%s", (out.Size() > 0 ? "+" : ""), t_unk);
+ if (value != 0) {
+ out += (!out.empty() ? "+" : "");
+ out += t_unk;
+ }
}
- return out.Transfer();
+ return out;
}
-CStrA ValueStr(Trackdir td);
-CStrA ValueStr(TrackdirBits td_bits);
-CStrA ValueStr(DiagDirection dd);
-CStrA ValueStr(SignalType t);
+std::string ValueStr(Trackdir td);
+std::string ValueStr(TrackdirBits td_bits);
+std::string ValueStr(DiagDirection dd);
+std::string ValueStr(SignalType t);
/** Class that represents the dump-into-string target. */
struct DumpTarget {
@@ -118,31 +121,31 @@ struct DumpTarget {
}
};
- typedef std::map<KnownStructKey, CStrA> KNOWN_NAMES;
+ typedef std::map<KnownStructKey, std::string> KNOWN_NAMES;
- CStrA m_out; ///< the output string
- int m_indent; ///< current indent/nesting level
- std::stack<CStrA> m_cur_struct; ///< here we will track the current structure name
- KNOWN_NAMES m_known_names; ///< map of known object instances and their structured names
+ std::string m_out; ///< the output string
+ int m_indent; ///< current indent/nesting level
+ std::stack<std::string> m_cur_struct; ///< here we will track the current structure name
+ KNOWN_NAMES m_known_names; ///< map of known object instances and their structured names
DumpTarget()
: m_indent(0)
{}
static size_t& LastTypeId();
- CStrA GetCurrentStructName();
- bool FindKnownName(size_t type_id, const void *ptr, CStrA &name);
+ std::string GetCurrentStructName();
+ bool FindKnownName(size_t type_id, const void *ptr, std::string &name);
void WriteIndent();
- void CDECL WriteLine(const char *format, ...) WARN_FORMAT(2, 3);
+ void WriteValue(const char *name, int value);
void WriteValue(const char *name, const char *value_str);
void WriteTile(const char *name, TileIndex t);
/** Dump given enum value (as a number and as named value) */
template <typename E> void WriteEnumT(const char *name, E e)
{
- WriteValue(name, ValueStr(e).Data());
+ WriteValue(name, ValueStr(e).c_str());
}
void BeginStruct(size_t type_id, const char *name, const void *ptr);
@@ -155,13 +158,14 @@ struct DumpTarget {
if (s == nullptr) {
/* No need to dump nullptr struct. */
- WriteLine("%s = <null>", name);
+ WriteValue(name, "<null>");
return;
}
- CStrA known_as;
+ std::string known_as;
if (FindKnownName(type_id, s, known_as)) {
/* We already know this one, no need to dump it. */
- WriteLine("%s = known_as.%s", name, known_as.Data());
+ std::string known_as_str = std::string("known_as.") + name;
+ WriteValue(name, known_as_str.c_str());
} else {
/* Still unknown, dump it */
BeginStruct(type_id, name, s);
diff --git a/src/misc/str.hpp b/src/misc/str.hpp
deleted file mode 100644
index 05a79786b..000000000
--- a/src/misc/str.hpp
+++ /dev/null
@@ -1,149 +0,0 @@
-/*
- * This file is part of OpenTTD.
- * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
- * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
- */
-
-/** @file str.hpp String formatting? */
-
-#ifndef STR_HPP
-#define STR_HPP
-
-#include <errno.h>
-#include <stdarg.h>
-#include "blob.hpp"
-#include "../core/math_func.hpp"
-#include "../string_func.h"
-
-/** Blob based case sensitive ANSI/UTF-8 string */
-struct CStrA : public CBlobT<char>
-{
- typedef CBlobT<char> base; ///< base class
-
- /** Create an empty CStrT */
- inline CStrA()
- {
- }
-
- /** Copy constructor */
- inline CStrA(const CStrA &src) : base(src)
- {
- base::FixTail();
- }
-
- /** Take over ownership constructor */
- inline CStrA(const OnTransfer &ot)
- : base(ot)
- {
- }
-
- /** Grow the actual buffer and fix the trailing zero at the end. */
- inline char *GrowSizeNC(uint count)
- {
- char *ret = base::GrowSizeNC(count);
- base::FixTail();
- return ret;
- }
-
- /** Append zero-ended C string. */
- inline void AppendStr(const char *str)
- {
- if (!StrEmpty(str)) {
- base::AppendRaw(str, strlen(str));
- base::FixTail();
- }
- }
-
- /** Append another CStrA. */
- inline void Append(const CStrA &src)
- {
- if (src.Length() > 0) {
- base::AppendRaw(src);
- base::FixTail();
- }
- }
-
- /** Assignment from C string. */
- inline CStrA &operator=(const char *src)
- {
- base::Clear();
- AppendStr(src);
- return *this;
- }
-
- /** Assignment from another CStrA. */
- inline CStrA &operator=(const CStrA &src)
- {
- if (&src != this) {
- base::Clear();
- base::AppendRaw(src.Data(), src.Size());
- base::FixTail();
- }
- return *this;
- }
-
- /** Lower-than operator (to support stl collections) */
- inline bool operator<(const CStrA &other) const
- {
- return strcmp(base::Data(), other.Data()) < 0;
- }
-
- /** Add formatted string (like vsprintf) at the end of existing contents. */
- int AddFormatL(const char *format, va_list args) WARN_FORMAT(2, 0)
- {
- size_t addSize = std::max<size_t>(strlen(format), 16);
- addSize += addSize / 2;
- int ret;
- int err = 0;
- for (;;) {
- char *buf = MakeFreeSpace(addSize);
- ret = vseprintf(buf, buf + base::GetReserve() - 1, format, args);
- if (ret >= (int)base::GetReserve()) {
- /* Greater return than given count means needed buffer size. */
- addSize = ret + 1;
- continue;
- }
- if (ret >= 0) {
- /* success */
- break;
- }
- err = errno;
- if (err != ERANGE && err != ENOENT && err != 0) {
- /* some strange failure */
- break;
- }
- /* small buffer (M$ implementation) */
- addSize *= 2;
- }
- if (ret > 0) {
- GrowSizeNC(ret);
- } else {
- base::FixTail();
- }
- return ret;
- }
-
- /** Add formatted string (like sprintf) at the end of existing contents. */
- int CDECL WARN_FORMAT(2, 3) AddFormat(const char *format, ...)
- {
- va_list args;
- va_start(args, format);
- int ret = AddFormatL(format, args);
- va_end(args);
- return ret;
- }
-
- /** Assign formatted string (like sprintf). */
- int CDECL WARN_FORMAT(2, 3) Format(const char *format, ...)
- {
- base::Free();
- va_list args;
- va_start(args, format);
- int ret = AddFormatL(format, args);
- va_end(args);
- return ret;
- }
-};
-
-#endif /* STR_HPP */