r445 - trunk

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Sun Jan 20 05:03:18 EST 2008


Author: icculus
Date: 2008-01-20 05:03:16 -0500 (Sun, 20 Jan 2008)
New Revision: 445

Modified:
   trunk/lua_glue.c
   trunk/platform.h
   trunk/platform_unix.c
   trunk/platform_windows.c
Log:
Another attempt to stop being a game developer; some more MojoPlatform_*
 functions now returned a malloc()'d string that the caller must free(),
 instead of counting on the caller to pass in a (maybe!) large enough buffer.


Modified: trunk/lua_glue.c
===================================================================
--- trunk/lua_glue.c	2008-01-20 09:04:50 UTC (rev 444)
+++ trunk/lua_glue.c	2008-01-20 10:03:16 UTC (rev 445)
@@ -1485,20 +1485,21 @@
     const char *envr = cmdlinestr("locale", "MOJOSETUP_LOCALE", NULL);
     char *homedir = NULL;
     char *binarypath = NULL;
-    char locale[16];
-    char ostype[64];
-    char osversion[64];
+    char *locale = NULL
+    char *ostype = NULL;
+    char *osversion = NULL;
 
     if (envr != NULL)
-        xstrncpy(locale, envr, sizeof (locale));
-    else if (!MojoPlatform_locale(locale, sizeof (locale)))
-        xstrncpy(locale, "???", sizeof (locale));
+        locale = xstrdup(envr);
+    else if ((locale = MojoPlatform_locale()) == NULL)
+        locale = xstrdup("???");
 
-    if (!MojoPlatform_osType(ostype, sizeof (ostype)))
-        xstrncpy(ostype, "???", sizeof (ostype));
-    if (!MojoPlatform_osVersion(osversion, sizeof (osversion)))
-        xstrncpy(osversion, "???", sizeof (osversion));
+    if ((ostype = MojoPlatform_osType()) == NULL)
+        ostype = xstrdup("???");
 
+    if ((osversion = MojoPlatform_osVersion()) == NULL)
+        osversion = xstrdup("???");
+
     assert(luaState == NULL);
 
     luaState = lua_newstate(MojoLua_alloc, NULL);
@@ -1622,6 +1623,9 @@
 
     free(binarypath);
     free(homedir);
+    free(osversion);
+    free(ostype);
+    free(locale);
 
     // Transfer control to Lua to setup some APIs and state...
     if (!MojoLua_runFile("mojosetup_init"))

Modified: trunk/platform.h
===================================================================
--- trunk/platform.h	2008-01-20 09:04:50 UTC (rev 444)
+++ trunk/platform.h	2008-01-20 10:03:16 UTC (rev 445)
@@ -262,12 +262,19 @@
 // Get the current locale, in the format "xx_YY" where "xx" is the language
 //  (en, fr, de...) and "_YY" is the country. (_US, _CA, etc). The country
 //  can be omitted. Don't include encoding, it's always UTF-8 at this time.
-// Return true if locale is known, false otherwise.
-boolean MojoPlatform_locale(char *buf, size_t len);
+// Returns locale string, or NULL if it couldn't be determined.
+//  Caller must free() the returned pointer!
+char *MojoPlatform_locale(void);
 
-boolean MojoPlatform_osType(char *buf, size_t len);
-boolean MojoPlatform_osVersion(char *buf, size_t len);
+// !!! FIXME: document me.
+// Caller must free() the returned pointer!
+char *MojoPlatform_osType(void);
 
+// !!! FIXME: document me.
+// Caller must free() the returned pointer!
+char *MojoPlatform_osVersion(void);
+
+
 // Basic platform detection.
 #if PLATFORM_WINDOWS
 #define PLATFORM_NAME "windows"

Modified: trunk/platform_unix.c
===================================================================
--- trunk/platform_unix.c	2008-01-20 09:04:50 UTC (rev 444)
+++ trunk/platform_unix.c	2008-01-20 10:03:16 UTC (rev 445)
@@ -367,23 +367,21 @@
 
 
 // This implementation is a bit naive.
-boolean MojoPlatform_locale(char *buf, size_t len)
+char *MojoPlatform_locale(void)
 {
-    boolean retval = false;
+    char *retval = NULL;
     const char *envr = getenv("LANG");
     if (envr != NULL)
     {
-        char *ptr = NULL;
-        xstrncpy(buf, envr, len);
-        ptr = strchr(buf, '.');  // chop off encoding if explicitly listed.
+        retval = xstrdup(envr);
+        ptr = strchr(retval, '.');  // chop off encoding if explicitly listed.
         if (ptr != NULL)
             *ptr = '\0';
-        retval = true;
     } // if
 
     #if PLATFORM_MACOSX
     else if (CFLocaleCreateCanonicalLocaleIdentifierFromString == NULL)
-        retval = false; // !!! FIXME: 10.2 compatibility?
+        retval = NULL; // !!! FIXME: 10.2 compatibility?
 
     else if (CFLocaleCreateCanonicalLocaleIdentifierFromString != NULL)
     {
@@ -400,9 +398,11 @@
                                                 kCFAllocatorDefault, primary);                if (locale != NULL)
                 if (locale != NULL)
                 {
-                    CFStringGetCString(locale, buf, len, kCFStringEncodingUTF8);
+                    const CFIndex len = (CFStringGetLength(locale) + 1) * 6;
+                    char *ptr = (char*) xmalloc(len);
+                    CFStringGetCString(locale, ptr, len, kCFStringEncodingUTF8);
                     CFRelease(locale);
-                    retval = true;
+                    retval = xrealloc(ptr, strlen(ptr) + 1);
                 } // if
             } // if
             CFRelease(languages);
