void ConvertUncode( const string &sString, wstring &wsString )
{
	int nSize = MultiByteToWideChar( CP_ACP, 0, sString.c_str(), -1, NULL, 0 );
	nSize --;
	wsString.resize( nSize );
	MultiByteToWideChar( CP_ACP, 0, sString.c_str(), -1, &wsString[0], nSize );
}
Exemple #2
0
void ErasePunctuation(wstring& str, bool keep_trailing) {
  auto rlast = str.rbegin();

  if (keep_trailing)
    rlast = std::find_if(str.rbegin(), str.rend(),
        [](wchar_t c) -> bool {
          return !(c == L'!' || // "Hayate no Gotoku!", "K-ON!"...
                   c == L'+' || // "Needless+"
                   c == L'\''); // "Gintama'"
        });

  auto it = std::remove_if(str.begin(), rlast.base(),
      [](int c) -> bool {
        // Control codes, white-space and punctuation characters
        if (c <= 255 && !isalnum(c))
          return true;
        // Unicode stars, hearts, notes, etc. (0x2000-0x2767)
        if (c > 8192 && c < 10087)
          return true;
        // Valid character
        return false;
      });

  if (keep_trailing)
    std::copy(rlast.base(), str.end(), it);

  str.resize(str.size() - (rlast.base() - it));
}
Exemple #3
0
wstring GetFileWithoutExtension(wstring str) {
  size_t pos = str.find_last_of(L".");

  if (pos != wstring::npos)
    str.resize(pos);

  return str;
}
Exemple #4
0
	void BinReader::_serialize( wstring& nValue, const wchar_t * nName, const wchar_t * nOptimal )
	{
		__u16 count_ = 0;
		__i8 value_ = 0;
		mStream.read((char *)(&count_), sizeof(__u16));
		nValue.resize(count_);
		mStream.read((char *)(&nValue[0]), sizeof(wchar_t) * count_);
	}
