From c03f452931adede1ad3f654a8329bbb58f5e4c61 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tobias=20Gl=C3=A4=C3=9Fer?= Date: Fri, 6 Aug 2004 11:43:10 +0000 Subject: [PATCH] Converted many char*s to std::strings. This is providing more safety from buffer overflows and anyway more straightforward string handling. SVN-Revision: 1715 --- lib/app/globals.cpp | 2 +- lib/app/globals.h | 4 +- lib/app/setup.cpp | 121 +++++++++++++++++++++-------------------------- lib/app/setup.h | 10 ++-- lib/utils/configfile.cpp | 17 ++----- lib/utils/configfile.h | 2 +- src/gameloop.cpp | 26 +++++----- src/level_subset.cpp | 16 +++---- src/leveleditor.cpp | 4 +- src/title.cpp | 16 ++++--- 10 files changed, 102 insertions(+), 116 deletions(-) diff --git a/lib/app/globals.cpp b/lib/app/globals.cpp index b363ec09c..0a328d4b5 100644 --- a/lib/app/globals.cpp +++ b/lib/app/globals.cpp @@ -58,7 +58,7 @@ bool launch_worldmap_mode = false; bool flip_levels_mode = false; /* SuperTux directory ($HOME/.supertux) and save directory($HOME/.supertux/save) */ -char *st_dir, *st_save_dir; +std::string st_dir, st_save_dir; SDL_Joystick * js; diff --git a/lib/app/globals.h b/lib/app/globals.h index 2894b2b38..246e1abc4 100644 --- a/lib/app/globals.h +++ b/lib/app/globals.h @@ -72,8 +72,8 @@ namespace SuperTux extern bool flip_levels_mode; /* SuperTux directory ($HOME/.supertux) and save directory($HOME/.supertux/save) */ - extern char* st_dir; - extern char* st_save_dir; + extern std::string st_dir; + extern std::string st_save_dir; extern SDL_Joystick * js; diff --git a/lib/app/setup.cpp b/lib/app/setup.cpp index 75dd5f5e4..5ddcb0198 100644 --- a/lib/app/setup.cpp +++ b/lib/app/setup.cpp @@ -65,10 +65,10 @@ void seticon(void); void usage(char * prog, int ret); /* Does the given file exist and is it accessible? */ -int FileSystem::faccessible(const char *filename) +int FileSystem::faccessible(const std::string& filename) { struct stat filestat; - if (stat(filename, &filestat) == -1) + if (stat(filename.c_str(), &filestat) == -1) { return false; } @@ -82,10 +82,10 @@ int FileSystem::faccessible(const char *filename) } /* Can we write to this location? */ -int FileSystem::fwriteable(const char *filename) +int FileSystem::fwriteable(const std::string& filename) { FILE* fi; - fi = fopen(filename, "wa"); + fi = fopen(filename.c_str(), "wa"); if (fi == NULL) { return false; @@ -94,14 +94,13 @@ int FileSystem::fwriteable(const char *filename) } /* Makes sure a directory is created in either the SuperTux home directory or the SuperTux base directory.*/ -int FileSystem::fcreatedir(const char* relative_dir) +int FileSystem::fcreatedir(const std::string& relative_dir) { - char path[1024]; - snprintf(path, 1024, "%s/%s/", st_dir, relative_dir); - if(mkdir(path,0755) != 0) + std::string path = st_dir + "/" + relative_dir + "/"; + if(mkdir(path.c_str(),0755) != 0) { - snprintf(path, 1024, "%s/%s/", datadir.c_str(), relative_dir); - if(mkdir(path,0755) != 0) + path = datadir + "/" + relative_dir + "/"; + if(mkdir(path.c_str(),0755) != 0) { return false; } @@ -119,29 +118,28 @@ int FileSystem::fcreatedir(const char* relative_dir) /* Get all names of sub-directories in a certain directory. */ /* Returns the number of sub-directories found. */ /* Note: The user has to free the allocated space. */ -std::set FileSystem::dsubdirs(const char *rel_path,const char* expected_file) +std::set FileSystem::dsubdirs(const std::string &rel_path,const std::string& expected_file) { DIR *dirStructP; struct dirent *direntp; std::set sdirs; - char filename[1024]; - char path[1024]; + std::string filename; + std::string path = st_dir + "/" + rel_path; - sprintf(path,"%s/%s",st_dir,rel_path); - if((dirStructP = opendir(path)) != NULL) + if((dirStructP = opendir(path.c_str())) != NULL) { while((direntp = readdir(dirStructP)) != NULL) { - char absolute_filename[1024]; + std::string absolute_filename; struct stat buf; - sprintf(absolute_filename, "%s/%s", path, direntp->d_name); + absolute_filename = path + "/" + direntp->d_name; - if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode)) + if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode)) { - if(expected_file != NULL) + if(!expected_file.empty()) { - sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file); + filename = path + "/" + direntp->d_name + "/" + expected_file; if(!faccessible(filename)) continue; } @@ -152,29 +150,29 @@ std::set FileSystem::dsubdirs(const char *rel_path,const char* exp closedir(dirStructP); } - sprintf(path,"%s/%s",datadir.c_str(),rel_path); - if((dirStructP = opendir(path)) != NULL) + path = datadir + "/" + rel_path; + if((dirStructP = opendir(path.c_str())) != NULL) { while((direntp = readdir(dirStructP)) != NULL) { - char absolute_filename[1024]; + std::string absolute_filename; struct stat buf; - sprintf(absolute_filename, "%s/%s", path, direntp->d_name); + absolute_filename = path + "/" + direntp->d_name; - if (stat(absolute_filename, &buf) == 0 && S_ISDIR(buf.st_mode)) + if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode)) { - if(expected_file != NULL) + if(!expected_file.empty()) { - sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file); - if(!faccessible(filename)) + filename = path + "/" + direntp->d_name + "/" + expected_file; + if(!faccessible(filename.c_str())) { continue; } else { - sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file); - if(faccessible(filename)) + filename = st_dir + "/" + rel_path + "/" + direntp->d_name + "/" + expected_file; + if(faccessible(filename.c_str())) continue; } } @@ -188,32 +186,31 @@ std::set FileSystem::dsubdirs(const char *rel_path,const char* exp return sdirs; } -std::set FileSystem::dfiles(const char *rel_path, const char* glob, const char* exception_str) +std::set FileSystem::dfiles(const std::string& rel_path, const std::string& glob, const std::string& exception_str) { DIR *dirStructP; struct dirent *direntp; std::set sdirs; - char path[1024]; + std::string path = st_dir + "/" + rel_path; - sprintf(path,"%s/%s",st_dir,rel_path); - if((dirStructP = opendir(path)) != NULL) + if((dirStructP = opendir(path.c_str())) != NULL) { while((direntp = readdir(dirStructP)) != NULL) { - char absolute_filename[1024]; + std::string absolute_filename; struct stat buf; - sprintf(absolute_filename, "%s/%s", path, direntp->d_name); + absolute_filename = path + "/" + direntp->d_name; - if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode)) + if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISREG(buf.st_mode)) { - if(exception_str != NULL) + if(!exception_str.empty()) { - if(strstr(direntp->d_name,exception_str) != NULL) + if(strstr(direntp->d_name,exception_str.c_str()) != NULL) continue; } - if(glob != NULL) - if(strstr(direntp->d_name,glob) == NULL) + if(!glob.empty()) + if(strstr(direntp->d_name,glob.c_str()) == NULL) continue; sdirs.insert(direntp->d_name); @@ -222,25 +219,25 @@ std::set FileSystem::dfiles(const char *rel_path, const char* glob closedir(dirStructP); } - sprintf(path,"%s/%s",datadir.c_str(),rel_path); - if((dirStructP = opendir(path)) != NULL) + path = datadir + "/" + rel_path; + if((dirStructP = opendir(path.c_str())) != NULL) { while((direntp = readdir(dirStructP)) != NULL) { - char absolute_filename[1024]; + std::string absolute_filename; struct stat buf; - sprintf(absolute_filename, "%s/%s", path, direntp->d_name); + absolute_filename = path + "/" + direntp->d_name; - if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode)) + if (stat(absolute_filename.c_str(), &buf) == 0 && S_ISREG(buf.st_mode)) { - if(exception_str != NULL) + if(!exception_str.empty()) { - if(strstr(direntp->d_name,exception_str) != NULL) + if(strstr(direntp->d_name,exception_str.c_str()) != NULL) continue; } - if(glob != NULL) - if(strstr(direntp->d_name,glob) == NULL) + if(!glob.empty()) + if(strstr(direntp->d_name,glob.c_str()) == NULL) continue; sdirs.insert(direntp->d_name); @@ -263,8 +260,7 @@ package_version = _package_version; /* Set SuperTux configuration and save directories */ void Setup::directories(void) { - char *home; - char str[1024]; + std::string home; /* Get home directory (from $HOME variable)... if we can't determine it, use the current directory ("."): */ if (getenv("HOME") != NULL) @@ -272,30 +268,21 @@ void Setup::directories(void) else home = "."; - std::string st_dir_tmp = "/." + package_symbol_name; - st_dir = (char *) malloc(sizeof(char) * (strlen(home) + - strlen(st_dir_tmp.c_str()) + 1)); - strcpy(st_dir, home); - strcat(st_dir,st_dir_tmp.c_str()); + st_dir = home + "/." + package_symbol_name; /* Remove .supertux config-file from old SuperTux versions */ if(FileSystem::faccessible(st_dir)) { - remove - (st_dir); + remove(st_dir.c_str()); } - st_save_dir = (char *) malloc(sizeof(char) * (strlen(st_dir) + strlen("/save") + 1)); - - strcpy(st_save_dir,st_dir); - strcat(st_save_dir,"/save"); + st_save_dir = st_dir + "/save"; /* Create them. In the case they exist they won't destroy anything. */ - mkdir(st_dir, 0755); - mkdir(st_save_dir, 0755); + mkdir(st_dir.c_str(), 0755); + mkdir(st_save_dir.c_str(), 0755); - sprintf(str, "%s/levels", st_dir); - mkdir(str, 0755); + mkdir((st_dir + "/levels").c_str(), 0755); // User has not that a datadir, so we try some magic if (datadir.empty()) diff --git a/lib/app/setup.h b/lib/app/setup.h index 4c9146402..3acca6c88 100644 --- a/lib/app/setup.h +++ b/lib/app/setup.h @@ -31,12 +31,12 @@ namespace SuperTux { /// File system utility functions struct FileSystem { - static int faccessible(const char *filename); - static int fcreatedir(const char* relative_dir); - static int fwriteable(const char *filename); + static int faccessible(const std::string& filename); + static int fcreatedir(const std::string& relative_dir); + static int fwriteable(const std::string& filename); static std::set read_directory(const std::string& pathname); - static std::set dsubdirs(const char *rel_path, const char* expected_file); - static std::set dfiles(const char *rel_path, const char* glob, const char* exception_str); + static std::set dsubdirs(const std::string& rel_path, const std::string& expected_file); + static std::set dfiles(const std::string& rel_path, const std::string& glob, const std::string& exception_str); }; /// All you need to get an application up and running diff --git a/lib/utils/configfile.cpp b/lib/utils/configfile.cpp index 31a350230..bbbb501d5 100644 --- a/lib/utils/configfile.cpp +++ b/lib/utils/configfile.cpp @@ -49,32 +49,25 @@ static void defaults () SoundManager::get()->enable_music(true); } -FILE * SuperTux::opendata(const char * rel_filename, const char * mode) +FILE * SuperTux::opendata(const std::string& rel_filename, const char *mode) { - char * filename = NULL; + std::string filename; FILE * fi; - filename = (char *) malloc(sizeof(char) * (strlen(st_dir) + - strlen(rel_filename) + 1)); - - strcpy(filename, st_dir); - /* Open the high score file: */ - - strcat(filename, rel_filename); + filename = st_dir + rel_filename; /* Try opening the file: */ - fi = fopen(filename, mode); + fi = fopen(filename.c_str(), mode); if (fi == NULL) { - fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename); + fprintf(stderr, "Warning: Unable to open the file \"%s\" ", filename.c_str()); if (strcmp(mode, "r") == 0) fprintf(stderr, "for read!!!\n"); else if (strcmp(mode, "w") == 0) fprintf(stderr, "for write!!!\n"); } - free( filename ); return(fi); } diff --git a/lib/utils/configfile.h b/lib/utils/configfile.h index a6471cc42..17ab2c378 100644 --- a/lib/utils/configfile.h +++ b/lib/utils/configfile.h @@ -24,7 +24,7 @@ namespace SuperTux { -FILE * opendata(const char * filename, const char * mode); +FILE * opendata(const std::string& filename, const char * mode); class Config { public: diff --git a/src/gameloop.cpp b/src/gameloop.cpp index 740663e0a..97cecb9a8 100644 --- a/src/gameloop.cpp +++ b/src/gameloop.cpp @@ -20,6 +20,7 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include +#include #include #include #include @@ -799,12 +800,14 @@ GameSession::drawresultscreen(void) std::string slotinfo(int slot) { - char tmp[1024]; - char slotfile[1024]; + std::string tmp; + std::string slotfile; std::string title; - sprintf(slotfile,"%s/slot%d.stsg",st_save_dir,slot); + std::stringstream stream; + stream << slot; + slotfile = st_save_dir + "/slot" + stream.str() + ".stsg"; - lisp_object_t* savegame = lisp_read_from_file(slotfile); + lisp_object_t* savegame = lisp_read_from_file(slotfile.c_str()); if (savegame) { LispReader reader(lisp_cdr(savegame)); @@ -812,15 +815,15 @@ std::string slotinfo(int slot) lisp_free(savegame); } - if (access(slotfile, F_OK) == 0) + if (access(slotfile.c_str(), F_OK) == 0) { if (!title.empty()) - snprintf(tmp,1024,"Slot %d - %s",slot, title.c_str()); + tmp = "Slot " + stream.str() + " - " + title; else - snprintf(tmp, 1024,_("Slot %d - Savegame"),slot); + tmp = "Slot " + stream.str() + " - Savegame"; } else - sprintf(tmp,_("Slot %d - Free"),slot); + tmp = std::string(_("Slot")) + " " + stream.str() + " - " + std::string(_("Free")); return tmp; } @@ -831,10 +834,11 @@ bool process_load_game_menu() if(slot != -1 && load_game_menu->get_item_by_id(slot).kind == MN_ACTION) { - char slotfile[1024]; - snprintf(slotfile, 1024, "%s/slot%d.stsg", st_save_dir, slot); + std::stringstream stream; + stream << slot; + std::string slotfile = st_save_dir + "/slot" + stream.str() + ".stsg"; - if (access(slotfile, F_OK) != 0) + if (access(slotfile.c_str(), F_OK) != 0) { draw_intro(); } diff --git a/src/level_subset.cpp b/src/level_subset.cpp index c364e6b5a..704ced19f 100644 --- a/src/level_subset.cpp +++ b/src/level_subset.cpp @@ -82,16 +82,16 @@ void LevelSubset::load(const char* subset) // Check in which directory our subset is located (ie. ~/.supertux/ // or SUPERTUX_DATADIR) - char filename[1024]; - snprintf(filename, 1024, "%s/levels/%s/", st_dir, subset); - if (access(filename, R_OK) == 0) + std::string filename; + filename = st_dir + "/levels/" + subset + "/"; + if (access(filename.c_str(), R_OK) == 0) { directory = filename; } else { - snprintf(filename, 1024, "%s/levels/%s/", datadir.c_str(), subset); - if (access(filename, R_OK) == 0) + filename = datadir + "/levels/" + subset + "/"; + if (access(filename.c_str(), R_OK) == 0) directory = filename; else std::cout << "Error: LevelSubset: couldn't find subset: " << subset << std::endl; @@ -104,14 +104,14 @@ void LevelSubset::load(const char* subset) // directory to see what we can find std::set files; - snprintf(filename, 1024, "%s/levels/%s/", st_dir, subset); - if(access(filename, R_OK) == 0) + filename = st_dir + "/levels/" + subset + "/"; + if(access(filename.c_str(), R_OK) == 0) { files = FileSystem::read_directory(filename); } else { - snprintf(filename, 1024, "%s/levels/%s/", datadir.c_str(), subset); + filename = datadir + "/levels/" + subset + "/"; files = FileSystem::read_directory(filename); } diff --git a/src/leveleditor.cpp b/src/leveleditor.cpp index daa38d298..a7c14476a 100644 --- a/src/leveleditor.cpp +++ b/src/leveleditor.cpp @@ -544,8 +544,8 @@ void LevelEditor::update_level_settings_menu() level_settings_menu->get_item_by_id(MNID_NAME).change_input(le_level->name.c_str()); level_settings_menu->get_item_by_id(MNID_AUTHOR).change_input(le_level->author.c_str()); - level_settings_menu->get_item_by_id(MNID_SONG).list.first = FileSystem::dfiles("music/",NULL, "-fast"); - level_settings_menu->get_item_by_id(MNID_BGIMG).list.first = FileSystem::dfiles("images/background",NULL, NULL); + level_settings_menu->get_item_by_id(MNID_SONG).list.first = FileSystem::dfiles("music/","", "-fast"); + level_settings_menu->get_item_by_id(MNID_BGIMG).list.first = FileSystem::dfiles("images/background","", ""); level_settings_menu->get_item_by_id(MNID_BGIMG).list.first.insert(""); level_settings_menu->get_item_by_id(MNID_PARTICLE).list.first.insert(""); level_settings_menu->get_item_by_id(MNID_PARTICLE).list.first.insert("snow"); diff --git a/src/title.cpp b/src/title.cpp index 1e913df98..1131b5c44 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -20,6 +20,7 @@ // 02111-1307, USA. #include +#include #include #include #include @@ -279,7 +280,7 @@ void title(void) img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true); /* Generating contrib maps by only using a string_list */ - worldmap_list = FileSystem::dfiles("levels/worldmap", NULL, "icyisland.stwm"); + worldmap_list = FileSystem::dfiles("levels/worldmap", "", "icyisland.stwm"); titlesession->get_current_sector()->activate(); titlesession->set_current(); @@ -379,14 +380,15 @@ void title(void) if(event.key.keysym.sym == SDLK_DELETE) { int slot = menu->get_active_item_id(); - char str[1024]; - sprintf(str,_("Are you sure you want to delete slot %d?"), slot); + std::stringstream stream; + stream << slot; + std::string str = _("Are you sure you want to delete slot") + stream.str() + "?"; - if(confirm_dialog(bkg_title, str)) + if(confirm_dialog(bkg_title, str.c_str())) { - sprintf(str,"%s/slot%d.stsg", st_save_dir, slot); - printf("Removing: %s\n",str); - remove(str); + str = st_save_dir + "/slot" + stream.str() + ".stsg"; + printf("Removing: %s\n",str.c_str()); + remove(str.c_str()); } update_load_save_game_menu(load_game_menu); -- 2.11.0