From 1e556333bda02a6f7cf4112f36a5aa3b5970409d Mon Sep 17 00:00:00 2001
From: Etienne Brateau <etienne.brateau@gmail.com>
Date: Sun, 20 Oct 2019 18:49:05 +0200
Subject: [PATCH] Move some fields into page

---
 include/logglobals.h |  5 ----
 include/page.h       |  9 +++++--
 src/display.c        |  6 +++--
 src/gate.c           |  8 +++---
 src/log.c            | 60 ++++++++++++++++++++++----------------------
 src/page.c           | 20 +++++++--------
 src/pagereader.c     | 35 +++++++++++++++-----------
 src/pagewriter.c     | 10 ++++----
 8 files changed, 81 insertions(+), 72 deletions(-)

diff --git a/include/logglobals.h b/include/logglobals.h
index f903a35..cfa6fb5 100644
--- a/include/logglobals.h
+++ b/include/logglobals.h
@@ -65,11 +65,6 @@ extern short curwcolor;   /* Current VLSI wire color */
 extern char cureditmode;   /* Current editing (gate-tapping) mode */
 extern short curlistgroup;   /* Current group in library list */
 
-extern int vlsimode[log_maxpages];   /* Page contains VLSI circuits */
-extern long xoffp[log_maxpages], yoffp[log_maxpages];
-/* XOFF, YOFF for non-current pages */
-extern short gatecount[log_maxpages];   /* Highest used gate-ID number */
-extern char *curfilename[log_maxpages];   /* Name of last-loaded file */
 extern long realcurpage;
 
 extern baseptrs copybuf;   /* Del/Copy/Paste buffer */
diff --git a/include/page.h b/include/page.h
index 6e4ad5c..9797ac9 100644
--- a/include/page.h
+++ b/include/page.h
@@ -16,6 +16,13 @@ typedef struct log_regrec {
 } log_regrec;   /* Sensitive region */
 
 typedef struct log_page {
+	long xoffp;  ///< XOFF for non-current pages
+	long yoffp;  ///< YOFF for non-current pages
+	int vlsimode;   ///< Page contains VLSI circuits
+	short gatecount;   ///< Highest used gate-ID number
+	char *curfilename;   ///< Name of last-loaded file
+	long pagestamp;
+	int pagechanged;
 	log_grec *gbase; /* base gate of the page */
 	log_srec *sbase; /* base solderpoint of the page */
 	log_hwrec *hwbase; /* base horizontal wire of the page */
@@ -23,8 +30,6 @@ typedef struct log_page {
 	log_lrec *lbase; /* base label of page */
 	log_brec *bbase; /* base box of page */
 	log_regrec *pageregions; /* base region of page */
-	long pagestamp;
-	int pagechanged;
 } log_page;
 
 void chpage(log_page *page);
diff --git a/src/display.c b/src/display.c
index 81255da..57c7ef1 100644
--- a/src/display.c
+++ b/src/display.c
@@ -19,6 +19,7 @@ void setscale(short s)
 void zoomto(short z)
 {
 	short s0, i, FORLIM;
+	log_page *page;
 
 	if (-2 > z || z > 2 || z == zoom)
 	{
@@ -32,8 +33,9 @@ void zoomto(short z)
 	FORLIM = gg.numpages;
 	for (i = 0; i < FORLIM; i++)
 	{
-		xoffp[i] = (xoffp[i] + across / 2) * gg.scale / s0 - across / 2;
-		yoffp[i] = (yoffp[i] + baseline / 2) * gg.scale / s0 - baseline / 2;
+		page = gg.pages[i];
+		page->xoffp = (page->xoffp + across / 2) * gg.scale / s0 - across / 2;
+		page->yoffp = (page->yoffp + baseline / 2) * gg.scale / s0 - baseline / 2;
 	}
 	refrfunc();
 }
diff --git a/src/gate.c b/src/gate.c
index 4042f57..940afb5 100644
--- a/src/gate.c
+++ b/src/gate.c
@@ -653,7 +653,7 @@ void addgate2(short x, short y, short gtype, short sig, log_gattrrec *attrs)
 	log_page *current_page = get_current_page();
 
 	newgate2(&g, gtype, sig, attrs);
-	if (gatecount[gg.curpage - 1] == log_maxshint)
+	if (current_page->gatecount == log_maxshint)
 	{
 		i = 0;
 		gmax = 0;
@@ -670,12 +670,12 @@ void addgate2(short x, short y, short gtype, short sig, log_gattrrec *attrs)
 			}
 		} while (flag);
 		g->gc = i;
-		gatecount[gg.curpage - 1] = gmax;
+		current_page->gatecount = gmax;
 	}
 	else
 	{
-		gatecount[gg.curpage - 1]++;
-		g->gc = gatecount[gg.curpage - 1];
+		current_page->gatecount++;
+		g->gc = current_page->gatecount;
 	}
 	g->x = x;
 	g->y = y;
