<URL: http://bugs.freeciv.org/Ticket/Display.html?id=40681 >

The attached patch factors out duplicate save name generating
code into a new function generate_save_name(). This function
also adds the current turn number to savefile names it
generates, so that save files become:

  <savename>_T<turn>_<year>.sav.gz

e.g. "civgame_T001_-3950.sav.gz".


Requested in the forums:
http://forum.freeciv.org/viewtopic.php?p=23039#23039


-----------------------------------------------------------------------
どのやり方が一番いいですか?靴下で?
diff --git a/server/settings.c b/server/settings.c
index f13b13b..32c9224 100644
--- a/server/settings.c
+++ b/server/settings.c
@@ -1014,7 +1014,7 @@ struct settings_s settings[] = {
 	     SSET_META, SSET_INTERNAL, SSET_VITAL, SSET_SERVER_ONLY,
 	     N_("Auto-save name prefix"),
 	     N_("Automatically saved games will have name "
-		"\"<prefix><year>.sav\". This setting sets "
+		"\"<prefix>_T<turn>_<year>.sav\". This setting sets "
 		"the <prefix> part."), NULL,
 	     GAME_DEFAULT_SAVE_NAME)
 
diff --git a/server/srv_main.c b/server/srv_main.c
index b386a40..f7dcef7 100644
--- a/server/srv_main.c
+++ b/server/srv_main.c
@@ -866,6 +866,30 @@ static void end_turn(void)
 }
 
 /**************************************************************************
+  Generate a default save file name and place it in the provided buffer.
+  The name will be of the form "<name>_T<turn>_<year><suf>" where:
+
+    <name> = game.save_name
+    <turn> = game.info.turn (zero padded to 3 places)
+    <year> = game.info.year (zero padded to 5 places with 1 sign character)
+    <suf>  = "m" (for "manual save") if 'is_auto_save' is FALSE
+
+  Returns the number of characters written, or the number of characters
+  that would have been written if truncation occurs.
+**************************************************************************/
+static int generate_save_name(char *buf, int buflen, bool is_auto_save)
+{
+  int nb;
+
+  /* NB: If you change the format here, be sure to update the above
+   * function comment and the help text for the savegame setting. */
+  nb = my_snprintf(buf, buflen, "%s_T%03d_%+05d%s",
+                   game.save_name, game.info.turn, game.info.year,
+                   is_auto_save ? "" : "m");
+  return nb;
+}
+
+/**************************************************************************
 Unconditionally save the game, with specified filename.
 Always prints a message: either save ok, or failed.
 
@@ -890,10 +914,9 @@ void save_game(char *orig_filename, const char *save_reason)
     *dot = '\0';
   }
 
-  /* If orig_filename is NULL or empty, use "civgame.info.year>m". */
+  /* If orig_filename is NULL or empty, use a generated default name. */
   if (filename[0] == '\0'){
-    my_snprintf(filename, sizeof(filename),
-	"%s%+05dm", game.save_name, game.info.year);
+    generate_save_name(filename, sizeof(filename), FALSE);
   }
   
   timer_cpu = new_timer_start(TIMER_CPU, TIMER_ACTIVE);
@@ -947,8 +970,7 @@ void save_game_auto(const char *save_reason)
 
   assert(strlen(game.save_name)<256);
   
-  my_snprintf(filename, sizeof(filename),
-	      "%s%+05d.sav", game.save_name, game.info.year);
+  generate_save_name(filename, sizeof(filename), TRUE);
   save_game(filename, save_reason);
   save_ppm();
 }
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to