diff options
| author | Filip Strömbäck <fstromback@gmail.com> | 2021-03-16 13:01:32 +0100 |
|---|---|---|
| committer | Filip Strömbäck <fstromback@gmail.com> | 2021-03-16 13:01:32 +0100 |
| commit | b7002c53057d4ae049237786d26f00a762dbc2ea (patch) | |
| tree | f13985eaafedba1d64a6aa835bf93392186d0164 /src/standalone/upg6 | |
| parent | cda174afc8e1bf6b779726965f5eaa030d80b1dc (diff) | |
| download | pintos-rs-b7002c53057d4ae049237786d26f00a762dbc2ea.tar.gz | |
Updated names in standalone/ to match new lab numbering.
Diffstat (limited to 'src/standalone/upg6')
| -rw-r--r-- | src/standalone/upg6/main.c | 124 | ||||
| -rw-r--r-- | src/standalone/upg6/map.c | 1 | ||||
| -rw-r--r-- | src/standalone/upg6/map.h | 1 |
3 files changed, 0 insertions, 126 deletions
diff --git a/src/standalone/upg6/main.c b/src/standalone/upg6/main.c deleted file mode 100644 index c481d40..0000000 --- a/src/standalone/upg6/main.c +++ /dev/null @@ -1,124 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "map.h" - -/* Recommended compile commmand: - * - * gcc -Wall -Wextra -std=gnu99 -pedantic -g main.c map.c - * - * Recommended way to test your solution: - * - * valgrind --tool=memcheck ./a.out - */ -#error Read comments above, then remove this line. - - -/* Can be used to inform compiler about unused parameters (prevent - * warning). Useful when a funtion pointer expect a certain set of - * parameters, but you only need some of them. */ -#define UNUSED __attribute__((unused)) - -/* The code assumes that key_t is `int' and value_t is `char*' */ - -/* function passed as parameter to map_remove_if in order to free the - * memory for all inseted values, and return true to remove them from - * the map */ -bool do_free(key_t k UNUSED, value_t v, int aux UNUSED) -{ - free(v); /*! free memory */ - return true; /* and remove from collection */ -} - -/* function to display all values in the map that are less than the - * aux argument */ -void print_less(key_t k UNUSED, value_t v, int aux) -{ - /* atoi converst from sequence of character to integer, it will fail - * when the characters are letters, check the manpage to see how */ - if ( atoi(v) < aux) - { - printf("%s ", v); - } -} - - -#define LOOPS 10 - -char* my_strdup(char* str) -{ - /*! calculate the length and add space for '\0' */ - int len = strlen(str) + 1; - /*! allocate memory just large enough */ - char* dst = (char*)malloc(len); - /*! copy all characters in src to dst */ - strncpy(dst, str, len); - - return dst; /*(!) return our deep copy of str */ -} - -int main() -{ - struct map container; - char input_buffer[10]; - char* obj; - int id; - int i; - - map_init(&container); - - /* remember to try to insert more values than you map can hold */ - printf("Insert values: "); - for ( i = 0; i < LOOPS; ++i) - { - /* insecure, scanf may overflow the input buffer array * - * very serious, but we ignore it in this test program */ - scanf("%s", input_buffer); - - /*! allocates a copy of the input and inserts in map */ - obj = my_strdup(input_buffer); - id = map_insert(&container, obj); - } - - /* remember to test with invalid keys (like 4711, or -1) */ - for ( i = 0; i < LOOPS; ++i) - { - printf("Enter id to find value for: "); - scanf("%d", &id); - - /*! find the value for a key in the map */ - obj = map_find(&container, id); - - /*! if it was found, display it */ -YOUR CODE - - /* since we leave the value in the map we may use it again and - * should not free the memory */ - } - - /* remember to test with invalid keys (like 4711, or -1) */ - for ( i = 0; i < LOOPS; ++i) - { - printf("Enter id to remove value for: "); - scanf("%d", &id); - - /*! find and remove a value for a key in the map */ - obj = map_remove(&container, id); - - /*! if it was found, display it */ -YOUR CODE - /* since we removed the value from the map we will never use it again and - * must properly free the memory (if it was allocated) */ - } - - /*! print all strings representing an integer less than N */ - printf("Will now display all values less than N. Choose N: "); - scanf("%d", &i); - map_for_each(&container, print_less, i); - - /*! free all remaining memory and remove from map */ - map_remove_if(&container, do_free, 0); - - return 0; -} diff --git a/src/standalone/upg6/map.c b/src/standalone/upg6/map.c deleted file mode 100644 index b323fbb..0000000 --- a/src/standalone/upg6/map.c +++ /dev/null @@ -1 +0,0 @@ -#include "map.h" diff --git a/src/standalone/upg6/map.h b/src/standalone/upg6/map.h deleted file mode 100644 index 9029ff5..0000000 --- a/src/standalone/upg6/map.h +++ /dev/null @@ -1 +0,0 @@ -/* do not forget the guard against multiple includes */ |
