CSc 265 Lab6


1	CSc 265 Lab Exercise 6 - more inspection
2	
3	Team Name:
4	
5	Team Members:
6	
7	
8	
9	
10	
11	
12	The following code is adapted from the LineStorage module from the
13	KWIC program used for the first programming exercise. You are to 
14	inspect the code for faults, using the table below to record the line
15	number where the fault occurs and a description of what the problem is.
16	When inspecting the code for each function, make sure you ask yourself
17	the following questions: Is the code syntactically correct (no compile
18	errors)? Does the code meet the interface specification? Does the code
19	maintain the state invariant?
20	
21	Line #|     breif description of fault
22	------|-----------------------------------------------------------------
23	      |
24	 185  | The word member of the structure pointed to by tmpWordPtr0 is
25	      | never free'd
26	      |
27	______|_________________________________________________________________
28	      |
29	 216  | LineCount is never incremented
30	      |	
31	      |
32	______|_________________________________________________________________
33	      |
34	 222  | The function does not return KWRANGEERR if LSNumLines() == 0
35	      |
36	      |
37	______|_________________________________________________________________
38	      |
39	 230  | The structure pointed to by newWordPtr never has memory 
40	      |	allocated for its word member
41	      |
42	______|_________________________________________________________________
43	      |
44	 227  | wordCount is a member of a structure and cannnot be accessed
45	      |	by wordCount++
46	      |
47	______|_________________________________________________________________
48	      |
49	 244  |	missing closing curly brace
50	      |
51	      |
52	______|_________________________________________________________________
53	
54	/**********kwic system header file**********/
55	#define KWMAXWORDLENGTH 100
56	
57	typedef enum {
58		KWFILEERROR=-3,
59		KWMEMORYERROR=-2,
60		KWRANGEERROR=-1,
61		KWSUCCESS=0} KWStatus;
62	/********** LineStorage module---interface specification **********/
63	/***** overview *****
64	The LineStorage module stores a list of lines, where each line is a
65	list of C strings.  The lines and the words within a line are indexed
66	zero-relative.
67	
68	Lines are added a word at a time, in an append-only fashion.  Each word
69	is added to the end of the highest-numbered line currently stored.
70	Word and line deletion are not supported.
71	
72	Any word in any line can be retrieved at any time.
73	*/
74	/***** exported functions *****/
75	/*
76	* Initialize the module with 0 lines
77	*/
78	void LSInit(void);
79	/*
80	* Remove all lines and words, free'ing any dynamically allocated storage
81	*/
82	void LSReset(void);
83	/*
84	* if not enough memory is available to add a new line then
85	*	return KWMEMORYERR
86	* else
87	*	add a line, containing no words, in position LSNumLines()
88	*	return KWSUCCESS
89	*/
90	KWStatus LSAddLine(void);
91	/*
92	* if LSNumLines() == 0 then
93	*	return KWRANGEERR
94	* else if not enough memory is available to store word then
95	*	return KWMEMORYERR
96	* else
97	*	add word to the end of line LSNumLines()-1
98	*	return KWSUCCESS
99	*/
100	KWStatus LSAddWord(char* word);
101	/*
102	* return the total number of lines currently stored
103	*/
104	int LSNumLines(void);
105	/***** usage example *****
106	
107	To store the lines:
108		"The C Programming Language"
109		"The Cat in the Hat"
110	
111	Issue the calls:
112		LSInit();
113		LSAddLine();
114		LSAddWord("The");
115		LSAddWord("C");
116		LSAddWord("Programming");
117		LSAddWord("Language");
118		LSAddLine();
119		LSAddWord("The");
120		LSAddWord("Cat");
121		LSAddWord("in");
122		LSAddWord("the");
123		LSAddWord("Hat");
124	*/
125	/********** LineStorage module---implementation **********/
126	#include 
127	#include 
128	#include "kwic.h"
129	#include "LineStorage.h"
130	
131	/***** local types *****/
132	/*Each line is a list of WordNodes.*/
133	typedef struct WordNode {
134		char* word;
135		struct WordNode* nextWordPtr;
136	} WordNode;
137	typedef WordNode* WordNodePtr;
138	
139	/*The LineStorage module stores a list of LineNodes*/
140	typedef struct LineNode {
141		struct LineNode* nextLinePtr;
142		WordNodePtr headWordPtr;
143		WordNodePtr tailWordPtr;
144		int wordCount;
145	} LineNode;
146	typedef LineNode* LineNodePtr;
147	
148	/***** local variables *****/
149	static LineNodePtr headLinePtr, tailLinePtr;
150	static int lineCount;
151	
152	/***** state invariant *****
153	1. headLinePtr points to a null-terminated linked list of LineNodes.
154	   tailLinePtr points to the last LineNode in this list.
155	   There are lineCount LineNodes in this list.
156	
157	2. For every LineNode allocated
158	   headWordPtr points to a null-terminated linked list of WordNodes.
159	   tailWordPtr points to the last WordNode in this list.
160	   There are wordCount WordNodes in this list.
161	
162	   For every WordNode allocated
163		word is a null-terminated array of characters.
164	
165	3. All of the LineNodes and WordNodes allocated by LineStorage
166	   (and not yet freed) are in the list structure headed by headLinePtr.
167	*/
168	/***** exported functions *****/
169	void LSInit(void)
170	{
171		headLinePtr = NULL;
172		tailLinePtr = NULL;
173		lineCount = 0;
174	}
175	
176	void LSReset(void)
177	{
178		LineNodePtr tmpLinePtr;
179		WordNodePtr tmpWordPtr0,tmpWordPtr1;
180	
181		while (headLinePtr != NULL) {
182			tmpWordPtr0 = headLinePtr->headWordPtr;
183			while (tmpWordPtr0 != NULL) {
184				tmpWordPtr1 = tmpWordPtr0->nextWordPtr;
185				free(tmpWordPtr0);
186				tmpWordPtr0 = tmpWordPtr1;
187			}
188			tmpLinePtr = headLinePtr;
189			headLinePtr = headLinePtr->nextLinePtr;
190			free(tmpLinePtr);
191		}
192		lineCount = 0;
193		tailLinePtr = NULL;
194	}
195	
196	KWStatus LSAddLine(void)
197	{
198		LineNodePtr newLinePtr;
199	
200		/* create and fill a LineNode */
201		newLinePtr = malloc(sizeof(LineNode));
202		if (newLinePtr == NULL)
203			return KWMEMORYERROR;
204		newLinePtr->nextLinePtr = NULL;
205		newLinePtr->headWordPtr = NULL;
206		newLinePtr->tailWordPtr = NULL;
207		newLinePtr->wordCount = 0;
208	
209		/* link in the new LineNode */
210		if (tailLinePtr == NULL) {
211			headLinePtr = newLinePtr;
212		} else {
213			tailLinePtr->nextLinePtr = newLinePtr;
214		}
215		tailLinePtr = newLinePtr;
216		return KWSUCCESS;
217	}
218	
219	KWStatus LSAddWord(char* word)
220	{
221		WordNodePtr newWordPtr;
222	
223		/* create and fill a WordNode */
224		newWordPtr = malloc(sizeof(WordNode));
225		if (newWordPtr == NULL)
226			return KWMEMORYERROR;
227		wordCount++;
228		strcpy(newWordPtr->word,word);
229		newWordPtr->nextWordPtr = NULL;
230	
231		/* link in the new WordNode */
232		if (tailLinePtr->tailWordPtr == NULL) { /* empty line */
233			tailLinePtr->headWordPtr = newWordPtr;
234		} else {
235			tailLinePtr->tailWordPtr->nextWordPtr = newWordPtr;
236		}
237		tailLinePtr->tailWordPtr = newWordPtr;
238		return KWSUCCESS;
239	}
240	
241	int LSNumLines(void)
242	{
243		return lineCount;