Phase 1 and Phase2 implemented

This commit is contained in:
2024-05-14 20:58:31 +05:30
commit 13c94b0908
8 changed files with 520 additions and 0 deletions

7
src/CMakeLists.txt Normal file
View File

@@ -0,0 +1,7 @@
set(MLD_SRC "mld.c")
add_library(${MLD_LIB} STATIC
${MLD_SRC})
target_include_directories(${MLD_LIB} PUBLIC
${HEADER_DIR})

152
src/mld.c Normal file
View File

@@ -0,0 +1,152 @@
#include <stdio.h>
#include <stdlib.h>
#include "mld.h"
#include "css.h"
#include <assert.h>
#include <memory.h>
char* DATA_TYPE[] = { "UINT8", "UINT32", "INT32",
"CHAR", "OBJ_PTR", "VOID_PTR", "FLOAT",
"DOUBLE", "OBJ_STRUCT" };
/**********Printing functions**********/
void
print_structure_rec(struct_db_rec_t* struct_rec) {
if (!struct_rec)
return;
int j = 0;
field_info_t* field = NULL;
printf(ANSI_COLOR_CYAN "|--------------------------------------------------------------|\n" ANSI_COLOR_RESET);
printf(ANSI_COLOR_YELLOW "| %-20s | size = %-8d | #flds = %-3d |\n" ANSI_COLOR_RESET,
struct_rec->struct_name, struct_rec->ds_size, struct_rec->n_fields);
printf(ANSI_COLOR_CYAN "|--------------------------------------------------------------|--------------------------------------------------------------------------------------|\n" ANSI_COLOR_RESET);
for (j = 0; j < struct_rec->n_fields; j++) {
field = &struct_rec->fields[j];
printf(" %-20s |", "");
printf("%-3d %-20s | dtype = %-15s | size = %-5d | offset = %-6d | nstructname = %-20s |\n",
j, field->fname, DATA_TYPE[field->dtype], field->size, field->offset, field->nested_str_name);
printf(" %-20s |", "");
printf(ANSI_COLOR_CYAN "----------------------------------------------------------------------------------------------------------------------------|\n" ANSI_COLOR_RESET);
}
}
void
print_structure_db(struct_db_t* struct_db) {
if (!struct_db)
return;
printf("printing STRUCTURE DATABASE\n");
int i = 0;
struct_db_rec_t* struct_rec = NULL;
struct_rec = struct_db->head;
printf("No of Structures Registered = %d\n", struct_db->count);
while (struct_rec) {
/****%p format specifier is used to print value of void* pointer in hexadecimal format(prints the address value)****/
printf("Structure No : %d (%p)\n", i++, struct_rec);
print_structure_rec(struct_rec);
struct_rec = struct_rec->next;
}
}
/**********Printing functions**********/
/******Function to add the structure record to structure db******/
/*return 0 on success, -1 on failure from some reason*/
int
add_structure_to_struct_db(struct_db_t* struct_db, struct_db_rec_t* struct_rec) {
struct_db_rec_t* head = struct_db->head;
if (!head) {
struct_db->head = struct_rec;
struct_rec->next = NULL;
struct_db->count++;
return 0;
}
struct_rec->next = head;
struct_db->head = struct_rec;
struct_db->count++;
return 0;
}
/******Function to add the structure record to structure db******/
/******Function to lookup the structure record in structure db******/
/*return pointer of struct_db_rec_t on success, NULL on failure from some reason*/
static struct_db_rec_t*
struct_db_look_up(struct_db_t* struct_db, char* struct_name) {
if (!struct_db || !struct_name)
return NULL;
struct_db_rec_t* struct_rec = struct_db->head;
unsigned int sz_db = struct_db->count;
struct_db_rec_t* desired_rec = NULL;
int i = 0;
while (i < sz_db) {
if (!strncmp(struct_rec->struct_name, struct_name, MAX_STRUCTURE_NAME_SIZE)) {
desired_rec = struct_rec;
break;
}
struct_rec = struct_rec->next;
i++;
}
return desired_rec;
}
/******Function to lookup the structure record in structure db******/
/******Function to lookup the object record in object db******/
static object_db_rec_t*
object_db_look_up(object_db_t* object_db,
void* ptr) {
object_db_rec_t* head = object_db->head;
if (!head)
return NULL;
unsigned int sz_db = object_db->count;
object_db_rec_t* desired_obj = NULL;
int i = 0;
while (i < sz_db) {
if (head->ptr == ptr) {
desired_obj = head->ptr;
break;
}
head = head->next;
i++;
}
return desired_obj;
}
/******Function to lookup the object record in object db******/