home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of Shareware - Software Farm 2
/
wosw_2.zip
/
wosw_2
/
CPROG
/
DDJ0190.ZIP
/
DOUGLAS.LST
next >
Wrap
File List
|
1989-12-26
|
3KB
|
153 lines
_ERROR MESSAGE MANAGEMENT_
by Rohan Douglas
[LISTING ONE]
/* Include file for macros replacement of error function. */
#define error(m) _error(__FILE__, __LINE__)
extern void _error(char *, int);
[LISTING TWO]
/* Definition of error function. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"
static int get_err_msg(char *, char *, char *);
void
_error(char *fname, int lno)
{
char *period; /* Pointer for file extension */
char lstr[6]; /* Temp string for file line */
char key[15]; /* Message key (file:line) */
char msg[75]; /* Error message from file */
/* Strip out file extension from file name */
if ((period = strchr(fname, '.')) != NULL)
*period = '\0';
/* Create file:linenumber key */
sprintf(key, "%s:%s", fname, itoa(lno, lstr,
10));
/* Look up error message from file */
if (!get_err_msg(fname, key, msg))
/* Not found in file, just print out
* file:lineno.
*/
printf("Error [%s]", key);
else
printf("%s [%s]", msg, key);
return;
} /* error() */
static int
get_err_msg(char *fname, char *key, char *msg)
{
FILE *fp; /* Error message file pointer */
char msg_key[14];/* Key for current line */
/* Open error file */
if (!(fp = fopen("error.dat", "r")))
return FALSE;
/* Scan file for message */
while (!feof(fp)) {
fscanf(fp, " %s ", msg_key);
fgets(msg, 75, fp);
if (!strcmpi(key, msg_key))
break;
}
fclose(fp);
/* Return false if message not found */
if (feof(fp))
return FALSE;
/* Remove CR from message string */
msg[strlen(msg)-1] = '\0';
return TRUE;
} /* get_err_msg() */
[LISTING THREE]
BEGIN {
FS = "\"";
printf("") > "error.txt";
printf("") > "error.dat";
}
/ *error *\(/ {
printf("%s :\n", $2) >> "error.txt";
ind = substr(FILENAME, 0, index(FILENAME, ".")
- 1);
printf("%s:%d\t%s\n", ind, NR, $2) >>
"error.dat";
getline();
i = index($0, "/* ");
if (i) i++;
while (i) {
printf("\t%s\n", substr($0, i + 2)) >>
"error.txt";
getline();
if (index($0, "*/")) break;
i = index($0, "* ");
}
}
Figure 1: Sample source file with embedded error message and
description.
#include <stdio.h>
#include "error.h"
main(int argc, char *argv[])
{
if (argc == 1)
error("Missing parameters");
/* No parameters have been passed
* to this test program. This error message
* will be displayed as an example if no
* parameters are passed to this test
* program.
*/
exit(0);
}
Figure 2: The user manual list resulting from Figure 1.
Missing parameters :
No parameters have been passed to
this test program. This error message
will be displayed as an example if no
parameters are passed to this test
program.
Figure 3: The message database resulting from Figure 1.
test:8 Missing parameters
Figure 4: The results of the sample program.
Missing parameters [test:8]