changeset 286:abd1675c32b5

Parse new location file format correctly.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 18 Jun 2007 01:47:07 +0000
parents d5b9b53c6875
children 7869ae2e04e5
files mkloc.c
diffstat 1 files changed, 87 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/mkloc.c	Mon Jun 18 00:57:17 2007 +0000
+++ b/mkloc.c	Mon Jun 18 01:47:07 2007 +0000
@@ -74,7 +74,7 @@
 	OUTFMT_SCRIPT
 };
 
-#define NMAX	(1024)
+#define NMAX	(2048)
 
 /* Variables
  */
@@ -215,8 +215,8 @@
 }
 
 
-BOOL addLocation(locations_t *l, int x, int y, int dir,
-	char *desc, int flags, char *coders, char *url, char *freeform)
+BOOL addLocation(locations_t *l, int x, int y, int dir, int flags,
+	char *desc, char *coders, char *url, char *freeform)
 {
 	locinfo_t *tmp;
 	
@@ -268,24 +268,37 @@
 {
 	size_t i = 0;
 
+	while (i < tmax && f->inLine[f->linePos] && f->inLine[f->linePos] != ';')
+		ts[i++] = f->inLine[f->linePos++];
+	ts[i] = 0;
+}
+
+
+void parseNumericElement(fileinfo_t *f, char *ts, size_t tmax)
+{
+	size_t i = 0;
+
 	while (i < tmax && f->inLine[f->linePos] && th_isdigit(f->inLine[f->linePos]))
 		ts[i++] = f->inLine[f->linePos++];
 	ts[i] = 0;
 }
 
-BOOL parseValue(fileinfo_t *f, int *val)
+
+BOOL parseValue(fileinfo_t *f, int *val, char *field)
 {
 	char tmpStr[NMAX+1];
 	
 	if (!th_isdigit(f->inLine[f->linePos])) {
-		THERR("Expected decimal value on column %d line #%d in '%s'\n",
-			f->linePos, f->lineNum, f->fileName);
+		THERR("Expected decimal value on column %d line #%d in field '%s'\n",
+			f->linePos, f->lineNum, field);
 		return FALSE;
 	}
 
-	parseElement(f, tmpStr, NMAX);
+	parseNumericElement(f, tmpStr, NMAX);
 	*val = atoi(tmpStr);
 	
+	th_findnext(f->inLine, &f->linePos);
+	
 	return TRUE;
 }
 
@@ -362,9 +375,9 @@
 			break;
 
 		/* End of flags entry */
-		case ' ':
-		case 0:
+		case 32:
 		case 9:
+		case ';':
 			endFlags = TRUE;
 			break;
 		
@@ -372,7 +385,9 @@
 		default:
 			return FALSE;
 		}
-		f->linePos++;
+		
+		if (!endFlags)
+			f->linePos++;
 	}
 	
 	return TRUE;
@@ -397,7 +412,7 @@
 	}
 	
 	/* Parse data */
