diff --git a/src/data.c b/src/data.c new file mode 100644 index 0000000..521eabb --- /dev/null +++ b/src/data.c @@ -0,0 +1,245 @@ +/* + * data.c - Data structure functions + * + * This file is part of libglacier. + * + * Liblacier is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * Libglacier is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Libglacier. If + * not, see . + */ + +/* ALL FUNCTIONS IN THIS FILE DEFINED IN: data.h */ + +/* + * node + * + * DESCRIPTION: Definition of node type. + * DEFINED IN: data.h + * + */ + +struct node { + char *data; + struct node *children[MAX_CHILDREN]; + int numChildren; +}; + +/* +* create_node +* +* DESCRIPTION: Create a dependency tree node. +* PARAMETERS: char *data +* DEFINED IN: data.h +* +*/ + +struct node +*create_node(char *data) +{ + struct node *newNode = (struct node*)malloc(sizeof(struct node)); + newNode->data = strdup(data); + newNode->numChildren = 0; + return newNode; +} + +/* + * add_child + * + * DESCRIPTION: Add a child node to a parent node. + * PARAMETERS: struct node *parent, struct node *child + * DEFINED IN: data.h + * + */ + +void +add_child(struct node *parent, struct node *child) +{ + if (parent->numChildren < MAX_CHILDREN) { + parent->children[parent->numChildren++] = child; + } else { + if (LG_VERBOSE == 1) { errlog("Maximum number of children exceeded"); } + exit(1); + } +} + +/* +* print_tree +* +* DESCRIPTION: Print a dependency tree. +* PARAMETERS: struct node *root, int level +* DEFINED IN: data.h +* +*/ + +void +print_tree(struct node *root, int level) +{ + if (root == NULL) { + return; + } + + for (int i = 0; i < level; i++) { + printf(" "); + } + + printf("%s\n", root->data); + + for (int i = 0; i < root->numChildren; i++) { + print_tree(root->children[i], level + 1); + } +} + +/* + * queue + * + * DESCRIPTION: Definition of queue type. + * DEFINED IN: data.h + * + */ + +typedef struct { + int items[MAX_SIZE]; + int front; + int rear; +} queue; + +/* +* init_queue +* +* DESCRIPTION: Initialize a queue. +* PARAMETERS: queue *q +* DEFINED IN: data.h +* +*/ + +void +init_queue(queue *q) +{ + q -> front = -1; + q -> rear = 0; +} + +/* +* queue_is_empty +* +* DESCRIPTION: Check if queue is empty. +* PARAMETERS: struct node *root, int level +* DEFINED IN: data.h +* +*/ + +bool queue_is_empty(queue *q) { return (q -> front == q -> rear -1); } + +/* +* queue_is_full +* +* DESCRIPTION: Check if queue is full. +* PARAMETERS: queue *q +* DEFINED IN: data.h +* +*/ + +bool queue_is_full(queue *q) { return (q -> rear == MAX_SIZE); } + +/* +* enqueue +* +* DESCRIPTION: Enqueue an element at the back of the queue. +* PARAMETERS: queue *q, int value +* DEFINED IN: data.h +* +*/ + +void +enqueue (queue *q, int value) +{ + if (queue_is_full(q)) { + printf("Queue is full\n"); + return; + } + + q -> items[q -> rear] = value; + q -> rear++; +} + +/* +* dequeue +* +* DESCRIPTION: Dequeue the element at the front of the queue. +* PARAMETERS: queue *q, int value +* DEFINED IN: data.h +* +*/ + +void +dequeue(queue *q) +{ + if (queue_is_empty(q)) { + printf("Queue is empty\n"); + return; + } + + q -> front++; +} + +/* +* peek +* +* DESCRIPTION: View the element at the front of the queue. +* PARAMETERS: struct node *root, int level +* DEFINED IN: data.h +* +*/ + +int +peek(queue *q) +{ + if (queue_is_empty(q)) { + printf("Queue is empty\n"); + return -1; + } + + return q -> items[q -> front + 1]; +} + +/* +* print_queue +* +* DESCRIPTION: Print the queue. +* PARAMETERS: queue *q +* DEFINED IN: data.h +* +*/ + +void +print_queue(queue *q) +{ + if (queue_is_empty(q)) { + printf("Queue is empty\n"); + return; + } + + printf("Current Queue: "); + + for (int i = q -> front + 1; i < q -> rear; i++) { + printf("%d ", q -> items[i]); + } + printf("\n"); +} + +/* + * mkworkspace + * + * DESCRIPTION: Creates a new Glacier workspace in /tmp. + * PARAMETERS: None. + * DEFINED IN: pkgops.h + * + */ + diff --git a/src/libglacier.c b/src/libglacier.c index 94b219f..d72b34a 100644 --- a/src/libglacier.c +++ b/src/libglacier.c @@ -36,6 +36,9 @@ #define MAX_CHILDREN 64 #define MAX_SIZE 256 +typedef unsigned int uint; +typedef unsigned char uchar; + /* * Global Variables * @@ -636,44 +639,22 @@ run_make_task(char TASK[]) } } -/* - * compare_file_hash - * - * DESCRIPTION: Compare two file hashes - * PARAMETERS: char ORIG_HASH[], char FILE[] - * DEFINED IN: security.h - * - */ - -/* -int -compare_file_hash(char ORIG_HASH[], char FILE[]) +void +print_hash(uchar *hash, uint length) { - FILE *pkg; - - pkg = fopen(FILE, "rb"); - if (pkg == NULL) { - return -1; + for (uint index = 0; index < length; index++) { + printf("%02x", hash[index]); } - unsigned char hash[SHA256_DIGEST_LENGTH]; - SHA256_CTX sha256; + printf("\n"); +} - SHA256_Init(&sha256); +void +stash_hash(uchar *hash, uint length) +{ + char stored_hash[32]; - const int hashBufferSize = 1024; - unsigned char hashBuffer[hashBufferSize]; - int bytesRead; - - while ((byesRead = fread(hashBuffer, 1, hashBufferSize, pkg)) != 0) { - SHA256_Update(&sha256, hashBuffer, bytesRead); - } - - SHA256_Final(hash, &sha256); - fclose(pkg); - - for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { - printf("%02x", hash[i]); + for (uint index = 0; index < length; index++) { + sprintf(stored_hash, "%02x", hash[index]); } } -*/