diff --git a/src/log.c b/src/log.c
index c8c64f0..af38f89 100644
--- a/src/log.c
+++ b/src/log.c
@@ -133,11 +133,6 @@ short curwcolor;   ///< Current VLSI wire color
 char cureditmode;   ///< Current editing (gate-tapping) mode
 short curlistgroup;   ///< Current group in library list
 
-int vlsimode[log_maxpages];   ///< Page contains VLSI circuits
-long xoffp[log_maxpages];  ///< XOFF for non-current pages
-long yoffp[log_maxpages];  ///< YOFF for non-current pages
-short gatecount[log_maxpages];   ///< Highest used gate-ID number
-char *curfilename[log_maxpages];   ///< Name of last-loaded file
 long realcurpage;
 
 baseptrs copybuf;   ///< Del/Copy/Paste buffer
@@ -2036,6 +2031,8 @@ void doimmedfunction()
 	char cmd[256];
 	strlist_t *l1;
 
+	log_page *current_page = get_current_page();
+
 	if (*gg.func != '\0')
 	{
 		l1 = strlist_find(commandlist, gg.func);
@@ -2154,10 +2151,10 @@ void doimmedfunction()
 	}
 	if (!strcmp(gg.func, "ALTPOSN"))
 	{
-		xoff0 = xoffp[gg.curpage - 1] - gg.xoff;
-		yoff0 = yoffp[gg.curpage - 1] - gg.yoff;
-		xoffp[gg.curpage - 1] = gg.xoff;
-		yoffp[gg.curpage - 1] = gg.yoff;
+		xoff0 = current_page->xoffp - gg.xoff;
+		yoff0 = current_page->yoffp - gg.yoff;
+		current_page->xoffp = gg.xoff;
+		current_page->yoffp = gg.yoff;
 		refrfunc();
 		return;
 	}
@@ -2736,8 +2733,8 @@ static void cutcopy(baseptrs *bases, short x1, short y1, short x2, short y2, int
 		b = b1;
 	}
 	bases->valid = true;
-	if (isPageEmpty(current_page) && curfilename[gg.curpage - 1] != NULL)
-		Free(curfilename[gg.curpage - 1]);
+	if (isPageEmpty(current_page) && current_page->curfilename != NULL)
+		Free(current_page->curfilename);
 }
 
 
@@ -5671,7 +5668,7 @@ void setvlsimode(int flag)
 	else
 		curwcolor = log_wcol_normal;
 	vlsi = flag;
-	vlsimode[gg.curpage - 1] = vlsi;
+	get_current_page()->vlsimode = vlsi;
 }
 
 
@@ -9003,7 +9000,7 @@ static void savecommand()
 				sprintf(STR2, "No changes to page %d", pgnum);
 				message(STR2);
 			}
-			else if (curfilename[pgnum - 1] == NULL)
+			else if (gg.pages[pgnum - 1]->curfilename == NULL)
 			{
 				sprintf(STR2, "Page %d: no file name!", pgnum);
 				message(STR2);
@@ -9012,7 +9009,7 @@ static void savecommand()
 			{
 				sprintf(STR1, "Saving page %d", pgnum);
 				message(STR1);
-				safesavepage(pgnum, curfilename[pgnum - 1]);
+				safesavepage(pgnum, gg.pages[pgnum - 1]->curfilename);
 			}
 		}
 	}
