home *** CD-ROM | disk | FTP | other *** search
/ James Briskly's Game Magazine 2 / JBGM002S.ZIP / SOURCE / GLOBAL.CPP < prev    next >
C/C++ Source or Header  |  1995-08-22  |  7KB  |  258 lines

  1. /*
  2.     FIPS - the First nondestructive Interactive Partition Splitting program
  3.  
  4.     Module global.cpp
  5.  
  6.     RCS - Header:
  7.     $Header: c:/daten/fips/source/main/RCS/global.cpp 1.4 1995/01/19 00:00:52 schaefer Exp schaefer $
  8.  
  9.     Copyright (C) 1993 Arno Schaefer
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License as published by
  13.     the Free Software Foundation; either version 2 of the License, or
  14.     (at your option) any later version.
  15.  
  16.     This program is distributed in the hope that it will be useful,
  17.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.     GNU General Public License for more details.
  20.  
  21.     You should have received a copy of the GNU General Public License
  22.     along with this program; if not, write to the Free Software
  23.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25.  
  26.     Report problems and direct all questions to:
  27.  
  28.     schaefer@rbg.informatik.th-darmstadt.de
  29. */
  30.  
  31. #include <stdarg.h>
  32. #include <conio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35.  
  36. #include "version.h"
  37. #include "global.h"
  38.  
  39. #define CTRL_C 3
  40.  
  41. global_vars global;
  42.  
  43. /* ----------------------------------------------------------------------- */
  44. /* Initialization of global variables                                      */
  45. /* ----------------------------------------------------------------------- */
  46.  
  47. global_vars::global_vars (void)
  48. {
  49.     test_mode = false;
  50.     verbose_mode = true;
  51.     debug_mode = false;
  52.  
  53.     drive_number_cmdline = 0;
  54. }
  55.  
  56. global_vars::~global_vars (void)
  57. {
  58.     if (debug_mode) fclose (debugfile);
  59. }
  60.  
  61. void exit_function (void)
  62. {
  63.     printx ("\nBye!\n");
  64. }
  65.  
  66. void global_vars::open_debugfile (int argc,char *argv[])
  67. {
  68.     if ((debugfile = fopen ("fipsinfo.dbg","wt")) == NULL)
  69.     {
  70.         global.debug_mode = false;
  71.         warning (true, "Can't open debug file");
  72.     }
  73.     else
  74.     {
  75.         fprintf (debugfile,"FIPS debug file\n\n");
  76.         fprintf (debugfile,"The command was: ");
  77.         while (argc--) fprintf (debugfile,argc ? "%s " : "%s", *argv++);
  78.         fprintf (debugfile,"\n\nTranscript of session:\n");
  79.     }
  80. }
  81.  
  82. /* ----------------------------------------------------------------------- */
  83. /* Replacement for printf - prints to screen and debugfile                 */
  84. /* ----------------------------------------------------------------------- */
  85.  
  86. void printx (char *fmt,...)
  87. {
  88.     va_list ap;
  89.     va_start (ap,fmt);
  90.     vprintf (fmt,ap);
  91.     if (global.debug_mode) vfprintf (global.debugfile,fmt,ap);
  92.     va_end (ap);
  93. }
  94.  
  95. /* ----------------------------------------------------------------------- */
  96. /* Replacement for getch - exit when CTRL-C is pressed                     */
  97. /* ----------------------------------------------------------------------- */
  98.  
  99. int getx (void)
  100. {
  101.     int character = getch();
  102.     if (character == CTRL_C)
  103.     {
  104.         printx ("\n");
  105.         exit (0);
  106.     }
  107.     return (character);
  108. }
  109.  
  110. /* ----------------------------------------------------------------------- */
  111. /* Copyright notice and version number                                     */
  112. /* ----------------------------------------------------------------------- */
  113.  
  114. void notice (void)
  115. {
  116.     printx ("\nFIPS version " FIPS_VERSION ", Copyright (C) 1993/94 Arno Schaefer\n\n");
  117.     printx ("DO NOT use FIPS in a multitasking environment like Windows, OS/2, Desqview,\n");
  118.     printx ("Novell Task manager or the Linux DOS emulator: boot from a DOS boot disk first.\n\n");
  119.     printx ("If you use OS/2 or a disk compressor, read the relevant sections in FIPS.DOC.\n\n");
  120.     printx ("FIPS comes with ABSOLUTELY NO WARRANTY, see file COPYING for details\n");
  121.     printx ("This is free software, and you are welcome to redistribute it\n");
  122.     printx ("under certain conditions; again see file COPYING for details.\n");
  123.  
  124.     printx ("\nPress any Key\n");
  125.     getx();
  126. }
  127.  
  128. /* ----------------------------------------------------------------------- */
  129. /* Hexdump binary data into a file                                         */
  130. /* ----------------------------------------------------------------------- */
  131.  
  132. void hexwrite (byte *buffer,int number,FILE *file)
  133. {
  134.     for (int i=0;i<number;i++)
  135.     {
  136.         fprintf (file,"%02X ",*(buffer+i));
  137.         if ((i+1)%16 == 0) fprintf (file,"\n");
  138.         else if ((i+1)%8 == 0) fprintf (file,"- ");
  139.     }
  140.     fprintf (file,"\n");
  141. }
  142.  
  143. /* ----------------------------------------------------------------------- */
  144. /* Error Handling                                                          */
  145. /* ----------------------------------------------------------------------- */
  146.  
  147. static void print_verbose_message (char *message)
  148. {
  149.     char line[256];
  150.     int length = 0;
  151.     FILE *error_msg_file;
  152.  
  153.     fprintf (stderr,"\n");
  154.     if (global.debug_mode) fprintf (global.debugfile,"\n");
  155.  
  156.     if ((error_msg_file = fopen ("errors.txt","rt")) == NULL)
  157.     {
  158.         fprintf (stderr,"File ERRORS.TXT not found - no verbose messages available\n");
  159.         if (global.debug_mode) fprintf (global.debugfile,"File ERRORS.TXT not found - no verbose messages available\n");
  160.         global.verbose_mode = false;
  161.         return;
  162.     }
  163.  
  164.     while (message[length] != 0 && message[length] != ':') length++;
  165.  
  166.     fgets (line,255,error_msg_file);
  167.     while (strncmp(message,line,length)) if (fgets (line,255,error_msg_file) == NULL) return;
  168.     fgets (line,255,error_msg_file);
  169.     while (!strncmp("  ",line,2))
  170.     {
  171.         fprintf (stderr,"%s",line+2);
  172.         if (global.debug_mode) fprintf (global.debugfile,"%s",line+2);
  173.         if (fgets (line,255,error_msg_file) == NULL) return;
  174.     }
  175.     fclose (error_msg_file);
  176. }
  177.  
  178. void error (char *message,...)
  179. {
  180.     va_list ap;
  181.  
  182.     va_start (ap,message);
  183.  
  184.     fprintf (stderr,"\nError: ");
  185.     vfprintf (stderr,message,ap);
  186.     fprintf (stderr,"\n");
  187.  
  188.     if (global.debug_mode)
  189.     {
  190.         fprintf (global.debugfile,"\nError: ");
  191.         vfprintf (global.debugfile,message,ap);
  192.         fprintf (global.debugfile,"\n");
  193.     }
  194.  
  195.     va_end (ap);
  196.  
  197.     if (global.verbose_mode) print_verbose_message (message);
  198.  
  199.     exit (-1);
  200. }
  201.  
  202. void warning (boolean wait_key, char *message,...)
  203. {
  204.     va_list ap;
  205.  
  206.     va_start (ap,message);
  207.  
  208.     fprintf (stderr,"\nWarning: ");
  209.     vfprintf (stderr,message,ap);
  210.     fprintf (stderr,"\n");
  211.  
  212.     if (global.debug_mode)
  213.     {
  214.         fprintf (global.debugfile,"\nWarning: ");
  215.         vfprintf (global.debugfile,message,ap);
  216.         fprintf (global.debugfile,"\n");
  217.     }
  218.  
  219.     va_end (ap);
  220.  
  221.     if (global.verbose_mode) print_verbose_message (message);
  222.  
  223.     if (wait_key)
  224.     {
  225.         fprintf (stderr,"\nPress any key\n");
  226.         if (global.debug_mode) fprintf (global.debugfile,"\nPress any key\n");
  227.  
  228.         getx ();
  229.     }
  230. }
  231.  
  232. void infomsg (char *message,...)
  233. {
  234.     va_list ap;
  235.  
  236.     va_start (ap,message);
  237.  
  238.     fprintf (stderr,"\nInfo: ");
  239.     vfprintf (stderr,message,ap);
  240.     fprintf (stderr,"\n");
  241.  
  242.     if (global.debug_mode)
  243.     {
  244.         fprintf (global.debugfile,"\nInfo: ");
  245.         vfprintf (global.debugfile,message,ap);
  246.         fprintf (global.debugfile,"\n");
  247.     }
  248.  
  249.     va_end (ap);
  250.  
  251.     if (global.verbose_mode) print_verbose_message (message);
  252.  
  253.     fprintf (stderr,"\nPress any key\n");
  254.     if (global.debug_mode) fprintf (global.debugfile,"\nPress any key\n");
  255.  
  256.     getx ();
  257. }
  258.