redesigned lists to GLists

This commit is contained in:
Mario Hüttel 2018-05-07 15:57:47 +02:00
parent bc28446ce4
commit f20a59eb30
4 changed files with 79 additions and 78 deletions

View File

@ -1,4 +1,13 @@
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 2.8)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLIB REQUIRED glib-2.0)
include_directories(${GLIB_INCLUDE_DIRS})
link_directories(${GLIB_LINK_DIRS})
project(gds-render) project(gds-render)
add_executable(${PROJECT_NAME} "main.c" "gdsparse.c") add_executable(${PROJECT_NAME} "main.c" "gdsparse.c")
target_link_libraries(${PROJECT_NAME} ${GLIB_LDFLAGS})

View File

@ -22,54 +22,64 @@ enum record {
ENDEL = 0x1100 ENDEL = 0x1100
}; };
static struct gds_library * append_library(struct gds_library *curr_arr, unsigned int curr_count) static GList * append_library(GList *curr_list)
{ {
unsigned int size = (curr_count +1) * sizeof(struct gds_library); struct gds_library *lib;
struct gds_library *ptr = (struct gds_library *)realloc(curr_arr, size);
if (ptr) { lib = (struct gds_library *)malloc(sizeof(struct gds_library));
ptr[curr_count].cells_count = 0; if(lib) {
ptr[curr_count].name[0] = 0; lib->cells = NULL;
} lib->name[0] = 0;
return ptr; lib->unit_to_meters = 1; // Default. Will be overwritten
} else
return NULL;
return g_list_append(curr_list, lib);
} }
static struct gds_graphics * append_graphics(struct gds_graphics *curr_arr, unsigned int curr_count) static GList * append_graphics(GList *curr_list, enum graphics_type type)
{ {
unsigned int size = (curr_count +1) * sizeof(struct gds_graphics); struct gds_graphics *gfx;
struct gds_graphics *ptr = (struct gds_graphics *)realloc(curr_arr, size);
if (ptr != NULL) { gfx = (struct gds_graphics *)malloc(sizeof(struct gds_graphics));
ptr[curr_count].vertices_count = 0; if (gfx) {
ptr[curr_count].vertices = NULL; gfx->datatype = 0;
} gfx->layer = 0;
return ptr; gfx->vertices = NULL;
gfx->width_absolute = 0;
gfx->type = type;
} else
return NULL;
return g_list_append(curr_list, gfx);
} }
static struct gds_cell * append_cell(struct gds_cell *curr_arr, unsigned int curr_count) static GList * append_cell(GList *curr_list)
{ {
unsigned int size = (curr_count +1) * sizeof(struct gds_cell); struct gds_cell *cell;
struct gds_cell *ptr = (struct gds_cell *)realloc(curr_arr, size);
if (ptr != NULL) { cell = (struct gds_cell *)malloc(sizeof(struct gds_cell));
ptr[curr_count].child_cells_count = 0; if (cell) {
ptr[curr_count].graphic_objs_count = 0; cell->child_cells = NULL;
ptr[curr_count].child_cells = NULL; cell->graphic_objs = NULL;
ptr[curr_count].graphic_objs = NULL; cell->name[0] = 0;
ptr[curr_count].name[0] = 0; } else
return NULL;
return g_list_append(curr_list, cell);
} }
return ptr; static GList * append_cell_ref(GList *curr_list)
}
static gds_cell_instance * append_cell_ref(struct gds_cell_instance* curr_arr, unsigned int curr_count)
{ {
unsigned int size = (curr_count + 1) * sizeof(struct gds_cell_instance); struct gds_cell_instance *inst;
struct gds_cell_instance *ptr = (struct gds_cell_instance *)realloc(curr_arr, size);
if (ptr) {
ptr[curr_count].cell_ref = NULL;
ptr[curr_count].ref_name[0] = 0;
}
return ptr;
inst = (struct gds_cell_instance *)malloc(sizeof(struct gds_cell_instance));
if (inst) {
inst->cell_ref = NULL;
inst->ref_name[0] = 0;
} else
return NULL;
return g_list_append(curr_list, inst);
} }
static int name_library(struct gds_library *current_library, unsigned int bytes, char* data) { static int name_library(struct gds_library *current_library, unsigned int bytes, char* data) {
@ -116,7 +126,7 @@ static int name_cell(struct gds_cell *cell, unsigned int bytes, char* data) {
} }
int parse_gds_from_file(const char *filename, struct gds_library **library_array, unsigned int *count) int parse_gds_from_file(const char *filename, GList **library_list)
{ {
char workbuff[1024]; char workbuff[1024];
int read; int read;
@ -130,10 +140,9 @@ int parse_gds_from_file(const char *filename, struct gds_library **library_array
struct gds_graphics *current_graphics = NULL; struct gds_graphics *current_graphics = NULL;
struct gds_cell_instance *current_s_reference = NULL; struct gds_cell_instance *current_s_reference = NULL;
//////////// ////////////
struct gds_library *lib_arr = NULL; GList *lib_list;
struct gds_cell *cell_arr = NULL;
unsigned int lib_arr_cnt = 0; lib_list = *library_list;
unsigned int cell_arr_cnt = 0;
/* open File */ /* open File */
gds_file = fopen(filename, "r"); gds_file = fopen(filename, "r");
@ -194,16 +203,15 @@ int parse_gds_from_file(const char *filename, struct gds_library **library_array
/* if begin: Allocate structures */ /* if begin: Allocate structures */
switch (rec_type) { switch (rec_type) {
case BGNLIB: case BGNLIB:
lib_arr = append_library(lib_arr, lib_arr_cnt); lib_list = append_library(lib_list);
lib_arr_cnt++; if (lib_list == NULL) {
if (lib_arr == NULL) {
GDS_ERROR("Allocating memory failed"); GDS_ERROR("Allocating memory failed");
run = -3; run = -3;
break; break;
} }
printf("Entering Lib\n"); printf("Entering Lib\n");
current_lib = &(lib_arr[lib_arr_cnt-1]); current_lib = (struct gds_library *)g_list_last(lib_list)->data;
break; break;
case ENDLIB: case ENDLIB:
if (current_lib == NULL) { if (current_lib == NULL) {
@ -218,23 +226,18 @@ int parse_gds_from_file(const char *filename, struct gds_library **library_array
GDS_ERROR("Closing Library with opened cells"); GDS_ERROR("Closing Library with opened cells");
break; break;
} }
current_lib->cells = cell_arr;
current_lib->cells_count = cell_arr_cnt;
current_lib = NULL; current_lib = NULL;
printf("Leaving Library\n"); printf("Leaving Library\n");
break; break;
case BGNSTR: case BGNSTR:
cell_arr = append_cell(cell_arr, cell_arr_cnt); current_lib->cells = append_cell(current_lib->cells);
cell_arr_cnt++; if (current_lib->cells == NULL) {
if (cell_arr == NULL) {
GDS_ERROR("Allocating memory failed"); GDS_ERROR("Allocating memory failed");
run = -3; run = -3;
break; break;
} }
printf("Entering Cell\n"); printf("Entering Cell\n");
current_cell = &(cell_arr[cell_arr_cnt-1]); current_cell = (struct gds_cell *)g_list_last(current_lib->cells)->data;
break; break;
case ENDSTR: case ENDSTR:
if (current_cell == NULL) { if (current_cell == NULL) {
@ -257,15 +260,13 @@ int parse_gds_from_file(const char *filename, struct gds_library **library_array
run = -3; run = -3;
break; break;
} }
current_cell->graphic_objs = append_graphics(current_cell->graphic_objs, current_cell->graphic_objs_count); current_cell->graphic_objs = append_graphics(current_cell->graphic_objs, GRAPHIC_POLYGON);
current_cell->graphic_objs_count++;
if (current_cell->graphic_objs == NULL) { if (current_cell->graphic_objs == NULL) {
GDS_ERROR("Memory allocation failed"); GDS_ERROR("Memory allocation failed");
run = -4; run = -4;
break; break;
} }
current_graphics = &(current_cell->graphic_objs[current_cell->graphic_objs_count-1]); current_graphics = (struct gds_graphics *) g_list_last(current_cell->graphic_objs)->data;
current_graphics->type = GRAPHIC_POLYGON;
printf("\tEntering boundary\n"); printf("\tEntering boundary\n");
break; break;
case SREF: case SREF:
@ -274,7 +275,7 @@ int parse_gds_from_file(const char *filename, struct gds_library **library_array
run = -3; run = -3;
break; break;
} }
current_cell->child_cells = append_cell_ref(current_cell->child_cells, current_cell->child_cells_count); current_cell->child_cells = append_cell_ref(current_cell->child_cells);
if (current_cell->child_cells == NULL) { if (current_cell->child_cells == NULL) {
GDS_ERROR("Memory allocation failed"); GDS_ERROR("Memory allocation failed");
run = -4; run = -4;
@ -288,15 +289,13 @@ int parse_gds_from_file(const char *filename, struct gds_library **library_array
run = -3; run = -3;
break; break;
} }
current_cell->graphic_objs = append_graphics(current_cell->graphic_objs, current_cell->graphic_objs_count); current_cell->graphic_objs = append_graphics(current_cell->graphic_objs, GRAPHIC_PATH);
current_cell->graphic_objs_count++;
if (current_cell->graphic_objs == NULL) { if (current_cell->graphic_objs == NULL) {
GDS_ERROR("Memory allocation failed"); GDS_ERROR("Memory allocation failed");
run = -4; run = -4;
break; break;
} }
current_graphics = &(current_cell->graphic_objs[current_cell->graphic_objs_count-1]); current_graphics = (struct gds_graphics *) g_list_last(current_cell->graphic_objs)->data;
current_graphics->type = GRAPHIC_PATH;
printf("\tEntering Path\n"); printf("\tEntering Path\n");
break; break;
case ENDEL: case ENDEL:
@ -342,8 +341,7 @@ int parse_gds_from_file(const char *filename, struct gds_library **library_array
/* Iterate and find references to cells */ /* Iterate and find references to cells */
*library_array = lib_arr; *library_list = lib_list;
*count = lib_arr_cnt;
return run; return run;

View File

@ -2,6 +2,7 @@
#define __GDSPARSE_H__ #define __GDSPARSE_H__
#include <stdint.h> #include <stdint.h>
#include <glib.h>
#define CELL_NAME_MAX (100) #define CELL_NAME_MAX (100)
@ -22,8 +23,7 @@ struct gds_point {
struct gds_graphics { struct gds_graphics {
enum graphics_type type; enum graphics_type type;
struct gds_point *vertices; GList *vertices;
int vertices_count;
unsigned int path_width; unsigned int path_width;
int width_absolute; int width_absolute;
uint16_t layer; uint16_t layer;
@ -43,22 +43,19 @@ struct gds_cell {
char name[CELL_NAME_MAX]; char name[CELL_NAME_MAX];
struct gds_time_field mod_time; struct gds_time_field mod_time;
struct gds_time_field access_time; struct gds_time_field access_time;
struct gds_cell_instance *child_cells; GList *child_cells;
int child_cells_count; GList *graphic_objs;
struct gds_graphics *graphic_objs;
int graphic_objs_count;
}; };
struct gds_library { struct gds_library {
char name[CELL_NAME_MAX]; char name[CELL_NAME_MAX];
struct gds_time_field time; struct gds_time_field time;
double unit_to_meters; double unit_to_meters;
struct gds_cell *cells; GList *cells;
int cells_count;
}; };
int parse_gds_from_file(const char *filename, struct gds_library **library_array, unsigned int *count); int parse_gds_from_file(const char *filename, GList **library_array);
#endif /* __GDSPARSE_H__ */ #endif /* __GDSPARSE_H__ */

9
main.c
View File

@ -1,17 +1,14 @@
#include <stdio.h> #include <stdio.h>
#include "gdsparse.h" #include "gdsparse.h"
int main() int main()
{ {
struct gds_library *libs; GList *libs = NULL;
unsigned int count = 0; unsigned int count = 0;
int i = 0; int i = 0;
parse_gds_from_file("/home/mari/Desktop/test.gds", &libs, &count); parse_gds_from_file("/home/mari/Desktop/test.gds", &libs);
printf("Lib count: %u\n", count);
for (i = 0; i < count; i++) {
printf("Lib %s: %d cells\n", libs[i].name, libs[i].cells_count);
}
return 0; return 0;
} }