[前][次][番号順一覧][スレッド一覧]

ruby-changes:43927

From: nobu <ko1@a...>
Date: Wed, 24 Aug 2016 22:04:47 +0900 (JST)
Subject: [ruby-changes:43927] nobu:r56000 (trunk): win32/file.c: use enum

nobu	2016-08-24 22:04:36 +0900 (Wed, 24 Aug 2016)

  New Revision: 56000

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56000

  Log:
    win32/file.c: use enum
    
    * win32/file.c (home_dir): use enum instead of magic numbers.

  Modified files:
    trunk/win32/file.c
Index: win32/file.c
===================================================================
--- win32/file.c	(revision 55999)
+++ win32/file.c	(revision 56000)
@@ -60,7 +60,9 @@ home_dir(void) https://github.com/ruby/ruby/blob/trunk/win32/file.c#L60
 {
     wchar_t *buffer = NULL;
     size_t buffer_len = 0, len = 0;
-    size_t home_env = 0;
+    enum {
+	HOME_NONE, ENV_HOME, ENV_DRIVEPATH, ENV_USERPROFILE
+    } home_type = HOME_NONE;
 
     /*
       GetEnvironmentVariableW when used with NULL will return the required
@@ -70,53 +72,44 @@ home_dir(void) https://github.com/ruby/ruby/blob/trunk/win32/file.c#L72
 
     if ((len = GetEnvironmentVariableW(L"HOME", NULL, 0)) != 0) {
 	buffer_len = len;
-	home_env = 1;
+	home_type = ENV_HOME;
     }
     else if ((len = GetEnvironmentVariableW(L"HOMEDRIVE", NULL, 0)) != 0) {
 	buffer_len = len;
 	if ((len = GetEnvironmentVariableW(L"HOMEPATH", NULL, 0)) != 0) {
 	    buffer_len += len;
-	    home_env = 2;
-	}
-	else {
-	    buffer_len = 0;
+	    home_type = ENV_DRIVEPATH;
 	}
     }
     else if ((len = GetEnvironmentVariableW(L"USERPROFILE", NULL, 0)) != 0) {
 	buffer_len = len;
-	home_env = 3;
+	home_type = ENV_USERPROFILE;
     }
 
+    if (!home_type) return NULL;
+
     /* allocate buffer */
-    if (home_env)
-	buffer = (wchar_t *)xmalloc(buffer_len * sizeof(wchar_t));
+    buffer = (wchar_t *)xmalloc(buffer_len * sizeof(wchar_t));
 
-    switch (home_env) {
-      case 1:
-	/* HOME */
+    switch (home_type) {
+      case ENV_HOME:
 	GetEnvironmentVariableW(L"HOME", buffer, buffer_len);
 	break;
-      case 2:
-	/* HOMEDRIVE + HOMEPATH */
+      case ENV_DRIVEPATH:
 	len = GetEnvironmentVariableW(L"HOMEDRIVE", buffer, buffer_len);
 	GetEnvironmentVariableW(L"HOMEPATH", buffer + len, buffer_len - len);
 	break;
-      case 3:
-	/* USERPROFILE */
+      case ENV_USERPROFILE:
 	GetEnvironmentVariableW(L"USERPROFILE", buffer, buffer_len);
 	break;
       default:
 	break;
     }
 
-    if (home_env) {
-	/* sanitize backslashes with forwardslashes */
-	replace_wchar(buffer, L'\\', L'/');
-
-	return buffer;
-    }
+    /* sanitize backslashes with forwardslashes */
+    replace_wchar(buffer, L'\\', L'/');
 
-    return NULL;
+    return buffer;
 }
 
 /* Remove trailing invalid ':$DATA' of the path. */

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]