/**************************************************************************** * * ****************************************************************************/ static char title[] = "dksort - Darklands Save Game Sorter"; /**************************************************************************** * * * Original Author (1992): * * Steven J. Cotellesse * * Modifications / Win32 adaptation (2000): * * Michael Petzold * * * * Developed with Borland C++ 4.53, this code compiles equally well for * * DOS 16-Bit and Win32 Console. * * * ****************************************************************************/ #include /* printf */ #include /* strcat, strcpy */ #include /* system, exit */ #include /* getdfree */ #include /* getcwd, etc.*/ #include /* getch */ static char version[] = "1.0"; static char game_dir[MAXPATH], save_dir[MAXPATH], save_dir_tmplt[MAXPATH]; static char file_names[200][13 + 1]; static long space_needed; static unsigned short dte[200], tme[200]; /****************************************************************************/ static int load_file_name_list (const char *templt) { int i; struct ffblk fb; space_needed = 0L; i = 0; if (findfirst (templt, &fb, 0) == 0) { strcpy (file_names[i], fb.ff_name); dte[i] = fb.ff_fdate; tme[i] = fb.ff_ftime; space_needed += fb.ff_fsize; ++i; } while (findnext (&fb) == 0 && i < 200) { strcpy (file_names[i], fb.ff_name); dte[i] = fb.ff_fdate; tme[i] = fb.ff_ftime; space_needed += fb.ff_fsize; ++i; } if (findnext (&fb) == 0) { printf ("More than 200 files found (%i) - " "only first 200 will be re-sorted\n", i); } return (i); } /****************************************************************************/ static int enough_disk_space (int num_files, long bytes_needed) { struct dfree dt; double bytes_free; getdfree (0, &dt); /* 0: current drive */ if (dt.df_sclus == (unsigned) -1) { /* error condition */ return (-1); } bytes_free = ((double) dt.df_avail * /* free clusters */ (double) dt.df_sclus * /* sectors per cluster */ (double) dt.df_bsec); /* bytes per sector */ /* printf ("(%.0f bytes free on current drive)\n", bytes_free); */ if (dt.df_avail <= num_files || /* at least one cluster per file */ bytes_free <= bytes_needed) { return (-1); } return (0); } /****************************************************************************/ static int copy_files_to_cwd (void) { int rslt; char cmd[256]; printf ("Copying files to current directory...\n"); strcpy (cmd, "COPY "); strcat (cmd, save_dir); strcat (cmd, "DKSAVE*.??V > NUL"); rslt = system (cmd); if (rslt != 0) { printf ("Unable to invoke command interpreter to copy files\n"); printf ("(command: %s)\n", cmd); } return (rslt); } /****************************************************************************/ static void sort_files (int num_files) { int i, j; unsigned short dd, tt; char tname[13 + 1]; /* DL uses DOS names */ printf ("Sorting files...\n"); for (i = 0; i < (num_files + 1); i++) { for (j = (i + 1); j < num_files; j++) { if ((dte[j] > dte[i]) || ((dte[j] == dte[i]) && (tme[j] > tme[i])) || ((dte[j] == dte[i]) && (tme[j] == tme[i]) && (strcmp (file_names[j], file_names[i]) > 0 ))) { strcpy (tname, file_names[i]); strcpy (file_names[i], file_names[j]); strcpy (file_names[j], tname); dd = dte[i]; dte[i] = dte[j]; dte[j] = dd; tt = tme[i]; tme[i] = tme[j]; tme[j] = tt; } } } } /****************************************************************************/ static void delete_save_files (void) { int rslt; char cmd[256]; printf ("Deleting original save game files...\n"); strcpy (cmd, "DEL "); strcat (cmd, save_dir); strcat (cmd, "DKSAVE*.??V > NUL"); rslt = system (cmd); if (rslt != 0) { printf ("Unable to invoke command interpreter to remove files\n"); printf ("(command: %s)\n", cmd); } } /****************************************************************************/ static int copy_from_to (const char *file_name, const char *from_dir, const char *to_dir) { int rslt; char cmd[256]; strcpy (cmd, "COPY "); strcat (cmd, from_dir); strcat (cmd, file_name); strcat (cmd, " "); strcat (cmd, to_dir); strcat (cmd, file_name); strcat (cmd, " > NUL"); rslt = system (cmd); if (rslt != 0) { printf ("Unable to invoke command interpreter to copy files\n"); printf ("(command: %s)\n", cmd); } return (rslt); } /****************************************************************************/ static int copy_files_to_savedir (int num_files) { int i, rslt; i = 0; rslt = 0; printf ("Copying save game files to saves directory...\n"); while ((rslt == 0) && (i < num_files)) { rslt = copy_from_to (file_names[i], game_dir, save_dir); ++i; } return (rslt); } /****************************************************************************/ static void delete_temp_files (void) { int rslt; char cmd[256]; printf ("Deleting temporary files...\n"); strcpy (cmd, "DEL "); strcat (cmd, game_dir); strcat (cmd, "DKSAVE*.??V > NUL"); rslt = system (cmd); if (rslt != 0) { printf ("Unable to invoke command interpreter to remove files\n"); printf ("(command: %s)\n", cmd); } } /****************************************************************************/ static void wait_to_exit (void) { printf ("Press any key to exit...\n"); (void) getch (); } /****************************************************************************/ static void error_exit (void) { printf ("Files not re-sorted\n"); wait_to_exit (); exit (1); } /****************************************************************************/ int main (void) { int num_files; printf ("%s ", title); printf ("V%s\n\n", version); if (getcwd (game_dir, MAXPATH) == NULL) { printf ("Could not allocate CWD buffer\n"); error_exit (); } if (game_dir[3] != '\0') /* Root dir already has trailing \ */ strcat (game_dir, "\\"); strcpy (save_dir, game_dir); strcat (save_dir, "SAVES\\"); strcpy (save_dir_tmplt, save_dir); strcat (save_dir_tmplt, "DKSAVE*.??V"); num_files = load_file_name_list (save_dir_tmplt); if (num_files == 0) { printf ("No save game files found in directory %s\n", save_dir); printf ("(%s)\n", save_dir_tmplt); error_exit (); } if (enough_disk_space (num_files, space_needed + 2048) != 0) { printf ("Not enough disk space to re-sort. " "(%ld bytes are needed)\n", space_needed + 2048); error_exit (); } if (copy_files_to_cwd () != 0) { error_exit (); } sort_files (num_files); delete_save_files (); if (copy_files_to_savedir (num_files) != 0) { error_exit (); } delete_temp_files (); printf ("Files sorted!\n"); wait_to_exit (); return (0); } /****************************************************************************/