shithub: ft²

Download patch

ref: 4e9dd5ea4261a6adfd0b675d65ecabc89339ec01
parent: 43313efb3f2b47fc074e64ea2dcd43615d6420fd
author: qwx <qwx@sciops.net>
date: Sun Mar 5 18:04:47 EST 2023

diskop: improve abysmal directory load times

still a kludge, pending a better fix (todo)
unicharToCp437 seems to be the bottleneck here

--- a/src/ft2_diskop.c
+++ b/src/ft2_diskop.c
@@ -1565,14 +1565,15 @@
 {
 	DirRec *dirEntry = &FReq_Buffer[rad];
 
-	char *name = unicharToCp437(dirEntry->nameU, true);
-	if (name == NULL)
-		return NULL;
+	//char *name = unicharToCp437(dirEntry->nameU, true);
+	//if (name == NULL)
+	//	return NULL;
+	char *name = dirEntry->nameU;
 
 	const int32_t nameLen = (int32_t)strlen(name);
 	if (nameLen == 0)
 	{
-		free(name);
+		//free(name);
 		return NULL;
 	}
 
@@ -1579,7 +1580,7 @@
 	char *p = (char *)malloc(nameLen+1+1);
 	if (p == NULL)
 	{
-		free(name);
+		//free(name);
 		return NULL;
 	}
 
@@ -1594,7 +1595,7 @@
 
 		strcpy(&p[1], name);
 
-		free(name);
+		//free(name);
 		return p;
 	}
 	else
@@ -1606,7 +1607,7 @@
 		{
 			// sort by filename
 			strcpy(p, name);
-			free(name);
+			//free(name);
 			return p;
 		}
 		else
@@ -1616,7 +1617,7 @@
 			if (extLen <= 1)
 			{
 				strcpy(p, name);
-				free(name);
+				//free(name);
 				return p;
 			}
 
@@ -1625,12 +1626,27 @@
 			memcpy(&p[extLen-1], name, i);
 			p[nameLen-1] = '\0';
 
-			free(name);
+			//free(name);
 			return p;
 		}
 	}
 }
 
+static int dcmp(void *a, void *b)
+{
+	DirRec *d, *e;
+	char *n, *m;
+
+	// FIXME: reimplement dcmp with the stuff the rest does
+	d = a;
+	e = b;
+	//n = d->nameU;//unicharToCp437(d->nameU, true);
+	//m = e->nameU;//unicharToCp437(e->nameU, true);
+	n = ach(d - FReq_Buffer);
+	m = ach(e - FReq_Buffer);
+	return _stricmp(n, m);
+}
+
 static void sortDirectory(void)
 {
 	bool didSwap;
@@ -1637,6 +1653,8 @@
 
 	if (FReq_FileCount < 2)
 		return; // no need to sort
+	qsort(FReq_Buffer, FReq_FileCount, sizeof *FReq_Buffer, dcmp);
+	return;
 
 	uint32_t offset = FReq_FileCount >> 1;
 	while (offset > 0)