summaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
authorPiotr Dziwinski <piotrdz@gmail.com>2013-12-03 00:11:26 +0100
committerPiotr Dziwinski <piotrdz@gmail.com>2013-12-04 00:15:39 +0100
commit8deb1305726966b3b583865dec1ba7ba1327d8cb (patch)
tree291627fbe0123dc35c015b1f398585f204afe8ea /src/common
parentdae8d8738908a65a4ee4ef14d88681e196825126 (diff)
downloadcolobot-8deb1305726966b3b583865dec1ba7ba1327d8cb.tar.gz
colobot-8deb1305726966b3b583865dec1ba7ba1327d8cb.tar.bz2
colobot-8deb1305726966b3b583865dec1ba7ba1327d8cb.zip
Changed char[] to std::string in restext
Experimental changes
Diffstat (limited to 'src/common')
-rw-r--r--src/common/restext.cpp32
-rw-r--r--src/common/restext.h2
-rw-r--r--src/common/stringutils.cpp34
-rw-r--r--src/common/stringutils.h11
4 files changed, 54 insertions, 25 deletions
diff --git a/src/common/restext.cpp b/src/common/restext.cpp
index d61fa63..c5d0ceb 100644
--- a/src/common/restext.cpp
+++ b/src/common/restext.cpp
@@ -776,9 +776,7 @@ static KeyDesc keyTable[22] =
bool SearchKey(const char *cmd, InputSlot &key)
{
- int i;
-
- for ( i=0 ; i<22 ; i++ )
+ for (int i = 0; i < 22 ;i++)
{
if ( strstr(cmd, keyTable[i].name) == cmd )
{
@@ -791,14 +789,11 @@ bool SearchKey(const char *cmd, InputSlot &key)
// Replaces the commands "\key name;" in a text.
-static void PutKeyName(char* dst, const char* src)
+static void PutKeyName(std::string& dst, const char* src)
{
- InputSlot key;
- char name[50];
- int s, d, n;
- unsigned int res;
+ dst.clear();
- s = d = 0;
+ int s = 0;
while ( src[s] != 0 )
{
if ( src[s+0] == '\\' &&
@@ -807,18 +802,16 @@ static void PutKeyName(char* dst, const char* src)
src[s+3] == 'y' &&
src[s+4] == ' ' )
{
+ InputSlot key;
if ( SearchKey(src+s+5, key) )
{
- res = CRobotMain::GetInstancePointer()->GetInputBinding(key).primary;
+ unsigned int res = CRobotMain::GetInstancePointer()->GetInputBinding(key).primary;
if (res != KEY_INVALID)
{
- if ( GetResource(RES_KEY, res, name) )
+ std::string keyName;
+ if ( GetResource(RES_KEY, res, keyName) )
{
- n = 0;
- while ( name[n] != 0 )
- {
- dst[d++] = name[n++];
- }
+ dst.append(keyName);
while ( src[s++] != ';' );
continue;
}
@@ -826,9 +819,8 @@ static void PutKeyName(char* dst, const char* src)
}
}
- dst[d++] = src[s++];
+ dst.append(1, src[s++]);
}
- dst[d++] = 0;
}
// Returns the translated text of a resource that needs key substitution
@@ -905,13 +897,13 @@ static const char* GetResourceBase(ResType type, int num)
// Returns the text of a resource.
-bool GetResource(ResType type, int num, char* text)
+bool GetResource(ResType type, int num, std::string& text)
{
const char *tmpl = GetResourceBase(type, num);
if (!tmpl)
{
- text[0] = 0;
+ text.clear();
return false;
}
diff --git a/src/common/restext.h b/src/common/restext.h
index e4659e2..b5a3415 100644
--- a/src/common/restext.h
+++ b/src/common/restext.h
@@ -157,5 +157,5 @@ void InitializeRestext();
void SetGlobalGamerName(std::string name);
bool SearchKey(const char *cmd, InputSlot& slot);
-bool GetResource(ResType type, int num, char* text);
+bool GetResource(ResType type, int num, std::string& text);
diff --git a/src/common/stringutils.cpp b/src/common/stringutils.cpp
index 953abba..37169a5 100644
--- a/src/common/stringutils.cpp
+++ b/src/common/stringutils.cpp
@@ -17,6 +17,40 @@
#include "common/stringutils.h"
+#include <cstdarg>
+#include <vector>
+
+
+static std::string VFormat(const char *fmt, va_list ap)
+{
+ size_t size = 1024;
+ char stackbuf[1024];
+ std::vector<char> dynamicbuf;
+ char *buf = &stackbuf[0];
+
+ while (1)
+ {
+ int needed = vsnprintf (buf, size, fmt, ap);
+
+ if (needed <= static_cast<int>(size) && needed >= 0)
+ {
+ return std::string(buf, static_cast<size_t>(needed));
+ }
+
+ size = (needed > 0) ? (needed+1) : (size*2);
+ dynamicbuf.resize(size);
+ buf = &dynamicbuf[0];
+ }
+}
+
+std::string StrUtils::Format(const char *fmt, ...)
+{
+ va_list ap;
+ va_start(ap, fmt);
+ std::string buf = VFormat(fmt, ap);
+ va_end(ap);
+ return buf;
+}
std::string StrUtils::Replace(const std::string &str, const std::string &oldStr, const std::string &newStr)
{
diff --git a/src/common/stringutils.h b/src/common/stringutils.h
index c60bfb0..e80163a 100644
--- a/src/common/stringutils.h
+++ b/src/common/stringutils.h
@@ -31,11 +31,11 @@ namespace StrUtils {
/** If given, \a ok is set to true/false on success/failure.
Warning: To avoid unnecessary problems, *always* give full template qualifier e.g. ToString\<int\> */
template<class T>
-std::string ToString(T value, bool *ok = NULL)
+std::string ToString(T value, bool *ok = nullptr)
{
std::ostringstream s;
s << value;
- if (ok != NULL)
+ if (ok != nullptr)
*ok = !s.fail();
return s.str();
}
@@ -44,17 +44,20 @@ std::string ToString(T value, bool *ok = NULL)
/** If given, \a ok is set to true/false on success/failure.
Warning: To avoid unnecessary problems, *always* give full template qualifier e.g. FromString\<int\> */
template<class T>
-T FromString(const std::string &str, bool *ok = NULL)
+T FromString(const std::string &str, bool *ok = nullptr)
{
std::istringstream s;
s.str(str);
T value;
s >> value;
- if (ok != NULL)
+ if (ok != nullptr)
*ok = !s.fail();
return value;
}
+//! Replacement for sprintf()
+std::string Format(const char *fmt, ...);
+
//! Returns a string with every occurence of \a oldStr in \a str replaced to \a newStr
std::string Replace(const std::string &str, const std::string &oldStr, const std::string &newStr);