@@ -9020,8 +9017,9 @@ static void savecommand()
 	{
 		beginbottom();
 		nc_printf("File name to save: ");
-		if (curfilename[gg.curpage - 1] != NULL)
-			strcpy(filename, curfilename[gg.curpage - 1]);
+		log_page *current_page = get_current_page();
+		if (current_page->curfilename != NULL)
+			strcpy(filename, current_page->curfilename);
 		else
 			*filename = '\0';
 		readlnpass(filename, 3);
@@ -9041,12 +9039,14 @@ static void namecommand()
 {
 	char filename[256];
 	char STR2[256], STR3[256];
+	
+	log_page *current_page = get_current_page();
 
 	if (*gg.funcarg == '\0') {
 		beginbottom();
 		nc_printf("File name for page %ld", gg.curpage);
-		if (curfilename[gg.curpage - 1] != NULL)
-			nc_printf(" [%s]", curfilename[gg.curpage - 1]);
+		if (current_page->curfilename != NULL)
+			nc_printf(" [%s]", current_page->curfilename);
 		nc_printf(": ");
 		readlnpass(filename, 0);
 		endbottom();
@@ -9056,19 +9056,19 @@ static void namecommand()
 		strcpy(filename, gg.funcarg);
 	if (*filename != '\0') {
 		if (!strcmp(filename, "\"\"") || *strcpy(STR3, strltrim(filename)) == '\0')
-			Free(curfilename[gg.curpage - 1]);
+			Free(current_page->curfilename);
 		else {
 			newci_fixfname(filename, "lgf", "");
-			if (curfilename[gg.curpage - 1] == NULL)
-				curfilename[gg.curpage - 1] = (char *)Malloc(256);
-			strcpy(curfilename[gg.curpage - 1], filename);
+			if (current_page->curfilename == NULL)
+				current_page->curfilename = (char *)Malloc(256);
+			strcpy(current_page->curfilename, filename);
 		}
 	}
-	if (curfilename[gg.curpage - 1] == NULL) {
+	if (current_page->curfilename == NULL) {
 		sprintf(STR2, "No file name for page %ld", gg.curpage);
 		message(STR2);
 	} else {
-		sprintf(STR2, "Current file name is %s", curfilename[gg.curpage - 1]);
+		sprintf(STR2, "Current file name is %s", current_page->curfilename);
 		message(STR2);
 	}
 	clearfunc();
@@ -9672,8 +9672,8 @@ static void status_mem()
 			else
 				nc_printf("No     ");
 
-			if (curfilename[i] != NULL)
-				nc_puts(curfilename[i]);
+			if (gg.pages[i]->curfilename != NULL)
+				nc_puts(gg.pages[i]->curfilename);
 		}
 		else
 		{
@@ -11598,14 +11598,14 @@ static void initialize()
 	*gg.genfunc = '\0';
 	gg.xoff = origin;
 	gg.yoff = origin;
-	xoffp[0] = origin;
-	yoffp[0] = origin;
+	get_current_page()->xoffp = origin;
+	get_current_page()->yoffp = origin;
 	xoff0 = 0;
 	yoff0 = 0;
-	gatecount[0] = 0;
+	get_current_page()->gatecount = 0;
 	gg.nbase = NULL;
 	stamp(&gg.pages[0]->pagestamp);
-	curfilename[0] = NULL;
+	get_current_page()->curfilename = NULL;
 	initbuf(&copybuf);
 	nodeconflictbase = NULL;
 	gateconflictbase = NULL;
diff --git a/src/page.c b/src/page.c
index 04ddf8b..d6b0521 100644
--- a/src/page.c
+++ b/src/page.c
@@ -80,9 +80,9 @@ void newpage(short pg)
 	garbagecoll();
 	for (i = gg.numpages; i < pg; i++)
 	{
-		xoffp[i] = (origin + across / 2) * gg.scale / log_scale0 - across / 2;
-		yoffp[i] = (origin + baseline / 2) * gg.scale / log_scale0 - baseline / 2;
-		gatecount[i] = 0;
+		gg.pages[i]->xoffp = (origin + across / 2) * gg.scale / log_scale0 - across / 2;
+		gg.pages[i]->yoffp = (origin + baseline / 2) * gg.scale / log_scale0 - baseline / 2;
+		gg.pages[i]->gatecount = 0;
 		gg.pages[i]->gbase = NULL;
 		gg.pages[i]->sbase = NULL;
 		gg.pages[i]->hwbase = NULL;
@@ -92,16 +92,16 @@ void newpage(short pg)
 		gg.pages[i]->pagechanged = false;
 		stamp(&gg.pages[i]->pagestamp);
 		gg.pages[i]->pageregions = NULL;
-		curfilename[i] = NULL;
-		vlsimode[i] = vlsi;
+		gg.pages[i]->curfilename = NULL;
+		gg.pages[i]->vlsimode = vlsi;
 	}
 	if (pg != gg.curpage)
 	{
-		xoffp[gg.curpage - 1] = gg.xoff;
-		yoffp[gg.curpage - 1] = gg.yoff;
-		gg.xoff = xoffp[pg - 1];
-		gg.yoff = yoffp[pg - 1];
-		vlsi = vlsimode[pg - 1];
+		gg.pages[gg.curpage - 1]->xoffp = gg.xoff;
+		gg.pages[gg.curpage - 1]->yoffp = gg.yoff;
+		gg.xoff = gg.pages[pg - 1]->xoffp;
+		gg.yoff = gg.pages[pg - 1]->yoffp;
+		vlsi = gg.pages[pg - 1]->vlsimode;
 		curwcolor = log_wcol_normal;
 		gg.numpages = P_imax2((long)pg, gg.numpages);
 		gg.curpage = pg;
diff --git a/src/pagereader.c b/src/pagereader.c
index 144ac3e..de5a48e 100644
--- a/src/pagereader.c
+++ b/src/pagereader.c
@@ -116,6 +116,8 @@ void loadpage(char *filename_, char *reason_)
 	short FORLIM;
 	char *TEMP;
 
+	log_page *current_page = get_current_page();
+
 	strcpy(filename, filename_);
 	V.reason = reason_;
 	V.f = NULL;
@@ -138,11 +140,12 @@ void loadpage(char *filename_, char *reason_)
 		gg.actx = gg.curpage - 1;
 		calltools(act_clear);
 		
-		pageClear(gg.pages[gg.curpage - 1]);
+		pageClear(current_page);
+		current_page = get_current_page();
 
 		garbagecoll();
 		stamp(&gg.loadstamp);
-		stamp(&gg.pages[gg.curpage - 1]->pagestamp);
+		stamp(&current_page->pagestamp);
 		if (ver < 5)
 		{
 			loadlog30(&V.f, ver);
@@ -407,7 +410,7 @@ void loadpage(char *filename_, char *reason_)
 					g1 = g;
 					newgate(&g, j + x1 * log_kindoffset);
 					if (g1 == NULL)
-						gg.pages[gg.curpage - 1]->gbase = g;
+						current_page->gbase = g;
 					else
 						g1->next = g;
 					g->next = NULL;
@@ -494,11 +497,12 @@ void loadpage(char *filename_, char *reason_)
 		V.f = NULL;
 		if (V.ip != NULL)
 			Free(V.ip);
-		chpage(gg.pages[gg.curpage - 1]);
-		gg.pages[gg.curpage - 1]->pagechanged = false;
-		if (curfilename[gg.curpage - 1] == NULL)
-			curfilename[gg.curpage - 1] = (char *)Malloc(256);
-		strcpy(curfilename[gg.curpage - 1], filename);
+		chpage(current_page);
+		current_page = get_current_page();
+		current_page->pagechanged = false;
+		if (current_page->curfilename == NULL)
+			current_page->curfilename = (char *)Malloc(256);
+		strcpy(current_page->curfilename, filename);
 	}
 }
 
@@ -749,6 +753,8 @@ void readpage(char *filename_, char *reason)
 	char buf[256];
 	char *TEMP;
 
+	log_page *current_page = get_current_page();
+
 	strcpy(filename, filename_);
 	f = NULL;
 	*reason = '\0';
@@ -772,11 +778,12 @@ void readpage(char *filename_, char *reason)
 	if (f != NULL)
 		fclose(f);
 	f = NULL;
-	chpage(gg.pages[gg.curpage - 1]);
-	gg.pages[gg.curpage - 1]->pagechanged = false;
-	if (curfilename[gg.curpage - 1] == NULL)
-		curfilename[gg.curpage - 1] = (char *)Malloc(256);
-	strcpy(curfilename[gg.curpage - 1], filename);
+	chpage(current_page);
+	current_page = get_current_page();
+	current_page->pagechanged = false;
+	if (current_page->curfilename == NULL)
+		current_page->curfilename = (char *)Malloc(256);
+	strcpy(current_page->curfilename, filename);
 	if (f != NULL)
 		fclose(f);
 }
@@ -1133,5 +1140,5 @@ void loadlog30(FILE **f, short ver)
 		if (TEMP != NULL)
 			*TEMP = 0;
 	}
-	chpage(gg.pages[gg.curpage - 1]);
+	chpage(get_current_page());
 }
diff --git a/src/pagewriter.c b/src/pagewriter.c
index 669fa99..fc724d5 100644
--- a/src/pagewriter.c
+++ b/src/pagewriter.c
@@ -493,9 +493,9 @@ void savepage(short pgnum, char *filename_)
 			fclose(f);
 		f = NULL;
 		gg.pages[pgnum - 1]->pagechanged = false;
-		if (curfilename[pgnum - 1] == NULL)
-			curfilename[pgnum - 1] = (char *)Malloc(256);
-		strcpy(curfilename[pgnum - 1], filename);
+		if (gg.pages[pgnum - 1]->curfilename == NULL)
+			gg.pages[pgnum - 1]->curfilename = (char *)Malloc(256);
+		strcpy(gg.pages[pgnum - 1]->curfilename, filename);
 	}
 	if (f != NULL)
 		fclose(f);
@@ -508,8 +508,8 @@ void emergencysave(short pgnum)
 
 	TRY(try25);
 	nc_printf("File name");
-	if (curfilename[pgnum - 1] != NULL)
-		nc_printf("(was %s)", curfilename[pgnum - 1]);
+	if (gg.pages[pgnum - 1]->curfilename != NULL)
+		nc_printf("(was %s)", gg.pages[pgnum - 1]->curfilename);
 	nc_printf(": ");
 	nc_fgets(filename, 256, stdin);
 	if (*filename != '\0')
-- 
GitLab