From ff5b16446308979e828306c231486996281411bf Mon Sep 17 00:00:00 2001 From: Stefan Petersen Date: Mon, 5 Sep 2011 23:47:28 +0200 Subject: [PATCH] drill_file_p() now uses gerb_file operations to support zipped files. --- src/drill.c | 61 ++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/src/drill.c b/src/drill.c index cb8278d..1d2c4e8 100644 --- a/src/drill.c +++ b/src/drill.c @@ -849,9 +849,8 @@ parse_drillfile(gerb_file_t *fd, gerbv_HID_Attribute *attr_list, int n_attr, int gboolean drill_file_p(gerb_file_t *fd, gboolean *returnFoundBinary) { - char *buf = NULL, *tbuf; + char *buf = NULL, *tbuf = NULL; int len = 0; - int idx = 0; char *letter; int ascii; int zero = 48; /* ascii 0 */ @@ -866,42 +865,47 @@ drill_file_p(gerb_file_t *fd, gboolean *returnFoundBinary) gboolean found_Y = FALSE; gboolean end_comments = FALSE; - dprintf ("drill_file_p(%p, %p), fd->fd = %p, fd->zfd = %p %s\n", - fd, returnFoundBinary, fd->fd, fd->zfd, fd->filename); - - tbuf = g_malloc(MAXL); - if (tbuf == NULL) { - GERB_FATAL_ERROR("malloc buf failed while checking for drill file.\n"); - } + dprintf ("drill_file_p(%p, %p), fd->fd = %p, fd->zfd = %p %s\n", + fd, returnFoundBinary, fd->fd, fd->zfd, fd->filename); while (1) { - if (fd->datalen == idx) { + + /* Release the buffer if it was in use previously */ + if (tbuf) { + g_free(tbuf); + tbuf = NULL; + } + + /* Read one line at the time using gerb_file operations to support zip*/ + tbuf = gerb_fgetstring(fd, '\n'); + + /* EOF detected */ + if (tbuf == NULL) { break; } - buf = tbuf; + /* Read the \n */ + gerb_fgetc(fd); - /* We sneak a peak by (ab)using the gerb_file operations. It is - * better to use gerb_file operations, since the data in the file - * actually can be zipped. gerb_file operations handle that. */ + len = strlen(tbuf); + if (len == 0) { + continue; + } + + buf = tbuf; - len = ((fd->datalen - idx) < MAXL)? (fd->datalen - idx) : (MAXL - 1); - memset(tbuf, 0, MAXL); - memcpy(tbuf, &fd->data[idx], len); - idx += len; dprintf ("buf = \"%s\"\n", buf); - /* Check for comments at top of file. */ + /* Check for comments at top of file. */ if ((!end_comments) && (g_strstr_len(buf, len, ";") != NULL)) { for (i = 0; i < len - 1 ; ++i){ if ((buf[i] == '\n') && (buf[i+1] != ';') && - (buf[i+1] != '\r') && + (buf[i+1] != '\r') && (buf[i+1] != '\n')) { end_comments = TRUE; buf = &tbuf[i+1];/* set rest of parser to end of comments */ - if (!end_comments) { continue; } @@ -965,20 +969,23 @@ drill_file_p(gerb_file_t *fd, gboolean *returnFoundBinary) } } - free(tbuf); + g_free(tbuf); *returnFoundBinary = found_binary; - + + gerb_freset(fd); + /* Now form logical expression determining if this is a drill file */ - if ( ((found_X || found_Y) && found_T) && - (found_M48 || (found_percent && found_M30)) ) + if ( ((found_X || found_Y) && found_T) && + (found_M48 || (found_percent && found_M30)) ) { return TRUE; - else if (found_M48 && found_T && found_percent && found_M30) + } else if (found_M48 && found_T && found_percent && found_M30) { /* Pathological case of drill file with valid header and EOF but no drill XY locations. */ return TRUE; - else + } else { return FALSE; + } } /* drill_file_p */ -- 2.11.4.GIT