[PATCH] language environment & -encoding option

Takashi Okamoto kaffe@rufus.w3.org
Fri, 20 Jul 2001 07:40:43 +0900


--Multipart_Fri_Jul_20_07:40:43_2001-1
Content-Type: text/plain; charset=US-ASCII

Hi,kaffe.

I want to add the function which is supporting language environment.
It means kaffe investigate LANG environment and set user.language,
user.region,file.encoding properties. 
for example.

# set LANG=ja_JP.eucJP
# kaffe HelloWorld

Then, these properties are

	user.language: ja
	user.region: JP
	file.encoding: eucJP

It makes I18Nd application distinguish current language environment.

I also add -encoding option to this patch for character encoding. 


BTW, I asked kjc developers to add -encoding option. The current kjc 
     (in CVS) is implemented it. I prefer it rather than kjc-1.5A:)


regards.
----
Takashi Okamoto



--Multipart_Fri_Jul_20_07:40:43_2001-1
Content-Type: application/octet-stream; type=patch
Content-Disposition: attachment; filename="main.c.patch"
Content-Transfer-Encoding: 7bit

--- main.c.orig	Fri Jul 20 06:59:38 2001
+++ main.c	Fri Jul 20 06:58:48 2001
@@ -51,6 +51,8 @@
 
 static int options(char**);
 static void usage(void);
+static void setlang(void);
+static void addProperty(userProperty**, char*, char*);
 static size_t parseSize(char*);
 static int checkException(void);
 static int main2(JNIEnv* env, char *argv[], int farg, int argc);
@@ -112,6 +114,7 @@
         vmargs.classhome = cp;
 
 	/* Process program options */
+        setlang();
 	farg = options(argv);
 	argc = argc - farg;
 
@@ -277,7 +280,6 @@
 {
 	int i;
 	int sz;
-	userProperty* prop;
 
 	for (i = 1; argv[i] != 0; i++) {
 		if (argv[i][0] != '-') {
@@ -412,6 +414,17 @@
                 else if (strcmp(argv[i], "-jar") == 0) {
                         isJar = 1;
                 }
+                else if (strcmp(argv[i], "-encoding") == 0) {
+     		  i++;
+		  if (argv[i] == 0) {
+		    dprintf(
+			     "Error: -encoding option requires "
+			     "a encoding name.\n");
+
+		  }
+		  addProperty(&userProperties, "file.encoding", argv[i]);
+                }
+
 #if defined(KAFFE_PROFILER)
 		else if (strcmp(argv[i], "-prof") == 0) {
 			profFlag = 1;
@@ -513,10 +526,6 @@
 #endif
 		else if (argv[i][1] ==  'D') {
 			/* Set a property */
-			prop = malloc(sizeof(userProperty));
-			assert(prop != 0);
-			prop->next = userProperties;
-			userProperties = prop;
 			for (sz = 2; argv[i][sz] != 0; sz++) {
 				if (argv[i][sz] == '=') {
 					argv[i][sz] = 0;
@@ -524,8 +533,7 @@
 					break;
 				}
 			}
-			prop->key = &argv[i][2];
-			prop->value = &argv[i][sz];
+		        addProperty(&userProperties, &argv[i][2], &argv[i][sz]);
 		}
 		else if (argv[i][1] == 'X') {
 			dprintf(
@@ -629,4 +637,85 @@
 	}
 
 	return (sz);
+}
+
+/*
+ * set language environment 
+ */
+static
+void
+setlang()
+{
+  char *locale = NULL, *lang =NULL, *region = NULL, *buf;
+
+  locale = getenv("LANG");
+  if(locale == NULL) return;
+  if(strlen(locale)==1 && locale[0]=='C')return;
+
+  /* set user.language properties */
+
+  lang = strchr(locale,'_');
+  if(lang != NULL){
+    buf = (char *)malloc(sizeof(char)*(lang-locale+1));
+    strncpy(buf,locale,lang-locale);
+    buf[lang-locale] = '\0';
+    addProperty(&userProperties, "user.language", buf);
+  } else {
+    buf = locale; 
+    addProperty(&userProperties, "user.language", buf);
+    return ;
+  }
+
+  /* set user.region properties */
+
+  region = strchr(lang,'.');
+
+  if(region != NULL){
+    buf = (char *)malloc(sizeof(char)*(region-lang));
+    strncpy(buf,lang+1,region-lang-1);
+    buf[region-lang-1] = '\0';
+    addProperty(&userProperties, "user.region", buf);
+  } else {
+    addProperty(&userProperties, "user.region", lang + 1);
+    return;
+  }
+
+  /* set file.encoding properties */
+
+  addProperty(&userProperties, "file.encoding", region + 1);
+
+#if defined(DEBUG)
+  printf("%s:%s\n",prop->key,prop->value);
+#endif
+}
+
+
+/*
+ * set property.
+ * override property if it is exsiting.
+ */
+static
+void
+addProperty(userProperty **prop, char *key, char *value){
+  userProperty *p = *prop;
+  while(p != NULL){
+    if(strlen(p->key)==strlen(key) &&
+       strstr(p->key, key)!=NULL){
+#if defined(DEBUG)
+	 printf("replaceProperty:%s=%s\n",p->key,p->value);
+#endif
+	 p->value = value;
+       }
+    p = p->next;
+  }
+
+  p = malloc(sizeof(userProperty));
+  assert(p != 0);
+  p->next = *prop;
+  p->key = key;
+  p->value = value;
+  *prop = p;
+#if defined(DEBUG)
+  printf("addProperty:%s=%s\n",p->key,p->value);
+#endif
 }

--Multipart_Fri_Jul_20_07:40:43_2001-1--