├── 4-exercises ├── data-set-1 │ ├── key_count.txt │ ├── property_count.txt │ └── inventory_file.txt ├── data-set-2 │ ├── key_count.txt │ ├── property_count.txt │ └── inventory_file.txt ├── data-set-3 │ ├── key_count.txt │ ├── property_count.txt │ └── inventory_file.txt ├── exercise-question.pdf └── contents.txt ├── 3-c-implementation ├── common-files │ ├── key_count.txt │ ├── property_count.txt │ └── inventory_file.txt ├── using-library │ ├── libIncludeInven.a │ └── main.c ├── contents.txt └── using-source │ ├── settings.h │ ├── locals.h │ ├── inventory.h │ ├── main.c │ └── inventory.c ├── 2-docs ├── manual.pdf ├── how-to-start.pdf └── contents.txt ├── README.md └── 1-start-here ├── 2-how-to-start.txt └── 1-directory-tree.txt /4-exercises/data-set-1/key_count.txt: -------------------------------------------------------------------------------- 1 | 4 -------------------------------------------------------------------------------- /4-exercises/data-set-2/key_count.txt: -------------------------------------------------------------------------------- 1 | 4 -------------------------------------------------------------------------------- /4-exercises/data-set-3/key_count.txt: -------------------------------------------------------------------------------- 1 | 4 -------------------------------------------------------------------------------- /4-exercises/data-set-1/property_count.txt: -------------------------------------------------------------------------------- 1 | 9 -------------------------------------------------------------------------------- /4-exercises/data-set-2/property_count.txt: -------------------------------------------------------------------------------- 1 | 9 -------------------------------------------------------------------------------- /4-exercises/data-set-3/property_count.txt: -------------------------------------------------------------------------------- 1 | 11 -------------------------------------------------------------------------------- /3-c-implementation/common-files/key_count.txt: -------------------------------------------------------------------------------- 1 | 4 -------------------------------------------------------------------------------- /3-c-implementation/common-files/property_count.txt: -------------------------------------------------------------------------------- 1 | 18 -------------------------------------------------------------------------------- /2-docs/manual.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prakashbh/inventory-data-structure/HEAD/2-docs/manual.pdf -------------------------------------------------------------------------------- /2-docs/how-to-start.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prakashbh/inventory-data-structure/HEAD/2-docs/how-to-start.pdf -------------------------------------------------------------------------------- /4-exercises/exercise-question.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prakashbh/inventory-data-structure/HEAD/4-exercises/exercise-question.pdf -------------------------------------------------------------------------------- /2-docs/contents.txt: -------------------------------------------------------------------------------- 1 | Contents 2 | ======== 3 | 4 | - manual: read this first before you start with anything 5 | - how-to-start: documentation on how to use the code -------------------------------------------------------------------------------- /3-c-implementation/using-library/libIncludeInven.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/prakashbh/inventory-data-structure/HEAD/3-c-implementation/using-library/libIncludeInven.a -------------------------------------------------------------------------------- /4-exercises/contents.txt: -------------------------------------------------------------------------------- 1 | Contents 2 | ======== 3 | 4 | - Includes 3 data sets which can be used for inventory table 5 | - A pdf containing eight exercise questions of various levels -------------------------------------------------------------------------------- /3-c-implementation/contents.txt: -------------------------------------------------------------------------------- 1 | Contents 2 | ======== 3 | 4 | - common-files: files common to both implementations 5 | - using-library: using the inventory data structre as library and header file 6 | - using-source: using the inventory data sytructure as source code -------------------------------------------------------------------------------- /4-exercises/data-set-1/inventory_file.txt: -------------------------------------------------------------------------------- 1 | NULL ram kiran amit amar 2 | c X X X X 3 | java NULL X NULL X 4 | C++ X X NULL X 5 | Database NULL NULL X NULL 6 | .NET NULL NULL X NULL 7 | J2EE X X NULL NULL 8 | OS X X X X 9 | DS X NULL X X 10 | Networking X X X NULL 11 | -------------------------------------------------------------------------------- /4-exercises/data-set-2/inventory_file.txt: -------------------------------------------------------------------------------- 1 | NULL cereals meat junkfood milk 2 | name sprouts beef noodles butter 3 | proteins 40 60 NULL 20 4 | carbohydrates 28 71 20 32 5 | fat 20 40 40 30 6 | cholestrol 10 80 70 25 7 | glucose NULL 30 NULL 10 8 | sucrose NULL NULL NULL NULL 9 | vitamin 30 40 10 20 10 | iron 60 60 20 40 -------------------------------------------------------------------------------- /4-exercises/data-set-3/inventory_file.txt: -------------------------------------------------------------------------------- 1 | NULL tvs hero honda bajaj 2 | name victor pleasure hornet pulsar 3 | type 4-stroke moped 4-stroke 4-stroke 4 | CC 125 110 135 200 5 | Bhp 11.2 10 13 20.5 6 | WheelSize 17 13 17 17 7 | gear 4 NULL 5 5 8 | ABS NULL NULL NULL X 9 | battery 7lb 5lb 9lb 9lb 10 | fuel petrol petrol petrol petrol 11 | topspeed 100 80 130 170 12 | tank 10 5 12 13 -------------------------------------------------------------------------------- /3-c-implementation/common-files/inventory_file.txt: -------------------------------------------------------------------------------- 1 | NULL mobile tv soap bike 2 | name canvas bravia sandal bullet 3 | color gold black brown stone 4 | technology gsm led NULL NULL 5 | sim dual NULL NULL NULL 6 | os android-lollipop NULL NULL NULL 7 | chipset mediatek NULL NULL NULL 8 | cpu cortex-a53 NULL NULL NULL 9 | ram 3gb NULL NULL NULL 10 | memory 32gb NULL NULL NULL 11 | camera 8mp NULL NULL NULL 12 | battery li-ion NULL NULL 8ah-mf 13 | cost 15000 55000 70 141000 14 | power NULL ac NULL NULL 15 | weight 0.2 9 0.4 200 16 | screen 3 40 NULL NULL 17 | rating 4 4 4 4 18 | engine NULL NULL NULL 350cc 19 | tank NULL NULL NULL 20 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | Inventory Data structure is a data structure which can be used to manage inventory data items. To be generic, any kind of 2D data where we have keywords and properties associated with each tabulated in the form of a table. 3 | 4 | # Know More in Wiki 5 | Check out the Wiki Page to know more and how to use the data structure here: https://github.com/prakashbh/inventory-data-structure/wiki 6 | 7 | # Issues 8 | If you have any issues, Kindly log them here https://github.com/prakashbh/inventory-data-structure/issues by creating a new issue. It will be a lot easier if approprriate labels are assigned. We will get back as early as possible. 9 | 10 | -------------------------------------------------------------------------------- /1-start-here/2-how-to-start.txt: -------------------------------------------------------------------------------- 1 | How To Start 2 | ============ 3 | 4 | The directories are numbered accordingly as they have to be 5 | accessed. For the sake of completion following is the order: 6 | 7 | 8 | 1-start-here 9 | ------------ 10 | The directory which you are already accessing, which gives 11 | you these instructions and the tree layout. 12 | 13 | 14 | 2-docs 15 | ------ 16 | Read the manual and understand the data structure. Also read 17 | about how to use the code in another pdf document. 18 | 19 | 20 | 3-c-implementation 21 | ------------------ 22 | Use the implementation of your choice. The preferred option 23 | is to first go with source code and once comfortable, switch 24 | to the library version. 25 | 26 | 27 | 4-exercises 28 | ----------- 29 | This presents you with some sample data sets that you could 30 | work on and few exercise questions. The questions range from 31 | level simple to level complex. 32 | -------------------------------------------------------------------------------- /1-start-here/1-directory-tree.txt: -------------------------------------------------------------------------------- 1 | Directory Structure 2 | =================== 3 | 4 | . 5 | |-- 1-start-here 6 | | |-- 1-directory-tree.txt 7 | | `-- 2-how-to-start.txt 8 | |-- 2-docs 9 | | |-- contents.txt 10 | | |-- how-to-start.pdf 11 | | `-- manual.pdf 12 | |-- 3-c-implementation 13 | | |-- common-files 14 | | | |-- inventory_file.txt 15 | | | |-- key_count.txt 16 | | | `-- property_count.txt 17 | | |-- contents.txt 18 | | |-- using-library 19 | | | |-- inventory-header 20 | | | | `-- inventory.h 21 | | | |-- libIncludeInven.a 22 | | | `-- main.c 23 | | `-- using-source 24 | | |-- inventory.c 25 | | |-- inventory.h 26 | | |-- locals.h 27 | | |-- main.c 28 | | `-- settings.h 29 | |-- 4-exercises 30 | | |-- contents.txt 31 | | |-- data-set-1 32 | | | |-- inventory_file.txt 33 | | | |-- key_count.txt 34 | | | `-- property_count.txt 35 | | |-- data-set-2 36 | | | |-- inventory_file.txt 37 | | | |-- key_count.txt 38 | | | `-- property_count.txt 39 | | |-- data-set-3 40 | | | |-- inventory_file.txt 41 | | | |-- key_count.txt 42 | | | `-- property_count.txt 43 | | `-- exercise-question.pdf 44 | `-- README.md 45 | 46 | -------------------------------------------------------------------------------- /3-c-implementation/using-source/settings.h: -------------------------------------------------------------------------------- 1 | #ifndef SETTINGS_H_INCLUDED 2 | #define SETTINGS_H_INCLUDED 3 | 4 | // These variables will be initialized from the files 5 | // which maintains the count of keywords and properties 6 | int key_count = 0; 7 | int property_count = 0; 8 | 9 | 10 | // Memory allocated to the inventory table will be as per following: 11 | // 1. key_count + value initialized for key_size for keywords 12 | // 2. property_count + value initialized for property_size for properties 13 | // The property count is initialized to 2 as currently the properties 14 | // cannot be added once the value is set in this version release. 15 | // 3. length of each word in table as set in word_length variable. 16 | // Any modified settings can be done to respective variables 17 | // accordingly. 18 | int key_size = 20; 19 | int property_size = 2; 20 | int word_length = 25; 21 | 22 | 23 | // Flog file pointer is used to write the log files 24 | FILE *flog; 25 | 26 | // Below mentioned are error status/status codes 27 | #define SUCCESS 1 28 | #define FAILURE -1 29 | #define FILE_OPEN_ERROR 1001 30 | #define MEMORY_ALLOCATION_ERROR 1002 31 | #define FILE_DELETE_ERROR 1003 32 | #define COUNT_EXCEED_ERROR 1004 33 | #define RESULT_NOT_FOUND 1005 34 | #define FILE_EMPTY_ERROR 1006 35 | 36 | #endif // SETTINGS_H_INCLUDED 37 | -------------------------------------------------------------------------------- /3-c-implementation/using-source/locals.h: -------------------------------------------------------------------------------- 1 | #ifndef LOCALS_H_INCLUDED 2 | #define LOCALS_H_INCLUDED 3 | 4 | // This is a local variable file 5 | // Following variables can be used in the main program 6 | 7 | // Variable for take choice input from the user 8 | // Can be used for: 9 | // -- switch cases 10 | // -- menu options 11 | // -- Any other scenario where user is prompted for a choice to input 12 | int choice = 0; 13 | 14 | 15 | // The return status of success or failure of any function can be 16 | // captured in status variable 17 | int status = 0; 18 | 19 | 20 | // string_array is a variable to capture the string variables 21 | // Memory for this has to be allocated as per need based on 22 | // the number of strings to be loaded. 23 | char ** string_array; 24 | 25 | // The tindex variable can be used as a index in loops 26 | // It stands for temporary index 27 | int tindex = 0; 28 | 29 | // The variable index_number is used to refer to any index 30 | // value for Key or property 31 | // It is a integer value referring to index of any array 32 | int index_number = 0; 33 | 34 | 35 | // The variable string_value can be used to hold a string 36 | // The maximum value set is 25 and can be changed as per need 37 | char string_value[25]; 38 | 39 | 40 | // The variable count is used to hold the counters 41 | int count = 0; 42 | 43 | 44 | // Pointer to hold the single index [x,y] of the table 45 | int *sindex; 46 | 47 | 48 | // Use the search_index variable to hold the search results 49 | // It carries the indexes of the results in the inventory table 50 | // The index search_index[0][0] holds the count of number of search results. 51 | // If the value search_index[0][0] == -1, then no results were found. 52 | int search_index[100][2]; 53 | 54 | 55 | #endif // LOCALS_H_INCLUDED 56 | -------------------------------------------------------------------------------- /3-c-implementation/using-source/inventory.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************************** 2 | Copyright (c) 2016 Prakash B Hegade, Vishwanath A Telsang 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software 5 | and associated documentation files (the "Software"), to deal in the Software without restriction, 6 | including without limitation the rights to use, copy, modify, merge, publish, distribute, 7 | sub-license, and/or sell copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial 11 | portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 14 | NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 15 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 16 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 17 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 18 | 19 | ***************************************************************************************************/ 20 | #ifndef INVENTORY_H_INCLUDED 21 | #define INVENTORY_H_INCLUDED 22 | 23 | // Including all the required header files 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | 32 | // Variable to hold the inventory data containing the 33 | // keywords and properties 34 | char ***inventory; 35 | 36 | 37 | // Variable to capture the time details 38 | time_t mytime; 39 | 40 | 41 | // Keeps a log of activity. Any errors can be checked in this file 42 | FILE *flog; 43 | 44 | 45 | // Function Declarations 46 | int load(); 47 | int dump(); 48 | void open_log(); 49 | int inventory_memory_allocation(); 50 | void close_log(); 51 | void write_log(char * , char *, char *); 52 | int file_empty_check(char *); 53 | int load(); 54 | int inventory_memory_allocation(); 55 | int add_key(); 56 | char ** string_array_memory_allocation(int); 57 | char ** get_keys(); 58 | char ** get_properties(); 59 | char ** get_key_properties(int); 60 | char * get_key_name(int); 61 | char * get_property_name(int); 62 | int * search_key(char array[]); 63 | int * search_property(char property[]); 64 | void search_any(char c_array[], int i_array[100][2]); 65 | void print(); 66 | void print_formatted(); 67 | void inventory_memory_deallocation(); 68 | int dump(); 69 | 70 | #endif // INVENTORY_H_INCLUDED 71 | -------------------------------------------------------------------------------- /3-c-implementation/using-source/main.c: -------------------------------------------------------------------------------- 1 | #include "inventory.c" 2 | #include "locals.h" 3 | 4 | void menu() 5 | { 6 | printf("----------------------------------\n"); 7 | printf("\n\t\tMENU\n"); 8 | printf("----------------------------------\n"); 9 | printf("0-exit\n"); 10 | printf("1-Print Table\n"); 11 | printf("2-Get All Keys\n"); 12 | printf("3-Get All Specs\n"); 13 | printf("4-Get Properties for Key\n"); 14 | printf("5-Get Specific Key\n"); 15 | printf("6-Get Specific Property\n"); 16 | printf("7-Search for a Key\n"); 17 | printf("8-Search for a Property\n"); 18 | printf("9-Search for Any\n"); 19 | printf("10-Add a New Key\n"); 20 | printf("--------------------------------\n"); 21 | } 22 | 23 | int main() 24 | { 25 | status = load(); 26 | if(status != 1){ 27 | printf("Load Failed\n"); 28 | return 0; 29 | } 30 | 31 | while(1) 32 | { 33 | menu(); 34 | printf("Enter your choice\n"); 35 | scanf("%d", &choice); 36 | 37 | switch(choice) 38 | { 39 | case 0: printf("Data dumped to file\n"); 40 | status = dump(); 41 | if(status != 1) 42 | printf("Dump Failed\n. Please see the tempfile.txt\n"); 43 | return 0; 44 | 45 | 46 | case 1: print(); 47 | // a function print_formatted() can be called to print formatted table 48 | break; 49 | 50 | case 2: string_array = get_keys(); 51 | for (tindex = 0; tindex <= key_count; tindex++) 52 | printf("%s\n", string_array[tindex]); 53 | free(string_array); 54 | break; 55 | 56 | case 3: string_array = get_properties(); 57 | for (tindex = 0; tindex <= property_count; tindex++) 58 | printf("%s\n", string_array[tindex]); 59 | free(string_array); 60 | break; 61 | 62 | case 4: printf("Enter index number of key\n"); 63 | scanf("%d", &index_number); 64 | string_array = get_key_properties(index_number); 65 | if(string_array == NULL) 66 | printf("Invalid input or Failure\n"); 67 | else { 68 | for (tindex = 0; tindex <= property_count; tindex++) 69 | printf("%s\n", string_array[tindex]); 70 | free(string_array); 71 | } 72 | break; 73 | 74 | case 5: printf("Enter index number. There are %d keywords\n", key_count); 75 | scanf("%d", &index_number); 76 | printf("The returned keyword is %s\n", get_key_name(index_number)); 77 | break; 78 | 79 | case 6: printf("Enter index number.There are %d specs\n", property_count); 80 | scanf("%d", &index_number); 81 | printf("The returned spec is %s\n", get_property_name(index_number)); 82 | break; 83 | 84 | case 7: printf("\nEnter the key to be searched\n"); 85 | scanf("%s", string_value); 86 | 87 | sindex = search_key(string_value); 88 | if(sindex == NULL) 89 | printf("Key NOT found\n"); 90 | else 91 | printf("Key found at [%d %d]\n", sindex[0], sindex[1]); 92 | break; 93 | 94 | case 8: printf("\nEnter the spec to be searched\n"); 95 | scanf("%s", string_value); 96 | 97 | sindex = search_property(string_value); 98 | if(sindex == NULL) 99 | printf("Spec NOT found\n"); 100 | else 101 | printf("Spec found at [%d %d]\n", sindex[0], sindex[1]); 102 | break; 103 | 104 | case 9: printf("\nEnter any word to be searched\n"); 105 | scanf("%s", string_value); 106 | search_any(string_value, search_index); 107 | 108 | if(search_index[0][0] == -1) 109 | printf("No Occurrences found\n"); 110 | else { 111 | count = search_index[0][0]; 112 | for (tindex = 1; tindex<= count; tindex++) 113 | printf("Occurrence found at [%d %d]\n", search_index[tindex][0], search_index[tindex][1]); 114 | } 115 | break; 116 | 117 | case 10: add_key(); 118 | break; 119 | 120 | 121 | } 122 | 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /3-c-implementation/using-library/main.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************************** 2 | Copyright (c) 2016 Prakash B Hegade, Vishwanath A Telsang 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software 5 | and associated documentation files (the "Software"), to deal in the Software without restriction, 6 | including without limitation the rights to use, copy, modify, merge, publish, distribute, 7 | sub-license, and/or sell copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial 11 | portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 14 | NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 15 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 16 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 17 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 18 | 19 | ***************************************************************************************************/ 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | 26 | // This is a local variable file 27 | // Following variables can be used in the main program 28 | 29 | // Variable for take choice input from the user 30 | // Can be used for: 31 | // -- switch cases 32 | // -- menu options 33 | // -- Any other scenario where user is prompted for a choice to input 34 | int choice = 0; 35 | 36 | 37 | // The return status of success or failure of any function can be 38 | // captured in status variable 39 | int status = 0; 40 | 41 | 42 | // string_array is a variable to capture the string variables 43 | // Memory for this has to be allocated as per need based on 44 | // the number of strings to be loaded. 45 | char ** string_array; 46 | 47 | // The tindex variable can be used as a index in loops 48 | // It stands for temporary index 49 | int tindex = 0; 50 | 51 | // The variable index_number is used to refer to any index 52 | // value for Key or property 53 | // It is a integer value referring to index of any array 54 | int index_number = 0; 55 | 56 | 57 | // The variable string_value can be used to hold a string 58 | // The maximum value set is 25 and can be changed as per need 59 | char string_value[25]; 60 | 61 | 62 | // The variable count is used to hold the counters 63 | int count = 0; 64 | 65 | 66 | // Pointer to hold the single index [x,y] of the table 67 | int *sindex; 68 | 69 | 70 | // Use the search_index variable to hold the search results 71 | // It carries the indexes of the results in the inventory table 72 | // The index search_index[0][0] holds the count of number of search results. 73 | // If the value search_index[0][0] == -1, then no results were found. 74 | int search_index[100][2]; 75 | 76 | void menu() 77 | { 78 | printf("----------------------------------\n"); 79 | printf("\n\t\tMENU\n"); 80 | printf("----------------------------------\n"); 81 | printf("0-exit\n"); 82 | printf("1-Print Table\n"); 83 | printf("2-Get All Keys\n"); 84 | printf("3-Get All Specs\n"); 85 | printf("4-Get Properties for Key\n"); 86 | printf("5-Get Specific Key\n"); 87 | printf("6-Get Specific Property\n"); 88 | printf("7-Search for a Key\n"); 89 | printf("8-Search for a Property\n"); 90 | printf("9-Search for Any\n"); 91 | printf("10-Add a New Key\n"); 92 | printf("--------------------------------\n"); 93 | } 94 | 95 | int main() 96 | { 97 | status = load(); 98 | if(status != 1){ 99 | printf("Load Failed\n"); 100 | return 0; 101 | } 102 | 103 | while(1) 104 | { 105 | menu(); 106 | printf("Enter your choice\n"); 107 | scanf("%d", &choice); 108 | 109 | switch(choice) 110 | { 111 | case 0: printf("Data dumped to file\n"); 112 | status = dump(); 113 | if(status != 1) 114 | printf("Dump Failed\n. Please see the tempfile.txt\n"); 115 | return 0; 116 | 117 | 118 | case 1: print(); 119 | break; 120 | 121 | case 2: string_array = get_keys(); 122 | for (tindex = 0; tindex <= key_count; tindex++) 123 | printf("%s\n", string_array[tindex]); 124 | free(string_array); 125 | break; 126 | 127 | case 3: string_array = get_properties(); 128 | for (tindex = 0; tindex <= property_count; tindex++) 129 | printf("%s\n", string_array[tindex]); 130 | free(string_array); 131 | break; 132 | 133 | case 4: printf("Enter index number of key\n"); 134 | scanf("%d", &index_number); 135 | string_array = get_key_properties(index_number); 136 | if(string_array == NULL) 137 | printf("Invalid input or Failure\n"); 138 | else { 139 | for (tindex = 0; tindex <= property_count; tindex++) 140 | printf("%s\n", string_array[tindex]); 141 | free(string_array); 142 | } 143 | break; 144 | 145 | case 5: printf("Enter index number. There are %d keywords\n", key_count); 146 | scanf("%d", &index_number); 147 | printf("The returned keyword is %s\n", get_key_name(index_number)); 148 | break; 149 | 150 | case 6: printf("Enter index number.There are %d specs\n", property_count); 151 | scanf("%d", &index_number); 152 | printf("The returned spec is %s\n", get_property_name(index_number)); 153 | break; 154 | 155 | case 7: printf("\nEnter the key to be searched\n"); 156 | scanf("%s", string_value); 157 | 158 | sindex = search_key(string_value); 159 | if(sindex == NULL) 160 | printf("Key NOT found\n"); 161 | else 162 | printf("Key found at [%d %d]\n", sindex[0], sindex[1]); 163 | break; 164 | 165 | case 8: printf("\nEnter the spec to be searched\n"); 166 | scanf("%s", string_value); 167 | 168 | sindex = search_property(string_value); 169 | if(sindex == NULL) 170 | printf("Spec NOT found\n"); 171 | else 172 | printf("Spec found at [%d %d]\n", sindex[0], sindex[1]); 173 | break; 174 | 175 | case 9: printf("\nEnter any word to be searched\n"); 176 | scanf("%s", string_value); 177 | search_any(string_value, search_index); 178 | 179 | if(search_index[0][0] == -1) 180 | printf("No Occurrences found\n"); 181 | else { 182 | count = search_index[0][0]; 183 | for (tindex = 1; tindex<= count; tindex++) 184 | printf("Occurrence found at [%d %d]\n", search_index[tindex][0], search_index[tindex][1]); 185 | } 186 | break; 187 | 188 | case 10: add_key(); 189 | break; 190 | 191 | 192 | } 193 | 194 | } 195 | } 196 | -------------------------------------------------------------------------------- /3-c-implementation/using-source/inventory.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************************** 2 | Copyright (c) 2016 Prakash B Hegade, Vishwanath A Telsang 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software 5 | and associated documentation files (the "Software"), to deal in the Software without restriction, 6 | including without limitation the rights to use, copy, modify, merge, publish, distribute, 7 | sub-license, and/or sell copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all copies or substantial 11 | portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 14 | NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 15 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 16 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 17 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 18 | 19 | ***************************************************************************************************/ 20 | 21 | 22 | #include "inventory.h" 23 | #include "settings.h" 24 | 25 | 26 | //Function: open_log 27 | //Description: opens the log file to write the success or failure status 28 | //Input param: NULL 29 | //Return Type: NULL 30 | void open_log() 31 | { 32 | // Open the file 33 | flog = fopen("log.txt", "a+"); 34 | 35 | 36 | // If unable to open the file, alert the user 37 | if(flog == NULL) 38 | printf("Unable to open log file. No log status will be captured."); 39 | else 40 | fprintf(flog, "%s", "_________________________START OF ITERATION________________________"); 41 | 42 | fprintf(flog, "%s", "\n\n"); 43 | } 44 | 45 | 46 | //Function: close_log 47 | //Description: closes the log file if opened 48 | //Input param: NULL 49 | //Return Type: NULL 50 | void close_log() 51 | { 52 | if(flog != NULL) { 53 | // Add a message to indicate end of a iteration 54 | fprintf(flog, "%s", "_________________________END OF ITERATION________________________"); 55 | fprintf(flog, "%s", "\n\n"); 56 | 57 | // Close the file pointer 58 | fclose(flog); 59 | } 60 | } 61 | 62 | 63 | //Function: write_log 64 | //Description: logs the status with success or failure message 65 | //Input param: function name, status and message to display 66 | //Return Type: NULL 67 | void write_log(char * function_name, char *status, char * message) 68 | { 69 | // Write the appropriate log message with associated run time 70 | mytime = time(NULL); 71 | fprintf(flog, "%s %s : %s --> %s\n\n", ctime(&mytime), function_name, status, message); 72 | 73 | } 74 | 75 | 76 | //Function: file_empty_check 77 | //Description: Check whether file has content or not 78 | //Input param: name of the file 79 | //Return Type: integer 80 | // success state is returned if file has content 81 | // FILE_EMPTY_ERROR otherwise 82 | int file_empty_check(char *filename) 83 | { 84 | // Declaring "stat" a system call to know about content of file. 85 | struct stat filestat; 86 | stat (filename,&filestat); 87 | 88 | // Variables to hodl messages 89 | char message_failure[40]; 90 | char message_success[40]; 91 | 92 | strcpy(message_failure, filename); 93 | strcat(message_failure, " is empty"); 94 | 95 | strcpy(message_success, filename); 96 | strcat(message_success, " has content"); 97 | 98 | //check size of file and return appropriate status. 99 | if( filestat.st_size == 0 ){ 100 | write_log("file_empty_check","FILE_EMPTY_ERROR",message_failure); 101 | return FILE_EMPTY_ERROR; 102 | } 103 | else { 104 | write_log("file_empty_check","SUCCESS",message_success); 105 | return SUCCESS; 106 | } 107 | } 108 | 109 | 110 | //Function: inventory_memory_allocation 111 | //Description: dynamically allocates the memory to the inventory table 112 | //Input param: NULL 113 | //Return Type: integer 114 | // success status on successful memory allocation 115 | // MEMORY_ALLOCATION_ERROR on failure. 116 | int inventory_memory_allocation() 117 | { 118 | // Local variables 119 | int index1; 120 | int index2; 121 | 122 | // Calculate the required size for keywords and properties. 123 | // We allocate the the current count plus some additional 124 | // for the new incoming values for the table. 125 | property_size = property_size + property_count; 126 | key_size = key_size + key_count; 127 | 128 | inventory = malloc(property_size * sizeof(char**)); 129 | if(inventory == NULL) 130 | return MEMORY_ALLOCATION_ERROR; 131 | 132 | for(index1 = 0; index1 < property_size; index1++) { 133 | inventory[index1] = malloc(key_size * sizeof(char*)); 134 | if(inventory[index1] == NULL) 135 | return MEMORY_ALLOCATION_ERROR; 136 | for(index2 = 0; index2 < key_size; index2++) { 137 | inventory[index1][index2] = malloc(word_length * sizeof(char)); 138 | if(inventory[index1][index2] == NULL) 139 | return MEMORY_ALLOCATION_ERROR; 140 | } 141 | } 142 | 143 | write_log("inventory_memory_allocation", "SUCCESS", "Memory allocation Successful"); 144 | return SUCCESS; 145 | } 146 | 147 | 148 | //Function: load 149 | //Description: loads the key_count and property_count from the respective file 150 | // loads the inventory table data from secondary memory to primary 151 | //Input param: NULL 152 | //Return Type: integer 153 | // success status on successful operation 154 | // respective error status code otherwise 155 | int load() 156 | { 157 | 158 | // Start the log file 159 | open_log(); 160 | 161 | // Use a file pointer to open various files to load the values 162 | FILE *fp; 163 | 164 | //Local variables 165 | int index = 0; 166 | int key_index = 0; 167 | int status = 0; 168 | int file_status = 0; 169 | 170 | 171 | //check whether key_count.txt file is empty or not. 172 | file_status = file_empty_check("key_count.txt"); 173 | if (file_status == 1006) 174 | return FAILURE; 175 | 176 | //check whether property_count.txt file is empty or not. 177 | file_status = file_empty_check("property_count.txt"); 178 | if (file_status == 1006) 179 | return FAILURE; 180 | 181 | //check whether inventory_file.txt file is empty or not 182 | file_status = file_empty_check("inventory_file.txt"); 183 | if (file_status == 1006) 184 | return FAILURE; 185 | 186 | // Open the key_count file to read the number of keywords 187 | fp = fopen("key_count.txt","r"); 188 | if(fp == NULL) { 189 | write_log("load", "FILE_OPEN_ERROR", "Unable to open the key_count file"); 190 | return FAILURE; 191 | } 192 | 193 | fscanf(fp,"%d", &key_count); 194 | if(key_count <= 0 ) { 195 | write_log("load", "FAILURE", "Key count is 0 or less than 0"); 196 | return FAILURE; 197 | } 198 | write_log("load", "SUCCESS", "Key count read successfully"); 199 | fclose(fp); 200 | 201 | // Open the property_count file to read the number of properties 202 | fp = fopen("property_count.txt","r"); 203 | if(fp == NULL) { 204 | write_log("load", "FILE_OPEN_ERROR", "Unable to open the property_count file"); 205 | return FAILURE; 206 | } 207 | 208 | fscanf(fp,"%d", &property_count); 209 | if (property_count <= 0) { 210 | write_log("load", "FAILURE", "property count is 0 or less than 0"); 211 | return FAILURE; 212 | } 213 | write_log("load", "SUCCESS", "Property count read successfully"); 214 | fclose(fp); 215 | 216 | 217 | // Open the inventory_file to read the available inventory details 218 | fp = fopen("inventory_file.txt", "r"); 219 | if (fp == NULL) { 220 | write_log("load", "FILE_OPEN_ERROR", "Error in opening the inventory_file"); 221 | return FAILURE; 222 | } 223 | 224 | // Allocate the memory for inventory table 225 | status = inventory_memory_allocation(); 226 | 227 | if(status == 1002) { 228 | write_log("load", "MEMORY_ALLOCATION_ERROR", "No memory for inventory table"); 229 | return FAILURE; 230 | } 231 | 232 | // Load the details from file to main memory 233 | while(!feof(fp)) { 234 | for(index = 0; index <= key_count; index++) { 235 | fscanf(fp, "%s ", inventory[key_index][index]); 236 | } 237 | key_index++; 238 | } 239 | fclose(fp); 240 | 241 | write_log("load", "SUCCESS", "Inventory Load Successful"); 242 | return SUCCESS; 243 | } 244 | 245 | 246 | //Function: add_key 247 | //Description: reads the data from the file and loads the key and the details 248 | // into the inventory-table present in main memory 249 | //Input param: NULL 250 | //Return Type: integer type 251 | // success status is returned if key is successfully 252 | // added to inventory file 253 | // failure status otherwise 254 | int add_key() 255 | { 256 | // Local variables 257 | FILE *fp; 258 | char key[20]; 259 | char property[20]; 260 | int index=0; 261 | char filename[256]; 262 | int status = 0; 263 | int file_status = 0; 264 | 265 | // Read the file name from the user 266 | printf("\nEnter the file name which contains the keyword and property details:"); 267 | printf("\nNOTE:\n"); 268 | printf("1-The file name has to end with .txt extension\n"); 269 | printf("2-The file will not be validated for error input format\n"); 270 | printf("3-The file has to be placed in the project directory\n"); 271 | printf("4-Keep a backup of file if required.\n"); 272 | printf(" The file placed in directory will be deleted after load\n"); 273 | scanf("%s", filename); 274 | 275 | fp = fopen(filename, "r"); 276 | if(fp == NULL) { 277 | write_log("add_key", "FILE_OPEN_ERROR", "Unable to open the supplied file"); 278 | printf("Invalid file specified. Please check.\n\n"); 279 | return FAILURE; 280 | } 281 | 282 | // Check whether file having new key is empty 283 | file_status = file_empty_check(filename); 284 | if (file_status == 1006) { 285 | printf("\n\nFile having new key is empty\n"); 286 | return FAILURE; 287 | } 288 | 289 | // Increment the key count as the new keyword will be added 290 | key_count++; 291 | 292 | // Read the data from file and load into the main memory 293 | while(!feof(fp)) 294 | { 295 | fscanf(fp,"%s %s", key, property); 296 | strcpy(inventory[index][key_count], property); 297 | index++; 298 | } 299 | 300 | // Write the success log and close the file 301 | write_log("add_key", "SUCCESS", "The file contents were successfully copied"); 302 | fclose(fp); 303 | 304 | // Delete the file as, it is no more required 305 | status = remove(filename); 306 | if(status == 0 ) 307 | write_log("add_key", "SUCCESS", "The file was successfully deleted"); 308 | else 309 | write_log("add_key", "FILE_DELETE_ERROR", "The file was not deleted. Requires manual delete."); 310 | 311 | return SUCCESS; 312 | } 313 | 314 | 315 | //Function: string_array_memory_allocation 316 | //Description: dynamically allocate the memory for array of strings 317 | //Input param: size indicating number of strings for which the memory 318 | // needs to be allocated 319 | //Return Type: starting address on successful allocation 320 | // NULL on failure 321 | char ** string_array_memory_allocation(int size) 322 | { 323 | // Increase the size by one as 0th location is used for names 324 | size = size + 1; 325 | 326 | // First allocate the memory for array of pointers 327 | char **string_array = malloc(size * sizeof(char *)); 328 | if(string_array == NULL) 329 | return NULL; 330 | 331 | // We then allocate memory for each of the pointer 332 | int index; 333 | for (index = 0; index < size; index++) { 334 | string_array[index] = (char *)malloc(word_length * sizeof(char)); 335 | if(string_array[index] == NULL) 336 | return NULL; 337 | } 338 | 339 | // Return the successfully allocated array 340 | write_log("string_array_memory_allocation", "SUCCESS", "Memory allocated"); 341 | return string_array; 342 | } 343 | 344 | 345 | //Function: gets_keys 346 | //Description: loads all the keys in the character array 347 | //Input param: NULL 348 | //Return Type: pointer to character array 349 | // on success character array holding all keys 350 | // NULL otherwise 351 | char ** get_keys() 352 | { 353 | // Variable to load all the keys 354 | char **tstring_array; 355 | 356 | // Call the function to allocate the required memory 357 | tstring_array = string_array_memory_allocation(key_count); 358 | if(tstring_array == NULL) 359 | { 360 | write_log("get_keys", "MEMORY_ALLOCATION_ERROR", "No memory for keys array"); 361 | return NULL; 362 | } 363 | 364 | // Load all the keys from inventory table 365 | int index=0; 366 | for (index = 0; index <= key_count; index++) { 367 | tstring_array[index] = inventory[0][index]; 368 | } 369 | 370 | write_log("get_keys", "SUCCESS", "Keys successfully loaded to variable"); 371 | return tstring_array; 372 | } 373 | 374 | 375 | //Function: get_properties 376 | //Description: loads all the properties in the string array 377 | //Input param: NULL 378 | //Return Type: pointer to character array 379 | // on success character array holding all properties 380 | // NULL otherwise 381 | char ** get_properties() 382 | { 383 | // Variable to load all the properties 384 | char **tstring_array; 385 | 386 | // Call the function to allocate the required memory 387 | tstring_array = string_array_memory_allocation(property_count); 388 | if(tstring_array == NULL) 389 | { 390 | write_log("get_properties", "MEMORY_ALLOCATION_ERROR", "No memory for properties array"); 391 | return NULL; 392 | } 393 | 394 | // Load all the properties from inventory table 395 | int index=0; 396 | for (index = 0; index <= property_count; index++) { 397 | tstring_array[index] = inventory[index][0]; 398 | } 399 | 400 | write_log("get_properties", "SUCCESS", "Properties successfully loaded to variable"); 401 | return tstring_array; 402 | } 403 | 404 | 405 | //Function: get_key_properties 406 | //Description: loads all the properties for the given index of key, 407 | // from inventory table in the supplied array 408 | //Input param: key's index number for which all it's properties 409 | // has to be loaded 410 | //Return Type: pointer to character array 411 | // character array is returned holding all properties for given key 412 | // NULL otherwise 413 | char ** get_key_properties(int index_number) 414 | { 415 | if (index_number > key_count){ 416 | write_log("get_key_properties", "COUNT_EXCEED_ERROR", "Invalid counter supplied"); 417 | return NULL; 418 | } 419 | 420 | // Variable to load all the properties 421 | char **tstring_array; 422 | 423 | // Call the function to allocate the required memory 424 | tstring_array = string_array_memory_allocation(property_count); 425 | if(tstring_array == NULL) 426 | { 427 | write_log("get_key_properties", "MEMORY_ALLOCATION_ERROR", "No memory for properties array"); 428 | return NULL; 429 | } 430 | 431 | // Load all the properties from inventory table 432 | int index=0; 433 | for (index = 0; index <= property_count; index++) { 434 | tstring_array[index] = inventory[index][index_number]; 435 | } 436 | 437 | write_log("get_key_properties", "SUCCESS", "Requested Properties successfully loaded to variable"); 438 | return tstring_array; 439 | } 440 | 441 | 442 | //Function: get_key_name 443 | //Description: gets the name of the keyword at the specified location 444 | //Input param: integer value which mentions the index 445 | //Return Type: pointer to character array 446 | // returns the key if index exists 447 | // COUNT_EXCEED_ERROR otherwise 448 | char * get_key_name(int index_number) 449 | { 450 | // Check if the input supplied is in valid range 451 | if (index_number > key_count){ 452 | write_log("get_key_name", "COUNT_EXCEED_ERROR", "Invalid counter supplied"); 453 | return "COUNT_EXCEED_ERROR"; 454 | } 455 | else { 456 | write_log("get_key_name", "SUCCESS", "Returned expected value"); 457 | return inventory[0][index_number]; 458 | } 459 | } 460 | 461 | //Function: get_property_name 462 | //Description: gets the name of the property at the specified location 463 | //Input param: integer value which mentions the index 464 | //Return Type: character array 465 | // Returns the property if, index exists 466 | // COUNT_EXCEED_ERROR otherwise 467 | char * get_property_name(int index_number) 468 | { 469 | // Check if the input supplied is in valid range 470 | if (index_number > property_count) { 471 | write_log("get_property_name", "COUNT_EXCEED_ERROR", "Invalid counter supplied"); 472 | return "COUNT_EXCEED_ERROR"; 473 | } 474 | else { 475 | write_log("get_property_name", "SUCCESS", "Returned expected value"); 476 | return inventory[index_number][0]; 477 | } 478 | } 479 | 480 | 481 | //Function: search_key 482 | //Description: gets the index of the keyword to be searched 483 | //Input param: character array which holds keyword to be searched 484 | //Return Type: pointer to integer array 485 | // returns the keyword index if key exists 486 | // NULL value otherwise 487 | int * search_key(char tstring[20]) 488 | { 489 | // Local variables 490 | static int index[2]; 491 | int flag = 0; 492 | int temp_index = 0; 493 | 494 | // Keyword is always found at location index [0, x] 495 | // We can initialize the first index to 0 496 | index[0] = 0; 497 | 498 | // Convert the supplied input to lower cases 499 | for (temp_index = 0; temp_index < strlen(tstring); temp_index++) 500 | tstring[temp_index] = tolower(tstring[temp_index]); 501 | 502 | // Search for the required keyword 503 | for (temp_index = 1; temp_index <= key_count; temp_index++) { 504 | if((strcmp(inventory[0][temp_index], tstring)) == 0) { 505 | index[1] = temp_index; 506 | flag = 1; 507 | break; 508 | } 509 | } 510 | 511 | // Return the status based on the search result 512 | if(flag ==1){ 513 | write_log("search_key", "SUCCESS", "Key index returned"); 514 | return index; 515 | } 516 | else{ 517 | write_log("search_key", "RESULT_NOT_FOUND", "Invalid search Key"); 518 | return NULL; 519 | } 520 | } 521 | 522 | 523 | //Function: search_property 524 | //Description: gets the index of the property to be searched 525 | //Input param: character array which holds property to be searched 526 | //Return Type: pointer to integer array 527 | // returns the property index if property exists 528 | // NULL value otherwise 529 | int * search_property(char tstring[20]) 530 | { 531 | static int index[2]; 532 | int flag = 0; 533 | int temp_index = 0; 534 | 535 | // Keyword is always found at location index [x, 0] 536 | // We can initialize the second index to 0 537 | index[1] = 0; 538 | 539 | // Convert the supplied input to lower cases 540 | for (temp_index = 0; temp_index < strlen(tstring); temp_index++) 541 | tstring[temp_index] = tolower(tstring[temp_index]); 542 | 543 | // Search for the required keyword 544 | for (temp_index = 1; temp_index <= property_count; temp_index++) { 545 | if((strcmp(inventory[temp_index][0], tstring)) == 0) { 546 | index[0] = temp_index; 547 | flag = 1; 548 | break; 549 | } 550 | } 551 | 552 | // Return the status based on the search result 553 | if(flag == 1){ 554 | write_log("search_property", "SUCCESS", "Property index returned"); 555 | return index; 556 | } 557 | else{ 558 | write_log("search_property", "RESULT_NOT_FOUND", "Invalid search property"); 559 | return NULL; 560 | } 561 | 562 | } 563 | 564 | 565 | //Function: search_any 566 | //Description: searches for the given word in the entire table, for all 567 | // the occurrences 568 | // returns indexes of all occurrences of word if search word exists, 569 | // returns -1 otherwise. 570 | // variable to hold indices will be supplied as input param 571 | //Input param: character array, word to be searched and integer array 572 | // to hold the indexes of found locations 573 | //Return Type: NULL 574 | void search_any(char tstring[20], int search_index[100][2]) 575 | { 576 | // Local variables 577 | int spec_index; 578 | int key_index; 579 | int temp_index = 0; 580 | 581 | // Convert the supplied input to lower cases 582 | for (temp_index = 0; temp_index < strlen(tstring); temp_index++) 583 | tstring[temp_index] = tolower(tstring[temp_index]); 584 | 585 | // To load indexes of matched word 586 | temp_index = 1; 587 | 588 | // Search for the word 589 | for (spec_index = 0; spec_index <= property_count; spec_index++) { 590 | for (key_index = 0; key_index <= key_count; key_index++) { 591 | if((strcmp(inventory[spec_index][key_index], tstring)) == 0) { 592 | search_index[temp_index][0] = spec_index; 593 | search_index[temp_index][1] = key_index; 594 | temp_index++; 595 | } 596 | } 597 | } 598 | 599 | // Load the count of number of occurrences. 600 | // If no results found, initialize with -1 601 | if(temp_index == 1) { 602 | search_index[0][0] = -1; 603 | write_log("search_any", "RESULT_NOT_FOUND", "Invalid search term"); 604 | } 605 | else { 606 | search_index[0][0] = temp_index-1; 607 | write_log("search_any", "SUCCESS", "Results returned in index"); 608 | } 609 | } 610 | 611 | 612 | //Function: print 613 | //Description: displays all the entries in inventory table 614 | // formatting might not look cleaner if the table 615 | // size is large 616 | //Input param: NULL 617 | //Return Type: NULL 618 | void print() 619 | { 620 | // Local variables indexing the key and properties 621 | int kindex = 0; 622 | int pindex = 0; 623 | 624 | // Run the loop and print the inventory table details 625 | for (pindex = 0; pindex <= property_count; pindex++) { 626 | for (kindex = 0; kindex <= key_count; kindex++) { 627 | printf("%s\t", inventory[pindex][kindex]); 628 | } 629 | printf("\n"); 630 | } 631 | } 632 | 633 | // Function: print_formatted 634 | // Description: displays all the entries in inventory table 635 | // formatting will look cleaner even if the table 636 | // size is large 637 | // Input param: NULL 638 | // Return Type: NULL 639 | void print_formatted() 640 | { 641 | // Local variables indexing the key and properties and maximum sizes 642 | int kindex = 0; 643 | int pindex = 0; 644 | int max = 0; 645 | int temp_len = 0; 646 | 647 | // Creates a table to store the maximum size of a string in a column 648 | int *max_size = (int *)malloc(sizeof(int) * key_count); 649 | if (max_size == NULL) 650 | { 651 | write_log("print_formatted", "MEMORY_ALLOCATION_ERROR", "No memory for max_size array"); 652 | return; 653 | } 654 | 655 | // Stores the maximum size of a string in a column among other values 656 | for (kindex = 0; kindex <= key_count; kindex++) 657 | { 658 | for (pindex = 0; pindex <= property_count; pindex++) 659 | { 660 | temp_len = strlen(inventory[pindex][kindex]); 661 | if (max < temp_len) 662 | max = temp_len; 663 | } 664 | // Spacing to separate each column 665 | max_size[kindex] = (max + 4); 666 | max = 0; 667 | } 668 | 669 | // Run the loop and print the inventory table details with formatting 670 | for (pindex = 0; pindex <= property_count; pindex++) 671 | { 672 | for (kindex = 0; kindex <= key_count; kindex++) 673 | { 674 | // Adds right padding to the values appropriately 675 | printf("%-*s", max_size[kindex], inventory[pindex][kindex]); 676 | } 677 | printf("\n"); 678 | } 679 | } 680 | 681 | //Function: inventory_memory_deallocation 682 | //Description: free the allocated memory for inventory table 683 | //Input param: NULL 684 | //Return Type: NULL 685 | void inventory_memory_deallocation() 686 | { 687 | // Local variables 688 | int index1; 689 | int index2; 690 | 691 | for(index1 = 0; index1 < property_size; index1++) { 692 | for(index2 = 0; index2 < key_size; index2++) { 693 | free(inventory[index1][index2]); 694 | } 695 | free(inventory[index1]); 696 | } 697 | free(inventory); 698 | 699 | write_log("inventory_memory_deallocation", "SUCCESS", "Memory deallocation Successful"); 700 | } 701 | 702 | 703 | //Function: dump 704 | //Description: dumps the key_count and property_count of respective file, 705 | // dumps the inventory table data from main to secondary memory 706 | //Input param: NULL 707 | //Return Type: integer 708 | // Success status on successful operation 709 | // otherwise respective error status code 710 | int dump() 711 | { 712 | // Local variables 713 | int spec_index; 714 | int key_index; 715 | 716 | // File pointer 717 | FILE *fp; 718 | FILE *tfp; 719 | 720 | // Open the file in write mode and dump the latest key count 721 | fp = fopen("key_count.txt","w+"); 722 | if(fp == NULL) { 723 | write_log("dump", "FILE_OPEN_ERROR", "Unable to open the key_count file"); 724 | tfp = fopen("tempfile.txt", "a+"); 725 | fprintf(tfp, "%s","\nFollowing contents to be added in said file"); 726 | fprintf(tfp, "%s", "key_count.txt\n"); 727 | fprintf(tfp,"%d", key_count); 728 | fclose(tfp); 729 | return FAILURE; 730 | } 731 | fprintf(fp,"%d", key_count); 732 | write_log("dump", "SUCCESS", "Key count dumped successfully"); 733 | fclose(fp); 734 | 735 | // Open the file in write mode and dump the latest properties count 736 | fp = fopen("property_count.txt","w+"); 737 | if(fp == NULL) { 738 | write_log("dump", "FILE_OPEN_ERROR", "Unable to open the property_count file"); 739 | tfp = fopen("tempfile.txt", "a+"); 740 | fprintf(tfp, "%s","\nFollowing contents to be added in said file"); 741 | fprintf(tfp, "%s", "property_count.txt\n"); 742 | fprintf(tfp,"%d", property_count); 743 | fclose(tfp); 744 | return FAILURE; 745 | } 746 | fprintf(fp,"%d", property_count); 747 | write_log("dump", "SUCCESS", "Property count dumped successfully"); 748 | fclose(fp); 749 | 750 | // Open the inventory file and update the values from the main memory 751 | // Into the secondary storage 752 | fp = fopen("inventory_file.txt", "w+"); 753 | if (fp == NULL) { 754 | write_log("dump", "FILE_OPEN_ERROR", "Unable to open the inventory_file"); 755 | tfp = fopen("tempfile.txt", "a+"); 756 | fprintf(tfp, "%s","\nFollowing contents to be added in said file"); 757 | fprintf(tfp, "%s", "inventory_file.txt\n"); 758 | for (spec_index = 0; spec_index <= property_count; spec_index++) { 759 | for (key_index = 0; key_index <= key_count; key_index++) { 760 | fprintf(tfp, "%s ", inventory[spec_index][key_index]); 761 | } 762 | fprintf(tfp, "%s", "\n"); 763 | } 764 | fclose(tfp); 765 | return FAILURE; 766 | } 767 | 768 | for (spec_index = 0; spec_index <= property_count; spec_index++) { 769 | for (key_index = 0; key_index <= key_count; key_index++) { 770 | fprintf(fp, "%s ", inventory[spec_index][key_index]); 771 | } 772 | fprintf(fp, "%s", "\n"); 773 | } 774 | fclose(fp); 775 | 776 | // Free the memory for inventory 777 | inventory_memory_deallocation(); 778 | 779 | write_log("dump", "SUCCESS", "Inventory table dumped successfully"); 780 | 781 | // Close the log file 782 | close_log(); 783 | return SUCCESS; 784 | } 785 | --------------------------------------------------------------------------------