-	while (!feof(inFile) && fgets(inLine, sizeof(inLine), inFile)) {
+	while (fgets(inLine, sizeof(inLine), inFile) != NULL) {
 		for (i = 0; i < sizeof(inLine) && inLine[i] && inLine[i] != '\r' && inLine[i] != '\n'; i++);
 		inLine[i] = 0;
 		f.linePos = 0;
@@ -406,38 +421,39 @@
 		th_findnext(inLine, &f.linePos);
 			
 		if (th_isdigit(inLine[f.linePos])) {
-			int tx, ty, td, tf, tn;
+			int tmpX, tmpY, tmpDir, tmpFlags, tn;
+			char tmpDesc[NMAX], tmpCoders[NMAX], tmpURL[NMAX], tmpFreeform[NMAX];
 			
 			/* Get X coordinate */
-			if (!parseValue(&f, &tx))
+			if (!parseValue(&f, &tmpX, "X coord"))
 				return FALSE;
 				
-			if (!th_isspace(inLine[f.linePos])) {
-				THERR("Expected whitespace after X coord on line #%d in '%s'\n",
+			if (inLine[f.linePos] != ';') {
+				THERR("Expected ';' after X coord on line #%d in '%s'\n",
 					f.lineNum, f.fileName);
 				return FALSE;
 			}
 			
-			th_findnext(inLine, &f.linePos);
+			f.linePos++; th_findnext(inLine, &f.linePos);
 			
 			/* Get Y coordinate */
-			if (!parseValue(&f, &ty))
+			if (!parseValue(&f, &tmpY, "Y coord"))
 				return FALSE;
 			
-			if (!th_isspace(inLine[f.linePos])) {
-				THERR("Expected whitespace after Y coord on line #%d in '%s'\n",
+			if (inLine[f.linePos] != ';') {
+				THERR("Expected ';' after Y coord on line #%d in '%s'\n",
 					f.lineNum, f.fileName);
 				return FALSE;
 			}
 			
-			th_findnext(inLine, &f.linePos);
+			f.linePos++; th_findnext(inLine, &f.linePos);
 			
 			/* Get label direction / alignment */
-			if (!parseValue(&f, &td))
+			if (!parseValue(&f, &tmpDir, "direction"))
 				return FALSE;
 			
 			/* Get flags */
-			if (!parseFlags(&f, &tf)) {
+			if (!parseFlags(&f, &tmpFlags)) {
 				THERR("Expected flag(s) or whitespace after direction on line #%d in '%s'\n",
 					f.lineNum, f.fileName);
 				return FALSE;
@@ -445,31 +461,63 @@
 
 			th_findnext(inLine, &f.linePos);
 			
-			/* Get name */
+			if (inLine[f.linePos] != ';') {
+				THERR("Expected ';' after flags on line #%d in '%s'\n",
+					f.lineNum, f.fileName);
+				return FALSE;
+			}
+			
+			f.linePos++; th_findnext(inLine, &f.linePos);
+			
+			/* Get name/description */
 			if (inLine[f.linePos] == 0) {
 				THERR("No location name? on line #%d in '%s'\n",
 					f.lineNum, f.fileName);
 				return FALSE;
 			}
 			
+			parseElement(&f, tmpDesc, NMAX);
+			
+			if (inLine[f.linePos] != ';' && inLine[f.linePos] != 0) {
+				THERR("Expected ';' or line end after location desc on line #%d in '%s'\n",
+					f.lineNum, f.fileName);
+				return FALSE;
+			} else if (inLine[f.linePos] == ';') {
+				/* Get creators */
+				th_findnext(inLine, &f.linePos);
+				parseElement(&f, tmpCoders, NMAX);
+			
+				if (inLine[f.linePos] != ';') {
+					/* Get URL */
+					th_findnext(inLine, &f.linePos);
+					parseElement(&f, tmpURL, NMAX);
+
+					if (inLine[f.linePos] != ';') {
+						/* Get Freeform */
+						th_findnext(inLine, &f.linePos);
+						parseElement(&f, tmpFreeform, NMAX);
+					}
+				}
+			}
+			
 			/* Check if location already exists */
-			tx += offX;
-			ty += offY;
+			tmpX += offX;
+			tmpY += offY;
 			
-			tn = searchLocation(l, tx, ty, TRUE);
+			tn = searchLocation(l, tmpX, tmpY, TRUE);
 			if (tn >= 0) {
 				locinfo_t *tloc = l->locations[tn];
 				
 				THERR("Warning, location already in list!\n"
 				"(%d,%d) '%s' <-> (%d,%d) '%s'\n",
 				tloc->x, tloc->y, tloc->desc,
-				tx, ty, &(inLine[f.linePos])
-				);
+				tmpX, tmpY, tmpDesc);
 				return FALSE;
 			}
 			
 			/* Add new location to our list */
-			addLocation(l, tx, ty, td, &(inLine[f.linePos]), tf, NULL, NULL, NULL);
+			addLocation(l, tmpX, tmpY, tmpDir, tmpFlags,
+				tmpDesc, tmpCoders, tmpURL, tmpFreeform);
 			
 		} else if (inLine[f.linePos] != '#' && inLine[f.linePos] != 0) {
 			THERR("Invalid line #%d in '%s'\n",
@@ -855,33 +903,34 @@
 		if (*d == '?' || *d == '%' || *d == 'C') {
 			/* Check for new locations */
 			if (searchLocation(worldLoc, x, y, TRUE) < 0) {
-				char tmps[512];
-				int tmpf;
+				char tmpDesc[NMAX];
+				int tmpFlags;
 
 				if (*d == 'C') {
 					n = checkForAdjacent(worldLoc, x, y, LOCF_M_PCITY);
 					if (n >= 0) {
 						tmpl = worldLoc->locations[n];
-						tmpf = LOCF_M_PCITY | LOCF_INVIS;
-						snprintf(tmps, sizeof(tmps),
+						tmpFlags = LOCF_M_PCITY | LOCF_INVIS;
+						snprintf(tmpDesc, sizeof(tmpDesc),
 						"%s", tmpl->desc);
 						
 					} else {
 						numNewLoc++;
-						tmpf = LOCF_M_PCITY;
-						snprintf(tmps, sizeof(tmps),
+						tmpFlags = LOCF_M_PCITY;
+						snprintf(tmpDesc, sizeof(tmpDesc),
 						"%.3s-PCITY #%d",
 						srcFile, numNewLoc);
 					}
 				} else {
 					numNewLoc++;
-					tmpf = LOCF_NONE;
-					snprintf(tmps, sizeof(tmps),
+					tmpFlags = LOCF_NONE;
+					snprintf(tmpDesc, sizeof(tmpDesc),
 						"%.3s-UNK #%d",
 						srcFile, numNewLoc);
 				}
 				
-				addLocation(worldLoc, x, y, LOCD_NONE, tmps, tmpf, NULL, NULL, NULL);
+				addLocation(worldLoc, x, y, LOCD_NONE, tmpFlags,
+					tmpDesc, NULL, NULL, NULL);
 			}
 		} else {
 			/* Check for misplaced locations */