@@ -414,37 +414,37 @@
 } // MojoPlatform_locale
 
 
-boolean MojoPlatform_osType(char *buf, size_t len)
+char *MojoPlatform_osType(void)
 {
 #if PLATFORM_MACOSX
-    xstrncpy(buf, "macosx", len);
+    return xstrdup("macosx");
 #elif PLATFORM_BEOS
-    xstrncpy(buf, "beos", len);   // !!! FIXME: zeta? haiku?
+    return xstrdup("beos");   // !!! FIXME: zeta? haiku?
 #elif defined(linux) || defined(__linux) || defined(__linux__)
-    xstrncpy(buf, "linux", len);
+    return xstrdup("linux");
 #elif defined(__FreeBSD__) || defined(__DragonFly__)
-    xstrncpy(buf, "freebsd", len);
+    return xstrdup("freebsd");
 #elif defined(__NetBSD__)
-    xstrncpy(buf, "netbsd", len);
+    return xstrdup("netbsd");
 #elif defined(__OpenBSD__)
-    xstrncpy(buf, "openbsd", len);
+    return xstrdup("openbsd");
 #elif defined(bsdi) || defined(__bsdi) || defined(__bsdi__)
-    xstrncpy(buf, "bsdi", len);
+    return xstrdup("bsdi");
 #elif defined(_AIX)
-    xstrncpy(buf, "aix", len);
+    return xstrdup("aix");
 #elif defined(hpux) || defined(__hpux) || defined(__hpux__)
-    xstrncpy(buf, "hpux", len);
+    return xstrdup("hpux");
 #elif defined(sgi) || defined(__sgi) || defined(__sgi__) || defined(_SGI_SOURCE)
-    xstrncpy(buf, "irix", len);
+    return xstrdup("irix");
 #else
 #   error Please define your platform.
 #endif
 
-    return true;
+    return NULL;
 } // MojoPlatform_ostype
 
 
-boolean MojoPlatform_osVersion(char *buf, size_t len)
+char *MojoPlatform_osVersion()
 {
 #if PLATFORM_MACOSX
     // !!! FIXME: this is wrong...it doesn't with with 10.y.xx, where 'xx'
@@ -454,10 +454,10 @@
     long ver = 0x0000;
 	if (Gestalt(gestaltSystemVersion, &ver) == noErr)
     {
-        char str[16];
+        char *retval = (char *) xmalloc(16);
         snprintf(str, sizeof (str), "%X", (int) ver);
         snprintf(buf, len, "%c%c.%c.%c", str[0], str[1], str[2], str[3]);
-        return true;
+        return retval;
     } // if
 #else
     // This information may or may not actually MEAN anything. On BeOS, it's
@@ -465,13 +465,10 @@
     //  version, which doesn't necessarily help.
     struct utsname un;
     if (uname(&un) == 0)
-    {
-        xstrncpy(buf, un.release, len);
-        return true;
-    } // if
+        return xstrdup(un.release);
 #endif
 
-    return false;
+    return NULL;
 } // MojoPlatform_osversion
 
 

Modified: trunk/platform_windows.c
===================================================================
--- trunk/platform_windows.c	2008-01-20 09:04:50 UTC (rev 444)
+++ trunk/platform_windows.c	2008-01-20 10:03:16 UTC (rev 445)
@@ -924,9 +924,8 @@
 
 
 // This implementation is a bit naive.
-boolean MojoPlatform_locale(char *buf, size_t len)
+char *MojoPlatform_locale(void)
 {
-    boolean retval = false;
     char lang[16];
     char country[16];
 
@@ -940,33 +939,29 @@
 
     // Win95 systems will fail, because they don't have LOCALE_SISO*NAME ...
     if ((langrc != 0) && (ctryrc != 0))
-    {
-        snprintf(buf, len, "%s_%s", lang, country);
-        retval = true;
-    } // if
+        return format("%0_%1", lang, country);
 
-    return retval;
+    return NULL;
 } // MojoPlatform_locale
 
 
-boolean MojoPlatform_osType(char *buf, size_t len)
+char *MojoPlatform_osType(void)
 {
     if (osIsWin9x)
-        xstrncpy(buf, "win9x", len);
+        return xstrdup("win9x");
     else
-        xstrncpy(buf, "winnt", len);
+        return xstrdup("winnt");
 
-    return true;
+    return NULL;
 } // MojoPlatform_ostype
 
 
-boolean MojoPlatform_osVersion(char *buf, size_t len)
+char *MojoPlatform_osVersion(void)
 {
-    snprintf(buf, len, "%u.%u.%u",
-             (unsigned int) osMajorVer,
-             (unsigned int) osMinorVer,
-             (unsigned int) osBuildVer);
-    return true;
+    return format("%0.%1.%2",
+                  numStr(osMajorVer),
+                  numStr(osMinorVer),
+                  numStr(osBuildVer));
 } // MojoPlatform_osversion
 
 




More information about the mojosetup-commits mailing list