bool MyGetVolumeInformation( CFSTR rootPath, UString &volumeName, LPDWORD volumeSerialNumber, LPDWORD maximumComponentLength, LPDWORD fileSystemFlags, UString &fileSystemName) { BOOL res; #ifndef _UNICODE if (!g_IsNT) { TCHAR v[MAX_PATH + 2]; v[0] = 0; TCHAR f[MAX_PATH + 2]; f[0] = 0; res = GetVolumeInformation(fs2fas(rootPath), v, MAX_PATH, volumeSerialNumber, maximumComponentLength, fileSystemFlags, f, MAX_PATH); volumeName = MultiByteToUnicodeString(v); fileSystemName = MultiByteToUnicodeString(f); } else #endif { WCHAR v[MAX_PATH + 2]; v[0] = 0; WCHAR f[MAX_PATH + 2]; f[0] = 0; res = GetVolumeInformationW(fs2us(rootPath), v, MAX_PATH, volumeSerialNumber, maximumComponentLength, fileSystemFlags, f, MAX_PATH); volumeName = v; fileSystemName = f; } return BOOLToBool(res); }
UString GetPassword(CStdOutStream *outStream,bool verify) { #ifdef USE_FLTK const char *r = fl_password("Enter password", 0); AString oemPassword = ""; if (r) oemPassword = r; return MultiByteToUnicodeString(oemPassword, CP_OEMCP); #else /* USE_FLTK */ if (outStream) { *outStream << "\nEnter password" #ifdef MY_DISABLE_ECHO " (will not be echoed)" #endif ":"; outStream->Flush(); } #ifdef ENV_HAVE_GETPASS AString oemPassword = getpass(""); if ( (verify) && (outStream) ) { (*outStream) << "Verify password (will not be echoed) :"; outStream->Flush(); AString oemPassword2 = getpass(""); if (oemPassword != oemPassword2) throw "password verification failed"; } return MultiByteToUnicodeString(oemPassword, CP_OEMCP); #else return g_StdIn.ScanUStringUntilNewLine(); #endif #endif /* USE_FLTK */ }
UString GetPassword(CStdOutStream *outStream) { (*outStream) << "\nEnter password:"; outStream->Flush(); AString oemPassword = g_StdIn.ScanStringUntilNewLine(); return MultiByteToUnicodeString(oemPassword, CP_OEMCP); }
UString GetPassword(CStdOutStream *outStream,bool verify) { #ifdef USE_FLTK const char *r = fl_password("Enter password", 0); AString oemPassword = ""; if (r) oemPassword = r; #else /* USE_FLTK */ /* #ifdef ENV_HAVE_GETPASS (*outStream) << "\nEnter password (will not be echoed) :"; outStream->Flush(); AString oemPassword = getpass(""); if (verify) { (*outStream) << "Verify password (will not be echoed) :"; outStream->Flush(); AString oemPassword2 = getpass(""); if (oemPassword != oemPassword2) throw "password verification failed"; } #else */ (*outStream) << "\nEnter password:"; outStream->Flush(); AString oemPassword = g_StdIn.ScanStringUntilNewLine(); //#endif #endif /* USE_FLTK */ return MultiByteToUnicodeString(oemPassword, CP_OEMCP); }
bool MyFormatMessage(DWORD messageID, CSysString &message) { const char * txt = 0; AString msg; switch(messageID) { case ERROR_NO_MORE_FILES : txt = "No more files"; break ; case E_NOTIMPL : txt = "E_NOTIMPL"; break ; case E_NOINTERFACE : txt = "E_NOINTERFACE"; break ; case E_ABORT : txt = "E_ABORT"; break ; case E_FAIL : txt = "E_FAIL"; break ; case STG_E_INVALIDFUNCTION : txt = "STG_E_INVALIDFUNCTION"; break ; case E_OUTOFMEMORY : txt = "E_OUTOFMEMORY"; break ; case E_INVALIDARG : txt = "E_INVALIDARG"; break ; default: txt = strerror(messageID); } if (txt) { msg = txt; } else { char msgBuf[256]; snprintf(msgBuf,sizeof(msgBuf),"error #%x",(unsigned)messageID); msgBuf[sizeof(msgBuf)-1] = 0; msg = msgBuf; } msg += " "; #ifdef _UNICODE message = MultiByteToUnicodeString(msg); #else message = msg; #endif return true; }
void test_mbs(void) { wchar_t wstr1[256] = { L'e', 0xE8, // latin small letter e with grave 0xE9, // latin small letter e with acute L'a', 0xE0, // latin small letter a with grave 0x20AC, // euro sign L'b', 0 }; wchar_t wstr2[256]; char astr[256]; extern int global_use_utf16_conversion; global_use_utf16_conversion = 1; size_t len1 = wcslen(wstr1); printf("wstr1 - %d - '%ls'\n",(int)len1,wstr1); size_t len0 = wcstombs(astr,wstr1,sizeof(astr)); printf("astr - %d - '%s'\n",(int)len0,astr); size_t len2 = mbstowcs(wstr2,astr,sizeof(wstr2)/sizeof(*wstr2)); printf("wstr - %d - '%ls'\n",(int)len2,wstr2); if (wcscmp(wstr1,wstr2) != 0) { printf("ERROR during conversions wcs -> mbs -> wcs\n"); exit(EXIT_FAILURE); } char *ptr = astr; size_t len = 0; while (*ptr) { ptr = CharNextA(ptr); len += 1; } if (len != len1) { printf("ERROR CharNextA : len=%d, len1=%d\n",(int)len,(int)len1); exit(EXIT_FAILURE); } UString ustr(wstr1); assert(ustr.Length() == (int)len1); AString ansistr(astr); assert(ansistr.Length() == (int)len0); ansistr = UnicodeStringToMultiByte(ustr); assert(ansistr.Length() == (int)len0); assert(strcmp(ansistr,astr) == 0); assert(wcscmp(ustr,wstr1) == 0); UString ustr2 = MultiByteToUnicodeString(astr); assert(ustr2.Length() == (int)len1); assert(wcscmp(ustr2,wstr1) == 0); }
bool GetProgramFolderPath(UString &folder) { const char *p7zip_home_dir = getenv("P7ZIP_HOME_DIR"); if (p7zip_home_dir == 0) p7zip_home_dir="./"; folder = MultiByteToUnicodeString(p7zip_home_dir); return true; }
void mySplitCommandLine(int numArguments,const char *arguments[],UStringVector &parts) { { // define P7ZIP_HOME_DIR static char p7zip_home_dir[MAX_PATH]; AString dir,name; my_windows_split_path(arguments[0],dir,name); snprintf(p7zip_home_dir,sizeof(p7zip_home_dir),"P7ZIP_HOME_DIR=%s/",(const char *)dir); p7zip_home_dir[sizeof(p7zip_home_dir)-1] = 0; putenv(p7zip_home_dir); } #ifdef ENV_HAVE_LOCALE // set the program's current locale from the user's environment variables setlocale(LC_ALL,""); // auto-detect which conversion p7zip should use char *locale = setlocale(LC_CTYPE,0); if (locale) { size_t len = strlen(locale); char *locale_upper = (char *)malloc(len+1); if (locale_upper) { strcpy(locale_upper,locale); for(size_t i=0;i<len;i++) locale_upper[i] = toupper(locale_upper[i] & 255); if ( (strcmp(locale_upper,"") != 0) && (strcmp(locale_upper,"C") != 0) && (strcmp(locale_upper,"POSIX") != 0) ) { global_use_utf16_conversion = 1; } free(locale_upper); } } #elif defined(LOCALE_IS_UTF8) global_use_utf16_conversion = 1; // assume LC_CTYPE="utf8" #else global_use_utf16_conversion = 0; // assume LC_CTYPE="C" #endif parts.Clear(); for(int ind=0;ind < numArguments; ind++) { if ((ind <= 2) && (strcmp(arguments[ind],"-no-utf16") == 0)) { global_use_utf16_conversion = 0; } else if ((ind <= 2) && (strcmp(arguments[ind],"-utf16") == 0)) { global_use_utf16_conversion = 1; } else { UString tmp = MultiByteToUnicodeString(arguments[ind]); // tmp.Trim(); " " is a valid filename ... if (!tmp.IsEmpty()) { parts.Add(tmp); } } } }
FString GetModuleDirPrefix() { FString s; const char *p7zip_home_dir = getenv("P7ZIP_HOME_DIR"); if (p7zip_home_dir) { return MultiByteToUnicodeString(p7zip_home_dir,CP_ACP); } return FTEXT(".") FSTRING_PATH_SEPARATOR; }
wchar_t * MyStringLower(wchar_t *s) { if (s == 0) return 0; wchar_t *res = CharLowerW(s); if (res != 0 || ::GetLastError() != ERROR_CALL_NOT_IMPLEMENTED) return res; AString a = UnicodeStringToMultiByte(s); a.MakeLower(); return MyStringCopy(s, (const wchar_t *)MultiByteToUnicodeString(a)); }
UString CStdInStream::ScanUStringUntilNewLine() { AString s = ScanStringUntilNewLine(true); int codePage = g_CodePage; if (codePage == -1) codePage = CP_OEMCP; UString dest; if (codePage == CP_UTF8) ConvertUTF8ToUnicode(s, dest); else dest = MultiByteToUnicodeString(s, (UINT)codePage); return dest; }
bool CFindFile::FindFirst(CFSTR cfWildcard, CFileInfo &fi, bool ignoreLink) { if (!Close()) return false; AString Awildcard = UnicodeStringToMultiByte(cfWildcard, CP_ACP); const char * wildcard = (const char *)Awildcard; if ((!wildcard) || (wildcard[0]==0)) { SetLastError(ERROR_PATH_NOT_FOUND); return false; } my_windows_split_path(nameWindowToUnix(wildcard),_directory,_pattern); TRACEN((printf("CFindFile::FindFirst : %s (dirname=%s,pattern=%s)\n",wildcard,(const char *)_directory,(const char *)_pattern))) _dirp = ::opendir((const char *)_directory); TRACEN((printf("CFindFile::FindFirst : opendir=%p\n",_dirp))) if ((_dirp == 0) && (global_use_utf16_conversion)) { // Try to recover the original filename UString ustr = MultiByteToUnicodeString(_directory, 0); AString resultString; bool is_good = originalFilename(ustr, resultString); if (is_good) { _dirp = ::opendir((const char *)resultString); _directory = resultString; } } if (_dirp == 0) return false; struct dirent *dp; while ((dp = readdir(_dirp)) != NULL) { if (filter_pattern(dp->d_name,(const char *)_pattern,0) == 1) { int retf = fillin_CFileInfo(fi,(const char *)_directory,dp->d_name,ignoreLink); if (retf) { TRACEN((printf("CFindFile::FindFirst : closedir-1(dirp=%p)\n",_dirp))) closedir(_dirp); _dirp = 0; SetLastError( ERROR_NO_MORE_FILES ); return false; } TRACEN((printf("CFindFile::FindFirst -%s- true\n",dp->d_name))) return true; } }
bool MyGetModuleFileName(HMODULE hModule, UString &result) { result.Empty(); if (g_IsNT) { wchar_t fullPath[MAX_PATH + 2]; DWORD size = ::GetModuleFileNameW(hModule, fullPath, MAX_PATH + 1); if (size <= MAX_PATH && size != 0) { result = fullPath; return true; } return false; } CSysString resultSys; if (!MyGetModuleFileName(hModule, resultSys)) return false; result = MultiByteToUnicodeString(resultSys, GetCurrentCodePage()); return true; }
int MyStringCompareNoCase(const char *s1, const char *s2) { return MyStringCompareNoCase(MultiByteToUnicodeString(s1), MultiByteToUnicodeString(s2)); }
bool CFileBase::Create(LPCTSTR filename, DWORD dwDesiredAccess, DWORD dwShareMode, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,bool ignoreSymbolicLink) { Close(); const char * name = nameWindowToUnix(filename); #ifdef O_BINARY int flags = O_BINARY; #else int flags = 0; #endif #ifdef O_LARGEFILE flags |= O_LARGEFILE; #endif int mode = 0666; if (dwDesiredAccess & GENERIC_WRITE) flags |= O_WRONLY; if (dwDesiredAccess & GENERIC_READ) flags |= O_RDONLY; switch (dwCreationDisposition) { case CREATE_NEW : flags |= O_CREAT | O_EXCL; break; case CREATE_ALWAYS : flags |= O_CREAT; break; case OPEN_EXISTING : break; case OPEN_ALWAYS : flags |= O_CREAT; break; // case TRUNCATE_EXISTING : flags |= O_TRUNC; break; } /* size_t len = strlen(name); if (len >= 4) { if ( (tolower((unsigned char)name[len-1]) == 'e') && (tolower((unsigned char)name[len-2]) == 'x') && (tolower((unsigned char)name[len-3]) == 'e') && (name[len-4] == '.') ) { mode = 0777; } } */ _fd = -1; #ifdef HAVE_LSTAT if ((global_use_lstat) && (ignoreSymbolicLink == false)) { _size = readlink(name, _buffer, sizeof(_buffer)-1); if (_size > 0) { if (dwDesiredAccess & GENERIC_READ) { _fd = FD_LINK; _offset = 0; _buffer[_size]=0; } else if (dwDesiredAccess & GENERIC_WRITE) { // does not overwrite the file pointed by symbolic link if (!unlink(name)) return false; } } } #endif if (_fd == -1) { _fd = open(name,flags, mode); } if ((_fd == -1) && (global_use_utf16_conversion)) { // bug #1204993 - Try to recover the original filename UString ustr = MultiByteToUnicodeString(AString(name), 0); AString resultString; int is_good = 1; for (int i = 0; i < ustr.Length(); i++) { if (ustr[i] >= 256) { is_good = 0; break; } else { resultString += char(ustr[i]); } } if (is_good) { _fd = open((const char *)resultString,flags, mode); } } if (_fd == -1) { /* !HAVE_LSTAT : an invalid symbolic link => errno == ENOENT */ return false; } else { _unix_filename = name; } return true; }
HRESULT StringToGUIDA(const char *string, GUID &classID) { return StringToGUIDW(MultiByteToUnicodeString(string), classID); }
FString fas2fs(const AString &s) { return MultiByteToUnicodeString(s, GetCurrentCodePage()); }
UString MultiByteToUnicodeString(const char *src, UINT codePage) { return MultiByteToUnicodeString(AString(src), codePage); }
UString fs2us(const FString &s) { return MultiByteToUnicodeString((AString)s, GetCurrentCodePage()); }
UString CStdInStream::ScanUStringUntilNewLine() { AString s = ScanStringUntilNewLine(true); UString dest = MultiByteToUnicodeString(s, (UINT)-1); return dest; }