Converted many char*s to std::strings. This is providing more safety from buffer...
authorTobias Gläßer <tobi.web@gmx.de>
Fri, 6 Aug 2004 11:43:10 +0000 (11:43 +0000)
committerTobias Gläßer <tobi.web@gmx.de>
Fri, 6 Aug 2004 11:43:10 +0000 (11:43 +0000)
SVN-Revision: 1715

lib/app/globals.cpp
lib/app/globals.h
lib/app/setup.cpp
lib/app/setup.h
lib/utils/configfile.cpp
lib/utils/configfile.h
src/gameloop.cpp
src/level_subset.cpp
src/leveleditor.cpp
src/title.cpp

index b363ec0..0a328d4 100644 (file)
@@ -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;
 
index 2894b2b..246e1ab 100644 (file)
@@ -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;
 
index 75dd5f5..5ddcb01 100644 (file)
@@ -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<std::string> FileSystem::dsubdirs(const char *rel_path,const  char* expected_file)
+std::set<std::string> FileSystem::dsubdirs(const std::string &rel_path,const  std::string& expected_file)
 {
   DIR *dirStructP;
   struct dirent *direntp;
   std::set<std::string> 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<std::string> 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<std::string> FileSystem::dsubdirs(const char *rel_path,const  char* exp
   return sdirs;
 }
 
-std::set<std::string> FileSystem::dfiles(const char *rel_path, const  char* glob, const  char* exception_str)
+std::set<std::string> FileSystem::dfiles(const std::string& rel_path, const  std::string& glob, const  std::string& exception_str)
 {
   DIR *dirStructP;
   struct dirent *direntp;
   std::set<std::string> 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<std::string> 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())
index 4c91464..3acca6c 100644 (file)
@@ -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<std::string> read_directory(const std::string& pathname);
-    static std::set<std::string> dsubdirs(const char *rel_path, const char* expected_file);
-    static std::set<std::string> dfiles(const char *rel_path, const char* glob, const char* exception_str);
+    static std::set<std::string> dsubdirs(const std::string& rel_path, const std::string& expected_file);
+    static std::set<std::string> 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
index 31a3502..bbbb501 100644 (file)
@@ -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);
 }
index a6471cc..17ab2c3 100644 (file)
@@ -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:
index 740663e..97cecb9 100644 (file)
@@ -20,6 +20,7 @@
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include <iostream>
+#include <sstream>
 #include <cassert>
 #include <cstdio>
 #include <cstdlib>
@@ -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();
         }
index c364e6b..704ced1 100644 (file)
@@ -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<std::string> 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);
         }
   
index daa38d2..a7c1447 100644 (file)
@@ -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");
index 1e913df..1131b5c 100644 (file)
@@ -20,6 +20,7 @@
 //  02111-1307, USA.
 
 #include <iostream>
+#include <sstream>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -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);