4 Bears Online

Comics | Jokes about India | Men and Women Jokes | Funny articles and essays | Only in America | More serious Articles and Essays |
Articles/News   | Readings   | Humor   | Travel/Hiking   | More/Informational Links  

Good Programming: Templates and Examples

[From Dr. Jayant Haritsa's Database Systems Course.]

  • Module and Function Template
  • Example Module and Function
  • Example Makefile

Module and Function Template

/************************INCLUDES*******************************/

/************************EXTERNS********************************/


/************************LOCAL DEFINES**************************/


/*------------------------------------------------------------

FUNCTION Foo (argc, argv)

PARAMETER DESCRIPTION:

FUNCTION DESCRIPTION:

ALGORITHM:

WRITTEN ON:

AUTHOR:

BUGS:

ERRORS REPORTED:

GLOBAL VARIABLES MODIFIED:

IMPLEMENTATION NOTES (IF ANY):

------------------------------------------------------------*/

Foo (argc, argv)
int argc;
char **argv;

{
/************************LOCAL VARIABLES***********************/

/**************************************************************/

#ifdef TRACEARGS /* if TRACEARGS on */
/* print command line arguments */
short k; /* iteration counter */
printf ("%s:\n", argv[0]);
for (k = 1 ; k < argc; ++k)
printf ("\targv[%d] = %s\n", k, argv[k]);
#endif

#ifdef FUNCTRACE /* if FUNCTRACE on */
printf ("Entering Foo \n");
#endif


< C statements >


#ifdef FUNCTRACE /* if FUNCTRACE on */
printf ("Exiting Foo \n");
#endif

} /* foo */



Example Module and Function



/************************INCLUDES*******************************/
#include "../include/defs.h"
#include "../include/error.h"
#include "../include/globals.h"
#include
#include

/************************EXTERNS********************************/

extern OpenRel();
extern CloseRel();
extern GetNextRec();
extern InsertRec();
extern FindRelNum();
extern Errormsgs();

/************************LOCAL DEFINES**************************/


/*------------------------------------------------------------

FUNCTION Insert (argc, argv)

PARAMETER DESCRIPTION:
argc is the count of the arguments in the command line.
argv is the pointer to the array of pointers pointing to
the argument strings.

SPECIFICATIONS:
argv[0] = "insert"
argv[1] = relation name
argv[2] = attribute name 1
argv[3] = attribute value 1
...
argv[argc-2] = attribute name N
argv[argc-1] = attribute value N
argv[argc] = NIL

The routine is to implement the relational update operator "insert".
The tuple given in argv is to be inserted into the named relation.
Input values, which will be in ASCII form, are to be converted
appropriately, according to their specified format. Attributes
are to be matched up by name. Length of incoming string-type
data should be regulated. If it is too long, truncate it. If
it is too short, pad with NULLs. Inserting the tuple should not
introduce a duplicate. Any errors in the command are to be reported.

ALGORITHM:
1) Check for valid command.
2) Check that the append relation is neither the relation catalog
nor the attribute catalog.
3) Check that the relation exists, and if it does, open it.
4) Check that the number of attribute arguments corresponds to that
of the relation.
5) Check that the attribute arguments are unique, and that they match
up with the relation's attributes.
6) Create the tuple to be appended using the information in the
relation's attribute catalog and the values given in argv.
7) Compare the created tuple with every tuple in the relation, to
check whether the created tuple already exists in the relation.
8) If it exists, generate an error message. Otherwise, insert the
created tuple into the relation.
9) Close the relation.

WRITTEN ON:
1/8/94

AUTHOR:
P.V. Narasimha Rao

BUGS:
None found as of date.

ERRORS REPORTED:
1) BADCOMMAND - Invalid relational operator
2) RELNOEXIST - Append relation does not exist
3) WRONGNUMARGS - Wrong number of arguments for routine
4) DUPLICATEATTR - Attribute specified more than once
5) ATTRNOEXIST - Invalid relation attribute
6) RECORDEXIST - Record already exists
7) BADATTRTYPE - Invalid attribute type
8) CATACCESS - Tried to append to catalogs
9) CALLOCERROR - Error in call to calloc()

GLOBAL VARIABLES MODIFIED:
None.

IMPLEMENTATION NOTES (IF ANY):
Where the new record is inserted in the relation depends on the
implementation of InsertRec().

------------------------------------------------------------*/
Insert (argc, argv)
int argc;
char **argv;

{
.
.
.

< Your C statements >

.
.
.


return (OK); /* all's fine */
} /* insert */



Example Makefile




####################################################################
# This is a sample makefile for the physical layer.
#
# If you want to add a file, add it to both the SRC and OBJ lists.
# Use a backslash for continuation into the next line.
#
####################################################################

####################################################################
# FLAGS for the C compiler
####################################################################

# where to look for the header files
INCLUDE = ../include

DFLAG =
CFLAGS = -g $(DFLAG) -I$(INCLUDE)
# -g to generate symbolic info. used by
# the debugger.
# DFLAG can be used to turn specific debug
# messages 'on' or 'off'. Read up on
# "#ifdef" for more details

SRCS = \
error.c createcats.c opencats.c closecats.c openrel.c closerel.c findrelnum.c \
readpage.c flushpage.c writerec.c findrec.c getnextrec.c insertrec.c deleterec.c

OBJ = \
error.o createcats.o opencats.o closecats.o openrel.o closerel.o findrelnum.o \
readpage.o flushpage.o writerec.o findrec.o getnextrec.o insertrec.o deleterec.o

#############################################################
# stuff to build the executables
#############################################################

PHYSICAL.o : $(OBJ)
@echo generating $@
@ld -r $(OBJ) -o $@

$(OBJ) : $(INCLUDE)/globals.h $(INCLUDE)/defs.h $(INCLUDE)/error.h

###############################################################
# misc
###############################################################

tags:
ctags -t $(SRCS) $(INCLUDE)/defs.h $(INCLUDE)/error.h

lint:
lint $(SRCS) $(INCLUDE)/defs.h $(INCLUDE)/error.h > lint.out

clean: # remove all .o files and other garbage
rm *.o






Dr. Jayant Haritsa
haritsa@csa.iisc.ernet.in


http://www.4bearsonline.com/collections/cs/index.shtml