void
GUID2String( const GUID &guid, wstring &str )
{
	str.resize( 36 );

	swprintf_s( (wchar_t*)str.data(), 37, _T("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X"),
				guid.Data1, guid.Data2, guid.Data3,
				guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
				guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7] );
}
bool IO_CharDecoder::ReadUNICODE(IO_InputDataStream* stream, wstring& text)
{
	if(stream == NULL)
	{
		return false;
	}

	size_t length = 0;

	text.resize(IO_CHAR_BUFFER_SIZE);

	while(true)
	{
		int ch1 = stream->ReadByte() & 0xff;
		int ch2 = stream->ReadByte() & 0xff;
		int ch = ch1 | (ch2 << 8);

		if(length >= text.max_size())
		{
			text.resize(text.max_size() * 2);
		}

		if(ch == 0)
		{
			break;
		}

		if(ch == '\r')
		{
			stream->Skip(2);
		}

		if(ch == '\n')
		{
			break;
		}

		text[length++] = (wchar_t)ch;
	}

	return true;
}
Exemple #7
0
void EraseRight(wstring& str1, const wstring& str2, bool case_insensitive) {
  if (str1.length() < str2.length())
    return;

  if (case_insensitive) {
    if (!std::equal(str2.begin(), str2.end(), str1.end() - str2.length(),
                    &IsCharsEqual))
      return;
  } else {
    if (!std::equal(str2.begin(), str2.end(), str1.end() - str2.length()))
      return;
  }

  str1.resize(str1.length() - str2.length());
}
Exemple #8
0
// determine the directory for a given pathname
// (wstring only for now; feel free to make this a template if needed)
/*static*/ wstring File::DirectoryPathOf(wstring path)
{
#ifdef _WIN32
    // Win32 accepts forward slashes, but it seems that PathRemoveFileSpec() does not
    // TODO:
    // "PathCchCanonicalize does the / to \ conversion as a part of the canonicalization, it's
    // probably a good idea to do that anyway since I suspect that the '..' characters might
    // confuse the other PathCch functions" [Larry Osterman]
    // "Consider GetFullPathName both for canonicalization and last element finding." [Jay Krell]
    path = msra::strfun::ReplaceAll<wstring>(path, L"/", L"\\");

    HRESULT hr;
    if (IsWindows8OrGreater()) // PathCchRemoveFileSpec() only available on Windows 8+
    {
        typedef HRESULT(*PathCchRemoveFileSpecProc)(_Inout_updates_(_Inexpressible_(cchPath)) PWSTR, _In_ size_t);
        HINSTANCE hinstLib = LoadLibrary(TEXT("api-ms-win-core-path-l1-1-0.dll"));
        if (hinstLib == nullptr)
            RuntimeError("DirectoryPathOf: LoadLibrary() unexpectedly failed.");
        PathCchRemoveFileSpecProc PathCchRemoveFileSpec = reinterpret_cast<PathCchRemoveFileSpecProc>(GetProcAddress(hinstLib, "PathCchRemoveFileSpec"));
        if (!PathCchRemoveFileSpec)
            RuntimeError("DirectoryPathOf: GetProcAddress() unexpectedly failed.");

        // this is the actual function call we care about
        hr = PathCchRemoveFileSpec(&path[0], path.size());

        FreeLibrary(hinstLib);
    }
    else // on Windows 7-, use older PathRemoveFileSpec() instead
        hr = PathRemoveFileSpec(&path[0]) ? S_OK : S_FALSE;

    if (hr == S_OK) // done
        path.resize(wcslen(&path[0]));
    else if (hr == S_FALSE) // nothing to remove: use .
        path = L".";
    else
        RuntimeError("DirectoryPathOf: Path(Cch)RemoveFileSpec() unexpectedly failed with 0x%08x.", (unsigned int)hr);
#else
    auto pos = path.find_last_of(L"/");
    if (pos != path.npos)
        path.erase(pos);
    else // if no directory path at all, use current directory
        return L".";
#endif
    return path;
}
Exemple #9
0
static void do_log(int log_level, const char *msg, va_list args, void *param)
{
	fstream &logFile = *static_cast<fstream*>(param);
	char str[4096];

#ifndef _WIN32
	va_list args2;
	va_copy(args2, args);
#endif

	vsnprintf(str, 4095, msg, args);

#ifdef _WIN32
	if (IsDebuggerPresent()) {
		int wNum = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
		if (wNum > 1) {
			static wstring wide_buf;
			static mutex wide_mutex;

			lock_guard<mutex> lock(wide_mutex);
			wide_buf.reserve(wNum + 1);
			wide_buf.resize(wNum - 1);
			MultiByteToWideChar(CP_UTF8, 0, str, -1, &wide_buf[0],
					wNum);
			wide_buf.push_back('\n');

			OutputDebugStringW(wide_buf.c_str());
		}
	}
#else
	def_log_handler(log_level, msg, args2, nullptr);
#endif

	if (too_many_repeated_entries(logFile, msg, str))
		return;

	if (log_level <= LOG_INFO || log_verbose)
		LogStringChunk(logFile, str);

#if defined(_WIN32) && defined(OBS_DEBUGBREAK_ON_ERROR)
	if (log_level <= LOG_ERROR && IsDebuggerPresent())
		__debugbreak();
#endif
}
Exemple #10
0
void MediaPlayers::EditTitle(wstring& str, int player_index) {
    if (str.empty() || items[player_index].edits.empty()) return;

    for (unsigned int i = 0; i < items[player_index].edits.size(); i++) {
        switch (items[player_index].edits[i].mode) {
        // Erase
        case 1: {
            Replace(str, items[player_index].edits[i].value, L"", false, true);
            break;
        }
        // Cut right side
        case 2: {
            int pos = InStr(str, items[player_index].edits[i].value, 0);
            if (pos > -1) str.resize(pos);
            break;
        }
        }
    }

    TrimRight(str, L" -");
}
Exemple #11
0
void ConfigHelper::ExpandDotDotDot(wstring& featPath, const wstring& scpPath, wstring& scpDirCached)
{
    wstring delim = L"/\\";

    if (scpDirCached.empty())
    {
        scpDirCached = scpPath;
        wstring tail;
        auto pos = scpDirCached.find_last_of(delim);
        if (pos != wstring::npos)
        {
            tail = scpDirCached.substr(pos + 1);
            scpDirCached.resize(pos);
        }
        if (tail.empty()) // nothing was split off: no dir given, 'dir' contains the filename
            scpDirCached.swap(tail);
    }
    size_t pos = featPath.find(L"...");
    if (pos != featPath.npos)
        featPath = featPath.substr(0, pos) + scpDirCached + featPath.substr(pos + 3);
}
Exemple #12
0
    void utf8ToStdWstring(wstring & dest, const string & src)
    {
      int len = utf8DecodedLength(src);

      dest.resize(len);

      const uint8_t * ptr = (const uint8_t *) src.c_str();

      const uint8_t * sentinel = ptr + src.size();

      int characters = 0;

      // http://en.wikipedia.org/wiki/UTF-8

      while(ptr < sentinel) {
        unsigned c0 = *ptr++;

    unsigned left = sentinel - ptr;

    /* trace("utf8ToStdWstring # 0x%x 0x%x (%c)",
           (int) c0, (int) (c0 & 0xE0), (char) c0); */

        if((c0 & 0x80) == 0)
          dest[characters] = c0;
        else if((c0 & 0xE0) == 0xC0) {
      if(left < 1) {
        error("utf8ToStdWstring # Lacking 1 byte from a UTF8 string");
        return;
      }
          unsigned c1 = *ptr++;
          dest[characters] = (c1 & 0x3F) + ((c0 & 0x1F) << 6);
      /*trace("utf8ToStdWstring # Got 2 = 0x%x (%x %x)",
            (int) dest[characters], c0, c1); */
        }
        else if((c0 & 0xF0) == 0xE0) {
      if(left < 2) {
        error("utf8ToStdWstring # Lacking 2 bytes from a UTF8 string");
        return;
      }

          unsigned c1 = *ptr++;
          unsigned c2 = *ptr++;
          dest[characters] = (c2 & 0x3F) + ((((unsigned) c1) & 0x3F) << 6) +
            ((((unsigned) c0) & 0x0F) << 12);
        }
        else if((c0 & 0xF8) == 0xF0) {
      if(left < 3) {
        error("utf8ToStdWstring # Lacking 3 bytes from a UTF8 string");
        return;
      }

          unsigned c1 = *ptr++;
          unsigned c2 = *ptr++;
          unsigned c3 = *ptr++;
          dest[characters] = (c3 & 0x3) + ((c2 & 0x3F) << 6) +
            ((c1 & 0x3F) << 12) + ((c0 & 0x07) << 18);
        }
    else {
      Radiant::error("utf8ToStdWstring # Bad character 0x%x", (int) c0);
          return;
    }

        characters++;
      }
    }
