gds-render/gds-utils/gds-parser.c

1280 lines
34 KiB
C
Raw Permalink Normal View History

2018-05-10 00:14:00 +02:00
/*
2018-05-15 22:54:10 +02:00
* GDSII-Converter
2018-05-10 00:14:00 +02:00
* Copyright (C) 2018 Mario Hüttel <mario.huettel@gmx.net>
*
* This file is part of GDSII-Converter.
*
* GDSII-Converter is free software: you can redistribute it and/or modify
2018-05-16 16:29:34 +02:00
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
2018-05-10 00:14:00 +02:00
*
2018-05-15 22:54:10 +02:00
* GDSII-Converter is distributed in the hope that it will be useful,
2018-05-10 00:14:00 +02:00
* 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 GDSII-Converter. If not, see <http://www.gnu.org/licenses/>.
2018-05-10 00:14:00 +02:00
*/
/**
2018-07-25 16:44:04 +02:00
* @file gds-parser.c
* @brief Implementation of the GDS-Parser
* @author Mario Hüttel <mario.huettel@gmx.net>
*
* What's missing? - A lot:
* Support for 4 Byte real
* Support for pathtypes
* Support for datatypes (only layer so far)
* etc...
*/
/**
* @addtogroup GDS-Utilities
* @{
*/
2018-05-07 13:27:50 +02:00
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
2018-07-11 12:24:15 +02:00
#include <cairo.h>
#include <glib/gi18n.h>
2018-05-07 13:27:50 +02:00
#include <gds-render/gds-utils/gds-parser.h>
#include <gds-render/gds-utils/gds-statistics.h>
2018-12-22 19:11:09 +01:00
/**
* @brief Default units assumed for library.
* @note This value is usually overwritten with the value defined in the library.
*/
#define GDS_DEFAULT_UNITS (10E-9)
#define GDS_ERROR(fmt, ...) fprintf(stderr, "[PARSE_ERROR] " fmt "\n", ##__VA_ARGS__) /**< @brief Print GDS error*/
#define GDS_WARN(fmt, ...) fprintf(stderr, "[PARSE_WARNING] " fmt "\n", ##__VA_ARGS__) /**< @brief Print GDS warning */
2018-05-10 01:05:19 +02:00
#if GDS_PRINT_DEBUG_INFOS
2019-06-07 19:32:38 +02:00
/**< @brief standard printf. But can be disabled in code. */
#define GDS_INF(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define GDS_INF(fmt, ...)
#endif
enum gds_record {
2018-05-07 13:42:06 +02:00
INVALID = 0x0000,
2018-05-07 13:27:50 +02:00
HEADER = 0x0002,
BGNLIB = 0x0102,
LIBNAME = 0x0206,
UNITS = 0x0305,
ENDLIB = 0x0400,
2018-05-07 13:42:06 +02:00
BGNSTR = 0x0502,
2018-05-07 13:27:50 +02:00
STRNAME = 0x0606,
ENDSTR = 0x0700,
2018-05-07 15:17:29 +02:00
BOUNDARY = 0x0800,
PATH = 0x0900,
SREF = 0x0A00,
2018-05-07 17:36:14 +02:00
ENDEL = 0x1100,
XY = 0x1003,
MAG = 0x1B05,
ANGLE = 0x1C05,
SNAME = 0x1206,
STRANS = 0x1A01,
BOX = 0x2D00,
LAYER = 0x0D02,
DATATYPE = 0x0E02,
WIDTH = 0x0F03,
PATHTYPE = 0x2102,
COLROW = 0x1302,
AREF = 0x0B00
};
/**
* @brief Struct representing an array instantiation.
*
* This struct is defined locally because it is not exposed to the outside of the
* parser. Array references are internally converted to a bunch of standard @ref gds_cell_instance elements.
*/
struct gds_cell_array_instance {
char ref_name[CELL_NAME_MAX]; /**< @brief Name of referenced cell */
struct gds_cell *cell_ref; /**< @brief Referenced gds_cell structure */
struct gds_point control_points[3]; /**< @brief The three control points */
int flipped; /**< @brief Mirror each instance on x-axis before rotation */
double angle; /**< @brief Angle of rotation for each instance (counter clockwise) in degrees */
double magnification; /**< @brief Magnification of each instance */
int columns; /**< @brief Column count */
int rows; /**< @brief Row count */
2018-05-07 13:27:50 +02:00
};
/**
* @brief Name cell reference
* @param cell_inst Cell reference
* @param bytes Length of name
* @param data Name
* @return 0 if successful
*/
2018-05-10 01:13:27 +02:00
static int name_cell_ref(struct gds_cell_instance *cell_inst,
unsigned int bytes, char *data)
{
2018-05-07 17:36:14 +02:00
int len;
2018-05-10 01:13:27 +02:00
if (cell_inst == NULL) {
2018-05-07 17:36:14 +02:00
GDS_ERROR("Naming cell ref with no opened cell ref");
return -1;
}
data[bytes] = 0; // Append '0'
len = (int)strlen(data);
2018-05-07 17:36:14 +02:00
if (len > CELL_NAME_MAX-1) {
GDS_ERROR("Cell name '%s' too long: %d\n", data, len);
return -1;
}
2018-05-10 01:13:27 +02:00
/* else: */
strcpy(cell_inst->ref_name, data);
GDS_INF("\tCell referenced: %s\n", cell_inst->ref_name);
2018-05-10 01:13:27 +02:00
return 0;
2018-05-07 17:36:14 +02:00
}
/**
* @brief Name cell reference
* @param cell_inst Cell reference
* @param bytes Length of name
* @param data Name
* @return 0 if successful
*/
static int name_array_cell_ref(struct gds_cell_array_instance *cell_inst,
unsigned int bytes, char *data)
{
int len;
if (cell_inst == NULL) {
GDS_ERROR("Naming array cell ref with no opened cell ref");
return -1;
}
data[bytes] = 0; // Append '0'
len = (int)strlen(data);
if (len > CELL_NAME_MAX-1) {
GDS_ERROR("Cell name '%s' too long: %d\n", data, len);
return -1;
}
/* else: */
strcpy(cell_inst->ref_name, data);
GDS_INF("\tCell referenced: %s\n", cell_inst->ref_name);
return 0;
}
/**
* @brief Convert GDS 8-byte real to double
* @param data 8 Byte GDS real
* @return result
*/
static double gds_convert_double(const char *data)
{
bool sign_bit;
int i;
double ret_val;
char current_byte;
int bit = 0;
int exponent;
sign_bit = ((data[0] & 0x80) ? true : false);
/* Check for real 0 */
for (i = 0; i < 8; i++) {
if (data[i] != 0)
break;
if (i == 7) {
2019-06-08 12:43:01 +02:00
/* All 8 bytes are 0 */
return 0.0;
}
}
/* Value is other than 0 */
ret_val = 0.0;
for (i = 8; i < 64; i++) {
current_byte = data[i/8];
bit = i % 8;
/* isolate bit */
if ((current_byte & (0x80 >> bit)))
ret_val += pow(2, ((double)(-i+7)));
}
/* Parse exponent and sign bit */
exponent = (int)(data[0] & 0x7F);
exponent -= 64;
ret_val *= pow(16, exponent) * (sign_bit == true ? -1 : 1);
return ret_val;
}
/**
* @brief Convert GDS INT32 to int
* @param data Buffer containing the int
* @return result
*/
static signed int gds_convert_signed_int(const char *data)
2018-05-07 17:36:14 +02:00
{
2018-05-10 00:49:24 +02:00
int ret;
2018-05-10 01:13:27 +02:00
2018-05-07 17:36:14 +02:00
if (!data) {
2019-09-15 13:04:30 +02:00
GDS_ERROR("Conversion from GDS data to signed int failed.");
2018-05-07 17:36:14 +02:00
return 0;
}
2018-05-10 00:49:24 +02:00
ret = (signed int)(((((int)data[0]) & 0xFF) << 24) |
((((int)data[1]) & 0xFF) << 16) |
(((int)(data[2]) & 0xFF) << 8) |
(((int)(data[3]) & 0xFF) << 0));
return ret;
2018-05-07 17:36:14 +02:00
}
/**
* @brief Convert GDS INT16 to int16
* @param data Buffer containing the INT16
* @return result
*/
static int16_t gds_convert_signed_int16(const char *data)
2018-05-07 17:36:14 +02:00
{
if (!data) {
GDS_ERROR("This should not happen");
return 0;
}
2018-05-10 00:49:24 +02:00
return (int16_t)((((int16_t)(data[0]) & 0xFF) << 8) |
(((int16_t)(data[1]) & 0xFF) << 0));
2018-05-07 17:36:14 +02:00
}
/**
* @brief Convert GDS UINT16 String to uint16
* @param data Buffer containing the uint16
* @return result
*/
2019-09-15 13:04:30 +02:00
static uint16_t gds_convert_unsigned_int16(const char *data)
{
if (!data) {
GDS_ERROR("This should not happen");
return 0;
}
return (uint16_t)((((uint16_t)(data[0]) & 0xFF) << 8) |
(((uint16_t)(data[1]) & 0xFF) << 0));
}
/**
* @brief Append library to list
* @param curr_list List containing gds_library elements. May be NULL.
* @param library_ptr Return of newly created library.
* @return Newly created list pointer
*/
static GList *append_library(GList *curr_list, const struct gds_library_parsing_opts *opts,
struct gds_library **library_ptr)
2018-05-07 13:27:50 +02:00
{
2018-05-07 15:57:47 +02:00
struct gds_library *lib;
lib = (struct gds_library *)malloc(sizeof(struct gds_library));
2018-05-10 01:13:27 +02:00
if (lib) {
2018-05-07 15:57:47 +02:00
lib->cells = NULL;
lib->name[0] = 0;
2018-12-22 19:11:09 +01:00
lib->unit_in_meters = GDS_DEFAULT_UNITS; // Default. Will be overwritten
lib->cell_names = NULL;
/* Copy the settings into the library */
memcpy(&lib->parsing_opts, opts, sizeof(struct gds_library_parsing_opts));
lib->stats.cell_count = 0;
lib->stats.gfx_count = 0;
lib->stats.reference_count = 0;
lib->stats.vertex_count = 0;
} else {
2018-05-07 15:57:47 +02:00
return NULL;
}
if (library_ptr)
*library_ptr = lib;
2018-05-07 15:57:47 +02:00
return g_list_append(curr_list, lib);
2018-05-07 15:17:29 +02:00
}
/**
* @brief Prepend graphics to list
* @param curr_list List containing gds_graphics elements. May be NULL
* @param type Type of graphics
* @param graphics_ptr newly created graphic is written here
* @return new list pointer
*/
static __attribute__((warn_unused_result)) GList *prepend_graphics(GList *curr_list, enum graphics_type type,
2018-05-10 01:36:01 +02:00
struct gds_graphics **graphics_ptr)
2018-05-07 15:17:29 +02:00
{
2018-05-07 15:57:47 +02:00
struct gds_graphics *gfx;
gfx = (struct gds_graphics *)malloc(sizeof(struct gds_graphics));
if (gfx) {
gfx->datatype = 0;
gfx->layer = 0;
gfx->vertices = NULL;
gfx->width_absolute = 0;
gfx->gfx_type = type;
gfx->path_render_type = PATH_FLUSH;
2018-05-07 15:57:47 +02:00
} else
return NULL;
2018-05-10 01:36:01 +02:00
if (graphics_ptr)
*graphics_ptr = gfx;
return g_list_prepend(curr_list, gfx);
2018-05-07 13:27:50 +02:00
}
/**
* @brief Appends vertext List
* @param curr_list List containing gds_point elements. May be NULL.
* @param x x-coordinate of new point
* @param y y-coordinate of new point
* @return new Pointer to List.
*/
2018-05-10 01:13:27 +02:00
static GList *append_vertex(GList *curr_list, int x, int y)
2018-05-07 17:36:14 +02:00
{
struct gds_point *vertex;
2018-05-10 01:13:27 +02:00
2018-05-07 17:36:14 +02:00
vertex = (struct gds_point *)malloc(sizeof(struct gds_point));
if (vertex) {
vertex->x = x;
vertex->y = y;
} else
return NULL;
return g_list_append(curr_list, vertex);
}
/**
* @brief append_cell Append a gds_cell to a list
*
* Usage similar to append_cell_ref().
* @param curr_list List containing gds_cell elements. May be NULL
* @param cell_ptr newly created cell
* @return new pointer to list
*/
2018-05-10 01:36:01 +02:00
static GList *append_cell(GList *curr_list, struct gds_cell **cell_ptr)
2018-05-07 13:27:50 +02:00
{
2018-05-07 15:57:47 +02:00
struct gds_cell *cell;
cell = (struct gds_cell *)malloc(sizeof(struct gds_cell));
if (cell) {
cell->child_cells = NULL;
cell->graphic_objs = NULL;
cell->name[0] = 0;
cell->parent_library = NULL;
2019-03-05 20:08:10 +01:00
cell->checks.unresolved_child_count = GDS_CELL_CHECK_NOT_RUN;
cell->checks.affected_by_reference_loop = GDS_CELL_CHECK_NOT_RUN;
cell->stats.reference_count = 0;
cell->stats.total_vertex_count = 0;
2022-12-17 18:43:10 +01:00
cell->stats.total_gfx_count = 0;
cell->stats.gfx_count = 0;
cell->stats.vertex_count = 0;
2018-05-07 15:57:47 +02:00
} else
return NULL;
2018-05-10 01:36:01 +02:00
/* return cell */
if (cell_ptr)
*cell_ptr = cell;
2018-05-07 15:17:29 +02:00
2018-05-07 15:57:47 +02:00
return g_list_append(curr_list, cell);
2018-05-07 15:17:29 +02:00
}
/**
* @brief Append a cell reference to the reference GList.
*
* Appends a new gds_cell_instance to \p curr_list and returns the new element via \p instance_ptr
* @param curr_list List of gds_cell_instance elements. May be NULL
* @param instance_ptr newly created element
* @return new GList pointer
*/
2018-05-10 01:36:01 +02:00
static GList *append_cell_ref(GList *curr_list, struct gds_cell_instance **instance_ptr)
2018-05-07 15:17:29 +02:00
{
2018-05-07 15:57:47 +02:00
struct gds_cell_instance *inst;
2018-05-10 01:13:27 +02:00
inst = (struct gds_cell_instance *)
malloc(sizeof(struct gds_cell_instance));
2018-05-07 15:57:47 +02:00
if (inst) {
inst->cell_ref = NULL;
inst->ref_name[0] = 0;
2019-06-08 12:43:01 +02:00
inst->magnification = 1.0;
2018-05-07 17:36:14 +02:00
inst->flipped = 0;
2019-06-08 12:43:01 +02:00
inst->angle = 0.0;
2018-05-07 15:57:47 +02:00
} else
return NULL;
2018-05-07 15:17:29 +02:00
2018-05-10 01:36:01 +02:00
if (instance_ptr)
*instance_ptr = inst;
2018-05-07 15:57:47 +02:00
return g_list_append(curr_list, inst);
2018-05-07 13:27:50 +02:00
}
/**
* @brief Name a gds_library
* @param current_library Library to name
* @param bytes Lenght of name
* @param data Name
* @return 0 if successful
*/
2018-05-10 01:13:27 +02:00
static int name_library(struct gds_library *current_library,
unsigned int bytes, char *data)
{
2018-05-07 13:27:50 +02:00
int len;
2018-05-10 01:13:27 +02:00
if (current_library == NULL) {
2018-05-07 13:42:06 +02:00
GDS_ERROR("Naming cell with no opened library");
return -1;
}
2018-05-07 13:27:50 +02:00
data[bytes] = 0; // Append '0'
len = (int)strlen(data);
2018-05-07 13:27:50 +02:00
if (len > CELL_NAME_MAX-1) {
GDS_ERROR("Library name '%s' too long: %d\n", data, len);
return -1;
}
2018-05-10 01:13:27 +02:00
strcpy(current_library->name, data);
GDS_INF("Named library: %s\n", current_library->name);
2018-05-10 01:13:27 +02:00
return 0;
2018-05-07 13:27:50 +02:00
}
/**
* @brief Names a gds_cell
* @param cell Cell to name
* @param bytes Length of name
* @param data Name
* @param lib Library in which \p cell is located
* @return 0 id successful
*/
2018-05-10 01:13:27 +02:00
static int name_cell(struct gds_cell *cell, unsigned int bytes,
char *data, struct gds_library *lib)
{
2018-05-07 13:27:50 +02:00
int len;
2018-05-10 01:13:27 +02:00
if (cell == NULL) {
2018-05-07 13:42:06 +02:00
GDS_ERROR("Naming library with no opened library");
return -1;
}
2018-05-07 13:27:50 +02:00
data[bytes] = 0; // Append '0'
2018-12-22 19:11:09 +01:00
len = (int)strlen(data);
2018-05-07 13:27:50 +02:00
if (len > CELL_NAME_MAX-1) {
2018-05-07 13:42:06 +02:00
GDS_ERROR("Cell name '%s' too long: %d\n", data, len);
2018-05-07 13:27:50 +02:00
return -1;
}
2018-05-10 01:13:27 +02:00
strcpy(cell->name, data);
GDS_INF("Named cell: %s\n", cell->name);
2018-05-10 01:13:27 +02:00
/* Append cell name to lib's list of names */
lib->cell_names = g_list_append(lib->cell_names, cell->name);
2018-05-07 13:27:50 +02:00
2018-05-10 01:13:27 +02:00
return 0;
2018-05-07 13:27:50 +02:00
}
/**
* @brief Search for cell reference \p gcell_ref in \p glibrary
*
* Search cell referenced by \p gcell_ref inside \p glibrary and update gds_cell_instance::cell_ref with found #gds_cell
* @param gcell_ref gpointer cast of struct gds_cell_instance *
* @param glibrary gpointer cast of struct gds_library *
*/
static void parse_reference_list(gpointer gcell_ref, gpointer glibrary)
{
struct gds_cell_instance *inst = (struct gds_cell_instance *)gcell_ref;
struct gds_library *lib = (struct gds_library *)glibrary;
GList *cell_item;
struct gds_cell *cell;
GDS_INF("\t\t\tReference: %s: ", inst->ref_name);
/* Find cell */
2018-05-10 01:13:27 +02:00
for (cell_item = lib->cells; cell_item != NULL;
cell_item = cell_item->next) {
cell = (struct gds_cell *)cell_item->data;
/* Check if cell is found */
if (!strcmp(cell->name, inst->ref_name)) {
GDS_INF("found\n");
/* update reference link */
inst->cell_ref = cell;
return;
}
}
GDS_INF("MISSING!\n");
GDS_WARN("referenced cell could not be found in library");
}
/**
* @brief Simplify graphics objects
* @param graphics gfx struct
* @param user_data GDS library
*/
static void simplify_graphics(gpointer graphics, gpointer user_data)
{
struct gds_graphics *gfx;
const struct gds_point *first_vertex = NULL;
const struct gds_point *prev_vertex = NULL;
const struct gds_point *current_vertex;
GList *vertex_iter;
GList *next_iter;
(void)user_data;
size_t processed_count = 0U;
size_t removed_count = 0U;
gfx = (struct gds_graphics *)graphics;
if (gfx->gfx_type == GRAPHIC_POLYGON) {
GDS_INF("\t\t\tPolygon found\n");
/* Loop over all vertices */
for (vertex_iter = gfx->vertices; vertex_iter;) {
current_vertex = (const struct gds_point *)vertex_iter->data;
next_iter = g_list_next(vertex_iter);
processed_count++;
if (vertex_iter == gfx->vertices) {
/* This is the first vertex */
first_vertex = current_vertex;
prev_vertex = NULL;
}
if (prev_vertex) {
if (current_vertex->x == prev_vertex->x && current_vertex->y == prev_vertex->y) {
/* Vertex is the same as the previous one */
GDS_INF("\t\t\t\tDuplicate vertex (%d,%d). Removing...\n",
current_vertex->x, current_vertex->y);
/* Delete the current one from the list */
gfx->vertices = g_list_remove_link(gfx->vertices, vertex_iter);
/* Free the data */
if (vertex_iter->data)
free(vertex_iter->data);
vertex_iter->data = NULL;
g_list_free_1(vertex_iter);
removed_count++;
} else if (!g_list_next(vertex_iter)) {
/* This is the last element in the list */
if (current_vertex->x == first_vertex->x &&
current_vertex->y == first_vertex->y) {
GDS_INF("\t\t\t\tLast vertex is identical to first vertex (%d,%d). Removing\n",
current_vertex->x, current_vertex->y);
gfx->vertices = g_list_remove_link(gfx->vertices, vertex_iter);
if (vertex_iter->data)
free(vertex_iter->data);
g_list_free_1(vertex_iter);
removed_count++;
} else {
GDS_WARN("First vertex is not coincident with first vertex, although the GDS file format specifies this. However, this is not a problem.");
}
}
}
vertex_iter = next_iter;
prev_vertex = current_vertex;
}
GDS_INF("\t\t\tProcessed %zu vertices. %zu removed.\n", processed_count, removed_count);
}
}
/**
* @brief Scans cell and resolves references and simplifies polygons
* This function searches all the references in \p gcell and updates the gds_cell_instance::cell_ref field in each instance
*
* @param gcell pointer cast of #gds_cell *
* @param library Library where the cell references are searched in
*/
static void scan_cell_references_and_polygons(gpointer gcell, gpointer library)
{
struct gds_cell *cell = (struct gds_cell *)gcell;
struct gds_library *my_lib = (struct gds_library *)library;
int simplify_polygons;
2018-05-10 01:13:27 +02:00
simplify_polygons = my_lib->parsing_opts.simplified_polygons;
GDS_INF("\tScanning cell: %s\n", cell->name);
GDS_INF("\t\tCell references\n");
/* Scan all library references */
g_list_foreach(cell->child_cells, parse_reference_list, library);
GDS_INF("\t\tSimplifying Polygons%s\n", simplify_polygons ? "" : ": skipped");
if (simplify_polygons) {
g_list_foreach(cell->graphic_objs, simplify_graphics, library);
}
}
/**
* @brief Scans library's cell references
*
* This function searches all the references between cells and updates the gds_cell_instance::cell_ref field in each instance
* @param library_list_item List containing #gds_library elements
* @param user not used
*/
static void scan_library_references(gpointer library_list_item, gpointer user)
{
struct gds_library *lib = (struct gds_library *)library_list_item;
2019-02-27 20:27:01 +01:00
(void)user;
GDS_INF("Scanning Library: %s\n", lib->name);
g_list_foreach(lib->cells, scan_cell_references_and_polygons, lib);
}
static void calc_library_stats(gpointer library_list_item, gpointer user)
{
struct gds_library *lib = (struct gds_library *)library_list_item;
(void)user;
GDS_INF("Calculating stats for Library: %s\n", lib->name);
gds_statistics_calc_cummulative_counts_in_lib(lib);
}
/**
* @brief gds_parse_date
* @param buffer Buffer that contains the GDS Date field
* @param length Length of \p buffer
* @param mod_date Modification Date
* @param access_date Last Access Date
*/
static void gds_parse_date(const char *buffer, int length, struct gds_time_field *mod_date, struct gds_time_field *access_date)
{
struct gds_time_field *temp_date;
if (!access_date || !mod_date) {
GDS_WARN("Date structures invalid");
return;
}
if (length != (2*6*2)) {
GDS_WARN("Could not parse date field! Not the specified length");
return;
}
for (temp_date = mod_date; 1; temp_date = access_date) {
2019-09-15 13:04:30 +02:00
temp_date->year = gds_convert_unsigned_int16(buffer);
buffer += 2;
2019-09-15 13:04:30 +02:00
temp_date->month = gds_convert_unsigned_int16(buffer);
buffer += 2;
2019-09-15 13:04:30 +02:00
temp_date->day = gds_convert_unsigned_int16(buffer);
buffer += 2;
2019-09-15 13:04:30 +02:00
temp_date->hour = gds_convert_unsigned_int16(buffer);
buffer += 2;
2019-09-15 13:04:30 +02:00
temp_date->minute = gds_convert_unsigned_int16(buffer);
buffer += 2;
2019-09-15 13:04:30 +02:00
temp_date->second = gds_convert_unsigned_int16(buffer);
buffer += 2;
if (temp_date == access_date)
break;
}
}
/**
* @brief Convert AREF to a bunch of SREFs and append them to \p container_cell
*
* This function converts a single array reference (\p aref) to gds_cell_array_instance::rows * gds_cell_array_instance::columns
* single references (SREFs). See @ref gds_cell_instance.
*
* Both gds_cell_array_instance::rows and gds_cell_array_instance::columns must be larger than zero.
*
* @param[in] aref Array reference to parse
* @param[in] container_cell cell to add the converted single references to.
*/
static void convert_aref_to_sref(struct gds_cell_array_instance *aref, struct gds_cell *container_cell)
{
struct gds_point origin;
struct gds_point row_shift_vector;
struct gds_point col_shift_vector;
struct gds_cell_instance *sref_inst = NULL;
int col;
int row;
if (!aref || !container_cell)
return;
if (aref->columns == 0 || aref->rows == 0) {
GDS_ERROR("Conversion of array instance aborted. No rows / columns.");
return;
}
origin.x = aref->control_points[0].x;
origin.y = aref->control_points[0].y;
row_shift_vector.x = (aref->control_points[2].x - origin.x) / aref->rows;
row_shift_vector.y = (aref->control_points[2].y - origin.y) / aref->rows;
col_shift_vector.x = (aref->control_points[1].x - origin.x) / aref->columns;
col_shift_vector.y = (aref->control_points[1].y - origin.y) / aref->columns;
/* Iterate over columns and rows */
for (col = 0; col < aref->columns; col++) {
for (row = 0; row < aref->rows; row++) {
2019-11-28 17:59:06 +01:00
/* Create new instance for this row/column and configure data */
container_cell->child_cells = append_cell_ref(container_cell->child_cells, &sref_inst);
container_cell->stats.reference_count++;
if (!sref_inst) {
GDS_ERROR("Appending cell ref failed!");
continue;
}
sref_inst->angle = aref->angle;
sref_inst->magnification = aref->magnification;
sref_inst->flipped = aref->flipped;
strncpy(sref_inst->ref_name, aref->ref_name, CELL_NAME_MAX);
sref_inst->origin.x = origin.x + row_shift_vector.x * row + col_shift_vector.x * col;
sref_inst->origin.y = origin.y + row_shift_vector.y * row + col_shift_vector.y * col;
}
}
GDS_INF("Converted AREF to SREFs\n");
}
int parse_gds_from_file(const char *filename, GList **library_list,
const struct gds_library_parsing_opts *parsing_options)
2018-05-07 13:27:50 +02:00
{
2018-05-31 02:16:12 +02:00
char *workbuff;
2018-05-07 13:27:50 +02:00
int read;
2018-05-07 17:36:14 +02:00
int i;
2018-05-07 13:27:50 +02:00
int run = 1;
FILE *gds_file = NULL;
uint16_t rec_data_length;
enum gds_record rec_type;
2018-05-07 13:27:50 +02:00
struct gds_library *current_lib = NULL;
struct gds_cell *current_cell = NULL;
struct gds_graphics *current_graphics = NULL;
struct gds_cell_instance *current_s_reference = NULL;
struct gds_cell_array_instance *current_a_reference = NULL;
struct gds_cell_array_instance temp_a_reference;
2018-05-10 00:14:00 +02:00
int x, y;
2018-05-07 13:27:50 +02:00
////////////
2018-05-07 15:57:47 +02:00
GList *lib_list;
lib_list = *library_list;
2018-05-07 13:27:50 +02:00
2018-05-31 02:16:12 +02:00
/* Allocate working buffer */
workbuff = (char *)malloc(sizeof(char)*128*1024);
if(!workbuff)
return -100;
2018-05-07 13:27:50 +02:00
/* open File */
2018-06-04 12:26:31 +02:00
gds_file = fopen(filename, "rb");
2018-05-07 13:27:50 +02:00
if (gds_file == NULL) {
GDS_ERROR("Could not open File %s", filename);
return -1;
}
/* Record parser */
while (run == 1) {
2018-05-10 00:14:00 +02:00
rec_type = INVALID;
read = fread(workbuff, sizeof(char), 2, gds_file);
if (read != 2 && (current_cell != NULL ||
current_graphics != NULL ||
current_lib != NULL ||
current_s_reference != NULL)) {
GDS_ERROR("End of File. with openend structs/libs");
run = -2;
break;
} else if (read != 2) {
/* EOF */
run = 0;
break;
}
2019-09-15 13:04:30 +02:00
rec_data_length = gds_convert_unsigned_int16(workbuff);
2018-05-10 00:14:00 +02:00
if (rec_data_length < 4) {
/* Possible Zero-Padding: */
run = 0;
GDS_WARN("Zero Padding detected!");
if (current_cell != NULL ||
current_graphics != NULL ||
current_lib != NULL ||
current_s_reference != NULL) {
GDS_ERROR("Not all structures closed");
2018-05-07 13:27:50 +02:00
run = -2;
2018-05-10 00:14:00 +02:00
}
break;
}
rec_data_length -= 4;
read = fread(workbuff, sizeof(char), 2, gds_file);
if (read != 2) {
run = -2;
GDS_ERROR("Unexpected end of file");
break;
}
2019-09-15 13:04:30 +02:00
rec_type = gds_convert_unsigned_int16(workbuff);
2018-05-10 00:14:00 +02:00
2018-05-10 00:14:00 +02:00
/* if begin: Allocate structures */
switch (rec_type) {
case BGNLIB:
lib_list = append_library(lib_list, parsing_options, &current_lib);
2018-05-10 00:14:00 +02:00
if (lib_list == NULL) {
GDS_ERROR("Allocating memory failed");
run = -3;
2018-05-07 13:27:50 +02:00
break;
2018-05-10 00:14:00 +02:00
}
GDS_INF("Entering Lib\n");
2018-05-10 00:14:00 +02:00
break;
case ENDLIB:
if (current_lib == NULL) {
run = -4;
GDS_ERROR("Closing Library with no opened library");
2018-05-07 13:27:50 +02:00
break;
}
2018-05-10 00:14:00 +02:00
/* Check for open Cells */
if (current_cell != NULL) {
run = -4;
GDS_ERROR("Closing Library with opened cells");
2018-05-07 13:27:50 +02:00
break;
}
2018-05-10 00:14:00 +02:00
current_lib = NULL;
GDS_INF("Leaving Library\n");
2018-05-07 13:27:50 +02:00
break;
2018-05-10 00:14:00 +02:00
case BGNSTR:
if (current_lib == NULL) {
GDS_ERROR("Defining Cell outside of library!\n");
run = -4;
break;
}
2018-05-10 01:36:01 +02:00
current_lib->cells = append_cell(current_lib->cells, &current_cell);
2018-05-10 00:14:00 +02:00
if (current_lib->cells == NULL) {
GDS_ERROR("Allocating memory failed");
run = -3;
2018-05-07 13:27:50 +02:00
break;
}
current_cell->parent_library = current_lib;
2019-06-08 12:43:01 +02:00
GDS_INF("Entering cell\n");
2018-05-10 00:14:00 +02:00
break;
case ENDSTR:
if (current_cell == NULL) {
run = -4;
GDS_ERROR("Closing cell with no opened cell");
2018-05-07 13:27:50 +02:00
break;
2018-05-10 00:14:00 +02:00
}
/* Check for open Elements */
if (current_graphics != NULL || current_s_reference != NULL) {
run = -4;
GDS_ERROR("Closing cell with opened Elements");
2018-05-07 13:42:06 +02:00
break;
2018-05-10 00:14:00 +02:00
}
current_cell = NULL;
GDS_INF("Leaving Cell\n");
2018-05-10 00:14:00 +02:00
break;
2018-07-17 00:02:30 +02:00
case BOX:
2018-05-10 00:14:00 +02:00
case BOUNDARY:
if (current_cell == NULL) {
2018-07-17 00:02:30 +02:00
GDS_ERROR("Boundary/Box outside of cell");
2018-05-10 00:14:00 +02:00
run = -3;
2018-05-07 13:27:50 +02:00
break;
2018-05-10 00:14:00 +02:00
}
current_cell->graphic_objs = prepend_graphics(current_cell->graphic_objs,
(rec_type == BOUNDARY
? GRAPHIC_POLYGON
: GRAPHIC_BOX),
2018-07-17 00:02:30 +02:00
&current_graphics);
2018-05-10 00:14:00 +02:00
if (current_cell->graphic_objs == NULL) {
GDS_ERROR("Memory allocation failed");
run = -4;
2018-05-07 13:27:50 +02:00
break;
2018-05-10 00:14:00 +02:00
}
GDS_INF("\tEntering boundary of type %s\n", rec_type==BOUNDARY ? "polygon" : "box");
2018-05-10 00:14:00 +02:00
break;
case SREF:
if (current_cell == NULL) {
2019-03-06 20:04:49 +01:00
GDS_ERROR("Cell Reference outside of cell");
2018-05-10 00:14:00 +02:00
run = -3;
2018-05-07 15:17:29 +02:00
break;
2018-05-10 00:14:00 +02:00
}
2018-05-10 01:36:01 +02:00
current_cell->child_cells = append_cell_ref(current_cell->child_cells,
&current_s_reference);
current_cell->stats.reference_count++;
2018-05-10 00:14:00 +02:00
if (current_cell->child_cells == NULL) {
GDS_ERROR("Memory allocation failed");
run = -4;
break;
}
2018-05-07 15:17:29 +02:00
GDS_INF("\tEntering reference\n");
2018-05-10 00:14:00 +02:00
break;
case PATH:
if (current_cell == NULL) {
GDS_ERROR("Path outside of cell");
run = -3;
2018-05-07 15:17:29 +02:00
break;
2018-05-10 00:14:00 +02:00
}
current_cell->graphic_objs = prepend_graphics(current_cell->graphic_objs,
2018-05-10 01:36:01 +02:00
GRAPHIC_PATH, &current_graphics);
2018-05-10 00:14:00 +02:00
if (current_cell->graphic_objs == NULL) {
GDS_ERROR("Memory allocation failed");
run = -4;
2018-05-07 15:17:29 +02:00
break;
2018-05-10 00:14:00 +02:00
}
GDS_INF("\tEntering Path\n");
2018-05-10 00:14:00 +02:00
break;
case ENDEL:
if (current_graphics != NULL) {
GDS_INF("\tLeaving %s\n", (current_graphics->gfx_type == GRAPHIC_POLYGON ? "boundary"
: (current_graphics->gfx_type == GRAPHIC_PATH ? "path"
: "box")));
2018-05-10 00:14:00 +02:00
current_graphics = NULL;
if (current_cell) {
current_cell->stats.gfx_count++;
}
2018-05-10 00:14:00 +02:00
}
if (current_s_reference != NULL) {
GDS_INF("\tLeaving Reference\n");
2018-05-10 00:14:00 +02:00
current_s_reference = NULL;
}
if (current_a_reference != NULL) {
GDS_INF("\tLeaving Array Reference\n");
convert_aref_to_sref(current_a_reference, current_cell);
current_a_reference = NULL;
}
2018-05-10 00:14:00 +02:00
break;
case XY:
if (current_graphics) {
2018-05-07 13:27:50 +02:00
2018-05-10 00:14:00 +02:00
} else if (current_s_reference) {
if (rec_data_length != 8) {
GDS_WARN("Instance has weird coordinates. Rendered output might be wrong!");
2018-05-07 17:36:14 +02:00
}
} else if (current_a_reference) {
if (rec_data_length != (3*(4+4)))
GDS_WARN("Array instance has weird coordinates. Rendered output might be wrong!");
2018-05-07 13:27:50 +02:00
}
2018-05-10 00:14:00 +02:00
break;
case AREF:
if (current_cell == NULL) {
GDS_ERROR("Cell array reference outside of cell");
run = -3;
break;
}
2018-07-17 00:02:30 +02:00
if (current_a_reference != NULL) {
GDS_ERROR("Recursive cell array reference");
run = -3;
break;
}
GDS_INF("Entering Array Reference\n");
/* Array references are coverted after fully declared. Therefore,
* only a static buffer is needed
*/
current_a_reference = &temp_a_reference;
current_a_reference->ref_name[0] = '\0';
current_a_reference->angle = 0.0;
current_a_reference->magnification = 1.0;
current_a_reference->flipped = 0;
current_a_reference->rows = 0;
current_a_reference->columns = 0;
2018-05-10 00:14:00 +02:00
break;
case COLROW:
case MAG:
2018-05-10 00:14:00 +02:00
case ANGLE:
case STRANS:
case WIDTH:
case PATHTYPE:
2018-12-22 19:11:09 +01:00
case UNITS:
case LIBNAME:
case SNAME:
case LAYER:
case DATATYPE:
case STRNAME:
2018-12-22 19:11:09 +01:00
break;
2018-05-10 00:14:00 +02:00
default:
GDS_INF("Unhandled Record: %04x, len: %u\n", (unsigned int)rec_type, (unsigned int)rec_data_length);
2018-05-10 00:14:00 +02:00
break;
} /* switch(rec_type) */
2018-05-07 13:27:50 +02:00
2018-05-07 17:36:14 +02:00
2018-05-10 00:14:00 +02:00
/* No Data -> No Processing, go back to top */
if (!rec_data_length || run != 1) continue;
2018-05-07 13:27:50 +02:00
2018-05-10 00:14:00 +02:00
read = fread(workbuff, sizeof(char), rec_data_length, gds_file);
if (read != rec_data_length) {
2018-06-04 12:05:53 +02:00
GDS_ERROR("Could not read enough data: requested: %u, read: %u | Type: 0x%04x\n",
2018-05-31 02:16:12 +02:00
(unsigned int)rec_data_length, (unsigned int)read, (unsigned int)rec_type);
2018-05-10 00:14:00 +02:00
run = -5;
break;
}
switch (rec_type) {
case AREF:
case HEADER:
case ENDLIB:
case ENDSTR:
case BOUNDARY:
case PATH:
case SREF:
case ENDEL:
case BOX:
case INVALID:
break;
case COLROW:
if (!current_a_reference) {
GDS_ERROR("COLROW record defined outside of array instance");
break;
}
if (rec_data_length != 4 || read != 4) {
GDS_ERROR("COLUMN/ROW count record contains too few data. Won't set column and row counts (%d, %d)",
rec_data_length, read);
break;
}
current_a_reference->columns = (int)gds_convert_signed_int16(&workbuff[0]);
current_a_reference->rows = (int)gds_convert_signed_int16(&workbuff[2]);
GDS_INF("\tRows: %d\n\tColumns: %d\n", current_a_reference->rows, current_a_reference->columns);
break;
2018-12-22 19:11:09 +01:00
case UNITS:
if (!current_lib) {
GDS_WARN("Units defined outside of library!\n");
break;
}
if (rec_data_length != 16) {
GDS_WARN("Unit define incomplete. Will assume database unit of %E meters\n", current_lib->unit_in_meters);
break;
}
current_lib->unit_in_meters = gds_convert_double(&workbuff[8]);
GDS_INF("Length of database unit: %E meters\n", current_lib->unit_in_meters);
break;
case BGNLIB:
/* Parse date record */
gds_parse_date(workbuff, read, &current_lib->mod_time, &current_lib->access_time);
break;
case BGNSTR:
gds_parse_date(workbuff, read, &current_cell->mod_time, &current_cell->access_time);
break;
2018-05-10 00:14:00 +02:00
case LIBNAME:
name_library(current_lib, (unsigned int)read, workbuff);
2018-05-10 00:14:00 +02:00
break;
case STRNAME:
name_cell(current_cell, (unsigned int)read, workbuff, current_lib);
2018-05-10 00:14:00 +02:00
break;
case XY:
if (current_s_reference) {
/* Get origin of reference */
current_s_reference->origin.x = gds_convert_signed_int(workbuff);
current_s_reference->origin.y = gds_convert_signed_int(&workbuff[4]);
GDS_INF("\t\tSet origin to: %d/%d\n", current_s_reference->origin.x,
2018-05-10 00:14:00 +02:00
current_s_reference->origin.y);
} else if (current_graphics) {
for (i = 0; i < read/8; i++) {
x = gds_convert_signed_int(&workbuff[i*8]);
2018-05-10 00:49:24 +02:00
y = gds_convert_signed_int(&workbuff[i*8+4]);
2018-05-10 00:14:00 +02:00
current_graphics->vertices =
append_vertex(current_graphics->vertices, x, y);
GDS_INF("\t\tSet coordinate: %d/%d\n", x, y);
if (current_cell) {
current_cell->stats.vertex_count++;
}
2018-05-07 17:36:14 +02:00
}
} else if (current_a_reference) {
for (i = 0; i < 3; i++) {
x = gds_convert_signed_int(&workbuff[i*8]);
y = gds_convert_signed_int(&workbuff[i*8+4]);
current_a_reference->control_points[i].x = x;
current_a_reference->control_points[i].y = y;
GDS_INF("\tSet control point %d: %d/%d\n", i, x, y);
}
2018-05-10 00:14:00 +02:00
}
break;
case STRANS:
if (current_s_reference) {
current_s_reference->flipped = ((workbuff[0] & 0x80) ? 1 : 0);
} else if (current_a_reference) {
current_a_reference->flipped = ((workbuff[0] & 0x80) ? 1 : 0);
} else {
2018-05-10 00:14:00 +02:00
GDS_ERROR("Transformation defined outside of instance");
2018-05-07 17:36:14 +02:00
break;
2018-05-10 00:14:00 +02:00
}
break;
case SNAME:
2019-03-06 20:04:49 +01:00
if (current_s_reference) {
name_cell_ref(current_s_reference, (unsigned int)read, workbuff);
} else if (current_a_reference) {
name_array_cell_ref(current_a_reference, (unsigned int)read, workbuff);
2019-03-06 20:04:49 +01:00
} else {
2019-06-07 19:32:38 +02:00
GDS_ERROR("Reference name set outside of cell reference");
2019-03-06 20:04:49 +01:00
}
2018-05-10 00:14:00 +02:00
break;
case WIDTH:
if (!current_graphics) {
GDS_WARN("Width defined outside of path element");
}
current_graphics->width_absolute = gds_convert_signed_int(workbuff);
break;
2018-05-10 00:14:00 +02:00
case LAYER:
if (!current_graphics) {
GDS_WARN("Layer has to be defined inside graphics object. Probably unknown object. Implement it yourself!");
break;
2018-05-10 00:14:00 +02:00
}
current_graphics->layer = gds_convert_signed_int16(workbuff);
2018-05-10 00:49:24 +02:00
if (current_graphics->layer < 0) {
2018-05-10 01:11:07 +02:00
GDS_WARN("Layer negative!\n");
2018-05-10 00:49:24 +02:00
}
GDS_INF("\t\tAdded layer %d\n", (int)current_graphics->layer);
2018-05-10 00:14:00 +02:00
break;
case DATATYPE:
if (!current_graphics) {
GDS_WARN("Datatype has to be defined inside graphics object. Probably unknown object. Implement it yourself!");
break;
}
current_graphics->datatype = gds_convert_signed_int16(workbuff);
if (current_graphics->datatype < 0)
GDS_WARN("Datatype negative!");
GDS_INF("\t\tAdded datatype %d\n", (int)current_graphics->datatype);
break;
2018-05-10 00:14:00 +02:00
case MAG:
if (rec_data_length != 8) {
GDS_WARN("Magnification is not an 8 byte real. Results may be wrong");
}
if (current_graphics != NULL && current_s_reference != NULL) {
GDS_ERROR("Open Graphics and Cell Reference\n\tMissing ENDEL?");
run = -6;
break;
2018-05-10 00:14:00 +02:00
}
if (current_s_reference != NULL) {
current_s_reference->magnification = gds_convert_double(workbuff);
GDS_INF("\t\tMagnification defined: %lf\n", current_s_reference->magnification);
2018-05-10 00:14:00 +02:00
}
if (current_a_reference != NULL) {
current_a_reference->magnification = gds_convert_double(workbuff);
GDS_INF("\t\tMagnification defined: %lf\n", current_a_reference->magnification);
}
2018-05-10 00:14:00 +02:00
break;
case ANGLE:
if (rec_data_length != 8) {
GDS_WARN("Angle is not an 8 byte real. Results may be wrong");
}
if (current_graphics != NULL && current_s_reference != NULL && current_a_reference != NULL) {
2018-05-10 00:14:00 +02:00
GDS_ERROR("Open Graphics and Cell Reference\n\tMissing ENDEL?");
run = -6;
2018-05-07 17:36:14 +02:00
break;
2018-05-07 13:27:50 +02:00
}
2018-05-10 00:14:00 +02:00
if (current_s_reference != NULL) {
current_s_reference->angle = gds_convert_double(workbuff);
GDS_INF("\t\tAngle defined: %lf\n", current_s_reference->angle);
2018-05-10 00:14:00 +02:00
}
if (current_a_reference != NULL) {
current_a_reference->angle = gds_convert_double(workbuff);
GDS_INF("\t\tAngle defined: %lf\n", current_a_reference->angle);
}
2018-05-10 00:14:00 +02:00
break;
case PATHTYPE:
if (current_graphics == NULL) {
GDS_WARN("Path type defined outside of path. Ignoring");
break;
}
if (current_graphics->gfx_type == GRAPHIC_PATH) {
2019-03-06 20:04:49 +01:00
current_graphics->path_render_type = (enum path_type)gds_convert_signed_int16(workbuff);
GDS_INF("\t\tPathtype: %d\n", current_graphics->path_render_type);
} else {
GDS_WARN("Path type defined inside non-path graphics object. Ignoring");
}
break;
2018-05-07 13:27:50 +02:00
}
2018-05-10 00:14:00 +02:00
} /* while(run == 1) */
2018-05-07 13:27:50 +02:00
fclose(gds_file);
2018-05-31 02:16:12 +02:00
if (!run) {
/* Iterate and find references to cells */
g_list_foreach(lib_list, scan_library_references, NULL);
/* Calculate lib stats and cummulative total counts */
g_list_foreach(lib_list, calc_library_stats, NULL);
2018-05-31 02:16:12 +02:00
}
2018-05-07 15:17:29 +02:00
2018-05-07 15:57:47 +02:00
*library_list = lib_list;
2018-05-31 02:16:12 +02:00
free(workbuff);
2018-05-07 13:27:50 +02:00
return run;
}
/**
* @brief delete_cell_inst_element
* @param cell_inst
*/
static void delete_cell_inst_element(struct gds_cell_instance *cell_inst)
{
2019-03-06 18:16:14 +01:00
if (cell_inst)
free(cell_inst);
}
/**
* @brief delete_vertex
* @param vertex
*/
2018-05-17 20:31:25 +02:00
static void delete_vertex(struct gds_point *vertex)
{
2019-03-06 18:16:14 +01:00
if (vertex)
free(vertex);
2018-05-17 20:31:25 +02:00
}
/**
* @brief delete_graphics_obj
* @param gfx
*/
2018-05-17 20:31:25 +02:00
static void delete_graphics_obj(struct gds_graphics *gfx)
{
2019-03-06 18:16:14 +01:00
if (!gfx)
return;
2018-05-17 20:31:25 +02:00
g_list_free_full(gfx->vertices, (GDestroyNotify)delete_vertex);
free(gfx);
}
/**
* @brief delete_cell_element
* @param cell
*/
static void delete_cell_element(struct gds_cell *cell)
{
2019-03-06 18:16:14 +01:00
if (!cell)
return;
g_list_free_full(cell->child_cells, (GDestroyNotify)delete_cell_inst_element);
2018-05-17 20:31:25 +02:00
g_list_free_full(cell->graphic_objs, (GDestroyNotify)delete_graphics_obj);
free(cell);
}
/**
* @brief delete_library_element
* @param lib
*/
static void delete_library_element(struct gds_library *lib)
{
2019-03-06 18:16:14 +01:00
if (!lib)
return;
g_list_free(lib->cell_names);
g_list_free_full(lib->cells, (GDestroyNotify)delete_cell_element);
free(lib);
}
int clear_lib_list(GList **library_list)
{
2019-03-06 18:16:14 +01:00
if (!library_list)
return 0;
2018-05-17 20:31:25 +02:00
if (*library_list == NULL)
return 0;
2019-03-06 18:16:14 +01:00
g_list_free_full(*library_list, (GDestroyNotify)delete_library_element);
*library_list = NULL;
return 0;
}
/** @} */