home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 8 / IOPROG_8.ISO / install / fips / source / global.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-25  |  7.2 KB  |  258 lines

  1. /*
  2.  
  3.     FIPS - the First nondestructive Interactive Partition Splitting program
  4.  
  5.  
  6.  
  7.     Module global.cpp
  8.  
  9.  
  10.  
  11.     RCS - Header:
  12.  
  13.     $Header: c:/daten/fips/source/main/RCS/global.cpp 1.4 1995/01/19 00:00:52 schaefer Exp schaefer $
  14.  
  15.  
  16.  
  17.     Copyright (C) 1993 Arno Schaefer
  18.  
  19.  
  20.  
  21.     This program is free software; you can redistribute it and/or modify
  22.  
  23.     it under the terms of the GNU General Public License as published by
  24.  
  25.     the Free Software Foundation; either version 2 of the License, or
  26.  
  27.     (at your option) any later version.
  28.  
  29.  
  30.  
  31.     This program is distributed in the hope that it will be useful,
  32.  
  33.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  34.  
  35.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  36.  
  37.     GNU General Public License for more details.
  38.  
  39.  
  40.  
  41.     You should have received a copy of the GNU General Public License
  42.  
  43.     along with this program; if not, write to the Free Software
  44.  
  45.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  46.  
  47.  
  48.  
  49.  
  50.  
  51.     Report problems and direct all questions to:
  52.  
  53.  
  54.  
  55.     schaefer@rbg.informatik.th-darmstadt.de
  56.  
  57. */
  58.  
  59.  
  60.  
  61. #include <stdarg.h>
  62.  
  63. #include <conio.h>
  64.  
  65. #include <stdlib.h>
  66.  
  67. #include <string.h>
  68.  
  69.  
  70.  
  71. #include "version.h"
  72.  
  73. #include "global.h"
  74.  
  75.  
  76.  
  77. #define CTRL_C 3
  78.  
  79.  
  80.  
  81. global_vars global;
  82.  
  83.  
  84.  
  85. /* ----------------------------------------------------------------------- */
  86.  
  87. /* Initialization of global variables                                      */
  88.  
  89. /* ----------------------------------------------------------------------- */
  90.  
  91.  
  92.  
  93. global_vars::global_vars (void)
  94.  
  95. {
  96.  
  97.     test_mode = false;
  98.  
  99.     verbose_mode = true;
  100.  
  101.     debug_mode = false;
  102.  
  103.  
  104.  
  105.     drive_number_cmdline = 0;
  106.  
  107. }
  108.  
  109.  
  110.  
  111. global_vars::~global_vars (void)
  112.  
  113. {
  114.  
  115.     if (debug_mode) fclose (debugfile);
  116.  
  117. }
  118.  
  119.  
  120.  
  121. void exit_function (void)
  122.  
  123. {
  124.  
  125.     printx ("\nBye!\n");
  126.  
  127. }
  128.  
  129.  
  130.  
  131. void global_vars::open_debugfile (int argc,char *argv[])
  132.  
  133. {
  134.  
  135.     if ((debugfile = fopen ("fipsinfo.dbg","wt")) == NULL)
  136.  
  137.     {
  138.  
  139.         global.debug_mode = false;
  140.  
  141.         warning (true, "Can't open debug file");
  142.  
  143.     }
  144.  
  145.     else
  146.  
  147.     {
  148.  
  149.         fprintf (debugfile,"FIPS debug file\n\n");
  150.  
  151.         fprintf (debugfile,"The command was: ");
  152.  
  153.         while (argc--) fprintf (debugfile,argc ? "%s " : "%s", *argv++);
  154.  
  155.         fprintf (debugfile,"\n\nTranscript of session:\n");
  156.  
  157.     }
  158.  
  159. }
  160.  
  161.  
  162.  
  163. /* ----------------------------------------------------------------------- */
  164.  
  165. /* Replacement for printf - prints to screen and debugfile                 */
  166.  
  167. /* ----------------------------------------------------------------------- */
  168.  
  169.  
  170.  
  171. void printx (char *fmt,...)
  172.  
  173. {
  174.  
  175.     va_list ap;
  176.  
  177.     va_start (ap,fmt);
  178.  
  179.     vprintf (fmt,ap);
  180.  
  181.     if (global.debug_mode) vfprintf (global.debugfile,fmt,ap);
  182.  
  183.     va_end (ap);
  184.  
  185. }
  186.  
  187.  
  188.  
  189. /* ----------------------------------------------------------------------- */
  190.  
  191. /* Replacement for getch - exit when CTRL-C is pressed                     */
  192.  
  193. /* ----------------------------------------------------------------------- */
  194.  
  195.  
  196.  
  197. int getx (void)
  198.  
  199. {
  200.  
  201.     int character = getch();
  202.  
  203.     if (character == CTRL_C)
  204.  
  205.     {
  206.  
  207.         printx ("\n");
  208.  
  209.         exit (0);
  210.  
  211.     }
  212.  
  213.     return (character);
  214.  
  215. }
  216.  
  217.  
  218.  
  219. /* ----------------------------------------------------------------------- */
  220.  
  221. /* Copyright notice and version number                                     */
  222.  
  223. /* ----------------------------------------------------------------------- */
  224.  
  225.  
  226.  
  227. void notice (void)
  228.  
  229. {
  230.  
  231.     printx ("\nFIPS version " FIPS_VERSION ", Copyright (C) 1993/94 Arno Schaefer\n\n");
  232.  
  233.     printx ("DO NOT use FIPS in a multitasking environment like Windows, OS/2, Desqview,\n");
  234.  
  235.     printx ("Novell Task manager or the Linux DOS emulator: boot from a DOS boot disk first.\n\n");
  236.  
  237.     printx ("If you use OS/2 or a disk compressor, read the relevant sections in FIPS.DOC.\n\n");
  238.  
  239.     printx ("FIPS comes with ABSOLUTELY NO WARRANTY, see file COPYING for details\n");
  240.  
  241.     printx ("This is free software, and you are welcome to redistribute it\n");
  242.  
  243.     printx ("under certain conditions; again see file COPYING for details.\n");
  244.  
  245.  
  246.  
  247.     printx ("\nPress any Key\n");
  248.  
  249.     getx();
  250.  
  251. }
  252.  
  253.  
  254.  
  255. /* ----------------------------------------------------------------------- */
  256.  
  257. /* Hexdump binary data into a file                                         */
  258.  
  259. /* ----------------------------------------------------------------------- */
  260.  
  261.  
  262.  
  263. void hexwrite (byte *buffer,int number,FILE *file)
  264.  
  265. {
  266.  
  267.     for (int i=0;i<number;i++)
  268.  
  269.     {
  270.  
  271.         fprintf (file,"%02X ",*(buffer+i));
  272.  
  273.         if ((i+1)%16 == 0) fprintf (file,"\n");
  274.  
  275.         else if ((i+1)%8 == 0) fprintf (file,"- ");
  276.  
  277.     }
  278.  
  279.     fprintf (file,"\n");
  280.  
  281. }
  282.  
  283.  
  284.  
  285. /* ----------------------------------------------------------------------- */
  286.  
  287. /* Error Handling                                                          */
  288.  
  289. /* ----------------------------------------------------------------------- */
  290.  
  291.  
  292.  
  293. static void print_verbose_message (char *message)
  294.  
  295. {
  296.  
  297.     char line[256];
  298.  
  299.     int length = 0;
  300.  
  301.     FILE *error_msg_file;
  302.  
  303.  
  304.  
  305.     fprintf (stderr,"\n");
  306.  
  307.     if (global.debug_mode) fprintf (global.debugfile,"\n");
  308.  
  309.  
  310.  
  311.     if ((error_msg_file = fopen ("errors.txt","rt")) == NULL)
  312.  
  313.     {
  314.  
  315.         fprintf (stderr,"File ERRORS.TXT not found - no verbose messages available\n");
  316.  
  317.         if (global.debug_mode) fprintf (global.debugfile,"File ERRORS.TXT not found - no verbose messages available\n");
  318.  
  319.         global.verbose_mode = false;
  320.  
  321.         return;
  322.  
  323.     }
  324.  
  325.  
  326.  
  327.     while (message[length] != 0 && message[length] != ':') length++;
  328.  
  329.  
  330.  
  331.     fgets (line,255,error_msg_file);
  332.  
  333.     while (strncmp(message,line,length)) if (fgets (line,255,error_msg_file) == NULL) return;
  334.  
  335.     fgets (line,255,error_msg_file);
  336.  
  337.     while (!strncmp("  ",line,2))
  338.  
  339.     {
  340.  
  341.         fprintf (stderr,"%s",line+2);
  342.  
  343.         if (global.debug_mode) fprintf (global.debugfile,"%s",line+2);
  344.  
  345.         if (fgets (line,255,error_msg_file) == NULL) return;
  346.  
  347.     }
  348.  
  349.     fclose (error_msg_file);
  350.  
  351. }
  352.  
  353.  
  354.  
  355. void error (char *message,...)
  356.  
  357. {
  358.  
  359.     va_list ap;
  360.  
  361.  
  362.  
  363.     va_start (ap,message);
  364.  
  365.  
  366.  
  367.     fprintf (stderr,"\nError: ");
  368.  
  369.     vfprintf (stderr,message,ap);
  370.  
  371.     fprintf (stderr,"\n");
  372.  
  373.  
  374.  
  375.     if (global.debug_mode)
  376.  
  377.     {
  378.  
  379.         fprintf (global.debugfile,"\nError: ");
  380.  
  381.         vfprintf (global.debugfile,message,ap);
  382.  
  383.         fprintf (global.debugfile,"\n");
  384.  
  385.     }
  386.  
  387.  
  388.  
  389.     va_end (ap);
  390.  
  391.  
  392.  
  393.     if (global.verbose_mode) print_verbose_message (message);
  394.  
  395.  
  396.  
  397.     exit (-1);
  398.  
  399. }
  400.  
  401.  
  402.  
  403. void warning (boolean wait_key, char *message,...)
  404.  
  405. {
  406.  
  407.     va_list ap;
  408.  
  409.  
  410.  
  411.     va_start (ap,message);
  412.  
  413.  
  414.  
  415.     fprintf (stderr,"\nWarning: ");
  416.  
  417.     vfprintf (stderr,message,ap);
  418.  
  419.     fprintf (stderr,"\n");
  420.  
  421.  
  422.  
  423.     if (global.debug_mode)
  424.  
  425.     {
  426.  
  427.         fprintf (global.debugfile,"\nWarning: ");
  428.  
  429.         vfprintf (global.debugfile,message,ap);
  430.  
  431.         fprintf (global.debugfile,"\n");
  432.  
  433.     }
  434.  
  435.  
  436.  
  437.     va_end (ap);
  438.  
  439.  
  440.  
  441.     if (global.verbose_mode) print_verbose_message (message);
  442.  
  443.  
  444.  
  445.     if (wait_key)
  446.  
  447.     {
  448.  
  449.         fprintf (stderr,"\nPress any key\n");
  450.  
  451.         if (global.debug_mode) fprintf (global.debugfile,"\nPress any key\n");
  452.  
  453.  
  454.  
  455.         getx ();
  456.  
  457.     }
  458.  
  459. }
  460.  
  461.  
  462.  
  463. void infomsg (char *message,...)
  464.  
  465. {
  466.  
  467.     va_list ap;
  468.  
  469.  
  470.  
  471.     va_start (ap,message);
  472.  
  473.  
  474.  
  475.     fprintf (stderr,"\nInfo: ");
  476.  
  477.     vfprintf (stderr,message,ap);
  478.  
  479.     fprintf (stderr,"\n");
  480.  
  481.  
  482.  
  483.     if (global.debug_mode)
  484.  
  485.     {
  486.  
  487.         fprintf (global.debugfile,"\nInfo: ");
  488.  
  489.         vfprintf (global.debugfile,message,ap);
  490.  
  491.         fprintf (global.debugfile,"\n");
  492.  
  493.     }
  494.  
  495.  
  496.  
  497.     va_end (ap);
  498.  
  499.  
  500.  
  501.     if (global.verbose_mode) print_verbose_message (message);
  502.  
  503.  
  504.  
  505.     fprintf (stderr,"\nPress any key\n");
  506.  
  507.     if (global.debug_mode) fprintf (global.debugfile,"\nPress any key\n");
  508.  
  509.  
  510.  
  511.     getx ();
  512.  
  513. }
  514.  
  515.