Skip to content

Commit 2a9622c

Browse files
committed
Add to string functions for double and int conversion
1 parent 8ee1f53 commit 2a9622c

File tree

18 files changed

+112
-64
lines changed

18 files changed

+112
-64
lines changed

coresdk/src/backend/gpio_driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ namespace splashkit_lib
740740
vector<char> buffer(num_send_bytes);
741741
memcpy(buffer.data(), &cmd, num_send_bytes);
742742

743-
if (sk_send_bytes(&pi->socket, buffer.data(), num_send_bytes))
743+
if (sk_send_bytes(&pi->socket, buffer, num_send_bytes))
744744
{
745745
int num_bytes_recv = sk_read_bytes(&pi->socket, buffer.data(), num_send_bytes);
746746
if (num_bytes_recv == num_send_bytes)

coresdk/src/backend/network_driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ namespace splashkit_lib
101101
return result;
102102
}
103103

104-
int sk_send_bytes(sk_network_connection *con, char *buffer, unsigned long size)
104+
int sk_send_bytes(sk_network_connection *con, std::vector<char> &buffer, unsigned long size)
105105
{
106106
// not entry point...
107107
// printf("Sending %d\n", size);
108108
int sent = 0;
109109
if ((TCPsocket)con->_socket)
110110
{
111111
//printf("here -- %p\n", (TCPsocket)con->_socket);
112-
sent = SDLNet_TCP_Send((TCPsocket)con->_socket, buffer, static_cast<int>(size));
112+
sent = SDLNet_TCP_Send((TCPsocket)con->_socket, static_cast<void*>(buffer.data()), static_cast<int>(size));
113113
}
114114
// printf("Sent %d\n", sent);
115115
return sent;

coresdk/src/backend/network_driver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace splashkit_lib
1818
sk_network_connection sk_open_udp_connection(unsigned short port);
1919
sk_network_connection sk_open_tcp_connection(const char *host, unsigned short port);
2020

21-
int sk_send_bytes(sk_network_connection *con, char *buffer, unsigned long size);
21+
int sk_send_bytes(sk_network_connection *con, std::vector<char> &buffer, unsigned long size);
2222

2323
int sk_send_udp_message(sk_network_connection *con, const char *host, unsigned short port, const char *buffer, unsigned long size);
2424
void sk_read_udp_message(sk_network_connection *con, unsigned int *host, unsigned short *port, char *buffer, unsigned long *size);

coresdk/src/backend/web_server_driver.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#include <iostream>
1515
#include <cstring>
1616

17-
using std::to_string;
18-
1917
namespace splashkit_lib
2018
{
2119
static map<unsigned short, sk_web_server*> servers;
@@ -198,7 +196,7 @@ namespace splashkit_lib
198196
server->port = port;
199197
server->last_request = nullptr;
200198

201-
string port_str = to_string(port);
199+
string port_str = std::to_string(port);
202200

203201
// List of options. Last element must be NULL.
204202
const char *options[] = {"listening_ports", port_str.c_str(), NULL};

coresdk/src/coresdk/animations.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ using std::string;
2525
using std::vector;
2626
using std::map;
2727
using std::ifstream;
28-
using std::to_string;
2928

3029
namespace splashkit_lib
3130
{
@@ -93,7 +92,7 @@ namespace splashkit_lib
9392

9493
if (rows[my_row.id].id != -1)
9594
{
96-
LOG(WARNING) << "Error at line " + to_string(line_no) + " in animation " + filename + ". A frame with id " + to_string(my_row.id) + " already exists.";
95+
LOG(WARNING) << "Error at line " + std::to_string(line_no) + " in animation " + filename + ". A frame with id " + std::to_string(my_row.id) + " already exists.";
9796
return false;
9897
}
9998
else
@@ -113,7 +112,7 @@ namespace splashkit_lib
113112
{
114113
if (ids[j].name == my_id.name)
115114
{
116-
LOG(WARNING) << "Error at line " + to_string(line_no) + " in animation " + filename + ". The id " + my_id.name + " already exists.";
115+
LOG(WARNING) << "Error at line " + std::to_string(line_no) + " in animation " + filename + ". The id " + my_id.name + " already exists.";
117116
return false;
118117
}
119118
}
@@ -128,7 +127,7 @@ namespace splashkit_lib
128127
//
129128
// if (count_delimiter(data, ',') != 3)
130129
// {
131-
// LOG(WARNING) << "Error at line " + to_string(line_no) + " in animation " + filename + ". A frame must have 4 values separated as id,cell,dur,next");
130+
// LOG(WARNING) << "Error at line " + std::to_string(line_no) + " in animation " + filename + ". A frame must have 4 values separated as id,cell,dur,next");
132131
// return false;
133132
// }
134133
//
@@ -150,7 +149,7 @@ namespace splashkit_lib
150149

151150
if ( count_delimiter_with_ranges(data, ',') != 3 )
152151
{
153-
LOG(WARNING) << "Error at line " + to_string(line_no) + " in animation " + filename + ". A multi-frame must have 4 values separated as id-range,cell-range,dur,next";
152+
LOG(WARNING) << "Error at line " + std::to_string(line_no) + " in animation " + filename + ". A multi-frame must have 4 values separated as id-range,cell-range,dur,next";
154153
return false;
155154
}
156155

@@ -159,7 +158,7 @@ namespace splashkit_lib
159158

160159
if (id_range.size() != cell_range.size())
161160
{
162-
LOG(WARNING) << "Error at line " + to_string(line_no) + " in animation " + filename + ". The range of cells and ids is not the same length.";
161+
LOG(WARNING) << "Error at line " + std::to_string(line_no) + " in animation " + filename + ". The range of cells and ids is not the same length.";
163162
return false;
164163
}
165164

@@ -191,7 +190,7 @@ namespace splashkit_lib
191190

192191
if (count_delimiter_with_ranges(data, ',') != 1)
193192
{
194-
LOG(WARNING) << "Error at line " + to_string(line_no) + " in animation " + filename + ". An id must have 2 values separated as name,start-id";
193+
LOG(WARNING) << "Error at line " + std::to_string(line_no) + " in animation " + filename + ". An id must have 2 values separated as name,start-id";
195194
return false;
196195
}
197196

@@ -208,7 +207,7 @@ namespace splashkit_lib
208207

209208
if (count_delimiter(data, ',') != 2)
210209
{
211-
LOG(WARNING) << "Error at line " + to_string(line_no) + " in animation " + filename + ". A sound must have three parts frame #,sound name,sound file.";
210+
LOG(WARNING) << "Error at line " + std::to_string(line_no) + " in animation " + filename + ". A sound must have three parts frame #,sound name,sound file.";
212211
return;
213212
}
214213

@@ -220,15 +219,15 @@ namespace splashkit_lib
220219
{
221220
if(load_sound_effect(snd_id, snd_file) == nullptr)
222221
{
223-
LOG(WARNING) << "At line " + to_string(line_no) + " in animation " + filename + ": Cannot find " + snd_id + " sound file " + snd_file;
222+
LOG(WARNING) << "At line " + std::to_string(line_no) + " in animation " + filename + ": Cannot find " + snd_id + " sound file " + snd_file;
224223
return;
225224
}
226225
}
227226

228227
if (id >= 0 && id < rows.size())
229228
rows[id].snd = sound_effect_named(snd_id);
230229
else
231-
LOG(WARNING) << "At line " + to_string(line_no) + " in animation " + filename + ": No frame with id " + to_string(id) + " for sound file " + snd_file;
230+
LOG(WARNING) << "At line " + std::to_string(line_no) + " in animation " + filename + ": No frame with id " + std::to_string(id) + " for sound file " + snd_file;
232231
};
233232

234233
auto process_vector = [&]()
@@ -240,7 +239,7 @@ namespace splashkit_lib
240239

241240
if (count_delimiter(data, ',') != 2)
242241
{
243-
LOG(WARNING) << "Error at line " + to_string(line_no) + " in animation " + filename + ". A vector must have three parts frame #s, x value, y value.";
242+
LOG(WARNING) << "Error at line " + std::to_string(line_no) + " in animation " + filename + ". A vector must have three parts frame #s, x value, y value.";
244243
return;
245244
}
246245

@@ -250,13 +249,13 @@ namespace splashkit_lib
250249

251250
if (not try_str_to_double(x_val, x))
252251
{
253-
LOG(WARNING) << "Error at line " + to_string(line_no) + " in animation " + filename + ". X value must be a number.";
252+
LOG(WARNING) << "Error at line " + std::to_string(line_no) + " in animation " + filename + ". X value must be a number.";
254253
return;
255254
}
256255

257256
if (not try_str_to_double(y_val, y))
258257
{
259-
LOG(WARNING) << "Error at line " + to_string(line_no) + " in animation " + filename + ". Y value must be a number.";
258+
LOG(WARNING) << "Error at line " + std::to_string(line_no) + " in animation " + filename + ". Y value must be a number.";
260259
return;
261260
}
262261

@@ -282,7 +281,7 @@ namespace splashkit_lib
282281
// Verify that id is a single char
283282
if (line_id.length() != 1)
284283
{
285-
LOG(WARNING) << "Error at line " + to_string(line_no) + " in animation " + filename + ". Error with frame #: " + line_id + ". This should be a single character.";
284+
LOG(WARNING) << "Error at line " + std::to_string(line_no) + " in animation " + filename + ". Error with frame #: " + line_id + ". This should be a single character.";
286285
return;
287286
}
288287

@@ -305,7 +304,7 @@ namespace splashkit_lib
305304
process_vector();
306305
break;
307306
default:
308-
LOG(WARNING) << "Error at line " + to_string(line_no) + " in animation " + filename + ". Error with id: " + line_id + ". This should be one of f,m,i, s, or v.";
307+
LOG(WARNING) << "Error at line " + std::to_string(line_no) + " in animation " + filename + ". Error with id: " + line_id + ". This should be one of f,m,i, s, or v.";
309308
return;
310309
}
311310
};
@@ -344,7 +343,7 @@ namespace splashkit_lib
344343
{
345344
free_animation_script(result);
346345
result = nullptr;
347-
LOG(WARNING) << "Error in animation " + filename + ". Error with frame: " + to_string(j) + ". Next is outside of available frames.";
346+
LOG(WARNING) << "Error in animation " + filename + ". Error with frame: " + std::to_string(j) + ". Next is outside of available frames.";
348347
return;
349348
}
350349
else
@@ -409,7 +408,7 @@ namespace splashkit_lib
409408
if (sum_loop(current) == 0)
410409
{
411410
free_animation_script(result);
412-
LOG(WARNING) << "Error in animation " + filename + ". Animation contains a loop with duration 0 starting at cell " + to_string(current->index);
411+
LOG(WARNING) << "Error in animation " + filename + ". Animation contains a loop with duration 0 starting at cell " + std::to_string(current->index);
413412
return;
414413
}
415414
}
@@ -670,7 +669,7 @@ namespace splashkit_lib
670669

