Diff of /joe/gettext.c [75731f] .. [523079] Maximize Restore

  Switch to side-by-side view

--- a/joe/gettext.c
+++ b/joe/gettext.c
@@ -35,20 +35,15 @@
 		return s;
 }
 
-/* Load a .po file, convert entries to local character set and add them to
- * hash table */
-
 int load_po(FILE *f)
 {
-	unsigned char buf[1024];
-	unsigned char msgid[1024];
-	unsigned char msgstr[1024];
-	unsigned char bf[8192];
+	unsigned char *buf = 0;
+	unsigned char *msgid = vsdupz(USTR "");
+	unsigned char *msgstr = vsdupz(USTR "");
 	struct charmap *po_map = locale_map;
 	int preload_flag = 0;
-	msgid[0] = 0;
-	msgstr[0] = 0;
-	while (preload_flag || fgets((char *)buf,sizeof(buf)-1,f)) {
+
+	while (preload_flag || vsgets(isfree(&buf) ,f)) {
 		unsigned char *p;
 		preload_flag = 0;
 		p = buf;
@@ -56,14 +51,16 @@
 		if (!parse_field(&p, USTR "msgid")) {
 			int ofst = 0;
 			int len;
-			msgid[0] = 0;
+			unsigned char *bf = 0;
+			msgid = vscpyz(msgid, USTR "");
 			parse_ws(&p, '#');
-			while ((len = parse_string(&p, msgid + ofst, sizeof(msgid)-ofst)) >= 0) {
+			while ((len = parse_string(&p, &bf)) >= 0) {
+				msgid = vscat(msgid, sv(bf));
 				preload_flag = 0;
 				ofst += len;
 				parse_ws(&p, '#');
 				if (!*p) {
-					if (fgets((char *)buf,sizeof(buf) - 1,f)) {
+					if (vsgets(isfree(&buf),f)) {
 						p = buf;
 						preload_flag = 1;
 						parse_ws(&p, '#');
@@ -75,14 +72,16 @@
 		} else if (!parse_field(&p, USTR "msgstr")) {
 			int ofst = 0;
 			int len;
-			msgstr[0] = 0;
+			unsigned char *bf = 0;
+			msgstr = vscpyz(msgstr, USTR "");
 			parse_ws(&p, '#');
-			while ((len = parse_string(&p, msgstr + ofst, sizeof(msgstr)-ofst)) >= 0) {
+			while ((len = parse_string(&p, &bf)) >= 0) {
+				msgstr = vscat(msgstr, sv(bf));
 				preload_flag = 0;
 				ofst += len;
 				parse_ws(&p, '#');
 				if (!*p) {
-					if (fgets((char *)buf,sizeof(buf) - 1,f)) {
+					if (vsgets(isfree(&buf),f)) {
 						p = buf;
 						preload_flag = 1;
 						parse_ws(&p, '#');
@@ -93,11 +92,13 @@
 			}
 			if (msgid[0] && msgstr[0]) {
 				/* Convert to locale character map */
-				my_iconv(bf,locale_map,msgstr,po_map);
+				unsigned char *bf = my_iconv(NULL,locale_map,msgstr,po_map);
 				/* Add to hash table */
 				htadd(gettext_ht, zdup(msgid), zdup(bf));
 			} else if (!msgid[0] && msgstr[0]) {
 				unsigned char *p = (unsigned char *)strstr((char *)msgstr, "charset=");
+				msgid = vscpyz(msgid, msgstr); /* Make sure msgid is long enough */
+				msgid = vscpyz(msgid, USTR ""); /* Truncate it */
 				if (p) {
 					/* Copy character set name up to next delimiter */
 					int x;
@@ -116,6 +117,7 @@
 	}
 	bye:
 	fclose(f);
+	obj_free(msgid);
 	return 0;
 }
 
@@ -124,18 +126,18 @@
 void init_gettext(unsigned char *s)
 {
 	FILE *f;
-	unsigned char buf[1024];
-	joe_snprintf_2(buf, sizeof(buf), "%slang/%s.po",JOEDATA,s);
+	unsigned char *buf = vsfmt(NULL,0, USTR "%slang/%s.po",JOEDATA,s);
 	if ((f = fopen((char *)buf, "r"))) {
 		/* Try specific language, like en_GB */
 		gettext_ht = htmk(256);
 		load_po(f);
 	} else if (s[0] && s[1]) {
 		/* Try generic language, like en */
-		joe_snprintf_3(buf, sizeof(buf), "%slang/%c%c.po",JOEDATA,s[0],s[1]);
-		if ((f = fopen((char *)buf, "r"))) {
+		unsigned char *bf = vsfmt(NULL,0,USTR "%slang/%c%c.po",JOEDATA,s[0],s[1]);
+		if ((f = fopen((char *)bf, "r"))) {
 			gettext_ht = htmk(256);
 			load_po(f);
 		}
 	}
+	obj_free(buf);
 }