Here's some of my code re self-destructing objects. It's for DJGPP, there
are probably several lines that are going to be either truncated or split at
awkward positions and the TABs will be flying all over the place. See if you
can make sense out of it ... and you'd bloody well better not laugh.
=====
/*
PUNIX.H
~~~~~~~
Contains all "stuff" for PUNIX.
*/
#ifndef __PUNIX_H
#define __PUNIX_H
// defines
// ~~~~~~~
#define PROG_NAME "PUNIX"
// the program name
#define PROG_VERSION "0.01"
// the program version
#define ABORT_AND_NOTHING_QUITS
// comment out to make user enter quit|exit to get out
// prototypes
// ~~~~~~~~~~
int check_input(char *); // checks entered input
class PUNIX_prog { // fairly useless class
private:
int _argc;
char **_argv;
public:
PUNIX_prog(int, char **); // small constructor ...
~PUNIX_prog(); // ... and destructor
int check_args(); // checks the command-line parameters
void status(); // show current system status
void online_help(); // show some online help for user
void send_msg(char *, int = 0);
// prints the message to the console with or without
"\r\n"
};
#endif
=====
/*
MAIN.CPP
~~~~~~~~
Contains main().
*/
// include files
// ~~~~~~~~~~~~~
#include
#include
#include
#include
#include
#include "punix.h"
// general program constants
// ~~~~~~~~~~~~~~~~~~~~~~~~~
class PUNIX_prog *prog;
int main(int argc, char **argv) {
prog = new PUNIX_prog (argc, argv); // create object for program
if (prog == NULL) return 1;
cprintf("\r\n");
char *input = new char[101]; // create variable for input
while (1) { // loop until broken
char *current_path = new char[MAXPATH];
getcwd(current_path, MAXPATH); // get the current path and .
. .
cprintf("%s : ", current_path); // . . . and make it the
prompt
delete current_path;
int quitting = 0;
if (!get_input(input, 101)) { // if not aborted . . .
switch (check_input(input)) { // . . . check the
nput
case 0 : if (stricmp(input, ""))
cprintf("\r\n");
// if input, print blank line, then . . .
break; // . . . just continue
or . . .
case 1 : quitting = 1; break; // . . . quit
out
}
}
#ifdef ABORT_AND_NOTHING_QUITS
else if (!stricmp(input, "")) break;
// if abort and nothing entered -- quit
#endif
if (quitting == 1) break;
}
delete input; // kill variable
cprintf("\r\n");
delete prog;
return 0;
}
=====
/*
PROG.CPP
~~~~~~~~
Contains functions for class PUNIX_prog.
*/
// include files
// ~~~~~~~~~~~~~
#include
#include
#include
#include
#include
#include
#include
#include "punix.h"
PUNIX_prog::PUNIX_prog(int argc, char **argv) {
cprintf("\r\n");
send_msg(PROG_NAME " v" PROG_VERSION ",");
send_msg("compiled on " __DATE__ ", at " __TIME__);
_argc = argc;
_argv = argv;
if (!--_argc || !check_args()) {
send_msg("Seeding random number generator ... ", 1);
srand(1); // seed random number generator
send_msg("done\r\n", 2);
send_msg("Masking Ctrl-C = abort ... ", 1);
__djgpp_set_ctrl_c(0);
// do not kill program if Ctrl-C hit
send_msg("done\r\n", 2);
send_msg("Masking Ctrl-Break = abort ... ", 1);
_go32_want_ctrl_break(1);
// do not kill program if Ctrl-Break hit
send_msg("done\r\n", 2);
send_msg("Init completed.");
}
else delete this;
}
PUNIX_prog::~PUNIX_prog() {
send_msg(PROG_NAME " terminated.");
send_msg("very long string that will probably be destroyed utterly");
}
int PUNIX_prog::check_args() {
int args_found = 0;
for (int arg_number = 1 ; arg_number <= _argc ; arg_number++) {
if (!stricmp(_argv[arg_number], "--test")) {
send_msg("parameter \"--test\" recognised.");
args_found++;
}
}
if (args_found != _argc) {
send_msg("One or more parameters not recognised.");
return 1;
}
return 0;
}
void PUNIX_prog::status() {
cprintf("Currently, there is no status info.\r\n");
return;
}
void PUNIX_prog::online_help() {
cprintf("help ]- get this help screen\r\n"
"? ]\r\n"
"exit ]- quit " PROG_NAME "\r\n"
"quit ]\r\n"
"status -- get the current system status\r\n");
return;
}
void PUNIX_prog::send_msg(char *output, int newline) {
time_t current_time;
time(¤t_time);
char *str_current_time = ctime(¤t_time);
*(str_current_time + 24) = '\x00';
switch (newline) {
case 0 : cprintf("%s : %s\r\n", str_current_time, output); break;
case 1 : cprintf("%s : %s", str_current_time, output); break;
case 2 : cprintf("%s", output); break;
default : break;
}
return;
}
=====
--- PPoint 2.00
---------------
* Origin: Junyer's Workshop (3:640/772.3)
|