bool IO_CharDecoder::ReadUTF8(IO_InputDataStream* stream, wstring& text)
{
	if(stream == NULL)
	{
		return false;
	}

	int utflen = stream->ReadShort() & 0xffff;
	vector<char> bytes(utflen);	

	stream->ReadFull(&bytes[0], utflen);
	text.resize(utflen);

	int ch1, ch2, ch3;
	int length = 0;
	int index = 0;

	while(length < utflen)
	{
		ch1 = (int) bytes[length] & 0xff;
		switch(ch1 >> 4)
		{
		case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
			/* 0xxxxxxx*/
			length++;
			text[index++] = (wchar_t)ch1;
			break;
		case 12: case 13:
			/* 110x xxxx   10xx xxxx*/
			length += 2;
			if(length > utflen)
			{
				return false;
			}

			ch2 = (int)bytes[length - 1];
			if((ch2 & 0xC0) != 0x80)
			{
				return false;
			}

			text[index++] = (wchar_t)(((ch1 & 0x1F) << 6) | (ch2 & 0x3F));
			break;
		case 14:
			/* 1110 xxxx  10xx xxxx  10xx xxxx */
			length += 3;
			if(length > utflen)
			{
				return false;
			}

			ch2 = (int)bytes[length - 2];
			ch3 = (int)bytes[length - 1];

			if(((ch2 & 0xC0) != 0x80) || ((ch3 & 0xC0) != 0x80))
			{
				return false;
			}

			text[index++] = (wchar_t)(((ch1 & 0x0F) << 12) | ((ch2 & 0x3F) << 6) | ((ch3 & 0x3F) << 0));
			break;
		default:
			/* 10xx xxxx,  1111 xxxx */
			return false;
		}
	}

	return true;
}
Exemple #14
0
bool BKE_readFile(wstring &result, const wstring &filename)
{
#ifdef _MSC_VER
	ifstream fs(filename, ios_base::binary | ios_base::out);
#else
	char fname[FILENAME_MAX];
	bkpwcstombs(fname, filename.c_str(), FILENAME_MAX);
	ifstream fs(fname, ios_base::binary | ios_base::out);
#endif
	if (!fs.good())
		return false;
	fs.seekg(0, ios_base::end);
	string::size_type s = (string::size_type)fs.tellg();
	fs.seekg(0, ios_base::beg);
	string res;
	res.resize(s + 1);
	fs.read(&res[0], s);
	fs.close();
	if (res.size() >= 2 && (unsigned char)res[0] == 0xFF && (unsigned char)res[1] == 0xFE)
	{
		//Unicode16-LE
		result.clear();
		result.resize(s+1);
#ifdef HAVE_CODECVT
		auto& f = use_facet<codecvt<wchar_t, char16_t, mbstate_t>>(locale());
		mbstate_t mb=mbstate_t();
		const char16_t* from_next;
		wchar_t* to_next;
		f.in(mb, (char16_t*)&res[2], (char16_t*)&res[s], from_next, &result[0], &result[s], to_next);
#else
		utf16toucs4((uint32_t *)&result[0], (uint16_t *)&res[2], s / 2 - 1);
#endif
		return true;
	}
#ifndef WIN32
	mbstowcs(NULL, NULL, 0); // reset the conversion state
#endif
	if (res.size() >= 3 && (unsigned char)res[0] == 0xEF && (unsigned char)res[1] == 0xBB && (unsigned char)res[2] == 0xBF)
	{
		//UTF8-sig
		result.clear();
		result.resize(s + 1);
#ifdef WIN32
		//must use WINAPI on windows
		MultiByteToWideChar(CP_UTF8, 0, (LPCCH)(&res[3]), s - 3, &result[0], s);
#else
		setlocale(LC_ALL, "en_US.UTF-8");
		mbstowcs(&result[0], &res[3], s - 3);
#endif
		return true;
	}
	else
	{
		//use UTF8-unsig in linux and GB2312 in win
		result.resize(s + 1);
#ifdef WIN32
		setlocale(LC_ALL, "zh-CN.GB2312");
#else
		setlocale(LC_ALL, "en_US.UTF-8");
#endif
		mbstowcs(&result[0], &res[0], s);
		return true;
	}
}