Skip to content

Commit

Permalink
Another attempt to stop being a game developer; some more MojoPlatform_*
Browse files Browse the repository at this point in the history
 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.
  • Loading branch information
icculus committed Jan 20, 2008
1 parent 3b36520 commit 06d8799
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 60 deletions.
24 changes: 14 additions & 10 deletions lua_glue.c
Expand Up @@ -1485,19 +1485,20 @@ boolean MojoLua_initLua(void)
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);

Expand Down Expand Up @@ -1622,6 +1623,9 @@ boolean MojoLua_initLua(void)

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"))
Expand Down
15 changes: 11 additions & 4 deletions platform.h
Expand Up @@ -262,11 +262,18 @@ uint8 *MojoPlatform_decodeImage(const uint8 *data, uint32 size,
// 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);

// !!! 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);

boolean MojoPlatform_osType(char *buf, size_t len);
boolean MojoPlatform_osVersion(char *buf, size_t len);

// Basic platform detection.
#if PLATFORM_WINDOWS
Expand Down
55 changes: 26 additions & 29 deletions platform_unix.c
Expand Up @@ -367,23 +367,21 @@ char *MojoPlatform_homedir(void)


// 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)
{
Expand All @@ -400,9 +398,11 @@ boolean MojoPlatform_locale(char *buf, size_t len)
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);
Expand All @@ -414,37 +414,37 @@ boolean MojoPlatform_locale(char *buf, size_t len)
} // 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'
Expand All @@ -454,24 +454,21 @@ boolean MojoPlatform_osVersion(char *buf, size_t len)
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
// useful, but on other things, like Linux, it'll give you the kernel
// 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


Expand Down
29 changes: 12 additions & 17 deletions platform_windows.c
Expand Up @@ -924,9 +924,8 @@ char *MojoPlatform_homedir(void)


// 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];

Expand All @@ -940,33 +939,29 @@ boolean MojoPlatform_locale(char *buf, size_t len)

// 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


Expand Down

0 comments on commit 06d8799

Please sign in to comment.