commit f6707482
Name is now stored in a std::string. Next goal: UTF-8 codepoints
Changed files
diff --git a/source/code/ReadKeyboard.cpp b/source/code/ReadKeyboard.cpp
index a7860de..a3cf772 100644
--- a/source/code/ReadKeyboard.cpp
+++ b/source/code/ReadKeyboard.cpp
@@ -27,10 +27,8 @@ http://blockattack.sf.net
using namespace std;
ReadKeyboard::ReadKeyboard(void) {
- length = 0;
maxLength = 16;
position = 0;
- strcpy(textstring," ");
}
ReadKeyboard::~ReadKeyboard(void) {
@@ -41,50 +39,24 @@ Uint8 ReadKeyboard::CharsBeforeCursor() {
}
ReadKeyboard::ReadKeyboard(const char* oldName) {
- length = 0;
maxLength = 16;
position = 0;
- strcpy(textstring," ");
- strncpy(textstring,oldName,strlen(oldName));
- char charecter = textstring[maxLength+1];
- int i = maxLength+1;
- while ((charecter == ' ') && (i>0)) {
- i--;
- charecter = textstring[i];
- }
- if (i>0) {
- length = i+1;
- }
- else if (charecter == ' ') {
- length = 0;
- }
- else {
- length = 1;
- }
- position = length;
-
+ text_string = oldName;
+ position = text_string.length();
}
void ReadKeyboard::putchar(char thing) {
- if (length < maxLength) {
- for (int i = 28; i>position; i--) {
- textstring[i]=textstring[i-1];
- }
- textstring[position] = thing;
- length++;
+ if (text_string.length() < maxLength) {
+ text_string.insert(text_string.begin()+position, thing);
position++;
}
}
void ReadKeyboard::removeChar() {
- for (int i = position; i<28; i++) {
- textstring[i]=textstring[i+1];
- }
- textstring[28]=' ';
- if (length>0) {
- length--;
+ if (position < text_string.length()) {
+ text_string.erase(position);
}
}
@@ -108,7 +80,7 @@ bool ReadKeyboard::ReadKey(SDLKey keyPressed) {
return true;
}
if (keyPressed == SDLK_DELETE) {
- if ((length>0)&& (position<length)) {
+ if ((text_string.length()>0)&& (position<text_string.length())) {
ReadKeyboard::removeChar();
}
return true;
@@ -126,14 +98,14 @@ bool ReadKeyboard::ReadKey(SDLKey keyPressed) {
return true;
}
if (keyPressed == SDLK_END) {
- position=length;
+ position=text_string.length();
return true;
}
if ((keyPressed == SDLK_LEFT) && (position>0)) {
position--;
return true;
}
- if ((keyPressed == SDLK_RIGHT) && (position<length)) {
+ if ((keyPressed == SDLK_RIGHT) && (position<text_string.length())) {
position++;
return true;
}
@@ -141,6 +113,5 @@ bool ReadKeyboard::ReadKey(SDLKey keyPressed) {
}
const char* ReadKeyboard::GetString() {
- textstring[29]='\0';
- return &textstring[0];
+ return text_string.c_str();
}
diff --git a/source/code/ReadKeyboard.h b/source/code/ReadKeyboard.h
index b1b2738..8a46b8d 100644
--- a/source/code/ReadKeyboard.h
+++ b/source/code/ReadKeyboard.h
@@ -26,12 +26,13 @@ Added to project 5/11-2004
*/
#include "SDL.h"
+#include <string>
class ReadKeyboard
{
private:
- int length, maxLength, position;
- char textstring[30];
+ int maxLength, position;
+ std::string text_string;
void putchar(char);
void removeChar();
public:
diff --git a/source/code/global.hpp b/source/code/global.hpp
index ab5bcff..4f9e1c0 100644
--- a/source/code/global.hpp
+++ b/source/code/global.hpp
@@ -37,8 +37,8 @@ extern NFont nf_scoreboard_font;
extern bool MusicEnabled; //true if background music is enabled
extern bool SoundEnabled; //true if sound effects is enabled
extern bool bFullscreen; //true if game is running fullscreen
-extern char player1name[30];
-extern char player2name[30];
+extern std::string player1name;
+extern std::string player2name;
extern SDL_Surface *screen; //The whole screen;
extern std::shared_ptr<CppSdl::CppSdlImageHolder> mouse;
extern SDL_Surface* backgroundImage;
diff --git a/source/code/main.cpp b/source/code/main.cpp
index a9f5074..7e11d07 100644
--- a/source/code/main.cpp
+++ b/source/code/main.cpp
@@ -1404,10 +1404,10 @@ void MakeBackground(int xsize,int ysize,BlockGame& theGame, BlockGame& theGame2)
//Dialogbox
-bool OpenDialogbox(int x, int y, char* name) {
+bool OpenDialogbox(int x, int y, std::string& name) {
bool done = false; //We are done!
bool accept = false; //New name is accepted! (not Cancelled)
- ReadKeyboard rk = ReadKeyboard(name);
+ ReadKeyboard rk = ReadKeyboard(name.c_str());
string strHolder;
MakeBackground(xsize,ysize);
DrawIMG(background,screen,0,0);
@@ -1456,7 +1456,7 @@ bool OpenDialogbox(int x, int y, char* name) {
SDL_Flip(screen); //Update screen
} //while(!done)
- strcpy(name,rk.GetString());
+ name = rk.GetString();
bScreenLocked = false;
showDialog = false;
unicodeScope.Release();
@@ -2722,8 +2722,8 @@ int main(int argc, char* argv[]) {
player1keys=0;
player2keys=2;
- strcpy(player1name, "Player 1 \0");
- strcpy(player2name, "Player 2 \0");
+ player1name = "Player 1";
+ player2name = "Player 2";
Config* configSettings = Config::getInstance();
//configSettings->setString("aNumber"," A string");
@@ -2775,55 +2775,18 @@ int main(int argc, char* argv[]) {
keySettings[2].push = (SDLKey)configSettings->getInt("player2keypush");
}
if (configSettings->exists("player1name")) {
- strncpy(player1name,(configSettings->getString("player1name")).c_str(),28);
+ player1name = configSettings->getString("player1name");
}
if (configSettings->exists("player2name")) {
- strncpy(player2name,(configSettings->getString("player2name")).c_str(),28);
+ player2name = configSettings->getString("player2name");
}
if (verboseLevel) {
cout << "Data loaded from config file" << endl;
}
}
else {
- //Reads options from file:
- ifstream optionsFile(optionsPath.c_str(), ios::binary);
- if (optionsFile) {
- //reads data: xsize,ysize,fullescreen, player1keys, player2keys, MusicEnabled, SoundEnabled,player1name,player2name
- optionsFile.read(reinterpret_cast<char*>(&xsize), sizeof(int));
- optionsFile.read(reinterpret_cast<char*>(&ysize), sizeof(int));
- optionsFile.read(reinterpret_cast<char*>(&bFullscreen), sizeof(bool));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[0].up), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[0].down), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[0].left), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[0].right), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[0].change), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[0].push), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[2].up), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[2].down), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[2].left), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[2].right), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[2].change), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&keySettings[2].push), sizeof(SDLKey));
- optionsFile.read(reinterpret_cast<char*>(&MusicEnabled), sizeof(bool));
- optionsFile.read(reinterpret_cast<char*>(&SoundEnabled), sizeof(bool));
- optionsFile.read(player1name, 30*sizeof(char));
- optionsFile.read(player2name, 30*sizeof(char));
- //mouseplay?
- if (!optionsFile.eof()) {
- optionsFile.read(reinterpret_cast<char*>(&mouseplay1), sizeof(bool));
- optionsFile.read(reinterpret_cast<char*>(&mouseplay2), sizeof(bool));
- optionsFile.read(reinterpret_cast<char*>(&joyplay1),sizeof(bool));
- optionsFile.read(reinterpret_cast<char*>(&joyplay2),sizeof(bool));
- }
- optionsFile.close();
- if (verboseLevel) {
- cout << "Data loaded from oldstyle options file" << endl;
- }
- }
- else {
- if (verboseLevel) {
- cout << "Unable to load options file, using default values" << endl;
- }
+ if (verboseLevel) {
+ cout << "Unable to load options file, using default values" << endl;
}
}
diff --git a/source/code/mainVars.hpp b/source/code/mainVars.hpp
index a0faa76..f0ad0c9 100644
--- a/source/code/mainVars.hpp
+++ b/source/code/mainVars.hpp
@@ -197,8 +197,8 @@ static int ysize = 768;
static const int bsize = 50;
//Stores the players names (way to long, but at least no buffer overflows (max length is 16 for display reasons))
-char player1name[30];
-char player2name[30];
+std::string player1name;
+std::string player2name;
//paths
static std::string stageClearSavePath;
diff --git a/source/code/menudef.cpp b/source/code/menudef.cpp
index 710c995..6fca97d 100644
--- a/source/code/menudef.cpp
+++ b/source/code/menudef.cpp
@@ -44,7 +44,7 @@ struct control {
SDLKey push;
};
-bool OpenDialogbox(int x, int y, char* name);
+bool OpenDialogbox(int x, int y, std::string& name);
void OpenScoresDisplay();
extern control keySettings[3];
@@ -163,10 +163,9 @@ static void buttonActionPlayer2Name(Button* b) {
}
static void buttonActionPortChange(Button* b) {
- char port[30];
- snprintf(port,sizeof(port),"%i",Config::getInstance()->getInt("portv4") );
- if (OpenDialogbox(200,100,port) && atoi(port)) {
- Config::getInstance()->setInt("portv4",atoi(port));
+ string port = Config::getInstance()->getString("portv4");
+ if (OpenDialogbox(200,100,port) && atoi(port.c_str())) {
+ Config::getInstance()->setInt("portv4",atoi(port.c_str()));
boost::format f(_("Port: %1%") );
f % port;
b->setLabel(f.str());
@@ -174,8 +173,7 @@ static void buttonActionPortChange(Button* b) {
}
static void buttonActionIpChange(Button* b) {
- char ip[30];
- snprintf(ip,sizeof(ip),"%s",Config::getInstance()->getString("address0").c_str() );
+ string ip = Config::getInstance()->getString("address0");
if (OpenDialogbox(200,100,ip)) {
Config::getInstance()->setString("address0",ip);
boost::format f(_("Address: %1%") );