1 ////////////////////////////////////////////////////////////////////////////////
2 //    Scorched3D (c) 2000-2011
3 //
4 //    This file is part of Scorched3D.
5 //
6 //    Scorched3D is free software; you can redistribute it and/or modify
7 //    it under the terms of the GNU General Public License as published by
8 //    the Free Software Foundation; either version 2 of the License, or
9 //    (at your option) any later version.
10 //
11 //    Scorched3D is distributed in the hope that it will be useful,
12 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //    GNU General Public License for more details.
15 //
16 //    You should have received a copy of the GNU General Public License along
17 //    with this program; if not, write to the Free Software Foundation, Inc.,
18 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 #include <string>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <common/Defines.h>
25 #include <SDL/SDL.h>
26 #include <SDL/SDL_thread.h>
27 
trim(std::string & value)28 void S3D::trim(std::string &value)
29 {
30 	size_t start = value.find_first_not_of(" \t\n");
31 	size_t end = value.find_last_not_of(" \t\n");
32 	if (start == std::string::npos) value = "";
33 	else if (end == std::string::npos) value = "";
34 	else value = std::string(value, start, end-start+1);
35 }
36 
stristr(const char * x,const char * y)37 char *S3D::stristr(const char *x, const char *y)
38 {
39 	std::string newX(x);
40 	std::string newY(y);
41 	_strlwr((char *) newX.c_str());
42 	_strlwr((char *) newY.c_str());
43 
44 	char *result = (char *) strstr(newX.c_str(), newY.c_str());
45 	if (!result) return 0;
46 
47 	return (char *)(x + (result - newX.c_str()));
48 }
49 
50 #ifndef va_copy
51 # define va_copy(d, s)		(d) = (s)
52 #endif
53 
formatStringList(const char * format,va_list ap)54 std::string S3D::formatStringList(const char *format, va_list ap)
55 {
56 	int size = 256;
57 	char *p = new char[256];
58 	va_list ap_copy;
59 
60 	while (1)
61 	{
62 		/* Try to print in the allocated space. */
63 		va_copy(ap_copy, ap);
64 		int n = vsnprintf (p, size, format, ap_copy);
65 		va_end(ap_copy);
66 
67 		/* If that worked, return the string. */
68 		if (n > -1 && n < size) break;
69 
70 		/* Check size is within limits */
71 		if (size > 10 * 1024) break;
72 
73 		/* Else try again with more space. */
74 		if (n > -1)    /* glibc 2.1 */
75 			size = n+1; /* precisely what is needed */
76 		else           /* glibc 2.0 */
77 			size *= 2;  /* twice the old size */
78 
79 		/* Allocate more space */
80 		delete [] p;
81 		p = new char[size];
82 	}
83 
84 	std::string result(p);
85 	delete [] p;
86 
87 	return result;
88 }
89 
formatStringBuffer(const char * format,...)90 std::string S3D::formatStringBuffer(const char *format, ...)
91 {
92 	va_list ap;
93 	va_start(ap, format);
94 	std::string result = S3D::formatStringList(format, ap);
95 	va_end(ap);
96 
97 	return result;
98 }
99 
formatMoney(int amount)100 std::string S3D::formatMoney(int amount)
101 {
102 	if (abs(amount) < 1000)
103 	{
104 		return S3D::formatStringBuffer("$%i", amount);
105 	}
106 	else
107 	{
108 		int thou = amount / 1000;
109 		amount = abs(amount) - abs(thou * 1000);
110 		return S3D::formatStringBuffer("$%i,%03i", thou, amount);
111 	}
112 }
113