void get_default_username(Char *buf, Short max_len) { Char *tmp, *first_wspace; VoidHand h; tmp = md_malloc(sizeof(Char) * (dlkMaxUserNameLength + 1)); DlkGetSyncInfo(NULL, NULL, NULL, tmp, NULL, NULL); /* if it's too long, use the first name only */ // if (StrLen(tmp) > max_len-1) { if (StrLen(tmp) > 8) { first_wspace = StrChr(tmp, spaceChr); if (first_wspace) *(first_wspace) = '\0'; else tmp[max_len-1] = '\0'; } if (StrLen(tmp)) StrNCopy(buf, tmp, max_len); else { Short t = rund(3); switch(t) { case 0: StrPrintF(buf, "Noman"); break; case 1: StrPrintF(buf, "Outis"); break; // "no man" in greek, says the web default: StrPrintF(buf, "Metis"); break; // "no one"/"cunning" pun in greek } } h = MemPtrRecoverHandle(tmp); if (h) MemHandleFree(h); }
/*********************************************************************** * * FUNCTION: SubstituteStr * * DESCRIPTION: This routine substitutes the occurrence a token, within * a string, with another string. * * PARAMETERS: str - string containing token string * token - the string to be replaced * sub - the string to substitute for the token * subLen - length of the substitute string. * * RETURNED: pointer to the string * ***********************************************************************/ static Char* SubstituteStr(Char* str, const Char* token, const Char* sub, UInt16 subLen) { const UInt16 tokenLen = StrLen(token); const UInt16 charsToMove = subLen - tokenLen; const UInt16 strLen = StrLen(str); MemHandle strH = MemPtrRecoverHandle(str); const UInt16 blockSize = MemHandleSize(strH); Char* ptr = StrStr(str, token); ASSERT(str); ASSERT(token); ASSERT(sub); /* Find the start of the token string, if it doesn't exist, exit. */ if (ptr == NULL) return str; /* Resize the string if necessary. */ if (strLen + charsToMove + 1 >= blockSize) { MemHandleUnlock(strH); MemHandleResize(strH, strLen + charsToMove + 1); str = MemHandleLock(strH); ASSERT(str); ptr = StrStr(str, token); ASSERT(ptr); } /* Make room for the substitute string. */ if (charsToMove) MemMove(ptr + subLen, ptr + tokenLen, StrLen (ptr + tokenLen)+1); /* Replace the token with the substitute string. */ MemMove(ptr, sub, subLen); return str; }
void free_me(VoidPtr ptr) { VoidHand h; h = MemPtrRecoverHandle((ptr)); // hm, I could do MemPtrFree instead maybe.. if (h) MemHandleFree(h); }