671670
if ( idx < 0 or idx >= temp->animation_names.size())
672671
{
673-
LOG(WARNING) << "Attempting to get an animation that is not within range 0-" + to_string(temp->animations.size()-1) + ".";
672+
LOG(WARNING) << "Attempting to get an animation that is not within range 0-" + std::to_string(temp->animations.size()-1) + ".";
674673
return "";
675674
}
676675

@@ -699,7 +698,7 @@ namespace splashkit_lib
699698

700699
if ((idx < 0) or (idx >= script->animations.size()))
701700
{
702-
LOG(WARNING) << "Assigning an animation frame that is not within range 0-" + to_string(script->animations.size()-1) + ".";
701+
LOG(WARNING) << "Assigning an animation frame that is not within range 0-" + std::to_string(script->animations.size()-1) + ".";
703702
return;
704703
}
705704

@@ -790,7 +789,7 @@ namespace splashkit_lib
790789

791790
if ((idx < 0) or (idx >= script->animations.size()))
792791
{
793-
LOG(WARNING) << "Unable to create animation number " + to_string(idx) + " from script " + script->name;
792+
LOG(WARNING) << "Unable to create animation number " + std::to_string(idx) + " from script " + script->name;
794793
return result;
795794
}
796795

coresdk/src/coresdk/basics.cpp

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,23 @@ namespace splashkit_lib
162162
{
163163
return std::stod(text);
164164
}
165+
166+
string to_string(int value)
167+
{
168+
return std::to_string(value);
169+
}
170+
171+
string to_string(double value)
172+
{
173+
return std::to_string(value);
174+
}
175+
176+
string to_string(double value, int precision)
177+
{
178+
std::stringstream stream;
179+
stream << std::fixed << std::setprecision(precision) << value;
180+
return stream.str();
181+
}
165182

