name.c

Go to the documentation of this file.
00001 
00025 #include "menustate.h"
00026 //#include "text.h"
00027 #include <string.h>
00028 #include <stdlib.h>
00029 #include <assert.h>
00030 #if defined(USE_GETTEXT) || defined(DOXYGEN)
00031 #include <wchar.h>
00032 #include <libintl.h>
00033 #include <iconv.h>
00034 #endif
00035 
00044 static void SetUsername() {
00045     const char *str = NULL;
00046     // find the user's name from the enviornment settings
00047     if ((str = getenv("USER")) == NULL) {   // for unix
00048         // for windows (may be defined but blank on unix systems)
00049         str = getenv("USERNAME"); 
00050     }
00051     // check for no enviornment setting or zero length name
00052     if ((str == NULL) || (str[0] == 0)) {
00053         // use the default name
00054         str = (char*)strings[STRING_DEFAULTNAME];
00055     }
00056     #ifdef USE_GETTEXT
00057     else {
00058         // a name was found, but it needs to be converted
00059         #ifndef WIN32
00060         iconv_t descr = iconv_open("wchar_t", "");
00061         #else
00062         iconv_t descr = iconv_open("UTF-16LE", "");
00063         #endif
00064         // no error?
00065         if (descr != (iconv_t)-1) {
00066             const char *src = str;
00067             wchar_t *dest = menuSettings->localPlayerName;
00068             size_t srcLen = strlen(str);
00069             size_t destLen = sizeof(menuSettings->localPlayerName) *
00070             sizeof(wchar_t);
00071             iconv(descr, (char**)&src, &srcLen, (char**)&dest, &destLen);
00072             *dest = 0;
00073             iconv_close(descr);
00074             // all done
00075             return;
00076         }
00077     }
00078     #endif
00079     // copy over the name
00080     rtsa_strncpy(menuSettings->localPlayerName, (rtsa_char*)str,
00081     MAX_NAME_LEN - 1);
00082     // assure null termination
00083     menuSettings->localPlayerName[MAX_NAME_LEN - 1] = 0;
00084 }
00085 
00086 void GetLocalPlayerName(rtsa_char *dest) {
00087     assert(menuSettings != NULL);
00088     // see if the name has not already been set
00089     if (menuSettings->localPlayerName[0] == 0) {
00090         // get the user name
00091         SetUsername();
00092     }
00093     // copy the name over -- name length guaranteed within bounds
00094     rtsa_strcpy(dest, menuSettings->localPlayerName);
00095 }
00096 
00097 void SetLocalPlayerName(rtsa_char *name) {
00098     assert(menuSettings != NULL);
00099     // check for request to use the user name from the OS
00100     if ((name == NULL) || (name[0] == 0)) {
00101         // use the user name
00102         SetUsername();
00103     }
00104     else {
00105         // copy the requested name
00106         rtsa_strncpy(menuSettings->localPlayerName, name, MAX_NAME_LEN - 1);
00107         // assure null termination
00108         menuSettings->localPlayerName[MAX_NAME_LEN - 1] = 0;
00109     }
00110 }
00111 

Generated on Mon May 28 04:41:39 2007 for Retro Tank Super Attack by  doxygen 1.5.2