← cproj

Use stdlib func

cda0bd1744c1e717df0e7344db1e2b8331bb81e4 · ovidiucs · Jul 12, 2019 23:09

+52 −191 · 3 files changed

  • mylib/include/myhash.h +2 −1
  • mylib/src/myhash.c +31 −109
  • mylib/test/t_hash.c +19 −81
diff --git a/mylib/include/myhash.h b/mylib/include/myhash.h
index bd788d4..224cf55 100644
--- a/mylib/include/myhash.h
+++ b/mylib/include/myhash.h
@@ -1,4 +1,5 @@
 // Header file for hash routine
+#include <stdbool.h>
 
 
 // Associative array
@@ -21,7 +22,7 @@ typedef struct  {
 extern HashNode *h_insert (HashTable *hTable, char *key, char *value);
 extern HashTable *h_create ( unsigned int size );
 extern HashNode *h_search (HashTable *hTable, char *key);
-extern void h_delete (HashTable *hTable, char *key);
+extern bool h_delete (HashTable *hTable, char *key);
 extern int openFile(const char *fn);
 //void readFile(int fd);
 extern int closefile(int fd);
diff --git a/mylib/src/myhash.c b/mylib/src/myhash.c
index a7b2b17..42a8726 100644
--- a/mylib/src/myhash.c
+++ b/mylib/src/myhash.c
@@ -1,3 +1,4 @@
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -14,16 +15,11 @@
 #define BUFF 2048
 //  Free allocated memory for entire hash table
 #if 0
-void h_free (HashNode *hash) {
-	free(hash->h_key);
-	free(hash->h_value);
-	free(hash);
-}
 #endif
 
 // Hash a key and return integer result.
 
-static size_t hash(char *key, int hashSize) {
+static size_t hash(char *key, unsigned int hashSize) {
     unsigned int k = 0;
     while(*key != '\0')
             k = (k << 2) + (unsigned int) *key++;
@@ -152,111 +148,37 @@ HashNode *h_search (HashTable *hTable, char *key) {
 
 //        void *memset(void *s, int c, size_t n);
 
-void h_delete (HashTable *hTable, char *key) {
+bool h_delete (HashTable *hTable, char *key) {
+	// node 1 pointing to node priror to it
+	// node 2 is the current node we're on
+	HashNode *node1, *node2;
 	size_t resultHash = hash(key,hTable->h_size);
-	HashNode *freeNode;
-	HashNode *emptyMpde = NULL;
-
-	freeNode = h_search(hTable,key);
-	//fprintf(stderr, "%d", sizeof(freeNode));
-	//free(freeNode->h_key);
-	//free(freeNode->h_value);
-	// this deletes the values and key but does not clear the pointers
-	freeNode->h_key = (char *) memset((void *)freeNode->h_key, 0, sizeof(freeNode->h_key));
-	freeNode->h_value =  (char *) memset((void *)freeNode->h_value, 0, sizeof(freeNode->h_value));
-	//&freeNode = (HashNode *) memset((HashNode *)freeNode, 0, sizeof(freeNode));
-	 hTable->h_items[resultHash] = emptyMpde;
-	 free(freeNode);
-	 /*
-	 * (lldb) p *hTable->h_items[5]
-	 * (HashNode) $70 = {
-   * h_next = 0x0000000000000000
-   * h_key = 0x0000555555559300 "400"
-   * h_value = 0x0000555555559320 "1034444444444444"
-   * }
-	 * lldb)  p hTable->h_items[4]
-   * (HashNode *) $87 = 0x0000000000000000
-	 * (lldb)  p hTable->h_items[5]
-	 * (HashNode *) $81 = 0x00005555555592e0
-	 * (lldb)  p &hTable->h_items[5]
-   * (HashNode **) $83 = 0x00005555555592a8
-	 */
-}
-int openFile (const char *fn) {
-		// Return value for open()
-		int inputFd;
-
-		// set fd and check it
-		if ( ( inputFd = open(fn,O_RDONLY) )  < 0){
-			perror("Error on open");
-			return -1;
-
+	node1 = NULL;
+	node2 = hTable->h_items[resultHash];
+
+	
+	while (node2 != NULL) {
+	
+		if(strcmp(node2->h_key,key) == 0) {
+			// remove the node
+			free((void *) node2->h_key);
+			free((void *) node2->h_value);
+			if (node1 == NULL) {
+				hTable->h_items[resultHash] = node2->h_next;				
+			} else  {
+				node1->h_next = node2->h_next;					
+				// unlink from within the list and then do free and return
+				
+				// set node1 pointer to node2 next pointer
+			}
+			free((void *) node2);
+			return true;	
 		}
-		return inputFd;
-}
-/*
- * readline `sz` bytes from file `fn`, begin at file `offset`
- * storing all chars in `buf`. `buf` is terminated at the first
- * newline found .
- * return -1 on error, EOF with 0 chars read , success number of chars read
- */
-
-
-ssize_t readLine(char *buf, size_t sz, char *fn, off_t 	*offset){
-	// Open the file as read only
-	int fd = openFile(fn);
-
-	ssize_t idx = 0;
-
-	/* number of characetrs  */
-	ssize_t nchr = 0;
-
-    /* pointer to chars initialize to null */
-	char *p  = NULL;
-
-	/* position fd & read line */
-	if ( (nchr = lseek(fd, *offset, SEEK_SET)) !=-1  )
-	/* set number of bytes read into nchr variable */
-		nchr  = read (fd, buf, sz);
-	else
-		perror("Error on offset");
-	closeFile(fd);
-
-	/* read error */
-	if (nchr == -1)
-		fprintf(stderr, "%s(), error: read failure in '%s'.\n", __func__, fn);
-
-	/* end of file - no characters read */
-	if (nchr == 0)
-		return -1;
-
-	/* check each character */
-	p = buf;
-	while( (idx < nchr) && (*p != '\n') )
-				p++, idx++;
-	/* if the value is: '\n' -> '\0' then:  */
-	*p = 0;
-
-	/* newline not found */
-	if (idx == nchr) {
-		*offset += nchr;
-
-		/* check file for missing newline at end */
-		return nchr < (ssize_t) sz ? nchr : 0;
-
+		node1 = node2;
+		node2 = node2->h_next;	
 	}
-	/* set *offset to 1+idx*/
-	*offset += idx + 1;
-
-	return idx;
-}
+	
+	return false;
+	//free((void *) hash);
 
-int closeFile(int fd){
-	int closeFd;
-	if ( (closeFd = close(fd)) < 0)
-			perror("Error on close\n");
-//	else if(closeFd == 0) {
-//			fprintf(stderr, "Closed file descriptor\n");
-		return closeFd;
-//	}
 }
diff --git a/mylib/test/t_hash.c b/mylib/test/t_hash.c
index a003b56..086b8ce 100644
--- a/mylib/test/t_hash.c
+++ b/mylib/test/t_hash.c
@@ -18,7 +18,7 @@ static void dumpTable (HashTable *table) {
 	// A new nash node - can be null or something else
 	HashNode *node;
 
-
+	fputs("HASHTABLE DUMP \n",stderr);
 	do {
 		// Derefenrce one level and set it to node
 		node = *hNodePtr;
@@ -44,12 +44,29 @@ static void dumpTable (HashTable *table) {
 	} while (++hNodePtr < hNodePtrZ);
 }
 
+
 // insert multpile keys
 //
+
+
 int main(int argc, char** argv){
 	HashTable *tableResult = h_create(10);
 	assert(tableResult != NULL);
-
+	FILE *stream;
+	// The pointer to the line which was read
+	char *line = NULL;
+	size_t len = 0;
+	ssize_t nread;
+	
+	if (argc != 2) {
+		fprintf(stderr,"Usage: %s <filename>\n", argv[0]);
+	} else {
+		if ( (stream = fopen(argv[1], "r") ) == NULL) {
+			perror("Open failed");
+			exit(1);
+		} else {
+			
+		
 	HashNode  *nodeResult = h_insert(tableResult, "400", "101");
 	assert(nodeResult != NULL);
 
@@ -61,84 +78,5 @@ int main(int argc, char** argv){
 	h_delete(tableResult,"400");
 	dumpTable(tableResult);
 
-	// assign the file descriptor to the integer x
-	// and attempt to open a file
-	int x = openFile("../testdata");
-	fprintf(stderr,"%d\n",x);
-	// read the file
-	//readFile(x);
-	// close the file
-	closeFile(x);
-	//printf(stderr,"%d\n",r);
-
-
-    char line[128] = {0};
-    off_t offset = 0;
-    ssize_t len = 0;
-    size_t i = 0;
-
-	char *token;
-	char *key;
-	char *value;
-
-    /* using open/read, read each line in file into 'line' */
-    while ((len = readLine (line, 128, "../testdata" , &offset)) != -1) {
-			//strtok(line,":");
-			//printf (" line[%2zu] : %s (%zd chars)\n", i++, line, len);
-			token = strtok(line,":");
-			while( token != NULL ) {
-					i++;
-			if( i & 1 ) {
-				char *key = token;
-			} else {
-				char *value = token;
-			h_insert(tableResult, key, value);
-			}
-      		token = strtok(NULL, ":");
-   			}
-
-}
-			/*/while (token != NULL) {
-				fprintf(stderr, "%s\n", token);
-				 token = strtok(NULL, ":");
-			}
-			 */
-
-
-#if 0
-	int i = 0;
-
-
-	char *strings[2];
-	strings[0] = "aac";
-	strings[1] = "ccc";
-	strings[2] = "ddd";`
-	char *sndstring[2];
-	sndstring[0] = "fff";
-	sndstring[1] = "ggg";
-	sndstring[2] = "hhh";
-
-	do {
-		// Otherwise add to the the index returnded by the hasing function
-		// Will print
-	fprintf(stderr,"Struct HashTable address: %p, HashNode** : %p, HashNode* : %p, hash->h_items[%d]: %p\n\n",
-					h_result,
-					h_result->h_items,
-					h_result->h_items[resultHash],
-					resultHash,
-					h_result->h_items[resultHash]
-					);
-	fprintf(stderr,"Struct HashNode address: %p, HashNode value: %s, HashNode key: %s, HashNode next: %p\n",
-					h_Nresult,
-					h_Nresult->h_value,
-					h_Nresult->h_key,
-					h_Nresult->h_next
-					);
-		// Set i to...
-		i +=1;
-		// While condition is bad
-	} while (h_result->h_items[i]  != NULL);
-
-#endif
 	return 0;
 }