166183
bool is_binary(const string &bin_str)
167184
{
@@ -216,7 +233,25 @@ namespace splashkit_lib
216233
return 0;
217234
}
218235

219-
return stoi(bin_str, nullptr, 2);
236+
try
237+
{
238+
LOG(INFO) << bin_str.length() << " " << sizeof(int) * 8;
239+
240+
if (bin_str.length() > sizeof(int) * 8)
241+
{
242+
return std::numeric_limits<unsigned int>::max();
243+
}
244+
else
245+
{
246+
return stol(bin_str, nullptr, 2);
247+
}
248+
}
249+
catch(const std::exception& e)
250+
{
251+
LOG(ERROR) << "Invalid binary string \"" << bin_str << "\" passed to bin_to_dec. Returning 0.";
252+
return 0;
253+
}
254+
220255
}
221256

222257
string hex_to_bin(const string &hex_str)
@@ -310,7 +345,7 @@ namespace splashkit_lib
310345
return 0;
311346
}
312347

313-
return stoi(octal_string, nullptr, 8);
348+
return stol(octal_string, nullptr, 8);
314349
}
315350

316351
unsigned int hex_to_dec(const string &hex_string)
@@ -321,7 +356,7 @@ namespace splashkit_lib
321356
return 0;
322357
}
323358

324-
return stoi(hex_string, nullptr, 16);
359+
return stol(hex_string, nullptr, 16);
325360
}
326361

327362
string oct_to_bin(const string &octal_str)

coresdk/src/coresdk/basics.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,34 @@ namespace splashkit_lib
111111
*/
112112
double to_double(const string &text);
113113

114+
/**
115+
* Convert the passed in integer to a string.
116+
*
117+
* @param value the value to convert to a string.
118+
* @return string containing the integer value.
119+
*/
120+
string to_string(int value);
121+
122+
/**
123+
* Convert the passed in double to a string.
124+
*
125+
* @param value the value to convert to a string.
126+
* @return string containing the double value.
127+
*/
128+
string to_string(double value);
129+
130+
/**
131+
* Convert the passed in double to a string, restricting
132+
* the output the a given number of decimal places. For example
133+
* if value is 3.14159265359 and precision is 2 then the output
134+
* will be "3.14".
135+
*
136+
* @param value the value to convert to a string.
137+
* @param precision the number of decimal places to output.
138+
* @return string containing the double value.
139+
*/
140+
string to_string(double value, int precision);
141+
114142
/**
115143
* Returns the length of a string in characters.
116144
*

coresdk/src/coresdk/bundles.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <fstream>
2323

2424
using std::ifstream;
25-
using std::to_string;
2625

2726
namespace splashkit_lib
2827
{
@@ -91,7 +90,7 @@ namespace splashkit_lib
9190
int num_delim = count_delimiter(line, ',');
9291
if ( num_delim > 2 and num_delim != 7 )
9392
{
94-
LOG(WARNING) << "Incorrect cell options for bitmap " + line_name + " at " + to_string(line_no) + " of bundle " + name;
93+
LOG(WARNING) << "Incorrect cell options for bitmap " + line_name + " at " + std::to_string(line_no) + " of bundle " + name;
9594
return;
9695
}
9796
else if ( num_delim == 2 ) return;
@@ -114,19 +113,19 @@ namespace splashkit_lib
114113

115114
if ( kind == OTHER_RESOURCE )
116115
{
117-
LOG(WARNING) << "Unknown resource type at line " + to_string(line_no) + " of bundle " + name;
116+
LOG(WARNING) << "Unknown resource type at line " + std::to_string(line_no) + " of bundle " + name;
118117
return;
119118
}
120119

121120
if ( line_name.length() == 0 )
122121
{
123-
LOG(WARNING) << "Name missing for resource at line " + to_string(line_no) + " of bundle " + name;
122+
LOG(WARNING) << "Name missing for resource at line " + std::to_string(line_no) + " of bundle " + name;
124123
return;
125124
}
126125

127126
if ( line_path.length() == 0 && kind != TIMER_RESOURCE )
128127
{
129-
LOG(WARNING) << "Name missing for resource at line " + to_string(line_no) + " of bundle " + name;
128+
LOG(WARNING) << "Name missing for resource at line " + std::to_string(line_no) + " of bundle " + name;
130129
return;
131130
}
132131

0 commit comments

Comments
 (0)